기말 파이썬 코테 준비

김성민·2023년 6월 11일

메모

목록 보기
3/7
post-thumbnail

p184 12번

# 2부터 20까지의 모든 숫자에 대해 반복
for num in range(2, 21):
    # 소수 판별을 위한 변수 설정 (처음에는 num이 소수라고 가정)
    is_prime = True

    # 2부터 (num-1)까지의 모든 수로 num을 나누어 본다
    for i in range(2, num):
        if num % i == 0:  # 나누어 떨어진다면
            is_prime = False  # num은 소수가 아님
            break  # 더 이상 반복할 필요 없으므로 반복문 탈출

    # 반복문을 모두 돌았는데도 is_prime이 True라면 num은 소수
    if is_prime:
        print(num, end=' ')

p185 13번

temp = 0
for i in range(1,100,2):
    temp += i / (i+2)
print(temp)

p185 15번

n = int(input())

for i in range(1, n+1):	# Beware of sequence of if function
    if i % 3 == 0 and i % 5 ==0:
        print('fizzbuzz')
    elif i % 3 == 0:
        print('fizz')
    elif i % 5 == 0:
        print('buzz')
    else:
        print(i)

lambda function

- lambda (parameter) : (expression)
#일반적인 파이썬 함수
def func1(x):
	return x + 10

#람다 함수
func2 = lambda x : x + 10

result = 12

p221 1번

import math
def get_peri(radius=5.0):    # parameter = 반지름
    ans = 2 * math.pi * radius
    return ans
print(get_peri())
print(get_peri(4.0))
31.41592653589793
25.132741228718345

교재오류인지는 ㅁ?ㄹ

p221 2번

import math
def calc(a, b):
    print(f"({a} + {b}) = {a+b}")
    print(f"({a} - {b}) = {a - b}")
    print(f"({a} * {b}) = {a * b}")
    print(f"({a} / {b}) = {a / b}")
a = int(input())
b = int(input())

calc(a, b)

p221 3번

import math
def calc(a, b):
    return a+b,a-b,a*b,a/b
a = int(input())
b = int(input())
print(calc(a, b))

p221 4번

import math
def getGrade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"
print(getGrade(int(input())))

p222 6번

def check_pass(p):
    if len(p) < 8:
        return "length should be 8"
    if not any(letter.isdigit() for letter in p):
        return "should contain at least one integer"
    if not any(letter.isupper() for letter in p):
        return "should contain at least one Upper case"
    if not any(letter.islower() for letter in p):
        return "should contain at least one lower case"

    return "passed"


# 테스트
print(check_pass('password1'))
print(check_pass('Password'))
print(check_pass('PASSWORD1'))
print(check_pass('Passwor'))
print(check_pass('Password1'))

print(check_pass(input()))

p222 7번

def question(a,b):
    print(f'{a} + {b} = ?')
    ans = int(input())
    if ans == a+b:
        return 'Correct'
    else:
        return "Incorrect"


a, b = map(int,input().split())
print(question(a,b))

p222 8번

def getIntRange(a, b):
    while True:
        if 1 <= a <= 12:
            break
        else:
            a = int(input("a : (1~12): "))
    while True:
        if 1 <= b and b <= 31:
            break
        else:
            b = int(input("b : (1~31): "))
    return a, b

a, b = map(int, input().split())
a, b = getIntRange(a, b)
print(a, b)

223 9번

def getGCD(a, b):   # Define a function to get the greatest common divisor of a and b.
    if b == 0:      # If b is 0, return a. 
        return a    # This is the termination condition of the Euclidean algorithm.
    if a % b == 0:  # If the remainder of a divided by b is 0, then b is the greatest common divisor.
        return b    # So, return b.
    else:           # If neither of the two conditions above is satisfied,
        return getGCD(b, a % b)  # call the function recursively with b and the remainder of a divided by b as the new a and b.

a, b = map(int,input().split())   # Get two integers from the user and assign them to a and b.
print(getGCD(a, b))  # Compute and print the greatest common divisor of a and b.

p223 10번

import math
def isPrime(n):
    prime_num = []
    for i in range(2, n):
        check = True
        for j in range(2, int(math.sqrt(i))+1):
            if i % j == 0:
                check = False
        if check == True:
            prime_num.append(i)
    return prime_num

n = int(input())
print(*isPrime(n))

0개의 댓글