-4

question: 10. Most Frequent Character Write a program that lets the user enter a string and displays the character that appears most frequently in the string.

This is an answer for those who are studying intro to cs with "Starting out with Python" chapter 9 question 10. This question is answered solely with what I have learned in previous chapters of the book. I couldn't find anything similar on this website. This code might be OK for beginners like me, so I want to share it. I know this code looks bad, but it gets job done so... Original code I found on Youtube where it is written in Java, here is a link: https://www.youtube.com/watch?v=dyWYLXKSPus sorry for my broken English!)

string = "a11aawww1cccertgft1tzzzzzz1ggg111"
mylist_char = []
mylist_count = []
char = None
count = 0

for ch in string:
    temp_char = ch
    temp_count = 0
    for ch1 in string:
        if temp_char == ch1:
            temp_count += 1

    if temp_count > count:
        count = temp_count
        char = temp_char
mylist_char.append(char)
mylist_count.append(count)
for x in range(len(string)):
    for ch in string:
        temp_char = ch
        temp_count = 0
        for ch1 in string:
            if temp_char == ch1:
                temp_count += 1
        if temp_count == count and not(temp_char in mylist_char):
            mylist_char.append(temp_char)
            mylist_count.append(temp_count)
for x in range(len(mylist_char)):
    print("Character", mylist_char[x], "occurred", mylist_count[x], "times")
Denys
  • 114
  • 1
  • 6
  • Not sure why this picked up a -1 already-- probably because it feels like it's stretching the purpose of SO. Maybe if you phrased it as a question, and then answered your own question it would sit better with the community? Perhaps ask the question as it was asked in the text and then answer it yourself? That would also allow others to provide better quality answers for later reference. – Omegaman Jan 08 '16 at 05:09

2 Answers2

0

My issue with your solution is that it looks like Java rewritten in Python -- if you want to use Java, use Java. If you want to use Python, take advantage of what it has to offer. I've written a "simple" solution below that doesn't use any sophisticated Python functions (e.g. if I were really writing it, I'd use a defaultdict and a comprehension)

string = "a11aawww1cccertgft1tzzzzzz1ggg111"

dictionary = {}

for character in list(string):
    if character in dictionary:
        dictionary[character] += 1
    else:
        dictionary[character] = 1

results = []

for key, value in dictionary.items():
    results.append((value, key))

for value, key in reversed(sorted(results)):
    print("Character", key, "occurred", value, "times")
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • this problem is given before intro to dictionaries! – Denys Jan 23 '16 at 15:48
  • If you're at chapter 9 in a Python text book and they haven't introduced dictionaries yet, get a better text book. – cdlane Jan 23 '16 at 18:33
  • thanks for advice, but so far i love the book i'm going through, and dictionary is introduced in the next chapter. – Denys Jan 23 '16 at 23:58
0
# Most frequent Character

def most_frequent(a_string):

    # Create a string of symbols to exclude from counting.
    symbols = ' ,.-/?'
    characters = []
    characters_count = []

    # Check each individual character in the string.
    for ch in a_string:

        # Check that the character is not one of the symbols.
        if ch not in symbols:

            # If its not and we haven't seen it already, 
            # append it to the characters list.
            if ch not in characters:
                characters.append(ch)

                # And in the same index in the characters_count list
                characters_count.append(1)
            else:
                # If it is in the characters list, find its index
                # and add 1 to the same index at characters_count
                position = characters.index(ch)
                characters_count[position] = characters_count[position] + 1
    # find the largest value in the character_count list, it's index
    # and show the character at the same index at the characters list.
    print(characters[characters_count.index(max(characters_count))])

def main():
    # Get a string from the user.
    text = input('Give me some text and I will find you the most frequent character: ')
    most_frequent(text)

# Call main
main()
ggluv
  • 3
  • 2