Quiz: Greatest

Define a procedure, greatest, that takes as input a list of positive numbers, and returns the greatest number in that list. If the input list is empty, the output should be 0.

#민우코드
def greatest(list_of_numbers):
    list_of_numbers.sort()

    if len(list_of_numbers) != 0:
        return list_of_numbers[-1]
    else:
        return 0

#udacity
def greatest(list_of_numbers):
    number = 0;
    for el in list_of_numbers:
        if number < el: number = el    
    return number


print greatest([4,23,1])
#>>> 23
print greatest([])
#>>> 0

물론 나처럼 하면 안되는건 아니지만, for 문으로 할생각을 못했다는게 충격적이다. ㅠ

Quiz: Max Pages

#민우코드
def crawl_web(seed, max_pages):
    tocrawl = [seed]
    crawled = []
    while tocrawl and (len(crawled) != max_pages): # (len(crawled) < max_pages)가 더 좋을것 같다.
        page = tocrawl.pop()
        if page not in crawled:
            union(tocrawl, get_all_links(get_page(page)))
            crawled.append(page)
    return crawled

#udacity
def crawl_web(seed, max_pages):
    tocrawl = [seed]
    crawled = []
    while tocrawl:
        page = tocrawl.pop()
        if page not in crawled and len(crawled) < max_pages:
            union(tocrawl, get_all_links(get_page(page)))
            crawled.append(page)
    return crawled


print crawl_web("http://www.udacity.com/cs101x/index.html",1) 
#>>> ['http://www.udacity.com/cs101x/index.html']

print crawl_web("http://www.udacity.com/cs101x/index.html",3) 
#>>> ['http://www.udacity.com/cs101x/index.html', 
#>>> 'http://www.udacity.com/cs101x/flying.html', 
#>>> 'http://www.udacity.com/cs101x/walking.html']

Quiz: Max Depth

#민우코드
def crawl_web(seed,max_depth):
    tocrawl = [seed]
    next_depth = []
    crawled = []
    depth = 0
    while tocrawl and (depth <= max_depth):
        page = tocrawl.pop()
        if page not in crawled :
            union(next_depth, get_all_links(get_page(page)))
            crawled.append(page)

        if len(tocrawl) == 0 : 
            depth = depth+1
            union(tocrawl, next_depth)

    return crawled

#udacity
def crawl_web(seed,max_depth):
    tocrawl = [seed]
    next_depth = []
    crawled = []
    depth = 0
    while tocrawl and (depth <= max_depth):
        page = tocrawl.pop()
        if page not in crawled :
            union(next_depth, get_all_links(get_page(page)))
            crawled.append(page)

        if not tocrawl: #이런식으로 쓰는거 좋은건가?
            tocrawl, next_depth = next_depth, [] # 내꺼보다 이게 루프를 덜돌아서 좋은것 같다. 더 가독성도 좋고.
            depth = depth+1


    return crawled

#세상에 내가 한거랑 거의 똑같다니!! ㅠ..

조금 어려웠다 . 역시 생각을 하다보면 허접한 답이라도 나오는듯..ㅠ

Quiz: Sudoku

루프돌려서 모든 값을 포함하고 있는지 결과는 true,false로만 답하면 되는 아주 쉬운 문제다.

# A valid sudoku square satisfies these
# two properties:

#   1. Each column of the square contains
#       each of the whole numbers from 1 to n exactly once.

#   2. Each row of the square contains each
#       of the whole numbers from 1 to n exactly once.

# You may assume the the input is square and contains at
# least one row and column.

#민우코드
#모든 숫자가 들어가야 한다고해서 그것만 체크했다.
def check(outer_arr):
    leng = len(outer_arr)
    result = True
    for inner_arr in outer_arr:
        for num in range(1,leng+1):
            if(num not in inner_arr):
                return False

    return result

def check_sudoku(outer_arr):
    leng = len(outer_arr)

    #col
    col = []
    for num in range(0,leng):
        col.append([])
        for inner_arr in outer_arr:
            col[num].append(inner_arr[num])

    return check(outer_arr) and check(col)

#udacity
#열과 행에 모든수가 한번씩 들어가는지를 체크하였다.
#동시에 대각선성에 있는 값들을 확인한다.

def check_sudoku(p):
    n = len(p)
    digit = 1
    while digit <= n:
        i = 0
        while i < n:
            row_count = 0
            row_count = 0
            j = 0
            while j < n:
                if p[i][j] == digit:
                    row_count = row_count + 1
                if p[j][i] == digit:
                    row_count = row_count + 1
                j = j + 1 #
            if row_count != 1 or col_count !=1:
                return false
            i = i + 1
        digit = digit + 1
    return True

#둘다 별로다.
#while을 사용한방법은 내가 lesson13에서 썼던 방법인데 사용하지못한게 아쉽다.

results matching ""

    No results matching ""