๐Ÿ’ก1๋‹ฌ์ฐจ ํšŒ๊ณ  + ๐Ÿ“Python Coding Test ๋‹ต์•ˆ

jaam._.miniยท2023๋…„ 11์›” 26์ผ
0

๐Ÿ“’Python ๊ธฐ์ดˆ ์ˆ˜ํ•™

๋ชฉ๋ก ๋ณด๊ธฐ
46/46
post-custom-banner

ํ•œ๋‹ฌ์ฐจ ํšŒ๊ณ 


2023.11.01 ~ 11.26


์ฝ”๋”ฉ์„ ์ž˜ํ•˜๊ณ  ์‹ถ๋‹ค!
๋๋‚ด์ฃผ๊ฒŒ ํ•ด์„œ ์–ด๋–ค ๋ฌธ์ œ๋“ , ํ•˜๊ณ  ์‹ถ์€ ์–ด๋–ค ๊ฒƒ์ด๋“  ์ฒ™์ฒ™ ์ฝ”๋”ฉ ํ•ด๋‚ด๊ณ  ์‹ถ๋‹ค!!!!!!!!!!!!!!!!!!

ํ•˜์ง€๋งŒ, ๐Ÿถ๋งํ•œ ๋Š๋‚Œ์ด ์™”๋‹ค.






1๋ฒˆ


Q.
์ด๋ฆ„์ด tmp์ธ ํฌ๊ธฐ๊ฐ€ 0์ธ ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ์‹œ์˜ค.

๋‹ต : []


A.

tmp = []
print(tmp)

โ–ผ

[]



2๋ฒˆ โŒ


Q.
data_type ์ด๋ผ๋Š” ์ด๋ฆ„์˜ ๋ฐ์ดํ„ฐํ˜•์„ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”. ์ด ํ•จ์ˆ˜๋Š” ํ•˜๋‚˜์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ ๋ฐ์ดํ„ฐํ˜•์„ ์•Œ์•„๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Int, float, string, list, dict, tuple ๋“ฑ์„ ์•Œ์•„์•ผ ํ•˜๋ฉฐ, ๋งŒ์•ฝ list ํ˜• ๋ฐ์ดํ„ฐ๋ผ๋ฉด list ํ˜• ๋ฐ์ดํ„ฐ ์•ˆ์— ์žˆ๋Š” ๊ฐ ๋ฐ์ดํ„ฐ์˜ ๋ฐ์ดํ„ฐํƒ€์ž…๋„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์‹คํ–‰ ์˜ˆ์‹œ๋ฅผ ํ™•์ธํ•ด ์ฃผ์„ธ์š”

โ— ๋‹ต : list, <int, int, list <int, int>, string>


A.

def data_type(value):
    if type(value) == list:
        result = []
        for i in value:
            result.append(type(i))
        return list, result
    else:
        result = type(value)
        return result

print("2๋ฒˆ ๋‹ต์•ˆ")
print(data_type([2, 3, [2, 3], "Hello"]))

โ–ผ

(<class 'list'>, [<class 'int'>, <class 'int'>, <class 'list'>, <class 'str'>])



3๋ฒˆ


Q.
์šฐ๋ฆฌ๋‚˜๋ผ์™€ ๋‹ฌ๋ฆฌ ๋ฏธ๊ตญ์€ ๊ฐ„๋‹จํ•œ ์‹์‚ฌ๋งŒ ํ•ด๋„ ํŒ์„ ์ฃผ๋Š” ๋ฌธํ™”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํŒ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ํŒ์€ ํ•จ์ˆ˜์—์„œ ์ž…๋ ฅ ๋ฐ›๋„๋ก ํ•˜์ฃ . ํŒ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜๋Š” calc_tips๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํ•˜๊ณ , ์ž…๋ ฅ์€ ์ „์ฒด ๊ธˆ์•ก, ํŒ ๋น„์œจ๋กœ ํ•˜๋ฉด ๋˜๊ฒ ๋„ค์š”. ํ˜น์‹œ ์‚ฌ์šฉ์ž๊ฐ€ ํŒ ๋น„์œจ์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด ๋””ํดํŠธ๋กœ 10%๋ฅผ ์ ์šฉํ•˜๋„๋ก ํ•ด์ฃผ์„ธ์š”. ๊ทธ๋Ÿฐ๋ฐ, 1๋‹ฌ๋Ÿฌ ๋‹จ์œ„๋กœ ๋”ฑ๋”ฑ ๋–จ์–ด์กŒ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ๋™์ „์„ ๋“ค๊ณ  ๋‹ค๋‹ˆ๋Š”๊ฑด ๋ถˆํŽธํ•˜๋‹ˆ๊นŒ์š”. ๊ทธ๋ž˜์„œ ๋งŒ์•ฝ ๊ณ„์‚ฐํ–ˆ๋Š”๋ฐ, 5.6๋‹ฌ๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๋ฉด ๊ทธ๋ƒฅ 6๋‹ฌ๋Ÿฌ๋กœ, 5.1๋‹ฌ๋Ÿฌ๊ฐ€ ๋‚˜์™€๋„ ๊ทธ๋ƒฅ 6๋‹ฌ๋Ÿฌ๋กœ ๊ณ„์‚ฐํ•˜๋„๋ก ํ•ด์ฃผ์„ธ์š”. ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋Š” return์ด ์•„๋‹ˆ๋ผ print๊ฐ€ ๋˜๋„๋ก ํ•ด์ฃผ์„ธ์š”.


A.

import random
totalPrice = random.randint(100, 500)

from math import ceil
tipPercnt = 0.1

def calc_tips(totalPrice, tipPercnt): #ํŒ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜
    tipPrice = ceil(totalPrice * tipPercnt)
    print(f'ํŒ ๊ฐ€๊ฒฉ : {tipPrice}๋‹ฌ๋Ÿฌ')

print("3๋ฒˆ ๋‹ต์•ˆ")
print(f'์ „์ฒด๊ธˆ์•ก : {totalPrice}๋‹ฌ๋Ÿฌ')
calc_tips(totalPrice, tipPercnt)

โ–ผ

์ „์ฒด๊ธˆ์•ก : 444๋‹ฌ๋Ÿฌ
ํŒ ๊ฐ€๊ฒฉ : 45๋‹ฌ๋Ÿฌ



4๋ฒˆ


Q.
์—ฌ๋Ÿฌ ๋ฌธ์žฅ์œผ๋กœ ๋œ ๋ฌธ์„œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋งค์นญ๋˜๋Š” ๋ฌธ์ž์—ด์˜ ํšŸ์ˆ˜์™€ ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์œ„์น˜(index)๋ฅผ ์•Œ๋ ค์ฃผ๋Š” search_target์ด๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

โ— ๋‹ต : 3, [20, 50, 82]


A.

def search_target(sentence, target):
    number = 0
    index = []

    n = 0
    while n < len(sentence):

        n = sentence.find(target, n)
        if n == -1:
            break
        else:
            number += 1
            index.append(n)

        n += 1

    print(f'{number},{index}')


print("4๋ฒˆ ๋‹ต์•ˆ")

sentence = ("Beautiful is better than ugly. "
            "Explicit is better than implicit. "
            "Simple is better than complex.")
target = "than"

search_target(sentence, target)

โ–ผ

3,[20, 50, 82]



5๋ฒˆ โŒ


Q.
์—ฌ๋Ÿฌ๋ถ„๋“ค์€ div_ab๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”. ์ด ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ ์ธ์ž๋กœ a, b๋ฅผ ๋ฐ›์œผ๋ฉฐ, ๋ฐ›์€ ์ธ์ž์˜ ์ฒซ ๋ฒˆ์งธ๋ฅผ a, ๋‘ ๋ฒˆ์งธ๋ฅผ b๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ ์ธ์ž๊ฐ€ ๊ผญ ์ •์ˆ˜์—ฌ์•ผ ํ•˜๋ฉฐ, ํ•จ์ˆ˜ ์ฝ”๋“œ ๋‚ด๋ถ€์—์„œ ์ •์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋„๋ก ํ•ด์„œ ์ •์ˆ˜์ผ ๋•Œ๋งŒ ๋‚˜๋ˆ—์…ˆ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•ด์ฃผ์„ธ์š”. ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ(return)์€ ์—†๊ณ , ํ•จ์ˆ˜์˜ ๋งˆ์ง€๋ง‰์— print ๋ฌธ์œผ๋กœ a / b๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋‚˜๋ˆ—์…ˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์˜ ํ˜•ํƒœ๋Š” ์ •ํ™•ํ•˜๊ฒŒ๋Š” ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ถœ๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ํ•ญ์€ ๋ชซ, ๋‘ ๋ฒˆ์งธ ํ•ญ์€ ๋‚˜๋จธ์ง€๊ฐ€ ๋˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”. ์—ฌ๋Ÿฌ๋ถ„์—๊ฒŒ ํ•œ ๊ฐ€์ง€ ์•ˆํƒ€๊นŒ์šด ์†Œ์‹์€ div_ab๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ ๋‚˜๋ˆ—์…ˆ ๊ธฐํ˜ธ( / )๋‚˜ ์–ด๋–ค ๋ชจ๋“ˆ์ด๋“  ๋‚˜๋ˆ—์…ˆ์„ ์ง์ ‘ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ช…๋ น์„ ์‚ฌ์šฉํ•  ์ˆœ ์—†์Šต๋‹ˆ๋‹ค. ๋‚˜๋ˆ—์…ˆ ๋ช…๋ น ์—†์ด ๋‚˜๋ˆ—์…ˆ์„ ์ˆ˜ํ–‰ํ•ด ์ฃผ์„ธ์š”.

โ— ๋‹ต : 3๋‚˜๋ˆ„๊ธฐ 2์˜ ๊ฒฐ๊ณผ : ๋ชซ 1, ๋‚˜๋จธ์ง€ 1


A.

def div_ab(a, b):

    if type(a) == int and type(b) == int:
        num1 = 0
        numA = a
        numB = b
        n = 0

        while True:

            if numA < numB:
                break

            else:
                numA -= numB
                num1 += 1
            n += 1

        mod = numB - numA

    print(f'{a}๋‚˜๋ˆ„๊ธฐ {b}์˜ ๊ฒฐ๊ณผ: ๋ชซ{num1}, ๋‚˜๋จธ์ง€{mod}')

print("5๋ฒˆ ๋‹ต์•ˆ")
a = 3
b = 2
div_ab(a, b)

โ–ผ

3๋‚˜๋ˆ„๊ธฐ 2์˜ ๊ฒฐ๊ณผ: ๋ชซ1, ๋‚˜๋จธ์ง€1



6๋ฒˆ


Q.

ํ•˜๋…ธ์ดํƒ‘ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฒจ๋ถ€๋œ ์‚ฌ์ง„์˜ ๊ธ€์ž๊ฐ€ ํ๋ฆฟํ•˜์ง€๋งŒ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์„ ์„ค๋ช…ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํฌ๊ธฐ ์ˆœ์œผ๋กœ ์Œ“์—ฌ์žˆ๋Š” ์›๋ฐ˜ ์„ธ ๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์›๋ฐ˜์€ A, B, C ์žฅ์†Œ ์ค‘ ํ˜„์žฌ๋Š” A์— ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋…ธ์ด ํƒ‘ ๋ฌธ์ œ๋Š” ์ž‘์€ ์›๋ฐ˜ ์œ„์— ํฐ ์›๋ฐ˜์ด ์˜ฌ๋ ค์งˆ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ๊ณผ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜๋งŒ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ง€์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. A์—์„œ C๋กœ ๊ฐ‘์ž๊ธฐ ๊ฑด๋„ˆ๊ฐ€๋Š” ๊ฒƒ์€ ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.
์—ฌ๋Ÿฌ๋ถ„์€ Python์œผ๋กœ ์•„๋ž˜ ๊ทธ๋ฆผ์— ๋ฌ˜์‚ฌ๋œ ํ•˜๋…ธ์ด ํƒ‘ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์˜ ๋ฌธ์ œ๋Š” ํ•˜๋…ธ์ดํƒ‘ ์›๋ฐ˜์˜ ๊ฐœ์ˆ˜๋Š” n๊ฐœ์ž…๋‹ˆ๋‹ค. ์ด n๊ฐœ์˜ ์›๋ฐ˜์„ B ์ง€์  ํ˜น์€ C ์ง€์ ์œผ๋กœ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ•˜๋…ธ์ดํƒ‘ ๋ฌธ์ œ๋ฅผ ํ’€ ์—ฌ๋Ÿฌ๋ถ„์ด ์ž‘์„ฑํ•ด ์ฃผ์–ด์•ผํ•  ํ•จ์ˆ˜๋Š” ์›๋ฐ˜์˜ ๊ฐœ์ˆ˜(n)์™€ ์–ด๋””์„œ ์›๋ฐ˜์ด ์‹œ์ž‘ํ•˜๊ณ (start_point) ์–ด๋””์„œ ์˜ฎ๊ธฐ๋Š”์ง€(end_point)๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

โ— ๋‹ต :

A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B

A.

def hanoi_sol(discCnt, fromBar, toBar, viaBar):
    if discCnt == 1:
        print(f'{fromBar} โ†’ {toBar}')

    else:
        hanoi_sol(discCnt-1, fromBar, viaBar, toBar)
        # (discCnt-1)๊ฐœ๋“ค์„ ๊ฒฝ์œ  ๊ธฐ๋‘ฅ์œผ๋กœ ์ด๋™
        print(f'{fromBar} โ†’ {toBar}')
        # discCnt์„ ๋ชฉ์  ๊ธฐ๋‘ฅ์œผ๋กœ ์ด๋™
        hanoi_sol(discCnt-1, viaBar, toBar, fromBar)
        # (discCnt-1)๊ฐœ๋“ค์„ ๋„์ฐฉ ๊ธฐ๋‘ฅ์œผ๋กœ ์ด๋™

#3๊ฐœ์˜ ์›ํŒ์„, 1๋ฒˆ ์ถœ๋ฐœ๊ธฐ๋‘ฅ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ, 2๋ฒˆ ๋„์ฐฉ๊ธฐ๋‘ฅ ๊นŒ์ง€ ์˜ฎ๊ธฐ๊ฒ ๋‹ค, ๊ฒฝ์œ  ๊ธฐ๋‘ฅ์€ 3๋ฒˆ ์ด๋‹ค
hanoi_sol(3, 'A', 'B', 'C')

โ–ผ

A โ†’ B
A โ†’ C
B โ†’ C
A โ†’ B
C โ†’ A
C โ†’ B
A โ†’ B

์ œ๋กœ๋ฒ ์ด์Šค ๋ฐ์ดํ„ฐ ์Šค์ฟจ
profile
๋น„์ „๊ณต์ž์˜ ๋ฐ์ดํ„ฐ ๊ณต๋ถ€๋ฒ•
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€