Coding/Code in the Morning

211227_Code in the morning

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 12. 27. 11:26

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

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…: ์ •์ˆ˜ ์ œ๊ณฑ๊ทผ ํŒ๋ณ„

์ž„์˜์˜ ์–‘์˜ ์ •์ˆ˜ n์— ๋Œ€ํ•ด, n์ด ์–ด๋–ค ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.
n์ด ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ด๋ผ๋ฉด x+1์˜ ์ œ๊ณฑ์„ ๋ฆฌํ„ดํ•˜๊ณ , n์ด ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ด ์•„๋‹ˆ๋ผ๋ฉด -1์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

 

 

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

  • n์€ 1์ด์ƒ, 50000000000000 ์ดํ•˜์ธ ์–‘์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

 

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

def solution(n):
    answer = 0
    for i in range (1, n+1):
        if n == (i**2) :
            answer = (i+1)**2
            break
        else:
            answer = -1

    return answer

 

* range์™€ break๋ฅผ ์ž˜ ์จ์•ผ! 

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

def nextSqure(n):
    sqrt = n ** (1/2)

    if sqrt % 1 == 0:
        return (sqrt + 1) ** 2
    return 'no'

*     **(1/2)๊ฐ€ ์ œ๊ณฑ๊ทผ์ด๋ผ๋Š” ์ข‹์€ ์•„์ด๋””์–ด! 

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ •์ˆ˜ ์ œ๊ณฑ๊ทผ ํŒ๋ณ„

์ž„์˜์˜ ์–‘์˜ ์ •์ˆ˜ n์— ๋Œ€ํ•ด, n์ด ์–ด๋–ค ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. n์ด ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ด๋ผ๋ฉด x+1์˜ ์ œ๊ณฑ์„ ๋ฆฌํ„ดํ•˜๊ณ , n์ด ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ด ์•„๋‹ˆ๋ผ๋ฉด -1์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ

programmers.co.kr

 

 

 

 


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

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…: ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ

๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๊ฐ€ 4 ํ˜น์€ 6์ด๊ณ , ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋ผ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด s๊ฐ€ "a234"์ด๋ฉด False๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  "1234"๋ผ๋ฉด True๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

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

  • s๋Š” ๊ธธ์ด 1 ์ด์ƒ, ๊ธธ์ด 8 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

 

 

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

def solution(s):
    answer = True
    if (len(s)==4 or len(s)==6) and s.isdigit() :
        answer = True
    else:
        answer = False
    return answer

 

* s๊ฐ€ ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜ isdigit()

 

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

def alpha_string46(s):
    return s.isdigit() and (len(s) == 4 or len(s) == 6)

if else๋ฌธ์„ ๋…ผ๋ฆฌ์‹์„ ์‚ฌ์šฉํ•ด์„œ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค! 

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ธฐ๋ณธ

๋ฌธ์ž์—ด s์˜ ๊ธธ์ด๊ฐ€ 4 ํ˜น์€ 6์ด๊ณ , ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋ผ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด s๊ฐ€ "a234"์ด๋ฉด False๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  "1234"๋ผ๋ฉด True๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ œํ•œ ์‚ฌํ•ญ s๋Š” ๊ธธ์ด 1

programmers.co.kr

 

 


 

 

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

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…: ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ

ํ•จ์ˆ˜ solution์€ ์ •์ˆ˜ n์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. n์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ƒˆ๋กœ์šด ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด n์ด 118372๋ฉด 873211์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

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

  • n์€ 1์ด์ƒ 8000000000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

 

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

def solution(n):
    answer = 0
    lst = list(str(n))
    lst.sort(reverse = True) #sort ์˜ ๊ธฐ๋ณธ ๊ฐ’์€ False์—ฌ์„œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๋Š”๋ฐ, True๋กœ ๋ฐ”๊ฟ”์คŒ์œผ๋กœ์จ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ๊ฐ€๋Šฅ
    answer = int(''.join(lst)) #๋ฆฌ์ŠคํŠธ๋กœ ๋˜์–ด์žˆ๋˜ ๊ฒƒ์„ join์„ ์‚ฌ์šฉํ•ด์„œ ํ•ฉ์นœ๋‹ค, ๊ทธ๋ฆฌ๊ณ  int๋กœ ๋ณ€ํ™˜ํ•ด์คŒ
    return answer

 

 

* sorted(iterable) ํ•จ์ˆ˜๋Š” ์ž…๋ ฅ๊ฐ’์„ ์ •๋ ฌํ•œ ํ›„ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

>>> sorted([3, 1, 2])
[1, 2, 3]
>>> sorted(['a', 'c', 'b'])
['a', 'b', 'c']
>>> sorted("zero")
['e', 'o', 'r', 'z']
>>> sorted((3, 2, 1))
[1, 2, 3]

๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์—๋„ sort ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์˜ sort ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด ๊ทธ ์ž์ฒด๋ฅผ ์ •๋ ฌ๋งŒ ํ•  ๋ฟ ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋Œ๋ ค์ฃผ์ง€๋Š” ์•Š๋Š”๋‹ค.

 

 

* ''.join(๋ฆฌ์ŠคํŠธ)


''.join(๋ฆฌ์ŠคํŠธ)๋ฅผ ์ด์šฉํ•˜๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด์˜จ ['a', 'b', 'c'] ์ด๋Ÿฐ ์‹์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ 'abc'์˜ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์ณ์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜.

 

* list(s)๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ์ž๋ฃŒํ˜• s๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด ๋Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.

>>> list("python")
['p', 'y', 't', 'h', 'o', 'n']
>>> list((1,2,3))
[1, 2, 3]

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

 

 

 

'Coding > Code in the Morning' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

211229_Code in the morning  (0) 2021.12.29
211228_Code in the morning  (0) 2021.12.28
211224_Code in the morning  (0) 2021.12.24
211222_Code in the morning  (0) 2021.12.23
211206_Code in the morning  (0) 2021.12.07