*사용자로부터 입력 : input()*
1. 첫 번째 방법
def find_max(A):
max = A[0]
for i in range(len(A)): #range(start,end,step) : start이상, end미만, step간격
if A[i] > max:
max = A[i]
return max
array = [int(n) for n in input().split()]
print(array, "최댓값 = ", find_max(array))
2. 두 번째 방법(좀 더 간결하게!)
def find_max(A):
max = A[0]
for i in A[1:]
if i > max:
max = i
return max
array = [int(n) for n in input().split()]
print(array, "최댓값 = ", find_max(array))
split()이란?
괄호안에 들어가는 것을 구분자라고 하고, 구분자를 주지 않으면 공백개행 탭을 하나로 보고 나눈다.
구분자를 주었다면 결과의 길이 = 구분자+1
"1 2 3".split() #['1', '2', '3']
"1 2 3".split(' ') #['1', '2', '3']
"1,2,3".split() #['1,2,3']
"1,2,3".split(',') #['1', '2', '3']
"1,2,,3".split(',') #['1', '2', '', '3']
"1,2,,3".split(',,') #['1,2', '3']
728x90
'개발일기 > Python' 카테고리의 다른 글
[Python] 기본적인 자료구조 - 그래프(Graph) (0) | 2023.04.24 |
---|---|
[Python] 알고리즘 효율성 분석 (0) | 2023.04.24 |
[Python] 알고리즘 - 최대 공약수 구하기(유클리드 알고리즘) (0) | 2023.03.14 |
[Python] 기본적인 자료구조 - 리스트(list), 스택, 큐 (0) | 2023.03.14 |
[Python] 리스트(List)관련 자주 쓰이는 기능 정리 (0) | 2023.03.14 |