0

I am currently working on python, and I do not understand this much. I am looking for help with this question, before the dictionaries. This question is to be completed without any dictionaries. The problem is I do not know much about the max function.

So Far I have:

AlphaCount = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

for ch in text: 
   ch = ch.upper()
   index=Alpha.find(ch)

   if index >-1:
       AlphaCount[index] = AlphaCount[index]+1
maahl
  • 547
  • 3
  • 17
  • 5
    And the problem with this code is? (apart from wrong capitalisation on `if`) – UnholySheep Nov 16 '16 at 16:30
  • You'd better use a dictionary instead ... – MMF Nov 16 '16 at 16:33
  • @MMF- Looks like this (homework?) question "is to be completed without any dictionaries." – Surreal Dreams Nov 16 '16 at 16:34
  • The key question is whether or not joint-highest frequency values need to be considered, if not then `max(set(text), key=text.count)`, see http://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-a-list – Chris_Rands Nov 16 '16 at 16:55
  • If you really want to do it without dictionary, then you should start by sorting the text `sorted(text.upper())` and then iterate it. – Tg. Nov 16 '16 at 16:55

3 Answers3

1

You can use Counter

from collections import Counter
foo = 'wubalubadubdub'
Counter(list(foo))

To get the most frequent letter

Counter(list(foo)).most_common(1)
Mohammad Athar
  • 1,953
  • 1
  • 15
  • 31
  • 2
    No need to explicitly cast to list for the `most_common` call. Also technically `Counter` is a `dict` sub-class, so contradicts the OP's requirement – Chris_Rands Nov 16 '16 at 16:35
  • 3
    If dictionaries are not allowed, I highly doubt that `Counter` would be appreciated. – Shivendra Nov 16 '16 at 16:36
0

You can use set which will get only unique characters from the input. Then iterate over them and count how many times it occurs in the input with count. If it occurs more often then the max and isalpha (not a space) then set max to the count.

text='This is a test of tons of tall tales'
un=set(text.upper())
max=0
fav=''

for u in un:
    c=text.upper().count(u)
    if c>max and u.isalpha():
        max=c
        fav=u
print(fav) # T
print(max) # 6

EDIT

To do this from your code: fix capitalization(for, if) and then find and print/return the most common letter. Also AlphaCount has an extra 0, you only need 26.

text='This is a test of tons of tall talez'
AlphaCount=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Alpha='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

for ch in text: 
    ch= ch.upper()
    index=Alpha.find(ch)

    if index >-1:
        AlphaCount[index]+=1
print(AlphaCount) # the count of characters
print(max(AlphaCount)) # max value in list
print(AlphaCount.index(max(AlphaCount))) # index of max value
print(Alpha[AlphaCount.index(max(AlphaCount))]) # letter that occurs most frequently
depperm
  • 10,606
  • 4
  • 43
  • 67
0
def main():

    string = input('Enter a sentence: ')

    strings=string.lower()

    counter = 0
    total_counter = 0

    most_frequent_character = ""

    for ch in strings:
        for str in strings:
            if str == ch:

                counter += 1
        if counter > total_counter:
            total_counter = counter
            most_frequent_character = ch
        counter = 0

    print("The most frequent character is", most_frequent_character, "and it appears", total_counter, "times.")

main()
Michal
  • 2,078
  • 23
  • 36
Eni
  • 1