프로그래밍/알고리즘
-
모의고사프로그래밍/알고리즘 2021. 3. 25. 18:06
참고해서 푼 풀이 def solution(answers): student1 = [1, 2, 3, 4, 5] student2 = [2, 1, 2, 3, 2, 4, 2, 5] student3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5] score = [0, 0, 0] result = [] for index, answer in enumerate(answers): if answer == student1[index % len(student1)]: score[0] += 1 if answer == student2[index % len(student2)]: score[1] += 1 if answer == student3[index % len(student3)]: score[2] += 1 for index..
-
문자열 내 p와 y의 개수프로그래밍/알고리즘 2021. 3. 23. 15:50
나의 풀이 def solution(s): answer = True countP = 0 countY = 0 for s in s: if s == chr(80) or s == chr(112): countP += 1 elif s == chr(89) or s == chr(121): countY += 1 if countP == countY: return True else: return False return True s = "pPoooyY" n = "pyY" solution(s) solution(n) 가볍지만 길다. 다른 사람 풀이 1 def numPY(s): return s.lower().count('p') == s.lower().count('y') 모두 소문자로 바꾸고 카운트 함수를 사용했다. 대소문자에서 불편..
-
같은 숫자는 싫어프로그래밍/알고리즘 2021. 3. 23. 14:44
나의 풀이 def solution(arr): answer = [] for i in range(len(arr)): if i == 0: answer.append(arr[i]) elif arr[i] != answer[-1]: answer.append(arr[i]) return answer 다른 사람 풀이 1 def no_continuous(s): a = [] for i in s: if a[-1:] == [i]: continue a.append(i) return a 빈 배열에 의미없은 슬라이싱(?)을 해도 인덱스 에러가 나지 않는 것을 활용해 숫자가 같으면 continue 아니면 리스트에 추가하는 방법으로 해결하였다.
-
가운데 글자 가져오기프로그래밍/알고리즘 2021. 3. 23. 11:31
내가 한 풀이 제출 테스트에서 2번에서 계속 실패했다. def solution(s): answer = '' if len(s) < 2: answer = s return answer if len(s) % 2 == 0: # 짝수 count = (len(s) // 2) - 1 answer = s[count:-count] return answer else: # 홀수 count = int(len(s) // 2) answer = s[count:-count] return answer 다른 사람 풀이 1 def solution(s): if len(s) % 2 == 1: return s[len(s)//2] else: return s[(len(s)//2-1) : (len(s)//2+1)] 내가 짜놓은 코드와 비슷하지만 더 ..
-
문자열 다루기 기본프로그래밍/알고리즘 2021. 3. 22. 16:09
def solution(s): if len(s) == 4 or len(s) == 6: for i in range(len(s)): if not s[i].isdigit(): return False return True else: return False 다른사람 풀이 1 def alpha_string46(s): return s.isdigit() and len(s) in (4, 6) * if-else를 and로 바꿔서 True/False 반환 다른사람 풀이 2 def alpha_string46(s): try: int(s) except: return False return len(s) == 4 or len(s) == 6 * 풀면서 고민했었던 예외처리로 푸는 방법