Advertisement
NonplayerCharacter

Python 101

May 25th, 2021
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. # anagram('dog', 'god') should return True
  2.  
  3. def distill(x):
  4.     return sorted(list(x.replace(' ','').lower()))
  5.  
  6. def is_anagram(s1, s2):
  7.     s1 = distill(s1)
  8.     s2 = distill(s2)
  9.     return s1 == s2
  10.  
  11. is_anagram('clint eastwood','old west action')
  12.  
  13.  
  14. #####################################
  15.  
  16.  
  17. def pair_sum(arr, k):
  18.     if len(arr)<2:
  19.         return
  20.    
  21.     seen = set()
  22.     output = set()
  23.    
  24.     for num in arr:
  25.         target = k-num
  26.  
  27.         if target not in seen:
  28.             seen.add(num)
  29.         else:
  30.             output.add((min(num,target), max(num,target)))
  31.             # min and max - to arrange the items is ascending order. .. e.g. (1,4), (2,3)
  32.  
  33.     return len(output)
  34.            
  35. pair_sum([1,3,2,2,5,-1],4)
  36.  
  37.  
  38.  
  39. #####################################
  40.  
  41.  
  42. def finder(arr1, arr2):
  43.     #where arr2 is shuffled arr1 with a random item removed
  44.     arr1.sort()
  45.     arr2.sort()
  46.  
  47.     for num1, num2 in zip(arr1, arr2):
  48.         # zip joins 2 tuples so that the first item is made of the first item of the zip is made of
  49.         # the first item of the first tuple AND the first item of the second tuple
  50.         if num1 != num2:
  51.             return num1
  52.  
  53.     return arr1[-1] # last element in the array
  54.  
  55. arr1 = [1,2,3,4,5,6,7]
  56. arr2 = [3,7,2,1,4,6]
  57. finder(arr1, arr2)
  58.  
  59.  
  60. #####################################
  61.  
  62. import collections
  63.  
  64. def finder2(arr1, arr2):
  65.     d = collections.defaultdict(int)
  66.     for num in arr2:
  67.         d[num] += 1
  68.     for num in arr1:
  69.         if d[num] == 0:
  70.             return num
  71.         else:
  72.             d[num] -= 1
  73.  
  74. arr1 = [1,2,3,4,5,6,7]
  75. arr2 = [3,7,2,1,4,6]
  76. finder2(arr1, arr2)
  77.  
  78.  
  79.  
  80. #########################
  81. # Largest continuous sum
  82. #########################
  83. def large_cont_sum(arr):
  84.     if len(arr) == 0:
  85.         return 0
  86.  
  87.     max_sum = current_sum = arr[0]
  88.    
  89.     for num in arr[1:]: # arr[1:] skips the first element because it is in the previous line
  90.         current_sum = max(current_sum+num, num)
  91.         max_sum = max(current_sum, max_sum)
  92.        
  93.     return max_sum
  94.  
  95. large_cont_sum([1,2,-1,3,4,10,10,-10,-1])
  96.  
  97.  
  98.  
  99. ###################
  100. # Sentence reversal
  101. ###################
  102. sentence = '   June the   force   be with   you  '
  103. " ".join(reversed(sentence.strip().split()))
  104. # it works, but we need to "show work"
  105. # hence a more complicated version:
  106.  
  107. def rev_words(s):
  108.     words = []
  109.     length = len(s)
  110.     spaces = [' ']
  111.     i = 0
  112.     while i < length:
  113.         if s[i] not in spaces:
  114.             word_start = i
  115.             while i < length and s[i] not in spaces:
  116.                 i += 1
  117.             words.append(s[word_start:i])
  118.         i += 1
  119.     return ' '.join(reversed(words))
  120. rev_words('Hello John how are you')
  121.  
  122.  
  123. ###########################
  124. # 'Compress' a string
  125. ###########################
  126. # AAAAAABBBBBCCCCDD => A5B4C4D2
  127. # case-sensitive
  128. # AAB => A2B1
  129.  
  130. def compress(s):
  131.     r = ""
  132.     l = len(s)
  133.    
  134.     if l == 0:
  135.         return ""
  136.     if l == 1:
  137.         return s+"1"
  138.    
  139.     last = s[0]
  140.     cnt = 1
  141.     i = 1
  142.    
  143.     while i < l:
  144.  
  145.         if s[i] == s[i-1]:
  146.             cnt += 1
  147.         else:
  148.             r = r + s[i-1] + str(cnt)
  149.             cnt = 1
  150.            
  151.         i += 1
  152.    
  153.     r = r + s[i-1] + str(cnt)
  154.    
  155.     return r
  156.    
  157. compress("AAaaaBBBBcDD")
  158.  
  159.  
  160.  
  161. ###############################
  162. # Unique characters in a string
  163. ###############################
  164.  
  165. # built-in way:
  166.  
  167. def all_chars_unique(s):
  168.     return len(set(s)) == len(s)
  169.  
  170. all_chars_unique('abcdeb')
  171.  
  172. # our own way:
  173.  
  174. def unichar2(s):
  175.     chars = set()
  176.     for letter in s:
  177.         if letter in chars:
  178.             return False
  179.         else:
  180.             chars.add(letter)
  181.     return True
  182. unichar2('abcdcef')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement