albibacsi

codewars

Dec 16th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. # https://www.codewars.com/kata/dubstep/solutions/python
  2. def song_decoder(song):
  3.     import re
  4.     return re.sub('(WUB)+', ' ', song).strip()
  5.  
  6. # https://www.codewars.com/kata/take-a-ten-minute-walk/solutions/python
  7. def isValidWalk(walk):
  8.     return len(walk) == 10 and walk.count('n') == walk.count('s') and walk.count('e') == walk.count('w')
  9.  
  10. # https://www.codewars.com/kata/unique-in-order/solutions/python
  11. def unique_in_order(iterable):
  12.   r = []
  13.   for x in iterable:
  14.     x in r[-1:] or r.append(x)
  15.   return r
  16.  
  17. # https://www.codewars.com/kata/5656b6906de340bd1b0000ac/train/python
  18. def longest(s1, s2):
  19.     return ''.join(sorted((set(s1+s2))))
  20.  
  21. # https://www.codewars.com/kata/52685f7382004e774f0001f7/solutions/python
  22. # https://pyformat.info/
  23. def make_readable(seconds):
  24.     hours = int(seconds / 3600)
  25.     seconds -= hours*3600
  26.     minutes = int(seconds / 60)
  27.     seconds -= minutes*60
  28.     return '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)
  29.  
  30. def make_readable(s):
  31.     return '{:02}:{:02}:{:02}'.format(s / 3600, s / 60 % 60, s % 60)
  32.  
  33. # https://www.codewars.com/kata/53368a47e38700bd8300030d/solutions/python
  34. def namelist(names):
  35.     result = ''
  36.     if(names):
  37.         result = names.pop()['name']
  38.         if(names):
  39.             result = ', '.join([x['name'] for x in names]) + ' & ' + result
  40.     return result
  41.  
  42. def namelist(names):
  43.   return ", ".join([name["name"] for name in names])[::-1].replace(",", "& ",1)[::-1]
  44.  
  45. # https://www.codewars.com/kata/556deca17c58da83c00002db/solutions/python
  46. def tribonacci(signature, n):
  47.     for i in range(3, n):
  48.         signature.append(sum(signature[-3:]))
  49.     return signature[:n]
  50.  
  51. # https://www.codewars.com/kata/5842df8ccbd22792a4000245/solutions/python
  52. def expanded_form(num):
  53.     result = []
  54.     magnitude = len(str(num))
  55.    
  56.     for n in str(num):
  57.         n = int(n)
  58.         magnitude -= 1
  59.         if n>0:
  60.             result.append(str(n*10**magnitude))
  61.     return ' + '.join(result)
  62.  
  63. def expanded_form(num):
  64.     num = list(str(num))
  65.     return ' + '.join(x + '0' * (len(num) - y - 1) for y,x in enumerate(num) if x != '0')
  66.  
  67.  
  68. # https://www.codewars.com/kata/515de9ae9dcfc28eb6000001/train/python
  69. def solution(s):
  70.     result = []
  71.    
  72.     if len(s) % 2:
  73.         s += '_'
  74.    
  75.     while s:
  76.         result.append(s[:2])
  77.         s = s[2:]
  78.  
  79.     return result
  80.  
  81. def solution(s):
  82.     return re.findall(".{2}", s + "_")
  83.  
  84. # https://www.codewars.com/kata/roman-numerals-encoder/train/python
  85. def calc(num, romdict):
  86.     if num < 1:
  87.         return ''
  88.     if num < 4:
  89.         return romdict[1].__mul__(num)
  90.     elif num == 4:
  91.         return romdict[1] + romdict[5]
  92.     elif num == 5:
  93.         return romdict[5]
  94.     elif num >= 6 and num < 9:
  95.         return romdict[5] + romdict[1].__mul__(num-5)
  96.     return romdict[9]
  97.    
  98. def solution(n):
  99.     one = {1:'I', 5:'V', 9:'IX'}
  100.     ten = {1:'X', 5:'L', 9:'XC'}
  101.     hundred = {1:'C', 5:'D', 9:'CM'}
  102.     thousand = {1:'M'}
  103.  
  104.     _1000 = int(n/1000)
  105.     _100 = int(n/100 % 10)
  106.     _10 = int(n/10 % 10)
  107.     _1 = n % 10
  108.    
  109.     return '{}{}{}{}'.format(calc(_1000, thousand), calc(_100, hundred), calc(_10, ten), calc(_1, one) )
  110.  
  111.  
  112. def solution(n):
  113.     roman_numerals = {1000:'M',
  114.                       900: 'CM',
  115.                       500: 'D',
  116.                       400: 'CD',
  117.                       100: 'C',
  118.                       90: 'XC',
  119.                       50: 'L',
  120.                       40: 'XL',
  121.                       10: 'X',
  122.                       9: 'IX',
  123.                       5: 'V',
  124.                       4: 'IV',
  125.                       1: 'I'
  126.     }
  127.     roman_string = ''
  128.     for key in sorted(roman_numerals.keys(),reverse=True):
  129.         while n >= key:
  130.             roman_string += roman_numerals[key]
  131.             n -= key
  132.     return roman_string
Advertisement
Add Comment
Please, Sign In to add comment