##### Character Count HW | Need suggestions for optimization
 Character Count HW | Need suggestions for optimization NectDz Programmer named Tim Posts: 15 Threads: 7 Joined: May 2020 Reputation: 1 Nov-25-2020, 02:56 AM Hello, my homework asked to make a program that accepts a string as a parameter and that returns the letters that appeared the most without any duplicates. I would just like to know if there is any way I could of used any list comprehensions or optimized it in any way? Thanks! ```# Gets the max number in the list def maxNumber(L): i = 0 maxNum = L[0] while i < len(L): if L[i] >= maxNum: maxNum = L[i] i += 1 return maxNum # Returns a index list on the letters who appeared the most def indexes(maxNum,S): index = [] j = 0 for i in S: if S.count(i) == maxNum: index.append(j) j += 1 return index # Returns the letters that appeared the most def maxLetters(index,S): maxLetters = '' for i in index: if S[i] not in maxLetters: maxLetters += S[i] return maxLetters # Main Function def CharacterCount(S): # Removes Spaces S = S.replace(' ','') # Max Number - List Comprehension returns how many time times a letter appears maxNumbers = maxNumber([S.count(i) for i in S]) # Returns a index list on the letters who appeared the most index = indexes(maxNumbers,S) # Returns the letters that appeared the most maxLetter = maxLetters(index,S) print(maxLetter) CharacterCount('Python is great fun') CharacterCount('Good Times')``` Reply Larz60+ aetate et sapientia Posts: 9,977 Threads: 372 Joined: Sep 2016 Reputation: 421 Nov-25-2020, 08:47 AM to get character count: ```from collections import Counter def CharacterCount(S): S = S.strip().replace(' ','') return Counter(S) print(f"\nCounts 'Python is great fun': {CharacterCount('Python is great fun')}") print(f"Counts 'Good Times': {CharacterCount('Good Times')}\n")`````````Output:Counts 'Python is great fun': Counter({'t': 2, 'n': 2, 'P': 1, 'y': 1, 'h': 1, 'o': 1, 'i': 1, 's': 1, 'g': 1, 'r': 1, 'e': 1, 'a': 1, 'f': 1, 'u': 1}) Counts 'Good Times': Counter({'o': 2, 'G': 1, 'd': 1, 'T': 1, 'i': 1, 'm': 1, 'e': 1, 's': 1})`````` Reply

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020