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

์•Œ๊ณ ๋ฆฌ์ฆ˜/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/์Šคํ„ฐ๋”” 0์ฃผ์ฐจ] ํ•ด์‹œ ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก Lv.2 - ํŒŒ์ด์ฌ(Python)/ sort(key=len), Hash, Zip

 

๐Ÿ“‘ ๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42577

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

๋”๋ณด๊ธฐ

๋ฌธ์ œ ์„ค๋ช…

์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ ์ค‘, ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.
์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ, ๊ตฌ์กฐ๋Œ€ ์ „ํ™”๋ฒˆํ˜ธ๋Š” ์˜์„์ด์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค.

  • ๊ตฌ์กฐ๋Œ€ : 119
  • ๋ฐ•์ค€์˜ : 97 674 223
  • ์ง€์˜์„ : 11 9552 4421

์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด phone_book ์ด solution ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์–ด๋–ค ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฉด false๋ฅผ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด true๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • phone_book์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 1,000,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • ๊ฐ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • ๊ฐ™์€ ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ์ค‘๋ณตํ•ด์„œ ๋“ค์–ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์ œ

phone_bookreturn

["119", "97674223", "1195524421"] false
["123","456","789"] true
["12","123","1235","567","88"] false

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
์•ž์—์„œ ์„ค๋ช…ํ•œ ์˜ˆ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ธ ๊ฒฝ์šฐ๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ๋‹ต์€ true์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3
์ฒซ ๋ฒˆ์งธ ์ „ํ™”๋ฒˆํ˜ธ, “12”๊ฐ€ ๋‘ ๋ฒˆ์งธ ์ „ํ™”๋ฒˆํ˜ธ “123”์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ต์€ false์ž…๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

โœ’๏ธ ํ’€์ด (2021.08.03)

์ฒซ ๋ฒˆ์งธ ์ฝ”๋“œ
def solution(phone_book):
    phone_book.sort()
    for i in range(len(phone_book)-1):
        if phone_book[i] == phone_book[i+1][:len(phone_book[i])]:
            return False
    return True

 

 ์ด๊ฒƒ๋„ ๋ฌผ๋ก  ๋ญ ์ฐธ๊ณ ํ•˜๋ฉด์„œ ๊ฒจ์šฐ๊ฒจ์šฐ ์ง  ์ฝ”๋“œ๋‹ค. ํ•˜....ใ… 

 ํ•˜๋‚˜ ๋ฐฐ์šด ๊ฒƒ์€ ํŒŒ์ด์ฌ์—์„œ ๋ฐฐ์—ด ์ •๋ ฌ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค.

 

list.sort()

 

 

 sort()๋ฅผ ์“ฐ๋ฉด ์ˆซ์ž ํฌ๊ธฐ๋กœ ์ •๋ ฌ๋˜๋Š”๋ฐ, ๋ฌธ์ž์—ด์ด๋ผ๋„ ๊ฐ’์˜ ํฌ๊ธฐ๋Œ€๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค. ์ž๋ฐ”๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ์ถฉ๊ฒฉ์ ์ด๋‹ค(ใ…‹ใ…‹)

 ์˜ˆ๋ฅผ ๋“ค์–ด ["123", "99", "125", "1234", "22456"]์ด ์žˆ๋‹ค๊ณ  ํ•˜๋ฉด ["123", "1234", "125", "22456", "99"] ์ด๋ ‡๊ฒŒ ์ •๋ ฌ์ด ๋œ๋‹ค.

 

list.sort(key=len)

 

 key๋กœ len, ์ฆ‰ ๊ธธ์ด๋ฅผ ์ฃผ๋ฉด ๊ธธ์ด๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค. ์œ„์˜ ์˜ˆ๋ฅผ ๋“ค์ž๋ฉด ["99", "123", "125", "1234", "22456"]์ด ๋˜๊ฒ ๋‹ค.

 

 ์•„๋ฌดํŠผ ์ด๊ฑด ํ•˜๋‚˜ ๋ฐฐ์›Œ๊ฐ„ ์ ์ด๊ณ , phone_book์„ ๊ฐ’์˜ ํฌ๊ธฐ๋Œ€๋กœ ์ •๋ ฌํ•˜๋ฉด, ๋งŒ์•ฝ ๋’ค์˜ ์ˆซ์ž๊ฐ€ ์•ž์˜ ์ˆซ์ž๋ฅผ ์ ‘๋‘์–ด๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๋•Œ ๋ฌด์กฐ๊ฑด ์ธ์ ‘ํ•ด ์žˆ์„ ์ˆ˜ ๋ฐ–์— ์—†๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ธ์ ‘๊ฐ’๋งŒ ๋ณด๋ฉด ๋œ๋‹ค...

 

 

 

 

Hash ํ•จ์ˆ˜ ์ด์šฉ
def solution(phone_book):
    hashes = {} #ํ˜น์€ dict()
    for phone in phone_book:
        hashes[hash(phone)] = phone;

    for phone in phone_book:
        temp = ""
        for nums in phone:
            temp = temp + nums
            if hash(temp) in hashes and temp != phone:
                return False
    return True

 

 ์ด๊ฑด ๋ฐ”๋กœ ์–ด์ œ ํ’€์—ˆ๋˜ '์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜' ๋ฌธ์ œ์—์„œ ๋‚˜ํƒ€๋‚œ ํ•ด์‹œ ํ’€์ด์™€๋„ ๋น„์Šทํ•˜๋‹ค.

 ๋ฐฐ์—ด์— ํ•˜๋‚˜ํ•˜๋‚˜ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค, ํŠน์ • Hash๊ฐ’์ด ํ•ด์‹œ์— ์žˆ๋Š๋ƒ!๋ฅผ ๋ณด๋Š” ๊ฒƒ์ด ๋” ๋น ๋ฅธ ๋“ฏ ํ–ˆ๋‹ค 

 

 ์•„๋ฌดํŠผ ์ด๊ฒƒ๋„ ์–ด๋ ดํ’‹์ด ์ ‘๊ทผํ–ˆ๋˜ ๋ฐฉ๋ฒ•์ด๊ธด ํ–ˆ๋Š”๋ฐ ๋„ˆ๋ฌด ๊ฐ€๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์•ˆํ–ˆ๋”๋‹ˆ...๋งž๋Š” ํ’€์ด์—ˆ๋‚˜๋ณด๋‹ค.

 ์ด์ค‘ for๋ฌธ์„ ๋Œ์•„์„œ ํšจ์œจ์„ฑ์—์„œ ํƒˆ๋ฝํ•˜์ง€ ์•Š๋‚˜?ํ–ˆ๋Š”๋ฐ Hash ๋•๋ถ„์ธ์ง€ ์•ˆ๊ทธ๋Ÿฌ๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค. ์•Œ๋‹ค๊ฐ€๋„ ๋ชจ๋ฅผ ์ฝ”ํ…Œ...

 

 

 

zip ํ•จ์ˆ˜ ์ด์šฉ
def solution(phoneBook):
    phoneBook.sort()
    for p1, p2 in zip(phoneBook, phoneBook[1:]):
    # phoneBook์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” ๋ฌถ์ด๋Š”๊ฒŒ ์—†์ด ์‚ฌ๋ผ์ง„๋‹ค.
        print(p1, p2)
        if p2.startswith(p1):
            return False
    return True

 

 zip..zip...์–ด์ œ๋ถ€ํ„ฐ ๋‚˜๋ฅผ ์•Œ์ญ๋‹ฌ์ญํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” zip!

 zip์€ ๊ฐ™์€ ์œ„์น˜์— ์žˆ๋Š” ๊ฒƒ๋“ค๋ผ๋ฆฌ ๋ฌถ์–ด์ฃผ๋Š” ํ•จ์ˆ˜๋กœ ๋ณด์ธ๋‹ค.

 

arr1 = ["apple", "banana", "crocodile"]
arr2 = ["hello", "world"]
arr3 = ["blue", "yellow"]

for zip in zip(arr1, arr2, arr3):
    print(zip)
    
    
# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
# ('apple', 'hello', 'blue')
# ('banana', 'world', 'yellow')

 

 ์œ„์ฒ˜๋Ÿผ ์˜ˆ์‹œ๋ฅผ ์งœ ๋Œ๋ ค๋ดค๋”๋‹ˆ ํ›จ์”ฌ ์ดํ•ด๊ฐ€ ์‰ฝ๋‹ค.

 ๊ฐ™์€ ์œ„์น˜๋ผ๋ฆฌ ๋ฌถ์ด๋Š”๋ฐ, ๋งŒ์•ฝ ๊ฐ™์€ ์œ„์น˜์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์›์†Œ("crocodile")๊ฐ€ ์žˆ๋‹ค๋ฉด, ์ด ์›์†Œ๋Š” ํœ˜๋ฐœ๋˜์–ด ๋ฒ„๋ฆฐ๋‹ค.

 

 for๋ฌธ์—์„œ zip๋ง๊ณ  ์„ธ ๋ณ€์ˆ˜ a,b,c๋ฅผ ๋‘”๋‹ค๋ฉด ํŠœํ”Œ๋กœ ๋ฌถ์ธ ๊ฐ ์›์†Œ๋“ค์ด ํ•˜๋‚˜์”ฉ a,b,c์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

 ์ด ์ฝ”๋“œ์—์„œ ์•Œ๊ณ  ๊ฐ€์•ผํ•  ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ•จ์ˆ˜!!!!

 

 

 

startswith() ํ•จ์ˆ˜ ์ด์šฉ

 

 ์„ธ์ƒ์— ์ด๋Ÿฐ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋‹ˆ...๋”ฐ๋กœ ๋‚˜์ค‘์— ์ •๋ฆฌ๋ฅผ ํ•˜๊ฒ ์ง€๋งŒ ๊ฐ„๋‹จํ•˜๊ฒŒ๋งŒ ๋งํ•˜์ž๋ฉด ํŠน์ • ๋ฌธ์ž์—ด์ด ํŠน์ • ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ด๋‹ค. True๋‚˜ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

str = "๊ฐ€๋‚˜๋‹ค๋ผ๋งˆ๋ฐ”ํ•˜"
>> str.startswith("๊ฐ€")
>> True

>> str.startswith("๋งˆ", 4)	// ์‹œ์ž‘ ์ธ๋ฑ์Šค ์ง€์ • ๊ฐ€๋Šฅ
>> True

 

 ์œ„์™€ ๋น„์Šทํ•˜๊ฒŒ ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ์ฐพ๋Š” ํ•จ์ˆ˜๋กœ๋Š” find(), endswith()์ด ์žˆ์ง€๋งŒ ๋‚˜์ค‘์— ๋‹ค๋ค„๋ณด๊ฒ ๋‹ค.....

 

 

 

 

 

 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ์˜๊ฒฌ (2021.08.03)

 

 

 ํœด...์ง„์งœ ์ฝ”ํ…Œ ๋„ˆ๋ฌด ์–ด๋ ต๋‹ค. ๋ฌด์—‡๋ณด๋‹ค ๋‚ด๊ฐ€ ๋ชปํ•˜๋Š”๊ฑธ ๋‚จ๋“ค์—๊ฒŒ ๋ณด์—ฌ์ค˜์•ผ ํ•˜๋‹ˆ๊นŒ ์ชฝํŒ”๋ฆฐ๋‹คใ… .ใ… 

 ์ž๊ทน์€ ํ™•์‹คํžˆ ๋˜์–ด์„œ ์ด๋ ‡๊ฒŒ ๋งค์ผ ๋ฐค์„ ๊ณ ์ƒํ•˜๊ฒŒ ๋˜๊ธด ํ•˜์ง€๋งŒ, ์ •์‹ ์ ์œผ๋กœ ํž˜๋“ค๋‹ค...

 ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ๋Š” ํ•˜๋„ ์—Ž์–ด์„œ ์ €๋ ‡๊ฒŒ 66์ ์„ ๋งž์•˜๋˜ ์ฝ”๋“œ๋Š” ์ง€๊ธˆ ์ฐพ์œผ๋ ค๋‹ˆ ๊ธฐ์–ต๋‚˜์ง€ ์•Š๋Š”๋‹ค.

 ๋‹ค๋งŒ if๋ฌธ์—์„œ ๋งž์œผ๋ฉด ๋ฐ”๋กœ returnํ•˜๊ฒŒ ํ–ˆ๋”๋‹ˆ 10์ ์ด ์˜ฌ๋ž๋˜ ๊ธฐ์–ต์ด ์žˆ๋‹ค.

 ๋ฆฌํ„ด...๊ผฌ๋ฐ•๊ผฌ๋ฐ• ์ž˜์‹œํ‚ค์ž. ์—„ํ•œ ๊ณ„์‚ฐ ์‹œํ‚ค์ง€ ๋ง๊ณ .

 

 

 

 

 

 

๐Ÿ’ป ์†Œ์Šค์ฝ”๋“œ: ํŒŒ์ด์ฌ(Python)

(1)

def solution(phone_book):
    phone_book.sort()
    answer = True
    for i in range(len(phone_book)-1):
        if phone_book[i] == phone_book[i+1][:len(phone_book[i])]:
            answer = False
    return answer

 

(2)

def solution(phone_book):
    answer = True
    dic = {}
    
    for phone_number in phone_book:
        dic[phone_number] = 1
    for phone_number in phone_book:
        temp = ""
        for number in phone_number:
            temp += number
            if temp in dic and temp != phone_number:
                answer = False
                
    return answer

 

 

 

 

 

 

 

 

 

 

 

๐Ÿ’Ž ๐Ÿ’Ž ๐Ÿ’Ž

 

 

๏ผฐ๏ฝ๏ฝ“๏ฝ”๏ฝ…๏ฝ„ ๏ผข๏ฝ™ ๏ผณ๏ผก๏ผน

๐˜›๐˜ฉ๐˜ข๐˜ฏ๐˜ฌ๐˜ด ๐˜ง๐˜ฐ๐˜ณ ๐˜ณ๐˜ฆ๐˜ข๐˜ฅ๐˜ช๐˜ฏ๐˜จ