Home > Enterprise >  How can I make my output return '*' for the number of times each letter appears in a strin
How can I make my output return '*' for the number of times each letter appears in a strin

Time:11-18

So despite asking for help previously, a lot of the responses were extremely helpful, but me, as a beginner, I am struggling to see how they are being used..

Nevertheless, I had a go myself again and so I'm trying to create a program that will return '*' times the number of times each letter appears in a string, and here is what I have so far...

def numberofletters(filename: str):
    g = list(filename)
    f = []
    for x in set(g):
        f.append(x)
    return f
def numberofwords(filename):
    r = []
    for x in filename:
        r.append([numberofletters(filename),filename.count(x)*('*')])
    return r
print(numberofwords("How was your day"))

However, it doesn't work at all since this is the output I'm getting..

[[[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 3], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 3], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 3], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 1], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2], [[' ', 'u', 'w', 'H', 'y', 'd', 'r', 'a', 'o', 's'], 2]]

but the answer that my code is supposed to print out is something like this

Note that the output below is a sample of what the output should look like btw

e                         
t                   
s                  
i                 
a                
m             
r             
u             
l          
n          
o          
c        
d        
p        
b      
g   
h   
j  
v  

Please try to use as less built in functions as possible, and also please do not use import and key = lamda or stuff like that because I'm kinda new and I don't really know how to use it

It would really help if you just modified my code btw instead of creating a new one ig, Thank you!

CodePudding user response:

Your numberofletters function

  • does not return the number of letters, but the unique letters of a given word, so you should change your implementation to do that.

  • does not receive a file name in your example, but a phrase or a string, so you should rename the filename parameter to something more generic.

The following implementation uses a dictionary:

def letter_count(phrase: str) -> dict:
    # Creates a dictionary to store each letter to its count
    counter = {}

    # Iterate over each letter inside your phrase
    for letter in phrase:
        # If this letter is present in the counter,
        # return its count. Otherwise, return zero
        letter_count = counter.get(letter, 0)
        
        # Put this letter into the counter incrementing its value
        counter[letter] = letter_count   1
        
    return counter

Now, just sort and print it accordingly:

# Call the function to calculate its value
counter = letter_count("How was your day")

# Sort the dictionary based on the count of each letter in the descending order
sorted_counter = sorted(counter.items(), key=lambda x: x[1], reverse=True)

# Iterate over each letter and count inside the sorted counter
for letter, count in sorted_counter:
    # Print the letter and the ` ` character repeated `count` times
    print(letter, ' ' * count)

Since you don't know how to use key=lambda ..., I suggest you to take a look at key functions.

CodePudding user response:

This is as simple and as explicit as I can make it:

def letter_count(filename: str):
    chars = list(filename.lower()) # ['h', 'o', 'w', ' ', 'w', 'a', 's', ' ', 'y', 'o', 'u', 'r', ' ', 'd', 'a', 'y']
    chars_unique = set(chars) # {'s', 'h', 'u', 'w', 'o', 'd', 'a', 'r', ' ', 'y'}
    chars_unique.remove(' ') # {'s', 'h', 'u', 'w', 'o', 'd', 'a', 'r', 'y'}
    result = []
    for x in chars_unique:
        # creates a list e.g. ['w', '**'] and adds it to the result list
        result.append([x, chars.count(x)*('*')])
    return result

l_count = letter_count("How was your day") # [['s', '*'], ['h', '*'], ['u', '*'], ['w', '**'], ['o', '**'], ['d', '*'], ['a', '**'], ['r', '*'], [' ', '***'], ['y', '**']]

for c in l_count:
    print(c[0], c[1])

Which returns:

a **
r *
y **
h *
s *
u *
o **
d *
w **

If you want the bars to be in descending order, then you have to order the data before printing it:

# MODIFIED TO ORDER THE BARS
def letter_count(filename: str):
    chars = list(filename.lower()) # ['h', 'o', 'w', ' ', 'w', 'a', 's', ' ', 'y', 'o', 'u', 'r', ' ', 'd', 'a', 'y']
    chars_unique = set(chars) # {'s', 'h', 'u', 'w', 'o', 'd', 'a', 'r', ' ', 'y'}
    chars_unique.remove(' ') # {'s', 'h', 'u', 'w', 'o', 'd', 'a', 'r', 'y'}
    result = []
    for x in chars_unique:
        # creates a list e.g. ['w', '**'] and adds it to the result list
        result.append([x, chars.count(x)*('*')])
    result.sort(key=lambda x: len(x[1]), reverse=True)
    return result

Lets break this modification down:

.sort() Sorts a Python list inplace (modifies result directly)

lambda x: len(x[1]) Anonymous function, gets the length of the first index of the input Exactly the same as the following, except we can give the following a name f

def f(x):
    return len(x[1])

key=lambda x: len(x[1]) Defines what we want to sort on, the number of stars in this case which is the length first index of the inner lists

result.sort(key=lambda x: x[1], reverse=True) makes it DESCENDING order

Which gives:

y **
a **
o **
w **
r *
h *
d *
s *
u *
  • Related