More actions
(Repair batch-0005 pages from live compare) |
(Repair MoniWiki formatting after migration) |
||
| Line 6: | Line 6: | ||
책에서 숫자를 가지고 장난 친경우는 없는것 같아서... | 책에서 숫자를 가지고 장난 친경우는 없는것 같아서... | ||
== 최대값, 최소값 == | == 최대값, 최소값 == | ||
# 입력 3가지수를 받아서 if문을 이용하여 최대값 최소값을 출력하는 프로그램을 작성하세요. DeleteMe)스펙이 if문의 언급은 잘못된것 아닌가요? | # 입력 3가지수를 받아서 if문을 이용하여 최대값 최소값을 출력하는 프로그램을 작성하세요. [[DeleteMe]])스펙이 if문의 언급은 잘못된것 아닌가요? | ||
# 1 을 활용하여 10개의 입력중에 가장 최대, 최소 값을을 출력하는 프로그램을 작성하세요. | # 1 을 활용하여 10개의 입력중에 가장 최대, 최소 값을을 출력하는 프로그램을 작성하세요. | ||
== 풀이 == | == 풀이 == | ||
| Line 90: | Line 90: | ||
=== [[임인택]] 아님 === | === [[임인택]] 아님 === | ||
==== FunctionalProgramming ==== | ==== [[FunctionalProgramming]] ==== | ||
===== Using List Comprehension ===== | ===== Using List Comprehension ===== | ||
def printMaxMin(cnt): | def printMaxMin(cnt): | ||
| Line 150: | Line 150: | ||
** 제가 아는선(문법)에선 이방법이 가장 간단한듯; - [[임인택]] | ** 제가 아는선(문법)에선 이방법이 가장 간단한듯; - [[임인택]] | ||
이런 경우를 개선하기 위해서 map 함수가 있는것입니다. 이를 Haskell에서 차용해와 문법에 내장시키고 있는 것이 List Comprehension 이고 차후 [http://www.python.org/peps/pep-0289.html Genrator Expression]으로 확장될 예정입니다. 그리고 print 와 ,혼용은 그리 추천하지 않습니다. print를 여러번 호출하는것과 동일한 효과라서, 좋은 컴퓨터에서도 눈에 뜨일만큼 처리 속도가 늦습니다. --NeoCoin | 이런 경우를 개선하기 위해서 map 함수가 있는것입니다. 이를 Haskell에서 차용해와 문법에 내장시키고 있는 것이 List Comprehension 이고 차후 [http://www.python.org/peps/pep-0289.html Genrator Expression]으로 확장될 예정입니다. 그리고 print 와 ,혼용은 그리 추천하지 않습니다. print를 여러번 호출하는것과 동일한 효과라서, 좋은 컴퓨터에서도 눈에 뜨일만큼 처리 속도가 늦습니다. --[[NeoCoin]] | ||
a = raw_input() | a = raw_input() | ||
news = map(lambda x:int(x), a.split()) | news = map(lambda x:int(x), a.split()) | ||
Latest revision as of 00:34, 29 March 2026
문제풀이 첫번째 문제
흠..우리집 인터넷이 갑자기 먹통되는 바람에 지금에서야 올리네;; 씁...-_-;; 책에서 숫자를 가지고 장난 친경우는 없는것 같아서...
최대값, 최소값
- 입력 3가지수를 받아서 if문을 이용하여 최대값 최소값을 출력하는 프로그램을 작성하세요. DeleteMe)스펙이 if문의 언급은 잘못된것 아닌가요?
- 1 을 활용하여 10개의 입력중에 가장 최대, 최소 값을을 출력하는 프로그램을 작성하세요.
풀이
=== 임인택 === ==== Normal Method ==== print 'problem 1-1' print 'type 3 values' v1 = input() v2 = input() v3 = input() print 'max=',max(v1, v2, v3) print 'min=',min(v1, v2, v3) print 'problem 1-2' print 'type 10 values ' vv1 = input() vv2 = input() vv3 = input() vv4 = input() vv5 = input() vv6 = input() vv7 = input() vv8 = input() vv9 = input() vv10 = input() print 'max=',max(vv1,vv2,vv3,vv4,vv5,vv6,vv7,vv8,vv9,vv10) print 'min=',min(vv1,vv2,vv3,vv4,vv5,vv6,vv7,vv8,vv9,vv10) ref와 같이 input은 eval 을 받는거니까. 가능하게 되는 것. 그래서 이게 가능해집니다. --아무개 input( [prompt]) Equivalent to eval(raw_input(prompt)). Warning: This function is not safe from user errors! It expects a valid Python expression as input; if the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation. (On the other hand, sometimes this is exactly what you need when writing a quick script for expert use.) If the readline module was loaded, then input() will use it to provide elaborate line editing and history features. Consider using the raw_input() function for general input from users. >>> input() 1+2 3 ==== Using Recursion ==== def myproc(count,mx,mn): val = int(raw_input()) if count != 0 : if mx == None : return myproc(count-1, val, val) else : return myproc(count-1, max(mx, val),min(mn,val)) else : return max(mx,val),min(mn,val) print 'problem 1-1' max1,min1=myproc(2,None,None) print 'max=',max1,'min=',min1 print 'problem 1-2' max2,min2=myproc(9,None,None) print 'max=',max2,'min=',min2 ==== Using Iteration ==== def iterproc(count): mx=None mn=None for i in range(0,count): val = int(raw_input()) if mx == None: mx = val mn = val else : mx = max(mx,val) mn = min(mn,val) return mx,mn print 'problem 1-1' max1,min1=iterproc(3) print 'max=',max1,'min=',min1 print 'problem 1-2' max2,min2=iterproc(10) print 'max=',max2,'min=',min2 === 임인택 아님 === ==== FunctionalProgramming ==== ===== Using List Comprehension ===== def printMaxMin(cnt): inputList = [ input() for i in range(cnt) ] print 'max=%d, min=%d'%(max(inputList),min(inputList)) print 'problem 1-1' printMaxMin(3) print 'problem 1-2' printMaxMin(10) ===== Using map ===== def printMinMax(cnt): inputList = map(lambda x:input(),range(cnt)) print 'max=%d, min=%d'%(max(inputList),min(inputList)) print 'problem 1-1' printMinMax(3) print 'problem 1-2' printMinMax(10) ==== Using Generator for input ==== def inputNum(v=[]): while True: v.append(input()) yield max(v),min(v) maxMin=() print 'problem 1-1' inputNum_gen=inputNum() for i in range(3): maxMin=(inputNum_gen.next()) print 'max=%d, min=%d'%maxMin inputNum_gen=inputNum() print 'problem 1-2' for i in range(10): maxMin=(inputNum_gen.next()) print 'max=%d, min=%d'%maxMin
최대값, 최소값 다른것
- 다음과 같은 공백으로 구분되는 임의의 숫자 입력이 주어질때 최대, 최소값을 출력하세요.
(데이터 양은 Python과 머신이 처리할수 있는 범위내로 한정) - Python의 문자열 트릭(?)을 보이는 문제입니다. :)
=== 예 ===
input numbers with space:
1 2 3 4 5 5 6 7 8 9 0
max=9 min=0
=== 풀이 예 ===
inNums = [ int(i) for i in raw_input('input numbers with space:\n').split() ]
print 'max=%d min=%d' % (max(inNums),min(inNums))
=== 임인택 ===
a = raw_input()
news = []
for i in a.split():
news.append(int(i))
print max(news), min(news)
- 제가 아는선(문법)에선 이방법이 가장 간단한듯; - 임인택
이런 경우를 개선하기 위해서 map 함수가 있는것입니다. 이를 Haskell에서 차용해와 문법에 내장시키고 있는 것이 List Comprehension 이고 차후 Genrator Expression으로 확장될 예정입니다. 그리고 print 와 ,혼용은 그리 추천하지 않습니다. print를 여러번 호출하는것과 동일한 효과라서, 좋은 컴퓨터에서도 눈에 뜨일만큼 처리 속도가 늦습니다. --NeoCoin a = raw_input() news = map(lambda x:int(x), a.split()) print max(news), min(news)