#!/usr/bin/env python # coding: utf-8 # # Chapter 15 Practice Makes Perfect # # # # In[1]: # 01 연습이 최고를 만듭니다 # there is nothing in this lesson pass # In[2]: # 02 짝수인지 확인하기 def is_even(x): return x % 2 == 0 print (is_even(2)) print (is_even(13)) # In[3]: # 03 정수인지 확인하기 from math import floor def is_int(x): if floor(x) == x: return True else: return False print (is_int(10.1)) # In[4]: # 04 숫자들의 합 def digit_sum(n): ans = 0 while n > 0: ans += n % 10 n = floor(n / 10) # 원문은 2.7 기준 return ans print (digit_sum(3233)) # 참고) # Python3에서 int/int 값은 float가 됩니다. # Python2에서 int/int 값은 int 됩니다. # In[5]: # 05 팩토리얼(factorial) def factorial(x): ans = 1 while x > 0: ans *= x x -= 1 return ans print (factorial(10)) # In[6]: # 06 소수인지 확인하기 def is_prime(x): if x <= 1: return False for i in range(2, x): if x % i == 0: return False return True print (is_prime(13)) # In[7]: # 07 문자열 뒤집기 def reverse(s): res = "" for i in range(len(s) - 1, -1, -1): res += s[i] return res print (reverse("hello")) # KT의 브랜드 olleh가 hello를 reverse 한 것이죠 # In[8]: # 08 자음 걸러내기 def is_vowel(c): c = c.lower() if c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u': return True else: return False def anti_vowel(text): res = "" for i in range(0, len(text)): if not is_vowel(text[i]): res += text[i] return res print (anti_vowel('this is test text')) # In[9]: # 09 스크래블 게임 score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10} def scrabble_score(word): word = word.lower() ans = 0 for c in word: ans += score[c] return ans print (scrabble_score('cansentme')) # In[10]: # 10 문자열 검열 """ method 1: normal way to finish this task """ def censor(text, word): strings = text.split() for i in range(0, len(strings)): if strings[i] == word: strings[i] = "*" * len(word) return " ".join(strings) """ method 2: use KMP algorithm, but it's like employing a steam engine to crack a nut """ def get_next_array(word): nxt = [-1] for i in range(1, len(word)): n = nxt[i - 1] nxt.append(-1) while n != -1: if word[n + 1] == word[i]: nxt[i] = n + 1 break n = nxt[n] if n == -1 and word[0] == word[i]: nxt[i] = 0 return nxt def KMP(text, word, nxt, i): j = 0 while i < len(text) and j < len(word): while j != -1 and text[i] != word[j]: if j == 0: j = -1 else: j = nxt[j-1] + 1 i += 1 j += 1 if j == len(word): return i return -1 def censor(text, word): #use KMP algorithm nxt = get_next_array(word) i = 0 while i < len(text) and i != -1: i = KMP(text, word, nxt, i) if i == -1: break; newtext = text[0: i - len(word)] + "*" * len(word) + text[i :] text = newtext return text print(censor("this is test text", 'test')) # KMP: Knuth Morris Partt 이름을 따서 만들어진 문자열 검색 알고리즘 # In[11]: # 11 요소의 갯수 세기 def count(sequence, item): return sequence.count(item) print(count("this is test text", 't')) # In[12]: # 12 요소 걸러내기 def purify(lst): res = [] for e in lst: if e % 2 == 0: res.append(e) return res print(purify([2, 34, 67, 32, 12, 79])) # In[13]: # 13 곱셈 def product(lst): res = 1 for e in lst: res *= e return res print(product([2, 34, 67, 32, 12, 79])) # In[14]: # 14 중복 요소 제거하기 def remove_duplicates(lst): res = [] for e in lst: if e not in res: res.append(e) return res print(remove_duplicates([2, 12, 67, 2, 12])) # In[15]: # 15 중앙값 def median(lst): lst2 = sorted(lst) length = len(lst2) if length % 2 == 0: return (lst2[int(length / 2)] + lst2[int(length / 2 - 1)]) / 2.0 else: return lst2[int(length / 2)] print(median([2, 12, 67, 2, 12]))