Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # anagram('dog', 'god') should return True
- def distill(x):
- return sorted(list(x.replace(' ','').lower()))
- def is_anagram(s1, s2):
- s1 = distill(s1)
- s2 = distill(s2)
- return s1 == s2
- is_anagram('clint eastwood','old west action')
- #####################################
- def pair_sum(arr, k):
- if len(arr)<2:
- return
- seen = set()
- output = set()
- for num in arr:
- target = k-num
- if target not in seen:
- seen.add(num)
- else:
- output.add((min(num,target), max(num,target)))
- # min and max - to arrange the items is ascending order. .. e.g. (1,4), (2,3)
- return len(output)
- pair_sum([1,3,2,2,5,-1],4)
- #####################################
- def finder(arr1, arr2):
- #where arr2 is shuffled arr1 with a random item removed
- arr1.sort()
- arr2.sort()
- for num1, num2 in zip(arr1, arr2):
- # zip joins 2 tuples so that the first item is made of the first item of the zip is made of
- # the first item of the first tuple AND the first item of the second tuple
- if num1 != num2:
- return num1
- return arr1[-1] # last element in the array
- arr1 = [1,2,3,4,5,6,7]
- arr2 = [3,7,2,1,4,6]
- finder(arr1, arr2)
- #####################################
- import collections
- def finder2(arr1, arr2):
- d = collections.defaultdict(int)
- for num in arr2:
- d[num] += 1
- for num in arr1:
- if d[num] == 0:
- return num
- else:
- d[num] -= 1
- arr1 = [1,2,3,4,5,6,7]
- arr2 = [3,7,2,1,4,6]
- finder2(arr1, arr2)
- #########################
- # Largest continuous sum
- #########################
- def large_cont_sum(arr):
- if len(arr) == 0:
- return 0
- max_sum = current_sum = arr[0]
- for num in arr[1:]: # arr[1:] skips the first element because it is in the previous line
- current_sum = max(current_sum+num, num)
- max_sum = max(current_sum, max_sum)
- return max_sum
- large_cont_sum([1,2,-1,3,4,10,10,-10,-1])
- ###################
- # Sentence reversal
- ###################
- sentence = ' June the force be with you '
- " ".join(reversed(sentence.strip().split()))
- # it works, but we need to "show work"
- # hence a more complicated version:
- def rev_words(s):
- words = []
- length = len(s)
- spaces = [' ']
- i = 0
- while i < length:
- if s[i] not in spaces:
- word_start = i
- while i < length and s[i] not in spaces:
- i += 1
- words.append(s[word_start:i])
- i += 1
- return ' '.join(reversed(words))
- rev_words('Hello John how are you')
- ###########################
- # 'Compress' a string
- ###########################
- # AAAAAABBBBBCCCCDD => A5B4C4D2
- # case-sensitive
- # AAB => A2B1
- def compress(s):
- r = ""
- l = len(s)
- if l == 0:
- return ""
- if l == 1:
- return s+"1"
- last = s[0]
- cnt = 1
- i = 1
- while i < l:
- if s[i] == s[i-1]:
- cnt += 1
- else:
- r = r + s[i-1] + str(cnt)
- cnt = 1
- i += 1
- r = r + s[i-1] + str(cnt)
- return r
- compress("AAaaaBBBBcDD")
- ###############################
- # Unique characters in a string
- ###############################
- # built-in way:
- def all_chars_unique(s):
- return len(set(s)) == len(s)
- all_chars_unique('abcdeb')
- # our own way:
- def unichar2(s):
- chars = set()
- for letter in s:
- if letter in chars:
- return False
- else:
- chars.add(letter)
- return True
- unichar2('abcdcef')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement