Coding/Code in the Morning

211229_Code in the morning

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 12. 29. 13:05

Code in the morning, ๋งค์ผ ์•„์นจ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’‰๋‹ˆ๋‹ค.

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…: ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด

array์˜ ๊ฐ element ์ค‘ divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” element๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— -1์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

 

๐Ÿ“ ์ œํ•œ ์‚ฌํ•ญ

  • arr์€ ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • ์ •์ˆ˜ i, j์— ๋Œ€ํ•ด i ≠ j ์ด๋ฉด arr[i] ≠ arr[j] ์ž…๋‹ˆ๋‹ค.
  • divisor๋Š” ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • array๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.

 

 

๐Ÿ’ก ๋‚˜์˜ ํ’€์ด 

def solution(arr, divisor):
    answer = []
    for a in arr: 
        if ((a % divisor)==0):
            answer.append(a)
    if len(answer) == 0: 
        answer.append(-1)
    else:
        answer.sort()
    return answer

* Array์˜ ๊ณต์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•  ๋•Œ์—๋Š” len์ด 0์ด๋ž€ ๊ฒƒ์„ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ‘‰๐Ÿป ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆซ์ž ๋ฐฐ์—ด

array์˜ ๊ฐ element ์ค‘ divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. divisor๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” element๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— -1์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜

programmers.co.kr

 

 

 


 

 

Code in the morning, ๋งค์ผ ์•„์นจ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’‰๋‹ˆ๋‹ค. 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…: ๋ชจ์˜๊ณ ์‚ฌ

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค.

1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

๐Ÿ“ ์ œํ•œ ์‚ฌํ•ญ

  • ์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

 

 

๐Ÿ’ก ๋‚˜์˜ ํ’€์ด 

def solution(answers):
    answer = []
    # ์ •๋‹ต ์‚ฌ์ดํด
    a1 = [1,2,3,4,5] #5๊ฐœ ๋ฐ˜๋ณต
    a2 = [2,1,2,3,2,4,2,5] #8๊ฐœ ๋ฐ˜๋ณต
    a3 = [3,3,1,1,2,2,4,4,5,5] #10๊ฐœ ๋ฐ˜๋ณต
    
    score1 = 0
    score2 = 0
    score3 = 0
    
    #๋ฌธ์ œ๋ฅผ ๋งž์ท„๋Š”์ง€ ํ™•์ธ, ๋งž์ท„์„ ๋•Œ ์ ์ˆ˜ +1
    for i in range (0,len(answers)):
        b1 = i%5
        b2 = i%8
        b3 = i%10
        
        # i๋ฒˆ์งธ ๋ฌธํ•ญ์— ์ฐ์€ ๋‹ต
        if a1[b1] == answers[i]:
            score1 += 1
        
        if a2[b2] == answers[i]:
            score2 += 1
        
        if a3[b3] == answers[i]:
            score3 += 1
    winner = max(score1, score2, score3)
    if winner == score1:
        answer.append(1)
    if winner == score2:
        answer.append(2)
    if winner == score3:
        answer.append(3)
    return answer

 

๐Ÿ‘‰๐Ÿป ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ 

 


Code in the morning, ๋งค์ผ ์•„์นจ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’‰๋‹ˆ๋‹ค. 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…: ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜

์ˆ˜๋งŽ์€ ๋งˆ๋ผํ†ค ์„ ์ˆ˜๋“ค์ด ๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹จ ํ•œ ๋ช…์˜ ์„ ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋“  ์„ ์ˆ˜๊ฐ€ ๋งˆ๋ผํ†ค์„ ์™„์ฃผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด participant์™€ ์™„์ฃผํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด completion์ด ์ฃผ์–ด์งˆ ๋•Œ, ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

๐Ÿ“ ์ œํ•œ ์‚ฌํ•ญ

  • ๋งˆ๋ผํ†ค ๊ฒฝ๊ธฐ์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜์˜ ์ˆ˜๋Š” 1๋ช… ์ด์ƒ 100,000๋ช… ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • completion์˜ ๊ธธ์ด๋Š” participant์˜ ๊ธธ์ด๋ณด๋‹ค 1 ์ž‘์Šต๋‹ˆ๋‹ค.
  • ์ฐธ๊ฐ€์ž์˜ ์ด๋ฆ„์€ 1๊ฐœ ์ด์ƒ 20๊ฐœ ์ดํ•˜์˜ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฐธ๊ฐ€์ž ์ค‘์—๋Š” ๋™๋ช…์ด์ธ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ’ก ๋‚˜์˜ ํ’€์ด 

def solution(participant, completion):
    answer = ''
    for c in completion:
        participant.remove(c)
    participant = ''.join(participant)
    return participant

์‹œ๊ฐ„ ์ดˆ๊ณผ ^-ใ… 

 

 

* 2์ค‘ํฌ๋ฌธ์œผ๋กœ ํ’€์—ˆ๋”๋‹ˆ ํšจ์œจ์„ฑ์—์„œ ์ „๋ถ€ ์‹คํŒจ ํ•˜์˜€๋‹ค. ์ด๋Š” ์ œํ•œ์‚ฌํ•ญ์— ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 2์ค‘ํฌ๋ฌธ์€ ์‹œ๊ฐ„๋ณต์žก๋„ N^2 ์ด๋‹ค. ์ œํ•œ์‚ฌํ•ญ์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜๋Š” 1๋ช… ์ด์ƒ 10๋งŒ๋ช… ์ดํ•˜์ธ๋ฐ 10๋งŒ์„ N์— ๋Œ€์ž…ํ•˜๋ฉด 100์–ต์ด ๋œ๋‹ค. 100์–ต์ด๋ฉด ๋ฌด์กฐ๊ฑด ์‹œ๊ฐ„ ์ดˆ๊ณผ๋กœ ํ…Œ์ŠคํŠธ์— ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ๋งˆ์ง€๋…ธ์„ ์„ 1์–ต์ด๋ผ ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค.

* sort๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์—ˆ๋‹ค. sort๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๋กœ nlogn์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  for๋ฌธ ํ•˜๋‚˜๊ฐ€ ์žˆ๋Š” for๋ฌธ์€ ์‹œ๊ฐ„ ๋ณต์žก๋„ n. ๊ทธ๋ž˜์„œ ๋น…O๋กœ ๋ดค์„๋•Œ ์ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” nlogn์ด ๋œ๋‹ค. n์— 10๋งŒ์„ ๋Œ€์ž…ํ•ด๋ณด๋ฉด 50๋งŒ์ด๋‹ค. ์ถฉ๋ถ„ํžˆ ์‹œ๊ฐ„ํšจ์œจ์„ฑ์—์„œ ํ†ต๊ณผ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์ด๋‹ค.

 

 * ์ด ๋ฌธ์ œ๋Š” ํ•ด์‹œ๋กœ ํ’€์–ด์•ผ ํ•œ๋‹ค๊ณ  ํ•จ. ํ•ด์‹œ๋Š” ๋‚˜์ค‘์— ๋ฐฐ์›Œ์„œ ํ’€์–ด๋ณด๋Š”๊ฑธ๋กœ

 

--------


ํŒŒ์ด์ฌ์—์„œ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š” ํŒจํ„ด์ž…๋‹ˆ๋‹ค.

>>> s = [1, 3, 5]
>>> for i in range(len(s)):
...     print(s[i])
... 
1
3
5
  • ํŒŒ์ด์ฌ์—์„œ ๊ถŒ์žฅํ•˜๋Š” ํŒจํ„ด
>>> for v in s:
...     print(v)
... 
  • ๋ฆฌ์ŠคํŠธ.remove(๊ฐ’)
    ์ด ๋ฐฉ๋ฒ•์€ removeํ•จ์ˆ˜์— ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋ฉด ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์›์†Œ๋ฅผ ์ง€์›Œ์ค๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ๊ฐ’๋“ค์„ ์ง€์›Œ์ฃผ์ง€๋Š” ์•Š๊ณ  ๊ฐ€์žฅ ๋จผ์ € ๋ฐœ๊ฒฌ๋œ ์š”์†Œ๋ฅผ ์ง€์›Œ์ค๋‹ˆ๋‹ค. 

 

 

๐Ÿ’ก ๋‹ค๋ฅธ ํ•ด์„ค

def solution(participant, completion):
    answer = ''
    participant.sort()
    completion.sort()
    for i in range (len(completion)): 
        if participant[i] != completion[i]:
            return participant[i]
        
    return participant[-1]

1. ์šฐ์„  ์ •๋ ฌ์„ ํ•œ ํ›„

2. ๊ฐ ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•˜๋Š” ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ ํ›„, ๋ถˆ์ผ์น˜ ์‹œ ํ•ด๋‹น ๊ฐ’์„ ๋ฆฌํ„ด

3. ๋งˆ์ง€๋ง‰ ๋ฆฌํ„ด๋ฌธ์€, completion๊ณผ participant ๋งˆ์ง€๋ง‰๊นŒ์ง€ ๋น„๊ตํ•ด๋„ ๊ฐ’์ด ๋ชจ๋‘ ๊ฐ™์•˜๋‹ค๋ฉด ์ œ์ผ ๋งˆ์ง€๋ง‰ ์ •๋ ฌ์˜ ์ฐธ๊ฐ€์ž๊ฐ€ ์™„์ฃผ๋ชปํ–ˆ๋‹ค๋Š” ์˜๋ฏธ๊ฐ€ ๋˜๋ฏ€๋กœ, ์ œ์ผ ๋ ์š”์†Œ๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ๊ฒƒ. 

 

* ๋‚˜๋Š” ๋‹น์—ฐํžˆ particpant ๊ฐœ์ˆ˜๋กœ for ๋ฌธ์„ ๋Œ๋ ค์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ๊ทธ๋ ‡๊ฒŒ๋˜๋ฉด completion์˜ ์ธ๋ฑ์Šค๋ณด๋‹ค ์ˆซ์ž๊ฐ€ ์ผœ์ ธ์„œ ์—๋Ÿฌ๊ฐ€ ๋œจ๋”๋ผ๋‹ˆ!

 

์ถœ์ฒ˜: https://deftkang.tistory.com/164 [deftkang์˜ IT ๋ธ”๋กœ๊ทธ]

๐Ÿ‘‰๐Ÿป ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ 

 


Code in the morning, ๋งค์ผ ์•„์นจ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’‰๋‹ˆ๋‹ค. 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…: 2016๋…„

2016๋…„ 1์›” 1์ผ์€ ๊ธˆ์š”์ผ์ž…๋‹ˆ๋‹ค. 2016๋…„ a์›” b์ผ์€ ๋ฌด์Šจ ์š”์ผ์ผ๊นŒ์š”? ๋‘ ์ˆ˜ a ,b๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ 2016๋…„ a์›” b์ผ์ด ๋ฌด์Šจ ์š”์ผ์ธ์ง€ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์š”์ผ์˜ ์ด๋ฆ„์€ ์ผ์š”์ผ๋ถ€ํ„ฐ ํ† ์š”์ผ๊นŒ์ง€ ๊ฐ๊ฐ SUN,MON,TUE,WED,THU,FRI,SAT ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด a=5, b=24๋ผ๋ฉด 5์›” 24์ผ์€ ํ™”์š”์ผ์ด๋ฏ€๋กœ ๋ฌธ์ž์—ด "TUE"๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

 

 

๐Ÿ“ ์ œํ•œ ์‚ฌํ•ญ

  • 2016๋…„์€ ์œค๋…„์ž…๋‹ˆ๋‹ค.
  • 2016๋…„ a์›” b์ผ์€ ์‹ค์ œ๋กœ ์žˆ๋Š” ๋‚ ์ž…๋‹ˆ๋‹ค. (13์›” 26์ผ์ด๋‚˜ 2์›” 45์ผ๊ฐ™์€ ๋‚ ์งœ๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค)

 

 

๐Ÿ’ก ๋‚˜์˜ ํ’€์ด 

def solution(a, b):
    if (a == 1 or a == 4 or a == 7):
        index = 4
    elif (a == 3 or a == 11):
        index = 1
    elif (a == 5):
        index = 6
    elif (a == 6):
        index = 2
    elif (a == 9 or a == 12):
        index = 3
    elif a == 10:
        index = 5
            
    day_temp = index + b
    if day_temp%7 == 1:
        result = 'MON'
    elif day_temp%7 == 2:
        result = 'TUE'
    elif day_temp%7 == 3:
        result = 'WED'
    elif day_temp%7 == 4:
        result = 'THU'
    elif day_temp%7 == 5:
        result = 'FRI'
    elif day_temp%7 == 6:
        result = 'SAT'
    elif day_temp%7 == 0:
        result = 'SUN'
        
    return result

 

์›”๋ณ„๋กœ ๊ณ ์œ ๊ฐ’์„ ์ฃผ๊ณ , 

ํ•ด๋‹น ๊ณ ์œ ๊ฐ’ + ๋‚ ์งœ ํ•ด์„œ ๋‚˜๋จธ์ง€๋กœ ์š”์ผ์„ ๊ตฌํ–ˆ๋Š”๋ฐ, ๋˜ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค....

 

 

* python or ๊ตฌ๋ฌธ์„ ์“ธ ๋•Œ, 

a == 1 or 2 or 3 ์ด๋ ‡๊ฒŒ ์“ฐ๋ฉด X

a == 1 or a == 2 or a == 3 ์ด๋Ÿฐ์‹์œผ๋กœ ์จ์•ผ O

 

๐Ÿ’ก ๋‹ค๋ฅธ ํ•ด์„ค

def solution(a, b):
    months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    days = ['THU', 'FRI', 'SAT', 'SUN', 'MON', 'TUE', 'WED']
    return days[(sum(months[:a-1])+b)%7]

๋ฐฐ์—ด์„ ์—„์ฒญ ๋˜‘๋˜‘ํ•˜๊ฒŒ ํ™œ์šฉํ–ˆ๋‹ค. 

 

๐Ÿ‘‰๐Ÿป ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ