#!/usr/bin/env python3 ''' Find N most repeated words ''' import sys DATA = "one two three one two four one two three four five one three three three" ARR = DATA.split(' ') TOP_WORD_COUNT = 3 def find_unique_words(ARR): '''Get the set of unique words''' uniques = [] for word in ARR: if word not in uniques: uniques.append(word) return uniques def find_uniques_count(uniques): '''Return list of unique integers''' uniques_count = [] for w1 in uniques: count = 0 for w2 in ARR: if w1 == w2: count = count + 1 uniques_count.append(count) return uniques_count def find_element_location_in_list(mylist, myelement): '''Return location of element in original list''' location = 0 for i in mylist: if i == myelement: return location location = location + 1 def main(): '''Main Function''' uniques = find_unique_words(ARR) # print(uniques) uniques_count = find_uniques_count(uniques) # print(uniques_count) new_uniques_count = sorted(uniques_count) new_uniques_count.reverse() # print(new_uniques_count) for i in new_uniques_count[0:TOP_WORD_COUNT]: print('Finding word with count : ' + str(i)) x = find_element_location_in_list(uniques_count, int(i)) print(uniques[x]) if __name__ == "__main__": sys.exit(main()) ## Output # Finding word with count : 5 # three # Finding word with count : 4 # one # Finding word with count : 3 # two