Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pylint:disable=C0301
- """
- 1. Write a function called most_frequent that takes a string and prints the letters in decreasing order of frequency. Find text samples from several different languages and see how letter frequency varies between languages.
- """
- def most_frequent(words):
- d = {}
- for w in set(words):
- d[w] = words.count(w)
- # d = list(zip(d.keys(), d.values()))
- return "".join(sorted(d)[::-1])
- """
- 2. More anagrams
- a. Write a program that reads a word list from a file (see Section 9.1) and prints all the sets of words that are anagrams.
- """
- def read_file(filename):
- try:
- with open(filename) as f:
- r = f.read()
- lst = r.split("\n")
- finally:
- f.close()
- return print_anagram(lst)
- def print_anagram(words):
- offset = ord("a")
- for word in words:
- cnt = [0] * 26
- for char in word:
- cnt[ord(char) - offset] += 1
- print(chr(ord(char)))
- print(word)
- print(cnt)
- #--------------------------------------------------
- titles = {
- 1: "Most frequent",
- 2: "Anagrams"
- }
- print("""Which function you want to run?
- 1. Most frequent
- 2. Anagrams""")
- f_run = int(input())
- print(f"You choose {f_run}: {titles[f_run]}")
- if f_run == 1:
- t = "Enter the word: "
- ex = input(t)
- print(f"Result: {most_frequent(ex)}")
- elif f_run == 2:
- print(read_file("words.txt"))
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
- #--------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement