본문 바로가기
Service/Coding

Quiz 3 week - Python

by 포항돼지 2022. 2. 15.

 

Question 1

Fill in the blanks of this code to print out the numbers 1 through 7.

 
number = 1
while number <= 7:
    print(number, end=" ")
    number += 1

 

----------------------------------------------------------------------------------------------------------------------------------

 

The show_letters function should print out each letter of a word on a separate line. Fill in the blanks to make that happen.

한줄에 한 글자씩 나오게 print 해봐라

 

def show_letters(word):
    for __:
        print(__)

show_letters("Hello")
# Should print one line per letter

 

Answer:

def show_letters(word):
    for i in word:
        print(i)

show_letters("Hello")
# Should print one line per letter

 

------------------------------------------------------------------------------------------------------------------------

 

Complete the function digits(n) that returns how many digits the number has. For example: 25 has 2 digits and 144 has 3 digits. Tip: you can figure out the digits of a number by dividing it by 10 once per digit until there are no digits left.

글자 숫자에 맞게 digit print 나오는 function 만들어봐라

def digits(n):
    count = 0
    if n == 0:
      ___
    while (___):
        count += 1
        ___
    return count
    
print(digits(25))   # Should print 2
print(digits(144))  # Should print 3
print(digits(1000)) # Should print 4
print(digits(0))    # Should print 1

answer:

def digits(n):
    count = 0
    if n == 0:
      return 1
    while n > 0:
        count += 1
        n = n//10
    return count
    
print(digits(25))   # Should print 2
print(digits(144))  # Should print 3
print(digits(1000)) # Should print 4
print(digits(0))    # Should print 1

 

// == 버림 나눗셈, 소수점 뒤에 빼고 몫만 가져오는거.

이런식으로 0.xxxx 남을때까지 floor division 하면 값 나옴

------------------------------------------------------------------------------------------------------------------------

 

Question 4

This function prints out a multiplication table (where each number is the result of multiplying the first number of its row by the number at the top of its column). Fill in the blanks so that calling multiplication_table(1, 3) will print out:

start 에서 stop 까지 그 값 곱한 값 보여줘

 

1 2 3

2 4 6

3 6 9

def multiplication_table(start, stop):
    for x in ___:
        for y in ___:
            print(str(x*y), end=" ")
        print()

multiplication_table(13)
# Should print the multiplication table shown above

range function 안쓰면 int 는 iterable 못 하다고 나옴. 꼭 ragne function 써야됨

stop +1하는 이유는, stop 값을 포함 시키기 위함

 

answer

def multiplication_table(start, stop):
    for x in range(start,stop+1):
        for y in range(start,stop+1):
            print(str(x*y), end=" ")
        print()

multiplication_table(13)
# Should print the multiplication table shown above
 
 
end=" " 뜻은, print 하고 마지막에 뭐 붙일지 보여 준다는 것
 
이중 for 문은
for i in range(2,10):
    for y in range(2,10):
        print(i,"*",y,"=",i*y)
       
이런식으로 위에 i 리스트 값 1개 가지고와서 y for문 끝날때까지 계속 하는거, 구구단 으로 예를 들면 편하다
 

1 2 3 이렇게 나오는거는, 1*1 = 1 , 1*2 = 2 , 1*3= 3 다음줄로 가서(i ==2 ) 진행 되는 방식

 

------------------------------------------------------------------------------------------------------------------------

 

Question 5

The counter function counts down from start to stop when start is bigger than stop, and counts up from start to stop otherwise. Fill in the blanks to make this work correctly.

숫자 비교해서, start 값이 stop보다 높으면 내림차순, 반대면 오름차순, 같으면 up 으로 하나만 나오게 출력

 

def counter(start, stop):
    x = start
    if ___:
        return_string = "Counting down: "
        while x >= stop:
            return_string += str(x)
            if ___:
                return_string += ","
            ___
    else:
        return_string = "Counting up: "
        while x <= stop:
            return_string += str(x)
            if ___:
                return_string += ","
            ___
    return return_string

print(counter(110)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(21)) # Should be "Counting down: 2,1"
print(counter(55)) # Should be "Counting up: 5"

Answer

def counter(start, stop):
    x = start
    if x > stop:
        return_string = "Counting down: "
        while x > stop:
            return_string += str(x)
            if x >= stop:
                return_string += ","
            x -= 1
    else:
        return_string = "Counting up: "
        while x < stop+1:
            return_string += str(x)
            if x < stop:
                return_string += ","
            x += 1
    return return_string

print(counter(110)) # Should be "Counting up: 1,2,3,4,5,6,7,8,9,10"
print(counter(21)) # Should be "Counting down: 2,1"
print(counter(55)) # Should be "Counting up: 5"
 
모르겠다 ㅅㅂ
 
 

------------------------------------------------------------------------------------------------------------------------

 

Question 6

The even_numbers function returns a space-separated string of all positive numbers that are divisible by 2, up to and including the maximum that's passed into the function. For example, even_numbers(6) returns “2 4 6”. Fill in the blank to make this work.

 

 

def even_numbers(maximum):
    return_string = ""
    for x in ___:
        return_string += str(x) + " "
    return return_string.strip()

print(even_numbers(6))  # Should be 2 4 6
print(even_numbers(10)) # Should be 2 4 6 8 10
print(even_numbers(1))  # No numbers displayed
print(even_numbers(3))  # Should be 2
print(even_numbers(0))  # No numbers displayed

answer

def even_numbers(maximum):
    return_string = ""
    for x in range(1,maximum+1,2):
        return_string += str(x) + " "
    return return_string.strip()

print(even_numbers(6))  # Should be 2 4 6
print(even_numbers(10)) # Should be 2 4 6 8 10
print(even_numbers(1))  # No numbers displayed
print(even_numbers(3))  # Should be 2
print(even_numbers(0))  # No numbers displayed

 

for in ragne(1,maximum+,2) 3번째 parameter 조건, +2해 주는거 1,3,5,7 이런식으로 가게하는, 이게 핵심인듯

 

 

 

1, 1+3 , 4+3 , 7+3 = 10 이라서 10은 제외