본문 바로가기

데이터 - 기본 코드 및 알고리즘 연습/Codewar

(13)
[Codewars] Regex Password Validation ▶ 문제 : ▶ 내 답안 : 특수문자제거&대문자&소문자&숫자 regex = "^(?!(?=.*[!@#$^&%*()+=\_\-\[\]\\\/{}|:?,. ]).)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{6,}" ▶ 모범답안1 : regex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[^\W_]{6,}$" ▶ 모범답안2 : from re import compile, VERBOSE regex = compile(""" ^ # begin word (?=.*?[a-z]) # at least one lowercase letter (?=.*?[A-Z]) # at least one uppercase letter (?=.*?[0-9]) # at least one number [..
[Codewars] Car Park Escape 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 escape(carpark): if carpark == [[0, 0, 0, 0, 2]] : return [] command = [] D = 1 i = 0 while i stai..
[Codewars] validDate Regex ▶ 문제 : ▶ 내 답안 : import re valid_date = re.compile(r"""(?!\[00-\d*\])(?!\[\d*-00\]) #00-dd, #dd-00 제거 (?!\[1[3-9]\d*-[0-3]\d*\]) (?!\[[0-1]\d*-3[2-9]\d*\]) (?!\[02-29\])(?!\[02-30\])(?!\[02-31\]) (?!\[04-31\])(?!\[06-31\])(?!\[09-31\])(?!\[11-31\]) (?!\[[0-1]\d*-3[2-9]\d*\]) # 32이상 제거 \[[0-1]\d*-[0-3]\d*\]""", re.VERBOSE) ▶ 모범답안 : import re # Months - Days valid_date = re.compile(r""" \[ (0[13578..
[Codewars] Sine, cosine and others ▶ 문제 : 사인, 코사인, 탄젠트, 코탄젠트를 구하라 - value로 사인값이 주어진다. - list 안에 [ sin(사인), cos(코사인), tan(탄젠트), cot(코탄젠트) ] 값을 넣어 리턴하라. - 조건1) 모든 숫자는 2 demical place까지 반올림하라 (round) - 조건2) 탄젠트 or 코탄젠트 값을 계산할 수 없을 때는 리스트 안에 포함시키지 말 것. test.describe('Basic tests') test.assert_equals(sctc(1),[1, 0.0, 0.0]) test.assert_equals(sctc(1/2),[0.5, 0.87, 0.58, 1.73]) test.assert_equals(sctc(3 ** 0.5 / 2),[0.87, 0.5, 1.73, 0.5..
[Codewars] Double Every Other ▶ 문제 : ▶ 내 답안 : def increment_string(string): if string == "": return "1" try: if len(str(int(string)+1)) > len(string): return str(int(string) + 1) else: return "0"*(len(string) - len(str(int(string)+1))) + str(int(string)+1) except: for i, v in enumerate(string[::-1]): if v not in [str(x) for x in range(10)]: break org_str=string[:-i] if org_str == "" : return string + "1" else : org_num=strin..
[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) ▶ 배워야할 부분 : 가독성이 좋은 코드 > 짧은 코드
[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 문