본문 바로가기

분류 전체보기

(33)
[Codewars] Square Every Digit ▶ 문제 : ▶ 내 답안 : def square_digits(num): return int(''.join([ str(int(i)**2) for i in str(num) ])) ▶ 모범답안 : def square_digits(num) : ret = "" for x in str(num) : ret += str(int(x)**2) return int(ret) ▶ 배워야할 부분 : 가독성이 좋은 코드 > 짧은 코드
[프로그래머스] 나머지 한 점 알고리즘 문제 해설 - 나머지 한 점 프로그래머스의 모의테스트는 프로그래머스의 시스템에 익숙해지기 위한 테스트이며, 문제 자체는 2018 1ST KAKAO BLIND RECRUITMENT와 전혀 관계없습니다. 다만 모의테스트의 풀이에 대한 요청이 있어 programmers.co.kr ▶ 내답안 : collections import collections as cl def solution(v): coord = list(map(list,zip(*v))) x = cl.Counter(coord[0]).most_common()[-1][0] y = cl.Counter(coord[1]).most_common()[-1][0] return [x,y] - 직사각형의 네점의 좌표( x, y )는 2쌍씩 존재한다. - 1쌍만 ..
[Codewars] Connect Four Codewars: Achieve mastery through challenge Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential. www.codewars.com ▶ 문제 : ▶ 내 답안 : 시간초과로 실패 import pandas as pd def sameguys(con4) : r = ['Red','Red','Red','Red'] y = ['Yellow', 'Yellow', 'Yellow','Yellow'] if con4 == r : return "Red" elif con4 == y : return "Yellow" def who..
[Codewars] Product of consecutive Fib numbers ▶ 문제 : ▶ 내 답안 : def F(n) : a, b = 0, 1 for i in range(n): a, b = b, a+b return a def productFib(prod): answer = [] n = 0 while True : r_1 = F(n) r_2 = F(n+1) if r_1*r_2 > prod : return [r_1, r_2, False] if r_1*r_2 == prod : return [r_1, r_2, True] n += 1 ▶ 모범답안 : def productFib(prod): a, b = 0, 1 while prod > a * b: a, b = b, a + b return [a, b, prod == a * b] ▶ 배워야할 부분 : while 문
[프로그래머스] i번째 원소와 i+1번째 원소 파이썬을 파이썬답게 - i번째 원소와 i+1번째 원소 본 강의는 파이썬 문법을 이미 알고 있는 분들을 대상으로 만들어졌습니다. ##### 이런 분들께 추천합니다 * 파이썬 문법을 알고 계시는 분 * 알고리즘 문제를 조금 더 쉽게 풀고 싶은 분 * Python 코 programmers.co.kr - len과 인덱스 활용 def solution(mylist): output = [] for i in range(len(mylist)-1) : output.append(abs(mylist[i] - mylist[i+1])) return output - zip 사용 ( 길이가 짧은 쪽까지만 돔 ) def solution(mylist): answer = [] for number1, number2 in zip(mylist..
[Codewars] Kebabize Codewars: Achieve mastery through challenge Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential. www.codewars.com ▶ 문제 : ▶ 내 답안 : import re def kebabize(string): string = re.sub('[0-9]','',string) a = set([0]+[i for i in range(len(string)) if string[i] != string.lower()[i]]+[len(string)]) b = sorted(list(a)) c = [] for i..
[Codewars] Integers: Recreation One ▶ 문제 : ▶ 내 답안 : import math def list_squared(m, n): result = [] for i in range(m,n+1) : a = [ v for v in range(1, int(math.sqrt(i))+1) if i%v ==0 ] b = [ int(i/v) for v in range(1, int(math.sqrt(i))+1) if i%v ==0 ] c = list(set(a + b)) sq_sum = sum([math.pow(v,2) for v in c]) if math.sqrt(sq_sum) % 1 == 0 : result.append([i,int(sq_sum)]) return result - N의 모든 약수를 탐색하는 시간(이중 for문의 시간 복잡도) : O(N) ..
[Codewars] Build Tower Codewars: Achieve mastery through challenge Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential. www.codewars.com ▶ 문제 : ▶ 내 답안 : def tower_builder(n_floors): b = [ '*' + '*'*(2*i) for i in range(n_floors)] for i, v in enumerate(b) : b[i] = ' '*int((len(b[-1])-len(v))/2) + v + ' '*int((len(b[-1])-len(v))/2) return b ▶ 모범..