G2A Many GEOs
SHARE
TWEET

exercises.py

a guest Apr 9th, 2020 216 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pylint:disable=C0301
  2. """
  3. 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.
  4. """
  5. def most_frequent(words):
  6.     d = {}
  7.     for w in set(words):
  8.         d[w] = words.count(w)
  9.     # d = list(zip(d.keys(), d.values()))
  10.     return "".join(sorted(d)[::-1])
  11.  
  12.  
  13. """
  14. 2. More anagrams
  15.    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.
  16. """
  17. def read_file(filename):
  18.     try:
  19.         with open(filename) as f:
  20.             r = f.read()
  21.             lst = r.split("\n")
  22.     finally:
  23.         f.close()
  24.         return print_anagram(lst)
  25.        
  26. def print_anagram(words):
  27.     offset = ord("a")
  28.     for word in words:
  29.         cnt = [0] * 26
  30.         for char in word:
  31.             cnt[ord(char) - offset] += 1
  32.             print(chr(ord(char)))
  33.         print(word)
  34.         print(cnt)
  35.  
  36.  
  37. #--------------------------------------------------
  38. titles = {
  39.     1: "Most frequent",
  40.     2: "Anagrams"
  41. }
  42.  
  43. print("""Which function you want to run?
  44.         1. Most frequent
  45.         2. Anagrams""")
  46. f_run = int(input())
  47. print(f"You choose {f_run}: {titles[f_run]}")
  48. if f_run == 1:
  49.     t = "Enter the word: "
  50.     ex = input(t)
  51.     print(f"Result: {most_frequent(ex)}")
  52. elif f_run == 2:
  53.     print(read_file("words.txt"))
  54. #--------------------------------------------------
  55. #--------------------------------------------------
  56. #--------------------------------------------------
  57. #--------------------------------------------------
  58. #--------------------------------------------------
  59. #--------------------------------------------------
  60. #--------------------------------------------------
  61. #--------------------------------------------------
  62. #--------------------------------------------------
  63. #--------------------------------------------------
  64. #--------------------------------------------------
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top