Apr-02-2019, 11:16 AM
(This post was last modified: Apr-02-2019, 11:16 AM by souprqtpie.)
Hello
I am currently learning how to code in python, and am doing a set of challenges set by the course I am on.
there was a certain challenge I was unable to complete, and was given the solution for. I don't understand how the solution
works for the question, and it would be great if someone could break it down for me and explain each step.
The challenge is as follows:
create a function (count) which takes a string as a parameter and returns a dictionary listing the letters in the string
and how often each occurs.
for example:
know for sure, how solid my knowledge is.
Soup
I am currently learning how to code in python, and am doing a set of challenges set by the course I am on.
there was a certain challenge I was unable to complete, and was given the solution for. I don't understand how the solution
works for the question, and it would be great if someone could break it down for me and explain each step.
The challenge is as follows:
create a function (count) which takes a string as a parameter and returns a dictionary listing the letters in the string
and how often each occurs.
for example:
count("aa") == {"a": 2} count("abb") == {"a": 1, "b": 2} count("hello") == {"h": 1, "e": 1, "l": 2, "o": 1}The solution they provided:
def count(string): out = {} for letter in string: if letter not in out: out[letter] = 0 out[letter] = out[letter] + 1 return outMany thanks for any help, don't be afraid to be overly patronising in explanation, I won't take offence as you can't
know for sure, how solid my knowledge is.

Soup