4.1 단원은 주어진 리스트에서 합을 최대로 갖는 부분 합을 찾는 방법에 대해 공부했다. 앞에서 했던 merge sort와 비슷하게 리스트를 반으로 나누어서 최대 값을 찾아주면 된다. 그러나 한가지 경우가 더 있는데, 리스트를 나누어준 중간 값을 지나는 부분 합이다. find_maximum_cross 라는 함수를 추가로 정의해서 그 합을 왼쪽 리스트의 부분합, 오른쪽 리스트의 부분합 이렇게 세 개의 합과 비교해서 가장 큰 부분합의 시작인덱스와 끝 인덱스를 반환한다. python으로 작성을 해보았는데 쓸데없이 길어보이는 부분이 있지만 너무 귀찮아서 책에 있는 수도코드를 참고했다.def find_maximum_sub(a, low_index, high_index): if low_index == high_ind..
1. merge sort 직접 구현하기def merge_sort(alist): div = int(len(alist)/2) if div > 0 : return merge(merge_sort(alist[:div]), merge_sort(alist[div:])) else : return alist def merge(alist,blist): merged_list = [] i=0 j=0 while(i list[div]: return binary_search(value,list[div:]) else: return binary_search(value,list[:div]) else: if value == list[0]: print("yes") else: print("no")파이썬으로 구현한 binary_search 이..
Anaconda Prompt에서 개발환경 관리를 위해 사용하는 명령어는 아래와 같다.conda search python : 사용가능한 버전목록을 보여줌conda create -n py34 python python=3.4 anaconda : py34라는 이름으로 python3.4 개발환경을 하나 더 만들어줌activate py34 , deactivate py34 : 개발환경 변경가능 각 환경별로 패키지를 따로 관리한다.conda install python=3.4 : 아예 아나콘다의 기본파이썬 버전을 바꾸는 명령어
문제 링크https://www.codewars.com/kata/take-a-number-and-sum-its-digits-raised-to-the-consecutive-powers-and-dot-dot-dot-eureka/train/python 내 답def sum_dig_pow(a, b): list = [] for i in range(a,b+1): sum = 0 for n,k in enumerate(str(i),1): sum += int(k)**n if i == sum: list.append(i) return list 득점 많은 답def dig_pow(n): return sum(int(x)**y for y,x in enumerate(str(n), 1)) def sum_dig_pow(a, b): retur..
문제링크https://www.codewars.com/kata/554e4a2f232cdd87d9000038/train/python문자열에서 각 문자를 대응되는 문자로 바꿔주는 문제조건이 네가지 밖에 없어서 if와 elif를 이용하면 쉽게 할 수 있다.추천순 높은 두 가지 정답을 살펴보면 아래와 같다def DNA_strand(dna): return dna.translate(str.maketrans("ATCG","TAGC"))python 3.4부터 기본으로 지원되는 str.maketrans를 이용했다.intab과 outtab을 변수로 받아서 translate에 쓸 수 있는 transtable을 만들어준다.maketrans 메서드 설명은 아래 링크 참고함https://www.tutorialspoint.com/p..
문제 링크 https://www.codewars.com/kata/53368a47e38700bd8300030d/train/python딕셔너리 배열로 이름을 입력 받아서 정해진 포맷으로 출력하는 문제이다. 내 풀이는 아래와 같다.def namelist(names): str = '' for i,dic in enumerate(names, 1): if i != len(names) : str = str + dic['name'] + ', ' elif len(names) == 1 : str = dic['name'] else : str = str[:-2] + ' & ' + dic['name'] return strnames의 길이가 1인경우 이름만 출력하게 해주고 나머지 경우는 전부 이름뒤에 ', '을 붙여줬다. 그리고 ..
Count the number of Duplicates Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits. Example "abcde" -> 0 # no characters repeats more than once"aabbcde" -> 2 # 'a' and 'b'"aabBcde" ..
- Total
- Today
- Yesterday
- conda
- codewars
- 마스터킹
- 멜킨스포츠
- anaconda
- CHUWI HI8
- 하스스톤
- 개봉기
- PYTHON
- 연습문제
- Introduction to algorithms
- introduction to algorithms third edtion
- 치닝디핑
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |