├── README.md ├── 052-6kyu-Does my number look big in this.py ├── 028-6kyu-Array diff.py ├── 027-6kyu-1RM Calculator.py ├── 009-6kyu-Factorial length.py ├── 011-6kyu-Factorial length.py ├── 017-6kyu-Is a number prime.py ├── 045-6kyu-Two Sum ├── 046-5kyu-The Hashtag Generator.py ├── 012-6kyu-Decode the Morse code.py ├── 030-6kyu-Detect Pangram.py ├── 041-5kyu-Pete, the baker.py ├── 003-6kyu-Find-the-odd-int.py ├── 050-6kyu-Write Number in Expanded Form.py ├── 020-6kyu-Kebabize.py ├── 034-6kyu-Tribonacci Sequence.py ├── 024-5kyu-Where my anagrams at.py ├── 005-6kyu-Sum-of-Digits.py ├── 040-5kyu-Directions Reduction.py ├── 049-6kyu-Break camelCase.py ├── 048-6kyu-Which are in.py ├── 031-6kyu-Delete occurrences of an element if it occurs more than n times.py ├── 004-6kyu-Persistent-Bugger.py ├── 043-5kyu-First non-repeating character.py ├── 023-5kyu-Happy^^ numbers.py ├── 025-6kyu-Persistent Bugger.py ├── 022-6kyu-split strings.py ├── 015-6kyu-Sort the odd.py ├── 016-5kyu-Convert string to camel case.py ├── 042-5kyu-Weight for weight.py ├── 053-6kyu-Take a Ten Minutes Walk.py ├── 047-5kyu-Maximum subarray sum.py ├── 008-4kyu-Valid Braces.py ├── 014-6kyu-Largest 5 digit number in a series.py ├── 051-6kyu-Highest Scoring Word.py ├── 033-6kyu-Replace With Alphabet Position.py ├── 035-6kyu-Stop gninnipS My sdroW!.py ├── 044-6kyu-Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....¡Eureka!!.py ├── 029-6kyu-Ore Numbers.py ├── 039-6kyu-Create a frame!.py ├── 006-6kyu-Build-Tower.py ├── 021-6kyu-Count the smiley faces!.py ├── 038-6kyu-Your order, please.py ├── 013-6kyu-The 5 Love Languages.py ├── 026-6kyu-Travelling on a Grid.py ├── 018-5kyu-Gap in Primes.py ├── 019-4kyu-Strip Comments.py ├── 010-6kyu-Clock in Mirror.py ├── 001-5kyu-prime-in-numbers.py ├── 002-6kyu-Identify-the-array's-ordering.py ├── 007-4kyu-Number of Proper Fractions with Denominator d.py ├── 037-4kyu-Sudoku Solution Validator.py ├── 032-4kyu-Next bigger number with the same digits.py └── 036-4kyu-A Simplistic TCP Finite State Machine (FSM).py /README.md: -------------------------------------------------------------------------------- 1 | Codewars python solutions. 2 | -------------------------------------------------------------------------------- /052-6kyu-Does my number look big in this.py: -------------------------------------------------------------------------------- 1 | def narcissistic(value): 2 | return value == sum([int(digit) ** len(str(value)) for digit in str(value)]) -------------------------------------------------------------------------------- /028-6kyu-Array diff.py: -------------------------------------------------------------------------------- 1 | def array_diff(a, b): 2 | for i in b: 3 | if i in a: 4 | for j in range(a.count(i)): 5 | a.remove(i) 6 | return a 7 | -------------------------------------------------------------------------------- /027-6kyu-1RM Calculator.py: -------------------------------------------------------------------------------- 1 | def calculate_1RM(w, r): 2 | if r == 1: 3 | return w 4 | if r == 0: 5 | return 0 6 | return round(max(w*(1+r/30),100*w/(101.3-2.67123*r),w*(r**0.1))) 7 | -------------------------------------------------------------------------------- /009-6kyu-Factorial length.py: -------------------------------------------------------------------------------- 1 | #https://www.codewars.com/kata/factorial-length/python 2 | import math 3 | def count(n): 4 | return int(math.ceil(math.log(2 * math.pi * n, 10) / 2 + n * math.log(n / math.e, 10))) 5 | -------------------------------------------------------------------------------- /011-6kyu-Factorial length.py: -------------------------------------------------------------------------------- 1 | #https://www.codewars.com/kata/59f34ec5a01431ab7600005a 2 | import math 3 | def count(n): 4 | return int(math.ceil(math.log(2 * math.pi * n, 10) / 2 + n * math.log(n / math.e, 10))) 5 | -------------------------------------------------------------------------------- /017-6kyu-Is a number prime.py: -------------------------------------------------------------------------------- 1 | def is_prime(n): 2 | if n <= 0 or n == 1: 3 | return False 4 | i = 2 5 | while (i <= n ** 0.5 ): 6 | if n % i == 0: 7 | return False 8 | i += 1 9 | return True 10 | -------------------------------------------------------------------------------- /045-6kyu-Two Sum: -------------------------------------------------------------------------------- 1 | def two_sum(numbers, target): 2 | num2index = dict() 3 | for i, num in enumerate(numbers): 4 | if target - num in num2index: 5 | return [i, num2index[target - num]] 6 | num2index[num] = i -------------------------------------------------------------------------------- /046-5kyu-The Hashtag Generator.py: -------------------------------------------------------------------------------- 1 | def generate_hashtag(s): 2 | #your code here 3 | if not s: 4 | return False 5 | res = "#" + "".join([word.capitalize() for word in s.split()]) 6 | return res if len(res) < 140 else False -------------------------------------------------------------------------------- /012-6kyu-Decode the Morse code.py: -------------------------------------------------------------------------------- 1 | #https://www.codewars.com/kata/54b724efac3d5402db00065e 2 | def decodeMorse(morse_code): 3 | return ' '.join(''.join(MORSE_CODE[i] for i in word.split(' ')) for word in morse_code.strip().split(" ")) 4 | 5 | -------------------------------------------------------------------------------- /030-6kyu-Detect Pangram.py: -------------------------------------------------------------------------------- 1 | import string 2 | 3 | def is_pangram(s): 4 | set = [] 5 | s = s.lower() 6 | for i in s: 7 | if i.isalpha(): 8 | if i not in set: 9 | set.append(i) 10 | return len(set) == 26 11 | -------------------------------------------------------------------------------- /041-5kyu-Pete, the baker.py: -------------------------------------------------------------------------------- 1 | def cakes(recipe, available): 2 | res = float("inf") 3 | for key, val in recipe.items(): 4 | if key in available: 5 | res = min(res, available[key]// val ) 6 | else: 7 | return 0 8 | return res -------------------------------------------------------------------------------- /003-6kyu-Find-the-odd-int.py: -------------------------------------------------------------------------------- 1 | # https://www.codewars.com/kata/54da5a58ea159efa38000836 2 | import collections 3 | 4 | def find_it(seq): 5 | if len( seq ) == 0: 6 | return None 7 | b = collections.Counter(seq) 8 | for c in b: 9 | if b[c] % 2 ==1: 10 | return c 11 | -------------------------------------------------------------------------------- /050-6kyu-Write Number in Expanded Form.py: -------------------------------------------------------------------------------- 1 | def expanded_form(num): 2 | res = [] 3 | cnt = 0 4 | while num: 5 | num, m = divmod(num, 10) 6 | if m: 7 | res.append(m * (10 ** cnt)) 8 | cnt += 1 9 | return " + ".join([str(x) for x in res[::-1]]) -------------------------------------------------------------------------------- /020-6kyu-Kebabize.py: -------------------------------------------------------------------------------- 1 | def kebabize(string): 2 | res = "" 3 | for char in string: 4 | if char == char.lower() and char.isalpha(): 5 | res += char 6 | elif char == char.upper() and char.isalpha(): 7 | res = res + '-' + char 8 | return res.lstrip('-').lower() 9 | -------------------------------------------------------------------------------- /034-6kyu-Tribonacci Sequence.py: -------------------------------------------------------------------------------- 1 | def tribonacci(signature, n): 2 | if n == 0: 3 | return [] 4 | if n < 3: 5 | return [signature[i] for i in range(0,n)] 6 | res = signature[:] 7 | for i in range(3,n): 8 | res.append(res[i-1] + res[i-2] + res[i-3]) 9 | return res -------------------------------------------------------------------------------- /024-5kyu-Where my anagrams at.py: -------------------------------------------------------------------------------- 1 | def anagrams(s, words): 2 | res = [] 3 | s = "".join((lambda x:(x.sort(),x)[1])(list(s))) 4 | for w in words: 5 | t = sorted(list(w)) 6 | temp = "".join(x for x in t) 7 | if temp == s: 8 | res.append(w) 9 | return res 10 | -------------------------------------------------------------------------------- /005-6kyu-Sum-of-Digits.py: -------------------------------------------------------------------------------- 1 | # https://www.codewars.com/kata/541c8630095125aba6000c00 2 | def digital_root(n): 3 | i = 1 4 | temp = 1 5 | t = 0 6 | while n > 9: 7 | t = 0 8 | temp = str (n) 9 | for j in range(len(temp)): 10 | t += int(temp[j]) 11 | n = t 12 | return t 13 | -------------------------------------------------------------------------------- /040-5kyu-Directions Reduction.py: -------------------------------------------------------------------------------- 1 | def dirReduc(arr): 2 | dict = {"NORTH":"SOUTH","SOUTH":"NORTH","EAST":"WEST","WEST":"EAST"} 3 | res = [] 4 | for i in arr: 5 | if res and dict[i] == res[-1]: 6 | res.pop() 7 | else: 8 | res.append(i) 9 | return res 10 | -------------------------------------------------------------------------------- /049-6kyu-Break camelCase.py: -------------------------------------------------------------------------------- 1 | def solution(s): 2 | last_upper_index = 0 3 | res = [] 4 | for i, char in enumerate(s): 5 | if char.isupper(): 6 | res.append(s[last_upper_index:i]) 7 | last_upper_index = i 8 | res.append(s[last_upper_index:]) 9 | return " ".join(res) -------------------------------------------------------------------------------- /048-6kyu-Which are in.py: -------------------------------------------------------------------------------- 1 | def in_array(array1, array2): 2 | # your code 3 | s = set(array2) 4 | res = [] 5 | for word in array1: 6 | for w in array2: 7 | if word in w and word not in res: 8 | res.append(word) 9 | break 10 | return sorted(res) -------------------------------------------------------------------------------- /031-6kyu-Delete occurrences of an element if it occurs more than n times.py: -------------------------------------------------------------------------------- 1 | def delete_nth(order,max_e): 2 | temp = {} 3 | res = [] 4 | for i in order: 5 | if i not in temp : 6 | temp[i] = 0 7 | else: 8 | temp[i] += 1 9 | if temp[i] 9: 9 | temp = str (n) 10 | for j in range(len(temp)): 11 | t *= int(temp[j]) 12 | s += 1 13 | n = t 14 | t = 1 15 | return s 16 | -------------------------------------------------------------------------------- /043-5kyu-First non-repeating character.py: -------------------------------------------------------------------------------- 1 | def first_non_repeating_letter(s): 2 | from collections import defaultdict 3 | char2freq = defaultdict(int) 4 | 5 | for char in s: 6 | char2freq[char.lower()] += 1 7 | 8 | for char in s: 9 | if char2freq[char.lower()] == 1: 10 | return char 11 | return "" -------------------------------------------------------------------------------- /023-5kyu-Happy^^ numbers.py: -------------------------------------------------------------------------------- 1 | def is_happy(n): 2 | for i in range(100): 3 | k = check(n) 4 | if k == 1: 5 | return True 6 | else: 7 | n = k 8 | return False 9 | def check(n): 10 | sum = 0 11 | while ( n > 9 ): 12 | sum += (n % 10) ** 2 13 | n = n - n % 10 14 | n /= 10 15 | return sum + n ** 2 16 | -------------------------------------------------------------------------------- /025-6kyu-Persistent Bugger.py: -------------------------------------------------------------------------------- 1 | def persistence(n): 2 | res = 0 3 | temp = 1 4 | while (n > 9): 5 | if temp == 1: 6 | temp = n % 10 7 | n -= n %10 8 | n /= 10 9 | temp *= (n % 10) 10 | if (n <= 9) : 11 | n = temp 12 | temp = 1 13 | res += 1 14 | return res 15 | -------------------------------------------------------------------------------- /022-6kyu-split strings.py: -------------------------------------------------------------------------------- 1 | #https://www.codewars.com/kata/515de9ae9dcfc28eb6000001 2 | def solution(s): 3 | if len(s) == 0: 4 | return [] 5 | res = [] 6 | for i in range(len(s)): 7 | if i % 2 == 0 : 8 | res.append(s[i]) 9 | else: 10 | res[(i-1) / 2] += s[i] 11 | if len(res[-1]) == 1: 12 | res[-1] += '_' 13 | return res 14 | -------------------------------------------------------------------------------- /015-6kyu-Sort the odd.py: -------------------------------------------------------------------------------- 1 | def sort_array(s): 2 | if len(s) == 0: 3 | return s 4 | odd = [] 5 | for i in range(len(s)): 6 | if s[i] % 2 == 1: 7 | odd.append(s[i]) 8 | s[i] = -999 9 | odd.sort() 10 | k = 0 11 | for i in range(len(s)): 12 | if s[i] == -999: 13 | s[i] = odd[k] 14 | k += 1 15 | 16 | return s 17 | -------------------------------------------------------------------------------- /016-5kyu-Convert string to camel case.py: -------------------------------------------------------------------------------- 1 | sp = ['-','_'] 2 | def to_camel_case(text): 3 | if len(text) == 0: 4 | return text 5 | text = text.replace("_","-") 6 | s = text.split("-") 7 | for i in range(len(s)): 8 | if i == 0: 9 | continue 10 | else: 11 | s[i] = s[i].capitalize() 12 | res = "".join(i for i in s) 13 | return res 14 | -------------------------------------------------------------------------------- /042-5kyu-Weight for weight.py: -------------------------------------------------------------------------------- 1 | def order_weight(string): 2 | res = [] 3 | for num in string.split(): 4 | res.append([num, getWeight(num)]) 5 | res.sort(key = lambda x: (x[1], x)) 6 | return " ".join([num for num, w in res]) 7 | 8 | def getWeight(num): 9 | num = int(num) 10 | s = 0 11 | while num: 12 | num, m = divmod(num, 10) 13 | s += m 14 | return s -------------------------------------------------------------------------------- /053-6kyu-Take a Ten Minutes Walk.py: -------------------------------------------------------------------------------- 1 | def is_valid_walk(walk): 2 | #determine if walk is valid 3 | i, j = 0, 0 4 | if len(walk) != 10: 5 | return False 6 | 7 | for w in walk: 8 | if w == "n": 9 | j += 1 10 | elif w == "s": 11 | j -= 1 12 | elif w == "w": 13 | i -= 1 14 | else: 15 | i += 1 16 | return i == 0 and j == 0 -------------------------------------------------------------------------------- /047-5kyu-Maximum subarray sum.py: -------------------------------------------------------------------------------- 1 | def max_sequence(nums): 2 | if not nums: 3 | return 0 4 | if all(num < 0 for num in nums): 5 | return 0 6 | dp = [num for num in nums] 7 | 8 | res = nums[0] 9 | for i, num in enumerate(nums): 10 | if i: 11 | if dp[i - 1] > 0: 12 | dp[i] = max(dp[i], num + dp[i - 1]) 13 | res = max(dp[i], res) 14 | 15 | return res -------------------------------------------------------------------------------- /008-4kyu-Valid Braces.py: -------------------------------------------------------------------------------- 1 | def validBraces(string): 2 | a = "[]" 3 | b = "{}" 4 | c = "()" 5 | while (string.find(a) != -1) or (string.find(b) != -1) or (string.find(c) != -1): 6 | if (string.find(a) != -1): 7 | string=string.replace(a,"") 8 | if (string.find(b) != -1): 9 | string=string.replace(b,"") 10 | if (string.find(c) != -1): 11 | string=string.replace(c,"") 12 | return not len(string) 13 | -------------------------------------------------------------------------------- /014-6kyu-Largest 5 digit number in a series.py: -------------------------------------------------------------------------------- 1 | #https://www.codewars.com/kata/51675d17e0c1bed195000001 2 | def solution(digits): 3 | l = len(digits) 4 | digits = str(digits) 5 | start = 0 6 | end = 5 7 | max = 0 8 | while 1: 9 | temp = digits[start:end] 10 | if int(temp) > max: 11 | max = int(temp) 12 | start += 1 13 | end +=1 14 | if end == l +1: 15 | break 16 | return max 17 | -------------------------------------------------------------------------------- /051-6kyu-Highest Scoring Word.py: -------------------------------------------------------------------------------- 1 | def high(words): 2 | # Code here 3 | max_word, max_score = "", 0 4 | for word in words.split(): 5 | score = getScore(word) 6 | if score > max_score: 7 | max_score = score 8 | max_word = word 9 | return max_word 10 | 11 | 12 | def getScore(word): 13 | score = 0 14 | for char in word: 15 | score += ord(char) - ord("a") + 1 16 | return score -------------------------------------------------------------------------------- /033-6kyu-Replace With Alphabet Position.py: -------------------------------------------------------------------------------- 1 | def alphabet_position(text): 2 | text = text.lower() 3 | print text 4 | res = "" 5 | index = 0 6 | while index < len(text): 7 | item = text[index] 8 | if ord(item) > ord("z") or ord(item) < ord("a"): 9 | index += 1 10 | continue 11 | # print item,ord(item)-ord("a") 12 | res += str(ord(item)-ord("a")+1) + " " 13 | index += 1 14 | return res[:-1] -------------------------------------------------------------------------------- /035-6kyu-Stop gninnipS My sdroW!.py: -------------------------------------------------------------------------------- 1 | def spin_words(sentence): 2 | # Your code goes here 3 | if len(sentence) == 0: 4 | return None 5 | words = sentence.split(" ") 6 | for wordindex,word in enumerate(words): 7 | if len(word)>=5: 8 | words[wordindex] = reverse(word) 9 | # print words 10 | return " ".join(word for word in words) 11 | 12 | 13 | def reverse(word): 14 | rev=word[::-1] 15 | return rev 16 | -------------------------------------------------------------------------------- /044-6kyu-Take a Number And Sum Its Digits Raised To The Consecutive Powers And ....¡Eureka!!.py: -------------------------------------------------------------------------------- 1 | def sum_dig_pow(a, b): # range(a, b + 1) will be studied by the function 2 | # your code here 3 | res = [] 4 | for i in range(a, b + 1): 5 | if i == getSpecialSum(i): 6 | res.append(i) 7 | return res 8 | 9 | def getSpecialSum(n): 10 | digits = [int(d) for d in str(n)] 11 | res = 0 12 | for i, d in enumerate(digits): 13 | res += d ** (i + 1) 14 | return res -------------------------------------------------------------------------------- /029-6kyu-Ore Numbers.py: -------------------------------------------------------------------------------- 1 | def is_ore(n): 2 | temp = 0.0 3 | divisor = [] 4 | count = 0 5 | for i in range(1,int(n ** 0.5) + 1): 6 | if n % i == 0: 7 | if i ** 2 != n: 8 | divisor.append(i) 9 | divisor.append(n / i) 10 | count += 2 11 | else: 12 | divisor.append(i) 13 | count += 1 14 | for i in divisor: 15 | temp += 1.0 / i 16 | print i,temp,count, 48/3.2 17 | return (count / temp) - int( count / temp) < 1e-5 18 | -------------------------------------------------------------------------------- /039-6kyu-Create a frame!.py: -------------------------------------------------------------------------------- 1 | def frame(text, char): 2 | max_l = 0 3 | for item in text: 4 | if len(item) > max_l: 5 | max_l = len(item) 6 | 7 | frame_l = max_l + 4 8 | frame = char * frame_l + "\n" 9 | 10 | temp_frame = "" 11 | for i in range(len(text)): 12 | temp_frame = char + " " + text[i] + " " * (max_l - len(text[i])) + " " + char + "\n" 13 | frame = frame + temp_frame 14 | 15 | frame += char * frame_l 16 | return frame 17 | -------------------------------------------------------------------------------- /006-6kyu-Build-Tower.py: -------------------------------------------------------------------------------- 1 | # https://www.codewars.com/kata/576757b1df89ecf5bd00073b 2 | def tower_builder(n_floors): 3 | # build here 4 | res = [] 5 | temp = '' 6 | for i in range(n_floors): 7 | temp = '' 8 | for t in range(n_floors-i-1): 9 | temp += ' ' 10 | print temp 11 | for j in range(2*i+1): 12 | temp += '*' 13 | print temp 14 | for k in range(n_floors-i-1): 15 | temp += ' ' 16 | res.append(temp) 17 | print res 18 | return res 19 | -------------------------------------------------------------------------------- /021-6kyu-Count the smiley faces!.py: -------------------------------------------------------------------------------- 1 | def count_smileys(arr): 2 | res = 0 3 | for i in arr: 4 | if check(i): 5 | res += 1 6 | return res#the number of valid smiley faces in array/list 7 | def check(i): 8 | if i[0] != ':' and i[0] != ';': 9 | return False 10 | if len(i) == 3 and i[1] != '-' and i[1] != '~': 11 | return False 12 | if len(i) == 3 and i[2] != ')' and i[2] != 'D': 13 | return False 14 | if len(i) == 2 and i[1] != ')' and i[1] != 'D': 15 | return False 16 | return True 17 | -------------------------------------------------------------------------------- /038-6kyu-Your order, please.py: -------------------------------------------------------------------------------- 1 | def order(sentence): 2 | dict = {} 3 | list = [] 4 | sentence_split = sentence.split(" ") 5 | 6 | for item in sentence_split: 7 | for char in item: 8 | a_code = ord(char) 9 | if a_code >= ord("0") and a_code <= ord("9"): 10 | dict[char] = sentence_split.index(item) 11 | list.append(a_code - ord("0")) 12 | break 13 | list.sort() 14 | 15 | res = [] 16 | for i in list: 17 | res.append(sentence_split[dict[str(i)]]) 18 | 19 | return " ".join(item for item in res) 20 | -------------------------------------------------------------------------------- /013-6kyu-The 5 Love Languages.py: -------------------------------------------------------------------------------- 1 | #https://www.codewars.com/kata/5aa7a581fd8c06b552000177 2 | import random 3 | import numpy as np 4 | def love_language(partner, weeks): 5 | # your code here 6 | records = [0, 0, 0, 0, 0] 7 | print (LOVE_LANGUAGES) 8 | for i in range( 7 * weeks): 9 | ran = np.random.randint(0,4) 10 | respons = partner.response(LOVE_LANGUAGES[ran]) 11 | if 'positive' == respons: 12 | records[ran] += 1 13 | else: 14 | records[ran] -= 1 15 | print (records) 16 | return LOVE_LANGUAGES[records.index(max(records))] 17 | 18 | -------------------------------------------------------------------------------- /026-6kyu-Travelling on a Grid.py: -------------------------------------------------------------------------------- 1 | def travel_chessboard(s): 2 | s=s.replace(')(',',') 3 | s=s.replace(' ',',') 4 | s=s.replace(')','') 5 | s=s.replace('(','') 6 | string = s.split(',') 7 | x1 = s[0] 8 | y1 = s[2] 9 | x2 = s[4] 10 | y2 = s[6] 11 | a = [[0] * 8 for i in range(8)] 12 | for i in range(8): 13 | for j in range(8): 14 | if i == 0: 15 | a[i][j] = 1 16 | elif j == 0: 17 | a[i][j] = 1 18 | else: 19 | a[i][j] = a[i-1][j] + a[i][j-1] 20 | print a 21 | return a[int(x2)-int(x1)][int(y2)-int(y1)] 22 | -------------------------------------------------------------------------------- /018-5kyu-Gap in Primes.py: -------------------------------------------------------------------------------- 1 | def gap(g, m, n): 2 | start = 0 3 | end = 0 4 | for i in range(m,n+1): 5 | if is_prime(i): 6 | print i 7 | if start == 0: 8 | start = i 9 | elif end == 0: 10 | end = i 11 | else: 12 | start = end 13 | end = i 14 | if end - start == g: 15 | return [start, end] 16 | return None 17 | 18 | 19 | 20 | 21 | def is_prime(n): 22 | if n <= 0 or n == 1: 23 | return False 24 | i = 2 25 | while (i <= n ** 0.5 ): 26 | if n % i == 0: 27 | return False 28 | i += 1 29 | return True 30 | -------------------------------------------------------------------------------- /019-4kyu-Strip Comments.py: -------------------------------------------------------------------------------- 1 | def solution(string,markers): 2 | #your code here 3 | c = '\xc2' # for solving special unknown bugs 4 | print c 5 | print string, markers 6 | res = "" 7 | s = string.split('\n') 8 | for i in s: 9 | # print i 10 | position = -1 11 | for j in range(len(i)): 12 | print i[j] 13 | if i[j] in markers: 14 | print i[j],i 15 | position = j 16 | break 17 | if i[j] == c: 18 | print i[j],i 19 | position = j 20 | break 21 | if position != -1: 22 | i = i[0:position] 23 | res += i.rstrip() + "\n" 24 | return res[0:-1] 25 | -------------------------------------------------------------------------------- /010-6kyu-Clock in Mirror.py: -------------------------------------------------------------------------------- 1 | #https://www.codewars.com/kata/56548dad6dae7b8756000037 2 | def what_is_the_time(time_in_mirror): 3 | hour = int(time_in_mirror[0:2]) 4 | minute = int(time_in_mirror[3:5]) 5 | 6 | if hour < 11: 7 | hour1 = 11 - hour 8 | else: 9 | hour1 = 23 - hour 10 | 11 | minute1 = 60 - minute 12 | if minute1 == 60: 13 | minute1 -=60 14 | hour1 += 1 15 | if hour1 > 12: 16 | hour1 -=12 17 | ans = "" 18 | if hour1 > 9 : 19 | ans = str(hour1) + ':' 20 | else: 21 | ans = '0' + str(hour1) + ':' 22 | 23 | if minute1 > 9: 24 | ans += str(minute1) 25 | else: 26 | ans += '0' + str(minute1) 27 | return ans 28 | -------------------------------------------------------------------------------- /001-5kyu-prime-in-numbers.py: -------------------------------------------------------------------------------- 1 | # https://www.codewars.com/kata/54d512e62a5e54c96200019e 2 | 3 | def primeFactors(n): 4 | 5 | i = 2 6 | res = {} 7 | while n/i != 1: 8 | if n%i == 0: 9 | if i in res: 10 | res[i] = res[i]+1 11 | else: 12 | res[i] = 1 13 | n = n/i 14 | else: 15 | i+=1 16 | 17 | if i in res: 18 | res[i] = res[i]+1 19 | else: 20 | res[i] = 1 21 | t = '' 22 | res = sorted(res.items(),key = lambda a:a[0]) 23 | 24 | for key in res: 25 | if key[1] == 1: 26 | t = t + '('+str(key[0]) +')' 27 | else: 28 | t = t + '(' +str(key[0]) + '**' + str(key[1]) + ')' 29 | return t 30 | -------------------------------------------------------------------------------- /002-6kyu-Identify-the-array's-ordering.py: -------------------------------------------------------------------------------- 1 | # https://www.codewars.com/kata/57f669477c9a2b1b9700022d 2 | def order_type(arr): 3 | n = [] 4 | if len(arr) == 0: 5 | return 'Constant' 6 | for i in arr: 7 | if type(i) == int: 8 | i = str(i) 9 | n.append(len(i)) 10 | 11 | if n[0] == n[-1]: 12 | for i in n: 13 | if i != n[0]: 14 | return 'Unsorted' 15 | return 'Constant' 16 | elif n[0] < n[-1]: 17 | for i, ele in enumerate(n[1:]): 18 | if ele < n[i]: 19 | return 'Unsorted' 20 | return 'Increasing' 21 | elif n[0] > n[-1]: 22 | for i, ele in enumerate(n[1:]): 23 | if ele > n[i]: 24 | return 'Unsorted' 25 | return 'Decreasing' 26 | -------------------------------------------------------------------------------- /007-4kyu-Number of Proper Fractions with Denominator d.py: -------------------------------------------------------------------------------- 1 | # https://www.codewars.com/kata/55b7bb74a0256d4467000070 2 | def proper_fractions(n): 3 | #your code here 4 | if n == 1: 5 | return 0 6 | # if n == 15: 7 | # return 8 8 | temp = 1 9 | m = n 10 | l = int(n ** 0.5)+1 11 | for i in range(1, l): 12 | if is_prime(i): 13 | if m % i ==0: 14 | temp *= i-1 15 | m /= i 16 | while m % i ==0: 17 | temp *= i 18 | m /= i 19 | print temp,m 20 | if m > 1: 21 | temp *= m-1 22 | return temp 23 | 24 | def is_prime(n): 25 | if n == 0 or n == 1: 26 | return False 27 | i = 2 28 | while (i <= n ** 0.5 ): 29 | if n % i == 0: 30 | return False 31 | i += 1 32 | return True 33 | -------------------------------------------------------------------------------- /037-4kyu-Sudoku Solution Validator.py: -------------------------------------------------------------------------------- 1 | def validSolution(board): 2 | temp = [] 3 | for row in board: 4 | print row 5 | for i in range(1,10): 6 | if i not in row: 7 | # print row, "row failed" 8 | return False 9 | print "~~~~~~~~~Row Passed~~~~~~~~~~" 10 | for column in range(0,9): 11 | temp = [] 12 | for row in board: 13 | temp.append(row[column]) 14 | # print temp 15 | 16 | for i in range(1,10): 17 | if i not in temp: 18 | # print temp, "column failed" 19 | return False 20 | print "~~~~~~~~~~~Column passed~~~~~~~~~~~~~~~~~~~~~~" 21 | j = 0 22 | while (j <=2): 23 | temp = [] 24 | for row in range(3*j,3*j+3): 25 | k = 0 26 | for column in range(3*j,3*j+3): 27 | temp.append(board[row][column]) 28 | # print temp, row, column 29 | # print temp 30 | 31 | for i in range(1,10): 32 | if temp.count(i) != 1: 33 | return False 34 | j += 1 35 | 36 | return True -------------------------------------------------------------------------------- /032-4kyu-Next bigger number with the same digits.py: -------------------------------------------------------------------------------- 1 | def next_bigger(n): 2 | s = str(n) 3 | if check(s): 4 | return -1 5 | l = len(s) 6 | ss = [] 7 | for item in s: 8 | ss.append(int(item)) 9 | index = l-1 10 | while(index > 0): 11 | if int(ss[index-1]) < int(ss[index]): 12 | for j in range(l-1,index-1,-1): 13 | print ss[j], ss[index-1] 14 | if ss[j] > ss[index-1]: 15 | print ss[j],ss[index-1],ss 16 | temp = ss[j] 17 | ss[j] = ss[index-1] 18 | ss[index-1] = temp 19 | print ss,index,ss[index:] 20 | ss = ss[:index] + ss[index:][::-1] 21 | break 22 | break 23 | else: 24 | index -=1 25 | print ss 26 | res = 0 27 | for i in range(0,l): 28 | res += 10 ** i * ss[-(i+1)] 29 | print res 30 | return res 31 | def check(s): 32 | l = len(s) 33 | index = l-1 34 | while(index > 0): 35 | if int(s[index]) > int(s[index-1]): 36 | return False 37 | index -=1 38 | return True 39 | 40 | next_bigger(5) 41 | -------------------------------------------------------------------------------- /036-4kyu-A Simplistic TCP Finite State Machine (FSM).py: -------------------------------------------------------------------------------- 1 | def traverse_TCP_states(events): 2 | state = "CLOSED" # initial state, always 3 | AUTOM = {("CLOSED","APP_PASSIVE_OPEN"):"LISTEN", 4 | ("CLOSED","APP_ACTIVE_OPEN"):"SYN_SENT", 5 | ("LISTEN","RCV_SYN"):"SYN_RCVD", 6 | ("LISTEN","APP_SEND"):"SYN_SENT", 7 | ("LISTEN","APP_CLOSE"):"CLOSED", 8 | ("SYN_RCVD","APP_CLOSE"):"FIN_WAIT_1", 9 | ("SYN_RCVD","RCV_ACK"):"ESTABLISHED", 10 | ("SYN_SENT","RCV_SYN"):"SYN_RCVD", 11 | ("SYN_SENT","RCV_SYN_ACK"):"ESTABLISHED", 12 | ("SYN_SENT","APP_CLOSE"):"CLOSED", 13 | ("ESTABLISHED","APP_CLOSE"):"FIN_WAIT_1", 14 | ("ESTABLISHED","RCV_FIN"):"CLOSE_WAIT", 15 | ("FIN_WAIT_1","RCV_FIN"):"CLOSING", 16 | ("FIN_WAIT_1","RCV_FIN_ACK"):"TIME_WAIT", 17 | ("FIN_WAIT_1","RCV_ACK"):"FIN_WAIT_2", 18 | ("CLOSING","RCV_ACK"):"TIME_WAIT", 19 | ("FIN_WAIT_2","RCV_FIN"):"TIME_WAIT", 20 | ("TIME_WAIT","APP_TIMEOUT"):"CLOSED", 21 | ("CLOSE_WAIT","APP_CLOSE"):"LAST_ACK", 22 | ("LAST_ACK","RCV_ACK"):"CLOSED"} 23 | for event in events: 24 | if (state,event) in AUTOM: 25 | state = AUTOM[state,event] 26 | else: 27 | return "ERROR" 28 | return state --------------------------------------------------------------------------------