Lesson 1 : How to Get Started
파이썬 기초 문법
#7주를 분으로 바꾸기
print 7 * 7 * 24 * 60
Lesson 2 : How to Reapeat
문법공부하면서 따라가는게 좀 답답해서 문법을 미리 공부해버렸다.
find
sent = "my name is minwoo is my name is minwoo"
first = sent.find("name") #3 0부터 찾는건가? default가 0인듯.
second = sent.find("name", 4) #첫번째꺼 이후(index4)부터 찾는다.
if/else
def bigger(x,y):
if x>y:
r = x
else:
r = y
return r
||이 안됨. or은됨
>,<
#가장큰것만 찾는것.
def biggest(a,b,c):
if a>b:
if a>c:
return a
else:
return c
else:
if b>c:
return b
else:
return c
factorial(차례곱, 계승)
def factorial(n):
result = 1
while n >= 1:
result = result * n
n = n - 1
return result
print factorial(4)
while
i = 1
while i <= 4:
print i
i = i + 1
#위와 같은코드
while True:
if i > n:
break
print i
i = i + 1
Muliple Assignment
a,b=b,a #swap value. not same as a=b,b=a.
Lesson 2:Problem Set
Udacity
def udacians(s)
return "U" + s
udacitan("dacity") #Udacity
Median
def bigger(a,b):
if a > b:
return a
else:
return b
def biggest(a,b,c):
return bigger(a,bigger(b,c))
def median(a,b,c):
if a==biggest(a,b,c):
return bigger(b,c)
elif b==biggest(a,b,c):
return bigger(a,c)
else:
return bigger(a,b)
Blastoff
def countdown(n):
while n >= 0:
if n==0:
print("Blastoff!")
break
else:
print(n)
n -= 1
countdown(3)
#3
#2
#1
#Blastoff!
Find last
def find_last(a,b):
return a.rfind(b)
print find_last('aaaa', 'a')
#>>> 3
print find_last('aaaaa', 'aa')
#>>> 3
print find_last('aaaa', 'b')
#>>> -1
print find_last("111111111", "1")
#>>> 8
print find_last("222222222", "")
#>>> 9
Stamps
def stamps(n):
a=n/5
b=(n%5)/2
c=(n%5)%2
return (a,b,c)
print stamps(8)
#>>> (1, 1, 1) # one 5p stamp, one 2p stamp and one 1p stamp
print stamps(5)
#>>> (1, 0, 0) # one 5p stamp, no 2p stamps and no 1p stamps
print stamps(29)
#>>> (5, 2, 0) # five 5p stamps, two 2p stamps and no 1p stamps
print stamps(0)
#>>> (0, 0, 0) # no 5p stamps, no 2p stamps and no 1p stamps
Range of Set
def set_range(a,b,c):
arr = [a,b,c]
arr.sort()
return arr[-1]-arr[0]
print set_range(10, 4, 7)
#>>> 6 # since 10 - 4 = 6
print set_range(1.1, 7.4, 18.7)
#>>> 17.6 # since 18.7 - 1.1 = 17.6
Lesson 2:Problem Set(Optianal 2)
Superhero Nuisance
def fix_machine(debris, product):
i = 0
while i < len(product):
if debris.find(product[i]) != -1:
i += 1
else:
return "Give me something that's not useless next time."
return product
### TEST CASES ###
print "Test case 1: ", fix_machine('UdaciousUdacitee', 'Udacity') == "Give me something that's not useless next time."
print "Test case 2: ", fix_machine('buy me dat Unicorn', 'Udacity') == 'Udacity'
print "Test case 3: ", fix_machine('AEIOU and sometimes y... c', 'Udacity') == 'Udacity'
print "Test case 4: ", fix_machine('wsx0-=mttrhix', 't-shirt') == 't-shirt'
Days Old
10 Row Abacus
존나
Leap Year Baby
어렵다
알고리즘 개못해 멍청이 같은 천민우..
Jungle Animal
def jungle_animal(animal, my_speed):
result=""
if animal == 'cheetah':
if my_speed > 115:
result="Run!"
else:
result="Stay calm and wait!"
elif animal == 'zebra':
result="Try to ride a zebra!"
else:
result="Introduce yourself!"
print result
jungle_animal('cheetah', 30)
#>>> "Stay calm and wait!"
jungle_animal('gorilla', 21)
#>>> "Introduce yourself!"