Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import Counter
- from collections import defaultdict
- # dict have nice O(1) lookup time, defaultdict is even faster!
- def rep1(elements):
- if len(elements) != 24:
- return False
- most_common = Counter(elements).most_common(1)[0] # find most common element
- return most_common[1] == 1 # returns if its repeated only one time
- def rep2(elements):
- if len(elements) != 24:
- return False
- couter = {} # element counter
- for el in elements:
- if not couter.get(el, False):
- # add element to register
- couter[el] = 1
- else:
- # element already registered
- return False
- return True # no interrupt so everything is fine
- def rep3(elements):
- if len(elements) != 24:
- return False
- # directory with default value of 0
- counter = defaultdict(int)
- for el in elements:
- counter[el] += 1 # increment by one element count
- highest = max(counter, key=counter.get) # find keyword with highest number
- return counter[highest] == 1 # returns if highest number is repeated
- def checker(f, good, bad):
- print('[{}]:'.format(f.__name__))
- print('\tfor good: {}'.format(f(good))) # feeds f with bad input
- print('\tfor bad: {}\n'.format(f(bad))) # feeds f wit good input
- if __name__ == '__main__':
- good = '1234567890qwertyuiopasdf'
- bad = '1134567890qwertyuiopasdf'
- checker(rep1, good, bad)
- checker(rep2, good, bad)
- checker(rep3, good, bad)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement