U E D R , A S I H C RSS

문제풀�/1회


1. 문제풀� 첫번째 문제

�..우리집 �터넷� 갑�기 먹통�는 바람� 지금�서야 올리네;;
�...-_-;;
책�서 숫�를 가지고 장난 친경우는 없는것 같아서...

1.1. 최대값, 최소값

1. ìž…ë ¥ 3가지수를 받아서 if문ì�„ ì�´ìš©í•˜ì—¬ 최대값 최소값ì�„ ìœë ¥í•˜ëŠ” 프로그램ì�„ 작성하세요. DeleteMe)스펙ì�´ if문ì�˜ 언급ì�€ 잘못ë�œê²ƒ 아닌가요?
2. 1 ì�„ 활용하여 10ê°œì�˜ 입력중ì—� 가장 최대, 최소 ê°’ì�„ì�„ ìœë ¥í•˜ëŠ” 프로그램ì�„ 작성하세요.

1.2. 풀�

1.2.1.1. Normal Method


~cpp 
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 � 받는거니까. 가능하게 �는 것. 그래서 �게 가능해집니다. --아무개

~cpp 
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. 
~cpp 
>>> input()
1+2
3

1.2.1.2. Using Recursion


~cpp 
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
   

1.2.1.3. Using Iteration


~cpp 
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
   
1.2.2.1.1. Using List Comprehension

~cpp 
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)
1.2.2.1.2. Using map

~cpp 
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)

1.2.2.2. Using Generator for input


~cpp 
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


1.3. 최대값, 최소값 다른것

  1. 다ì�Œê³¼ ê°™ì�€ 공백으로 구분ë�˜ëŠ” ìž„ì�˜ì�˜ 숫ìž� ìž…ë ¥ì�´ 주어질때 최대, 최소값ì�„ ìœë ¥í•˜ì„¸ìš”.
    (��터 양� Python과 머신� 처리할수 있는 범위내로 한정)
  2. Python� 문�열 트릭(?)� 보�는 문제입니다. :)

1.3.1. 예


~cpp 
input numbers with space:
1 2 3 4 5 5 6 7 8 9 0 
max=9 min=0

1.3.2. 풀� 예


~cpp 
inNums = [ int(i) for i in raw_input('input numbers with space:\n').split() ]
print 'max=%d min=%d' % (max(inNums),min(inNums))

1.3.3. 임��


~cpp 
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

~cpp 
a = raw_input()
news = map(lambda x:int(x), a.split())
print max(news), min(news)
  

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:30
Processing time 0.0063 sec