Problem Set

모르는 단어

  • linear : 정비례하는것? 그뭐야 일정하게 증가하고 , 감소하는것.
  • statement : 진술, 생각을 말하는 것
  • suppose : 가정하다.
  • implement : 도구, 이행하다.
  • comparison : 비교

Hash String

이해안됨

def hash_string(keyword, buckets):
    h = 0
    for c in keyword:
        h = (h + ord(c)) % buckets 
    return h

Quiz: Is Offered

courses = {
    'feb2012': { 'cs101': {'name': 'Building a Search Engine',
                           'teacher': 'Dave',
                           'assistant': 'Peter C.'},
                 'cs373': {'name': 'Programming a Robotic Car',
                           'teacher': 'Sebastian',
                           'assistant': 'Andy'}},
       ...
}

#민우코드
def is_offered(courses, course, hexamester):
    if hexamester in courses:
        if course in courses[hexamester]:
            return True
        else:
            return False
#udacity
def is_offered(courses, course, hexamester):
    return course in courses[hexamester]:
#in은 그자체로 True, False를 반환...

print is_offered(courses, 'cs003', 'apr2012')
#>>> False

Quiz: When Offered

#민우코드드 == udacity
def when_offered(courses,course):
    offered = []
    for c in courses:
        if course in courses[c]:
            offered.append(c)
    return offered


print when_offered (courses, 'cs101')
#>>> ['apr2012', 'feb2012']

print when_offered(courses, 'bio893')
#>>> []

Quiz: Involved

# courses
# [hex]
#'feb2012': { 
#    [course]
#    'cs101': { 
#        [key]
#        'name': 'Building a Search Engine',
#        'teacher': 'Dave',
#        'assistant': 'Peter C.'
#    }
#}

#민우코드
def involved(courses, person):
    output = {}
    for hex in courses:
        for course in courses[hex]:
            if person == courses[hex][course]['teacher']:
                if hex in output:
                    output[hex].append(course)
                else:
                    output[hex] = [course]
    return output  

#udacity
def involved(courses, person):
    output = {}
    for hex in courses:
        for course in courses[hex]:
            for key in courses[hex][course]:
                if courses[hex][course][key] == person:
                    if hex in output:
                        output[hex].append(course)
                    else:
                        ouput[hex] = [course]
    return output

#내코드가 더 나은것 같은데?

키넣고 밸류 뽑아내는게 연상이 잘 안된다..

Quiz: Refactoring



Quiz: Memoization

cache = {}

#민우코드
def cached_execution(cache, proc, proc_input):
    if proc_input in cache:
        return cache[proc_input]
    else:
        cache[proc_input] = proc(proc_input)
        return cache[proc_input]

#udacity
def cached_execution(cache, proc, proc_input):
    if proc_input not in cache:
        cache[proc_input] = proc(proc_input)
    return cache[proc_input]

메모이제이션 잘쓰고싶다.

Quiz: Shift a Letter

# Write a procedure, shift, which takes as its input a lowercase letter,
# a-z and returns the next letter in the alphabet after it, with 'a' 
# following 'z'.

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

def shift(letter):
    index = alphabet.index(letter)
    if index + 1 == len(alphabet):
        return alphabet[0]
    else:
        return alphabet[index + 1]


print shift('a')
#>>> b
print shift('n')
#>>> o
print shift('z')
#>>> a

Quiz: Shift n Letters

# Write a procedure, shift_n_letters which takes as its input a lowercase
# letter, a-z, and an integer n, and returns the letter n steps in the
# alphabet after it. Note that 'a' follows 'z', and that n can be positive,
# negative or zero.

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

def shift_n_letters(letter, n):
    index = alphabet.index(letter)
    if index + n >= len(alphabet):
        return alphabet[(index + n - len(alphabet))]
    else:
        return alphabet[index + n]


print shift_n_letters('s', 1)
#>>> t
print shift_n_letters('s', 2)
#>>> u
print shift_n_letters('s', 10)
#>>> c
print shift_n_letters('s', -10)
#>>> i

Quiz: Rotate

# Write a procedure, rotate which takes as its input a string of lower case
# letters, a-z, and spaces, and an integer n, and returns the string constructed
# by shifting each of the letters n steps, and leaving the spaces unchanged.
# Note that 'a' follows 'z'. You can use an additional procedure if you
# choose to as long as rotate returns the correct string.
# Note that n can be positive, negative or zero.

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

def shift_n_letters(letter, n):
    index = alphabet.index(letter)
    if index + n >= len(alphabet):
        return alphabet[(index + n - len(alphabet))]
    else:
        return alphabet[index + n]


def rotate(words, n):
    # Your code here
    bucket = {}
    word_A = words.split()
    result = ''
    idx = 0

    for word in word_A:
        for i in range(0,len(word)):
            #메모이제이션 사용.
            if word[i] not in bucket:
                bucket[word[i]] = shift_n_letters(word[i], n)
            result = result + bucket[word[i]]

        #아래 감동
        if len(word_A)> 1 and (idx != len(word_A)-1): 
            result = result + ' '

        idx = idx + 1
    return result

#print rotate('ifaj',-5)
#>>>'dave'
print rotate(("zw pfli tfuv nfibj tfiivtkcp pfl jyflcu "
               "sv rscv kf ivru kyzj"),-17)
#>>> 'if your code works correctly you should be able to read this'

항상 조건문을 최대한 간략하게 쓸 수 이쓴 방법을 생각하자 !

results matching ""

    No results matching ""