Skip to content

Latest commit

ย 

History

History
414 lines (366 loc) ยท 12.2 KB

README.md

File metadata and controls

414 lines (366 loc) ยท 12.2 KB

๐Ÿ“šCoding Test

License: MIT

Coding_Test๋ฅผ ์ค€๋น„ํ•˜๋Š” ๊ธฐ๋ก (์ฝ”ํ…Œ๋ฅผ ๊ฐ€๋ฟํžˆ ํ†ต๊ณผํ•˜๋Š” ๊ทธ๋‚ ๊นŒ์ง€)

Author

๐Ÿ‘ค MoonSangJin

์•Œ๊ณ ๋ฆฌ์ฆ˜ python method ์š”์•ฝ

Str ๊ด€๋ จ Method

Method Return Ex
str.split() ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์— ๋‹ค๋ผ ๋‚˜๋ˆ  list๋กœ ๋ฐ˜ํ™˜ str = 'I am jinoh'
str.split() -> ['I', 'am', 'jinoh']
'string'.join() string์„ listํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ค‘๊ฐ„์— ๋„ฃ์–ด ํ•ฉ์นœ str๋กœ ๋ฐ˜ํ™˜ li = ['I', 'am', 'jinoh'] ' 'join(li) -> 'I am jinoh'
str.replace() ๋ฌธ์ž์—ด์˜ ์›ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ฐ”๊พธ์–ด ๊ทธ string์„ ๋ฐ˜ํ™˜ str = 'I am jinoh'
str.replace('jinoh', 'python') -> 'I am python'
str.find() ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์˜ index ๋ฐ˜ํ™˜ ์—†์œผ๋ฉด -1 str = 'abba'
str.find('a') -> 1 ๋ฐ˜ํ™˜
str.find('a', 2) -> 3๋ฐ˜ํ™˜
str.startswith() ๋ฌธ์ž์—ด์˜ ์•ž์—์„œ ๋ถ€ํ„ฐ ์ฐพ์•„ index ๋ฐ˜ํ™˜ ์—†์œผ๋ฉด -1 str = 'I am jinoh'
str.startswith('I') -> True
str.startswith('I am') -> True
str.endswith() ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฌธ์ž์—ด์ด ๋๋‚˜๋ฉด True, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด False str = 'I am jinoh'
str.endswith('jinoh') -> True
str.endswith('joh') -> false
str.count() ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ฌธ์ž์—ด์ด ๋ช‡๊ฐœ ์žˆ๋Š”์ง€ ๋ฐ˜ํ™˜(๋Œ€ ์†Œ๋ฌธ์ž ๊ตฌ๋ถ„) str('๋ฌธ์ž์—ด', ์‹œ์ž‘์œ„์น˜, ๋์œ„์น˜) ๊ฐ€๋Šฅ str = 'I am jinoh'
str.count('i') -> 1๋ฐ˜ํ™˜
str.count('o', 7, len(str)) -> 1๋ฐ˜ํ™˜
str.index() find์™€ ๋™์ผํ•˜๋‚˜ ์—†์œผ๋ฉด ValueError str = 'I am jinoh'
str.index('a') -> 3๋ฐ˜ํ™˜
str.index('k') -> ValueError
str.isalpha() ๋ฌธ์ž์—ด์ด ์˜์–ด,ํ•œ๊ธ€๋กœ๋งŒ ์ด๋ฃจ์–ด์กŒ์œผ๋ฉด True ์•„๋‹ˆ๋ฉด False str1 = 'I am jinoh'
str2 = '123 I am'
str1.isalpha() -> True
str2.isalpha() -> False
str.isdigit() ๋ฌธ์ž์—ด์ด ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์กŒ์œผ๋ฉด True ์•„๋‹ˆ๋ฉด False str1 = 'I am jin5'
str2 = '123124'
str1.isdigit() -> False
str2.isdigit() -> True
str.islower() ๋ฌธ์ž์—ด์ด ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ๋งŒ ๋˜์–ด์กŒ์œผ๋ฉด True ์•„๋‹ˆ๋ฉด False str1 = 'I am jinoh'
str2 = 'I AM JINOH'
str1.isalpha() -> True
str2.isalpha() -> False
str.isupper() ๋ฌธ์ž์—ด์ด ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ๋˜์–ด์กŒ์œผ๋ฉด True ์•„๋‹ˆ๋ฉด False str1 = 'I am jinoh'
str2 = 'I AM JINOH'
str1.isupper() -> False
str2.isupper() -> True
str.lower() ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ str1 = 'I Am jinoh'
str1.lower() -> 'i am jinoh' ๋ฐ˜ํ™˜
str.upper() ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ str1 = 'I am jinoh'
str1.upper() -> 'I AM JINOH' ๋ฐ˜ํ™˜
str[::-1] ๋ฌธ์ž์—ด์„ ๋’ค์ง‘๋Š”๋‹ค str1 = 'hello'
str1[::-1] -> 'olleh' ๋ฐ˜ํ™˜

list ๊ด€๋ จ Method

Method Return Ex
list.append() ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋งจ ๋’ค์— ์ถ”๊ฐ€ ๋ฐ˜ํ™˜๊ฐ’ X li = ['I', 'am']
li.append('jinoh') -> ['I', 'am', 'jinoh']
list.pop() ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ์— ์ง€์ • ์ธ๋ฑ์Šค๋ฅผ ์ œ๊ฑฐ ํ›„ ๊ทธ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ li = ['I', 'am', 'kim', 'jinoh']
li.pop() -> ['I', 'am', 'kim'] default -1๋ฒˆ์งธ
li.pop(1) -> li = ['I', 'kim'] return 'jinoh'
list.remove() ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ œ๊ฑฐ ํ›„ ๊ทธ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ ์—†์œผ๋ฉด ValueError li = ['I', 'am', 'jinoh']
li.remove('I') -> ['am', 'jinoh']
li.remove('a') -> ValueError
list.reverse() ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ์ฒด๋ฅผ ๋ฆฌ์ŠคํŠธ ์•ˆ์—์„œ ๋ฐ˜๋Œ€๋กœ ํ•จ li = [1, 2, 3, 4]
li.reverse() -> [4, 3, 2, 1]
list.insert() ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ์˜ ์›ํ•˜๋Š” ์œ„์น˜์— ๊ฐ’์„ ๋Œ€์ž…ํ•œ๋‹ค li = [1, 2, 3, 4]
li.insert(0, 5) -> [5, 4, 3, 2, 1]
list.sort() ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•จ li = [2, 3, 1, 4]
li.sort() -> [1, 2, 3, 4] default๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ
li.sort(reverse = True) -> [4, 3, 2, 1]
list.count() ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ฌธ์ž์—ด์ด ๋ช‡๊ฐœ ์žˆ๋Š”์ง€ ๋ฐ˜ํ™˜(๋Œ€ ์†Œ๋ฌธ์ž ๊ตฌ๋ถ„) str('๋ฌธ์ž์—ด', ์‹œ์ž‘์œ„์น˜, ๋์œ„์น˜) ๊ฐ€๋Šฅ li = ['I', 'am','jinoh']
li.count('i') -> 0๋ฐ˜ํ™˜
li.count('am', 1, len(li)) -> 0๋ฐ˜ํ™˜
filter(func, iterable) iterable์—์„œ func ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ ๊ฐ์ฒด ๋ฐ˜ํ™˜ li = [1, 0 , 0, 1, 1]
li = list(filter(lambda x: x != 0, li))-> [1, 1, 1] ๋ฐ˜ํ™˜
sorted(iterable, key=lambda x: x) ์กฐ๊ฑด์— ๋งž๋Š” ๊ฒƒ์„ ์ •๋ ฌํ•˜์—ฌ ์ •๋ ฌ๋œ list๋ฐ˜ํ™˜ li = [[0,1], [1,0], [2,2], [-1,3]]
sorted(li, key=lambda x: x[0])
-> [[-1,3], [0,1], [1,0], [2,2]] ๋ฐ˜ํ™˜
sorted(li, key=lambda x: x[1])
-> [[1,0], [0,1], [2,2], [-1,3]] ๋ฐ˜ํ™˜
list.sort(key) ์กฐ๊ฑด์— ๋งž๋Š” ๊ฒƒ์„ ์ •๋ ฌํ•œ๋‹ค ๋ฐ˜ํ™˜๊ฐ’์€ None li = [[0,1], [1,0], [2,2], [-1,3]]
li.sort(key=lambda x: x[0])
-> li = [[-1,3], [0,1], [1,0], [2,2]] ๋ฐ˜ํ™˜
li.sort(key=lambda x: x[1])
-> li = [[1,0], [0,1], [2,2], [-1,3]] ๋ฐ˜ํ™˜
list.sort(key ๋‹ค์ค‘ ์ •๋ ฌ์กฐ๊ฑด) ๋‹ค์ค‘์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ’์„ ์ •๋ ฌํ•œ๋‹ค ๋‹ค์ค‘ ์ •๋ ฌ์กฐ๊ฑด
answer=sorted(data_list,key=lambda x:(x[1],x[0]))
์ด๋ฉด x[1]๋กœ ์ •๋ ฌํ›„์— x[0]์œผ๋กœ ํ•œ๋ฒˆ ๋” ์ •๋ ฌํ•œ๋‹ค.
๋น„๊ตํ•  ์•„์ดํ…œ์˜ ์š”์†Œ๊ฐ€ ๋ณต์ˆ˜ ๊ฐœ์ผ ๊ฒฝ์šฐ, ํŠœํ”Œ๋กœ ๊ทธ ์ˆœ์„œ๋ฅผ ๋‚ด๋ณด๋‚ด์ฃผ๋ฉด ๋œ๋‹ค.
ํŠœํ”Œ์ด 3๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค๋ฉด ๋ชจ๋“  ์›์†Œ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ์›์†Œ์˜ ์ˆœ์„œ์— ๋งž๊ฒŒ ์ •๋ ฌ๋˜๊ณ  ,์ฒซ๋ฒˆ์งธ ์›์†Œ์˜ ๊ฐ’์ด ๊ฐ™์€ ๊ฒฝ์šฐ ๋‘ ๋ฒˆ์งธ ์›์†Œ์˜ ์ˆœ์„œ์— ๋งž๊ฒŒ ์ •๋ ฌ๋˜๊ณ  ๊ฑฐ๊ธฐ์— ๋‘๋ฒˆ์งธ ์›์†Œ์˜ ๊ฐ’๊นŒ์ง€ ๊ฐ™์œผ๋ฉด ์„ธ๋ฒˆ์งธ ์›์†Œ์˜ ์ˆœ์„œ์— ๋งž๊ฒŒ ์ •๋ ฌ๋œ๋‹ค.
map(func, iterable) ๊ฐ ์š”์†Œ๋“ค์„ ํ•ด๋‹น ์กฐ๊ฑด์— ๋งž๊ฒŒ ๋ฐ”๊ฟ” ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (map object) li = ['1', '2', '3', '4']
map(int, li)
-> map object๋ฅผ ๋ฐ˜ํ™˜ํ•จ
์ œ๋Œ€๋กœ๋œ ์ถœ๋ ฅ์„ ์œ„ํ•ด์„œ๋Š”
li = list(map(int, li))
-> li = [1,2,3,4]๋กœ ๋ฐ”๋€œ
# ์‘์šฉ์„ ํ•ด๋ณด๋ฉด
def func(n): return n-1
li = map(func, li)
-> li = [0,1,2,3] ๋ฐ˜ํ™˜

dictionary

๋ถˆ๋ณ€ํ•œ key์™€ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋Š” value๋กœ ๋งตํ•‘๋œ ์ˆœ์„œ์—†๋Š” ์ง‘ํ•ฉ

dic = {"jinoh" : 24, "jinwuk" : 27}
// key๋กœ ํ•ด๋‹น value ์ ‘๊ทผ

dic['jinoh'] // 24 ๋ฐ˜ํ™˜

dic['jinsuk'] = 32 // ์ƒˆ๋กœ์šด ํ‚ค์™€ ๊ฐ’์„ ์ถ”๊ฐ€

dic['jinoh'] = 25 // ํ‚ค๊ฐ€ ์ค‘๋ณต๋˜๋ฉด ๋งˆ์ง€๋ง‰ ๊ฐ’์œผ๋กœ ๋ฎ์—ฌ์”Œ์›Œ์ง
// dic = {'jinoh' : 25, 'jinwuk' : 27, 'jinsuk' : 32 }

for key in dic:
    print(dic[key]) // ํ‚ค๊ฐ’์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ ์ˆœํšŒ๊ฐ€๋Šฅ


์ •๊ทœํ‘œํ˜„์‹

Method Return
p.match(str) ๋ฌธ์ž์—ด์ด ์กฐ๊ฑด์— ๋งž๋Š”์ง€ ํŒ๋‹จํ•ด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
p.findall(str) ์กฐ๊ฑด์— ๋งž๋Š” ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•ด ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜
p.match(str) ๋ฌธ์ž์—ด์ด ์กฐ๊ฑด์— ๋งž๋Š”์ง€ ํŒ๋‹จ
import re

str = '1a2b3c4d'
p = re.compile('[a-zA-Z]')
// ์œ„ ์ฝ”๋“œ์˜ ์ถ•์•ฝ result = re.match('[a-z]+', "1a2b3c4d")

p.findall(str) // ['a', 'b', 'c', 'd'] ์ผ์น˜๋œ ๋ฌธ์ž ์—†์œผ๋ฉด [] ๋ฐ˜ํ™˜
p.match(str) // 'None'

all = re.compile('[a-zA-Z]+')
result = all.match(str)

print(result.group()) // '1a2b3c4d' result๊ฐ€ None์ด ์•„๋‹ˆ์–ด์•ผ ํ•จ.



set (์ง‘ํ•ฉ)

Method Return
a & b ๋‘ ์ง‘ํ•ฉ์˜ ๊ต์ง‘ํ•ฉ์„ ๋ฐ˜ํ™˜
a | b ๋‘ ์ง‘ํ•ฉ์˜ ํ•ฉ์ง‘ํ•ฉ์„ ๋ฐ˜ํ™˜
a = [1, 1, 2, 2, 3, 4]
b = [3, 4, 5, 6]

a = set(a) // a = {1, 2, 3, 4}
b = set(b) // b = {3, 4, 5, 6}

c = a & b // c = {3, 4}
c = a | b // c = {1, 2, 3, 4, 5, 6}


Number

a = float(input())  //์‹ค์ˆ˜์ž…๋ ฅ
b = round(a, 2) //์†Œ์ˆ˜์  3๋ฒˆ์งธ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ์ฒ˜๋ฆฌํ•ด์„œ 2๋ฒˆ์งธ๊นŒ์ง€๋งŒ ๋‚˜ํƒ€๋‚ด๊ธฐ
print('%.2f' % b) //์†Œ์ˆ˜์  ๋‘˜์งธ์ž๋ฆฌ๊นŒ์ง€๋งŒ ๋‚˜ํƒ€๋‚ด๊ธฐ

Input

input()์€ ์ž…๋ ฅ๋ฐ›์€๊ฑธ ๋ฌธ์ž์—ด๋กœ ๋‚˜ํƒ€๋ƒ„
input().split()์€ ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ๊ณต๋ฐฑ์œผ๋กœ ๋‚˜๋ˆ ์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ฌ
map(int,input().split())์€ ๋งŒ๋“ค์–ด์ง„ ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“ ์›์†Œ์— int ํ•จ์ˆ˜ ์ ์šฉ
list(map(int,input().split()))๋Š” ๋ชจ๋“  ์›์†Œ์— intํ•จ์ˆ˜ ์ ์šฉ์‹œํ‚จ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ฌ

y, m, d = map(int, input().split('.'))  //.์œผ๋กœ ๋‚˜๋ˆ ์ง„๊ฑธ int๋กœ ๋งŒ๋“ค๊ณ 
print('%d.%02d.%02d' % (y, m, d)) // ๋ฌธ์ž์—ดํ˜•์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ๊ฑด๋ฐ ๋“ค์–ด๊ฐˆ ๋ณ€์ˆ˜๋Š” ๋”ฐ๋กœ %๋กœ ๋„ฃ๋Š”๋‹ค %02

a,b=input().split('.') //.์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ๋‘๊ฐœ๋ฅผ ๋ฐ›๋Š”๋ฐ
print(a+b) ํ•˜๋ฉด ๋ถ™์—ฌ์„œ ๋ฌธ์ž์—ด๋กœ๋‚˜์˜ค๋Š”๋ฐ
print(a,b) ํ•˜๋ฉด a๋‚˜์˜ค๊ณ  ํ•œ ์นธ ๋„๊ณ (์ŠคํŽ˜์ด์Šค) b๊ฐ€ ๋‚˜์˜จ๋‹ค

key=stdin.readline().rstrip() // stdin.readline()์—์„œ ์ž…๋ ฅ์ด ์—”ํ„ฐ์น˜๊ณ  ๋“ค์–ด์˜ฌ๋•Œ ์ฆ‰ ํ•œ์ค„์”ฉ ๋“ค์–ด์˜ฌ๋•Œ๋Š” \n ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด rstrip() ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.


์ž…๋ ฅ์ด 10๋งŒ์ด์ƒ์ด๋ฉด
input() ๋Œ€์‹  
from sys import stdin
input()์ž๋ฆฌ์— sys.stdin.readline() ์จ๋ณด์ž


Give a โญ๏ธ if this project helped you!


This README was generated with โค๏ธ by readme-md-generator