프로그래머스 level1 연습문제 10개 풀기
오늘 푼 문제들은 어제보다 까다로운 문제가 많았던것 같다.
def solution(n):
num=set(range(2,n+1))
for i in range(2,n+1):
if i in num:
num-=set(range(2*i,n+1,i))
return len(num)
def solution(n):
answer = ''
s = []
w = ['수','박']
for i in range(n):
s.append(w[i%2])
answer=''.join(s)
return answer
def water_melon(n):
s = "수박" * n
return s[:n]
def solution(s):
answer = int(s)
return answer
def strToInt(str):
result = 0
for idx, number in enumerate(str[::-1]):
if number == '-':
result *= -1
else:
result += int(number) * (10 ** idx)
return result
int(s)
로 하면 안되는건가..?def caesar(s, n):
s = list(s)
for i in range(len(s)):
if s[i].isupper():
s[i]=chr((ord(s[i])-ord('A')+ n)%26+ord('A'))
elif s[i].islower():
s[i]=chr((ord(s[i])-ord('a')+ n)%26+ord('a'))
return "".join(s)
chr
, ord
에 대해 정리하자.def solution(n):
answer = 0
s = []
for i in range(1, n+1):
if n%i == 0:
s.append(i)
answer = sum(s)
return answer
def sumDivisor(num):
# num / 2 의 수들만 검사하면 성능 약 2배 향상잼
return num + sum([i for i in range(1, (num // 2) + 1) if num % i == 0])
def solution(s):
answer = ''
w = ''
print(s.split())
for i in s.split():
for j in range(len(i)):
if j % 2 == 0:
w += (i[j:j+1].upper())
else:
w += (i[j:j+1].lower())
w += ' '
answer = ''.join(w).rstrip()
return answer
def toWeirdCase(s):
return ' '.join([''.join([c.upper() if i % 2 == 0 else c.lower() for i, c in enumerate(w)]) for w in s.split()])
def toWeirdCase(s):
return " ".join(map(lambda x: "".join([a.lower() if i % 2 else a.upper() for i, a in enumerate(x)]), s.split(" ")))
def solution(n):
answer = 0
for i in range(len(str(n))):
answer = answer + int(str(n)[i:i+1])
return answer
def sum_digit(number):
if number < 10:
return number;
return (number % 10) + sum_digit(number // 10)
def sum_digit(number):
return sum([int(i) for i in str(number)])
def solution(n):
answer = list(map(int, reversed(str(n))))
return answer
def digit_reverse(n):
return list(map(int, reversed(str(n))))
def digit_reverse(n):
return [int(i) for i in str(n)][::-1]
reverse
와 reversed
의 차이에 대해 정리하자.def solution(n):
answer = int(''.join(sorted(list(str(n)), reverse=True)))
return answer
def solution(n):
ls = list(str(n))
ls.sort(reverse = True)
return int("".join(ls))
sorted()
함수를 써봤는데 만족스러웠다.import math
def solution(n):
answer = pow(math.sqrt(n)+1, 2)
if int(math.sqrt(n)) != math.sqrt(n):
return -1
return answer
def nextSqure(n):
sqrt = n ** (1/2)
if sqrt % 1 == 0:
return (sqrt + 1) ** 2
return 'no'
math
를 import
할 필요가 없었는데..?!