Advertisement
makispaiktis

Duplicate Count

Oct 25th, 2019 (edited)
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. def duplicate_count(text):              # RETURNED TYPE = DICTIONARY
  2.  
  3.     # 1. Find the different letters in the given text (ALL LETTERS WILL BE CONTROLLED LIKE BEING LOWERCASE)
  4.     differentLetters = []
  5.     for ch in text:
  6.         if ch.lower() not in differentLetters:
  7.             differentLetters.append(ch.lower())
  8.  
  9.     # print("Different letters: " + str(differentLetters))
  10.  
  11.     # 2. Create a dictionary: Every of the different letters ---> will go to 0 (0 is the initial frequency of appearance)
  12.     # Dictionary will finally have: EACH DIFFERENT LETTER PAIREDWITH HIS FREQUENCY OF APPEARING
  13.     dictionary = {}
  14.     for letter in differentLetters:
  15.         dictionary[letter] = 0
  16.     # print("    Dictionary:    " + str(dictionary))
  17.  
  18.     # 3. I will scan my text to increase the values of the map
  19.     # For every character "ch" in my string = text, I want to repeat the following process
  20.     for ch in text:
  21.         for i in range(0, len(differentLetters)):
  22.             if ch.lower() == differentLetters[i]:            # That means I have a hit = (my character ch is equal to the i-th letter of my list differnetLetters)
  23.                 # Because of having a hit, letter "ch" is sure in my dictionary. I will increase by 1 its value
  24.                 dictionary[ch.lower()] = dictionary.get(ch.lower()) + 1
  25.                 break
  26.  
  27.     return dictionary
  28.  
  29.  
  30.  
  31. # MAIN PART OF THE PROGRAMME
  32. print("***************************************************************")
  33. print()
  34.  
  35. text = input("Insert your text here: ")
  36. print("Text: " + text)
  37. dictionary = duplicate_count(text)
  38. for key, value in dictionary.items():
  39.     print(key + " ---> " + str(value))
  40.  
  41. print()
  42. print("***************************************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement