알고리즘
[CodeWars] Count by X - 8Kyu
또롱또
2024. 11. 30. 06:38
728x90
https://www.codewars.com/kata/5513795bd3fafb56c200049e/train/python
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
대충 x의 배수를 n의 길이만큼 만들어 내라는 말이다.
다만 0보다 큰 숫자부터 시작해야한다.
아래처럼 배열을 하나 만들어서, 그 배열안에 숫자들을 append 해주고 return 해줬더니 통과했다.
파이썬은 push가 없고 append가 있다는걸 알았다.
def count_by(x, n):
result = []
for i in range(n):
result.append((i + 1) * x)
return result
코드를 줄여보는 노력을 해볼까 했지만, 보기에 깔끔하게 된거같아서 일단 제출을 했다.
다른 정답들을 확인해보니, 아래처럼 애초에 range 안에서 저렇게 쓸수도 있었고,
def count_by(x, n):
arr = []
for num in range(1, n+1):
result = x * num
arr.append(result)
return arr
아래처럼 숏코딩처럼 만든것도 있었다.
def count_by(x, n):
return [i * x for i in range(1, n + 1)]
728x90