Skip to content

๐Ÿ’” a record of breaking through algorithms ๐Ÿ”“

Notifications You must be signed in to change notification settings

hortenssiaa/break-through-algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

45 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ’” Breaking Through Algorithms

Contents

  1. Python grammer for coding-test
  2. Python library for coding-test


1. Python grammer for coding-test (code)

1.1 ์ž๋ฃŒํ˜•
1.2 List
1.3 ๋ฌธ์ž์—ด
1.4 ํŠœํ”Œ
1.5 Dictionary ์‚ฌ์ „
1.6 Set ์ง‘ํ•ฉ
1.7 ์กฐ๊ฑด๋ฌธ
1.8 ๋ฐ˜๋ณต๋ฌธ
1.9 ํ•จ์ˆ˜
1.10 ์ž…์ถœ๋ ฅ


1.1 ์ž๋ฃŒํ˜•

  • e
  • ์†Œ์ˆ˜์  ๊ฐ’ ๋น„๊ต

1.2 List

1.2.1 list comprehension
'''0๋ถ€ํ„ฐ 19๊นŒ์ง€์˜ ์ˆ˜ ์ค‘์—์„œ ํ™€์ˆ˜๋งŒ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ'''
array = [i for i in range(20) if i % 2 == 1]
print(array) # [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

'''1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆ˜์˜ ์ œ๊ณฑ ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ'''
array = [i*i for i in range(1, 10)]
print(array) # [1, 4, 9, 16, 25, 36, 49, 64, 81]

'''n X m ํฌ๊ธฐ์˜ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ ์ดˆ๊ธฐํ™”'''
'''
Usage of underbar _ ?
> ๊ฐ’ ์ƒ๊ด€์—†์ด, ๋ฐ˜๋ณต ํšŸ์ˆ˜๋งŒ ์ค‘์š”ํ•  ๋•Œ ์‚ฌ์šฉ! 
'''
n = 3
m = 4
array = [[0]*m for _ in range(n)]
print(array) # [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
1.2.2 list function
# ๋ณ€์ˆ˜๋ช….count(ํŠน์ •๊ฐ’) : O(N)
a = [1, 4, 3, 1]
print(a.count(1)) # 2

# ๋ณ€์ˆ˜๋ช….remove(ํŠน์ •๊ฐ’) : O(N)
a.remove(3)
print(a) # [1, 4, 1]



'''ํ•จ์ˆ˜ ์“ฐ์ง€์•Š๊ณ , remove_set ๋ฆฌ์ŠคํŠธ์— ํฌํ•จ์•ˆ๋œ ๊ฐ’๋งŒ ์ €์žฅํ•˜๊ธฐ
>> ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ ๋ คํ•œ ํ’€์ด '''
a = [1, 2, 3, 4, 5, 5, 5]
remove_set = [3, 5]

result = [i for i in a if i not in remove_set]
print(result) # [1, 2, 4]

1.3 ๋ฌธ์ž์—ด

a = "ABCDEF"
print(a[2:4]) # CD

1.4 ํŠœํ”Œ

  • ํŠœํ”Œ์€ ํ•œ๋ฒˆ ์„ ์–ธ๋œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
  • ( ) ์ด์šฉ
  • ํŠœํ”Œ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฐ’ ๋ณ€๊ฒฝ๋˜๊ณ  ์žˆ์ง€ ์•Š์€์ง€ ์ฒดํฌ ๊ฐ€๋Šฅ
  • ๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์‹œ ๋งŽ์ด ์‚ฌ์šฉ

    ์˜ˆ) ๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ, ๋‚ด๋ถ€์—์„œ ์šฐ์„ ์ˆœ์œ„ ํ ์ด์šฉํ•˜๋Š”๋ฐ, ์ด๋•Œ ํ๋Š” ํŠœํ”Œ๋กœ ์ž‘์„ฑ๋จ

    (๋น„์šฉ, ๋…ธ๋“œ๋ฒˆํ˜ธ) ํ˜•ํƒœ๋กœ ๋ฌถ์–ด์„œ ๊ด€๋ฆฌ

a = (1, 2, 3, 4)
print(a) # (1, 2, 3, 4)

# a[2] = 7 # error

1.5 Dictionary ์‚ฌ์ „

  • ๋ฐ์ดํ„ฐ์˜ ๊ฒ€์ƒ‰ / ์ˆ˜์ • : O(N)
data = dict()
data['์‚ฌ๊ณผ'] = 'Apple'
data['๋ฐ”๋‚˜๋‚˜'] = 'Banana'
data['์ฝ”์ฝ”๋„›'] = 'Coconut'

if '์‚ฌ๊ณผ' in data:
    print("์‚ฌ๊ณผ yes!")

1.6 Set ์ง‘ํ•ฉ

  • ํŠน์ •์›์†Œ ์กด์žฌ์œ ๋ฌด : O(N)
1.6.1 ์ง‘ํ•ฉ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•
data = set([1, 1, 2, 3, 4, 4, 5])
print(data)

data = {1, 1, 2, 3, 4, 4, 5}
print(data)
1.6.2 ์ง‘ํ•ฉ ์—ฐ์‚ฐ
a = set([1, 2, 3, 4, 5])
b = set([3, 4, 5, 6, 7])

# ํ•ฉ์ง‘ํ•ฉ
print(a | b) # {1, 2, 3, 4, 5, 6, 7}

# ๊ต์ง‘ํ•ฉ
print(a & b) # {3, 4, 5}

# ์ฐจ์ง‘ํ•ฉ
print((a - b)) # {1, 2}

List ํŠœํ”Œ ์ง‘ํ•ฉ ์‚ฌ์ „
[ ] ( ) { } [k,v]
in, not in

1.7 ์กฐ๊ฑด๋ฌธ

1.7.1 pass
'''์กฐ๊ฑด๋ฌธ ๊ฐ’์ด ์ฐธ์ด๋ผ๋„, ์•„๋ฌด๊ฒƒ๋„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์ง€ ์•Š์„๋•Œ pass ์ด์šฉ'''
score = 82
if score >= 80:
    pass # ๋‚˜์ค‘์— ์ž‘์„ฑํ•  ์ฝ”๋“œ
else:
    print("์„ฑ์ ์ด ๋‚ฎ์•„์š”")
1.7.2 conditional expression ์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹
'''๋ณ€์ˆ˜ = true์ผ๋•Œ๊ฐ’(1) if์กฐ๊ฑด๋ฌธ(2) else(3) false์ผ๋•Œ๊ฐ’(4)'''
score = 85
result = "Success" if score >= 80 else "Fail"

print(result)
1.7.3 ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ์›์†Œ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์„œ, ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ ์ž ํ• ๋•Œ
a = [1, 2, 3, 4, 5]
remove = [3, 5]

result = [i for i in a if i not in remove]
print(result) # [1, 2, 4]
1.7.4 ์ˆ˜ํ•™ ๋ถ€๋“ฑ์‹ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ!
x = 15
if x > 0 and x < 20:
    print(f"0 < {x} < 20")

if 0 < x < 20:
    print(f"0 < {x} < 20")

1.8 ๋ฐ˜๋ณต๋ฌธ

1.8.1 ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ
  • ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋“ฑ์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.
scores = [90, 85, 77, 65, 97]
cheating_list = [2, 4]

for i in range(5):
    if i + 1 in cheating_list:
        continue
    if scores[i] >= 80:
        print(i + 1, "๋ฒˆ ํ•™์ƒ์€ ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.")

1.9 ํ•จ์ˆ˜

  • ๋ฌธ์ œ ํ‘ธ๋Š” ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜ํ™”ํ•˜๋ฉด, ๋งค์šฐ ํšจ๊ณผ์ ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค.
1.9.1 global
  • ํ•จ์ˆ˜ ์•ˆ์—์„œ, ํ•จ์ˆ˜ ๋ฐ–์˜ ๋ณ€์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ
a = 0

def func():
    global a
    a += 1

for i in range(10):
    func()

print(a) # 10
1.9.2 ๋žŒ๋‹ค ํ‘œํ˜„์‹
  • python์˜ ์ •๋ ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ, ์ •๋ ฌ ๊ธฐ์ค€ (key)๋ฅผ ์„ค์ •ํ•  ๋•Œ์—๋„ ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.
def add(a, b):
    return a+b

# ์ผ๋ฐ˜์ ์ธ add() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
print(add(3, 7))

# ๋žŒ๋‹ค ํ‘œํ˜„์‹์œผ๋กœ ๊ตฌํ˜„ํ•œ add() ๋ฉ”์„œ๋“œ
print( (lambda a, b: a+b)(3, 7) )

1.10 ์ž…์ถœ๋ ฅ

1.10.1 ์ž…๋ ฅ์„ ์œ„ํ•œ ์ „ํ˜์ ์ธ ์†Œ์Šค์ฝ”๋“œ

list(map(int, input().split()))

# ex 1)
# ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ
n = int(input())
# ๊ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅ
data = list(map(int, input().split()))

data.sort(reverse=True)
print(data)
'''
input
5
65 90 75 34 99
result
[99, 90, 75, 65, 34]
'''


# ex 2)
# n, m, k๋ฅผ ๊ณตใ…‡๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅ
n, m, k = map(int, input().split())
print(n, m, k)

1.10.2 ๋” ๋น ๋ฅธ ์ฝ”๋“œ - sys.stdin.readline()
  • sys ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋Š”, ํ•œ ์ค„ ์ž…๋ ฅ์„ ๋ฐ›๊ณ  ๋‚˜์„œ rstrip() ์„ ๊ผญ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.
  • sys.stdin.readline() ๊นŒ์ง€๋งŒ ์ž‘์„ฑํ•˜๋ฉด, ์ž…๋ ฅ ํ›„ ์—”ํ„ฐ๊ฐ€ ์ค„๋ฐ”๊ฟˆ ๊ธฐํ˜ธ๋กœ ์ž…๋ ฅ๋œ๋‹ค.

    ์ด๋•Œ ์ด ๊ณต๋ฐฑ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋ ค๋ฉด, rstrip() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

import sys

# ๋ฌธ์ž์—ด ์ž…๋ ฅ๋ฐ›๊ธฐ
data = sys.stdin.readline().rstrip()
print(data)
'''


# ex 2)
# n, m, k๋ฅผ ๊ณตใ…‡๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ž…๋ ฅ
n, m, k = map(int, input().split())
print(n, m, k)

1.10.3 ๋ณ€์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅ
answer = 7

print("์ •๋‹ต์€ " + answer + " ์ž…๋‹ˆ๋‹ค.") # error
print("์ •๋‹ต์€ " + str(answer) + "์ž…๋‹ˆ๋‹ค.") # ์ •๋‹ต์€ 7์ž…๋‹ˆ๋‹ค.
print("์ •๋‹ต์€", answer, "์ž…๋‹ˆ๋‹ค.") # ์ •๋‹ต์€ 7 ์ž…๋‹ˆ๋‹ค. (๋„์–ด์“ฐ๊ธฐ ํฌํ•จ! )
print(f"์ •๋‹ต์€ {answer}์ž…๋‹ˆ๋‹ค.") # ์ •๋‹ต์€ 7์ž…๋‹ˆ๋‹ค.



2. Python library for coding-test (code)

About

๐Ÿ’” a record of breaking through algorithms ๐Ÿ”“

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages