This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sentence = 'there was once a fox who there enjoyed tea there was once a fox who there enjoyed tea tea' | |
| words = sentence.split() | |
| counts = {} | |
| for word in words: | |
| counts[word] = counts.get(word, 0) + 1 | |
| top3 = sorted(count.items(), key=lambda x: x[1], reverse=True)[:3] | |
| print(top3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #source: http://en.literateprograms.org/Quicksort_(Python) | |
| def qsort1(list): | |
| """Quicksort using list comprehensions""" | |
| if list == []: | |
| return [] | |
| else: | |
| pivot = list[0] | |
| lesser = qsort1([x for x in list[1:] if x < pivot]) | |
| greater = qsort1([x for x in list[1:] if x >= pivot]) | |
| return lesser + [pivot] + greater |