๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ Python/solve-algorithm

๋ฐฑ์ค€ 1755 [์ˆซ์ž๋†€์ด] : ํŒŒ์ด์ฌ & ๋ฌธ์ž์—ด ์ •๋ ฌ (with maketrans & translate, join)

๐Ÿ“Œ ๋ฌธ์ œ 

https://www.acmicpc.net/problem/1755

 

1755๋ฒˆ: ์ˆซ์ž๋†€์ด

79๋ฅผ ์˜์–ด๋กœ ์ฝ๋˜ ์ˆซ์ž ๋‹จ์œ„๋กœ ํ•˜๋‚˜์”ฉ ์ฝ๋Š”๋‹ค๋ฉด "seven nine"์ด ๋œ๋‹ค. 80์€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ "eight zero"๋ผ๊ณ  ์ฝ๋Š”๋‹ค. 79๋Š” 80๋ณด๋‹ค ์ž‘์ง€๋งŒ, ์˜์–ด๋กœ ์ˆซ์ž ํ•˜๋‚˜์”ฉ ์ฝ๋Š”๋‹ค๋ฉด "eight zero"๊ฐ€ "seven nine"๋ณด๋‹ค ์‚ฌ์ „์ˆœ์œผ๋กœ

www.acmicpc.net


๐Ÿ” ํ’€์ด 

1. ์ฒซ๋ฒˆ์งธ ์‹œ๋„ 

์ผ๋‹จ ํ’€์–ด๋ณธ ๋ฐฉ๋ฒ• !

  

[์ „์ฒด ์ฝ”๋“œ]

'''์ตœ์ข… ์ œ์ถœ ์ฝ”๋“œ'''
M , N = map(int, input().split())
num_list = [str(i) for i in range(M, N + 1)]

def convert_to_str(txt):
    dictionary = {'0':'zero', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine'}
    transTable = txt.maketrans(dictionary)
    txt = txt.translate(transTable)
    return txt

alphabet_list = []
for num in num_list:
    if len(num) == 1:
        alphabet_list.append((num, convert_to_str(num)))
    else:
        alphabet_list.append((num, convert_to_str(num[0]) + " " + convert_to_str(num[1])))

alphabet_list.sort(key = lambda x : (x[1]))

for i in range(len(alphabet_list)):
    if i % 10 == 0 and i!=0:
        print(sep = '\n')
    print(alphabet_list[i][0], end=" ")

[ํ•ด์„ค]

'''์ฝ”๋“œ์™€ ํ•จ๊ป˜ ํ’€์ด ์„ค๋ช…'''
# input ๊ฐ’ ๋ฐ›๊ณ , M๋ถ€ํ„ฐ N๊นŒ์ง€ ์ˆซ์ž๋ฅผ string ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ๋ฐ›์•„ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
# ๐Ÿฅ• tip : range(start,stop)์—์„œ stip-1๊นŒ์ง€์˜ ๋ฒ”์œ„๊นŒ์ง€๋งŒ ํ•ด๋‹น๋˜๋ฏ€๋กœ N๊นŒ์ง€ ํ•˜๋ ค๋ฉด N+1์œผ๋กœ ํ•ด์•ผ ํ•œ๋‹ค
M , N = map(int, input().split())
num_list = [str(i) for i in range(M, N + 1)]

# ์ˆซ์ž๋ฅผ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ - ์•„๋ž˜์—์„œ ์ž์„ธํ•˜๊ฒŒ ๋‹ค๋ฃฐ ์˜ˆ์ •!๐Ÿ”
def convert_to_str(txt):
    dictionary = {'0':'zero', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine'}
    transTable = txt.maketrans(dictionary)
    txt = txt.translate(transTable)
    return txt

'''์ˆซ์ž -> ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊พธ๋Š”๋ฐ
ํ•œ์ž๋ฆฌ ์ˆ˜์ด๋ฉด ๋ฐ”๋กœ, 
๋‘ ์ž๋ฆฌ ์ˆ˜์ด๋ฉด 'two', 'seven'์„ 'two seven'์ฒ˜๋Ÿผ ์ŠคํŽ˜์ด์Šค๋ฐ” ํ•˜๋‚˜๋ฅผ ๋‘๊ณ  ๋„ฃ์–ด์ค€๋‹ค
(input ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์ด 99์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•œ์ž๋ฆฌ or ๋‘ ์ž๋ฆฌ์ˆ˜์ด๋‹ค)'''
alphabet_list = []
for num in num_list:
    if len(num) == 1:
        alphabet_list.append((num, convert_to_str(num)))
    else:
        alphabet_list.append((num, convert_to_str(num[0]) + " " + convert_to_str(num[1])))

# ์ •๋ ฌํ•˜๊ธฐ - x[1]์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ๋‹ค : ์•ŒํŒŒ๋ฒณ์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ๋‹ค๋Š” ๋œป
# ๋ฆฌ์ŠคํŠธ ์›์†Œ ํ˜•ํƒœ๊ฐ€ ['1', 'one'] ์ด๋Ÿฐ ์‹์ด๋ผ 1๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค๋Š” ๋œป!
# ๐Ÿ”์•„๋ž˜์— ์ฐธ๊ณ  ๋งํฌ ํ™•์ธ!
alphabet_list.sort(key = lambda x : x[1])

# 10๊ฐœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ค„๋ฐ”๊ฟˆ('\n')์„ ๋„ฃ์–ด์ค€๋‹ค.
for i in range(len(alphabet_list)):
    if i % 10 == 0 and i!=0:
        print(sep = '\n')
    print(alphabet_list[i][0], end=" ")

  

๐Ÿ” ์ˆซ์ž๋ฅผ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ : ์—ฌ๋Ÿฌ ๋ฌธ์ž๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ํ•จ์ˆ˜ maketrans( ) & translate( )

1. maketrans( ) ๋ฉ”์„œ๋“œ๋Š” translate( ) ๋ฉ”์„œ๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •๋œ ๋ฌธ์ž๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ๋งคํ•‘ ํ…Œ์ด๋ธ”์„ ๋ฐ˜ํ™˜ํ•จ

2. string.maketrans( ) : ์›๋ž˜ ๋ฌธ์ž์—ด๊ณผ ๋Œ€์ฒด ๋ฌธ์ž์—ด ๊ฐ„์˜ ๋งคํ•‘ ํ…Œ์ด๋ธ”์„ ๋ฐ˜ํ™˜ํ•จ

3. string.translate( ) : ์ง€์ •๋œ ์ผ๋ถ€ ๋ฌธ์ž๊ฐ€ ์‚ฌ์ „์ด๋‚˜ ๋งคํ•‘ ํ…Œ์ด๋ธ”์— ์„ค๋ช…๋œ ๋ฌธ์ž๋กœ ๋Œ€์ฒด๋˜๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•จ โžก๏ธ ์—ฌ๊ธฐ์„œ๋Š” maketrans( )๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ํ…Œ์ด๋ธ”์„ ๋ฒˆ์—ญํ•˜๋Š” ์—ญํ• โœจ

4. string.maketrans(x, y, z)์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์•Œ์•„๋ณด๊ธฐ

Parameter Description
x Required parameter(ํ•„์ˆ˜)
- ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜๋งŒ ์ง€์ •๋œ ๊ฒฝ์šฐ : ๊ต์ฒด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋Š” dictionary์—ฌ์•ผ ํ•จ
- ๋‘๊ฐœ ์ด์ƒ : ๋ฐ”๊ฟ€ ๋ฌธ์ž์—ด
y optional parameter
- x๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๋ฌธ์ž์—ด(x์™€ ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ ๊ฐ™์•„์•ผ ํ•จ)
z optional parameter
- ์›๋ž˜ ๋ฌธ์ž์—ด์—์„œ ์ œ๊ฑฐํ•  ๋ฌธ์ž์—ด

5. string.translate(table) ํŒŒ๋ผ๋ฏธํ„ฐ table : Required parameter(ํ•„์ˆ˜), ๊ต์ฒด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๋Š” ์‚ฌ์ „ ๋˜๋Š” ๋งคํ•‘ ํ…Œ์ด๋ธ”

6. ์˜ˆ์‹œ ์ฝ”๋“œ

# ๊ฐ™์€ ๊ธธ์ด์˜ ๋ฌธ์ž๋งŒ ๋ณ€๊ฒฝ์ด ๋œ๋‹ค
txt = "Hi I am Dona!"
x = "I"
y = "i"
mytable = txt.maketrans(x, y)
print(txt.translate(mytable)) # Hi i am Dona!

 

     

         

๐Ÿ” ๋ฆฌ์ŠคํŠธ ์ •๋ ฌ ๊ด€๋ จ ์ฐธ๊ณ  ๋งํฌ : https://imdona.tistory.com/14

 

[python] List(2) list sorting : ๋‹ค์ค‘ ์กฐ๊ฑด ์ •๋ ฌ & ๋ฐฑ์ค€ 1181๋ฒˆ[๋‹จ์–ด ์ •๋ ฌ]

Hello๐Ÿ‘‹ I'm Dona!:) ์ง€๋‚œ ํฌ์ŠคํŒ…์— ์ด์–ด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์—์„œ ์ž์ฃผ ์“ฐ์ด๋Š” ๋‹ค์ค‘ ์กฐ๊ฑด ์ •๋ ฌ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. sort์™€ sorted์˜ documentation ์†Œ๊ฐœ๋Š” ์ง€๋‚œ ํฌ์ŠคํŒ… ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”! โฌ‡๏ธโฌ‡๏ธโฌ‡๏ธโฌ‡๏ธโฌ‡

imdona.tistory.com


2. ๋‘๋ฒˆ์งธ ์‹œ๋„ 

๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ธฐ๐Ÿงน

maketrans( )& translate( ) ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ, ๋”•์…”๋„ˆ๋ฆฌ์™€ join ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ถฉ๋ถ„ํžˆ ๋”์šฑ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€์ดํ•  ์ˆ˜ ์žˆ๋‹ค.

M, N = map(int, input().split())

dict = {'0':'zero', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine'}

alphabet_list = []
for i in range(M, N + 1):
    alphabet = ' '.join([dict[j] for j in str(i)])
    alphabet_list.append([i, alphabet])

alphabet_list.sort(key = lambda x : x[1])

for i in range(len(alphabet_list)):
    if i % 10 == 0 and i != 0:
        print(sep = '\n')
    print(alphabet_list[i][0], end = " ")

๐Ÿ” joinํ•จ์ˆ˜ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌ

  1. ์–ด๋–ค ํ•จ์ˆ˜์ธ๊ฐ€์š”? ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜จ ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ํ•ฉ์ณ์„œ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜!
  2. ''.join(๋ฆฌ์ŠคํŠธ) : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜จ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์ณ์„œ ๋ฆฌํ„ด
  3. ' ' ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์ด ๊ตฌ๋ถ„์ž!
  4. ์˜ˆ์‹œ์ฝ”๋“œ
## ์˜ˆ์ œ 1
my_list = ['a', 'b', 'c']
print(''.join(my_list))
# out : abc

## ์˜ˆ์ œ 2
coordinate_list = ['1 -1\n', '1 2\n', '2 2\n', '3 3\n', '0 4\n']
print(''.join(coordinate_list))
'''
out :
1 -1
1 2
2 2
3 3
0 4
'''

## ์˜ˆ์ œ 3
my_list = ['a', 'b', 'c']
print("--".join(my_list))
# out : a--b--c

 


๐Ÿ™ RERENCE