오늘은 총 5문제를 풀었다.
첫번째 문제는 6kyu 난이도 문제로
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
즉 주어진 number 안에 3이나 5의 배수를 찾아서 더해주는 것이다.
def solution(number): sum = 0 for i in range(number): if i%3==0 or i%5 ==0: sum +=i return sum
두번째 문제는 6kyu로
[7] should return 7, because it occurs 1 time (which is odd).
[0] should return 0, because it occurs 1 time (which is odd).
[1,1,2] should return 2, because it occurs 1 time (which is odd).
[0,1,0,1,0] should return 0, because it occurs 3 times (which is odd).
[1,2,2,3,3,3,4,3,3,3,2,2,1] should return 4, because it appears 1 time
리스트 안에 있는 원소의 갯수가 홀수인 것을 리턴하는 것이다.
def find_it(seq): count = {} for i in seq: try: count[i] += 1 except: count[i]=1 for i in count: if count[i] %2 != 0: return i
각 원소의 갯수를 dict형태로 바꿔서 원소를 2로 나눴을때 나머지가 0이 아닌 걸 리턴해 줬다.
다른사람의 풀이를 보니 count()를 사용한 걸 보고 더 효율적인 코드를 짜기 위해서 노력을 해야겠다고 생각했다.
세번째 문제는 5kyu 난이도로
pig_it('Pig latin is cool') # igPay atinlay siay oolcay
pig_it('Hello world !') # elloHay orldway !
각 단어의 첫 글자를 맨 끝으로 이동한 다음 단어 끝에 "ay"를 추가하며 구두점은 그대로 놔두는 것이다.
def pig_it(text): sli = text.split(" ") a = [] for i in sli: if i.isalpha(): i = i[1:] + i[0] +"ay" a.append(i) else: a.append(i) return " ".join(a)
split을 통해서 띄어쓰는 부분에서 문자열을 나눠주고 i가 알파벳일 경우 첫문자를 뒤로 뺀다음 ay를 추가시켰다.
네번째 문제는 5kyu 문제로
move_zeros([1, 0, 1, 2, 0, 1, 3]) # returns [1, 1, 2, 1, 3, 0, 0]
0을 맨 오른쪽으로 이동하면서 나머지 숫자의 순서는 그대로 출력해야한다.
def move_zeros(arr): empty = "" emptylist=[] for i in range(len(arr)): if arr[i] !=0: empty+=(str(arr[i])) empty = empty.ljust(len(arr),'0') for i in range(len(empty)): emptylist.append(int(empty[i])) return emptylist
0이 아니면 empty에 추가시켰고 원래 문자열 길이만큼 0을 추가한 후 list로 만들어서 출력했다.
다섯번째 문제는 5kyu 난이도 문제로
rgb(255, 255, 255) # returns FFFFFF
rgb(255, 255, 300) # returns FFFFFF
rgb(0,0,0) # returns 000000
rgb(148, 0, 211) # returns 9400D3
rgb를 hex 값을 나타내는 것으로 0보다 작거나 255보다 크면 가까운 수로 나타내야 한다.
def rgb(r, g, b): empty="" for i in (r,g,b): empty+= hex(max(0, min(255,i)))[2:].upper().zfill(2) return empty
max,min을 사용해서 255보다 크면 255가 0보다 작으면 0으로 값을 받을 수 있게 했다.