Advertisement
makispaiktis

Duplicate Count (Codewars)

Oct 25th, 2019 (edited)
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. def duplicate_count(text, checkTimes):              # 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.     counter = 0
  28.     for key, value in dictionary.items():
  29.         if value == checkTimes:
  30.             counter += 1
  31.     return counter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement