첫번째 문제는 5kyu 난이도 문제로
domain_name("http://github.com/carbonfive/raygun") == "github"
domain_name("http://www.zombie-bites.com") == "zombie-bites"
domain_name("https://www.cnet.com") == "cnet"
도메인 네임을 리턴하는 문제이다.
def domain_name(url): return url.split("://")[-1].split(".")[-2]
split을 사용해서 ://을 기점으로 나눈후 마지막을 출력하고 .으로 split 후 뒤에서 두번째 도메인 네임을 출력하는 방법을 사용했다.
두번째 문제는 6kyu 난이도 문제로
persistence(39) # returns 3, because 39=27, 27=14, 1*4=4
# and 4 has only one digit
persistence(999) # returns 4, because 999=729, 729=126,
# 1*2*6=12, and finally 1*2=2
persistence(4) # returns 0, because 4 is already a one-digit number
즉 한자리 숫자가 될때까지 숫자의 요소들을 곱해주는 문제이다.
def persistence(n): count=0 while n>9: count+=1 strn=str(n) total=1 for i in strn: total=total* int(i) n=total return count
while문을 이용해서 n이 두자리 수가 넘을때 계속 반복시켜 줬다. 마지막에는 total을 1로 설정 숫자들을 곱한 후 마지막 n을 total로 바꿔서 n이 한자리가 될때까지 반복하면서 count에 1을 더해준다.