Histogram - Going down a line when having 2+ letters of same word
I have a question saying this:
Write a function which receives a string ( containing only small letters and whitespaces ).
This function will print histogram for all letters with *, for example:
string - "aba ima", this will be printed:
abcdefghijklmnopqrstuvwxyz
** * *
*
*
Now, I tried this code, although it is 80% done, the remaining 20% of the work I cant seem to get it...
My problem is how to print the other letters in the string.
For example, aba ima, it has three "a", I cant seem to get it to print 3 lines for it.
My code:
def histogram(string):
abc = "abcdefghijklmnopqrstuvwxyz"
print(abc)
for letter in abc:
if letter in string:
abc = abc.replace(letter, "*")
else:
abc = abc.replace(letter, "_")
return abc
print(histogram("aba ima"))
my output:
abcdefghijklmnopqrstuvwxyz
**______*___*_____________
can anyone help me? so I can do if it says: aba ima, so Ill have 3 lines of *?
I asked the same question in a forum in my country, no one could help me. Asked the same at reddit, same thing. I gave up on it almsot, then decided to ask here to see if any different... I was told in all scenarios that I have to use index and find, but I cant seem to understand how I need to use it with this function.. its really annoying, I am stuck on it for a week + and because of it I cant continue on to my next homework ( sorting and then recursions ). Please help on this if anyone can, thanks...
EDIT:
another try of my code, didnt upload but now I will:
def make_counters(alphabet):
counters = []
for i in range(0, len(alphabet)):
counters.append(0) # must be a counter, so integer
return counters
abc = make_counters("abcdefghijklmnopqrstuvwxyz")
def histogram(string, alphabet):
print(alphabet)
counters = make_counters(alphabet)
for letter in alphabet:
if letter in alphabet:
alphabet.find(letter)
else:
pass
print(histogram("aba ima", abc))
SECOND EDIT: like that?
def histogram(string):
abc = "abcdefghijklmnopqrstuvwxyz"
print(abc)
for letter in abc:
if letter in string:
abc = abc.replace(letter, "*")
string = string.replace(letter, "*", 1)
print(abc)
else:
abc = abc.replace(letter, "_")
return abc
the output is:
abcdefghijklmnopqrstuvwxyz
*bcdefghijklmnopqrstuvwxyz
**cdefghijklmnopqrstuvwxyz
**______*jklmnopqrstuvwxyz
**______*___*nopqrstuvwxyz
**______*___*_____________
Process finished with exit code 0
THIRD EDIT:
def histogram(string):
abc = "abcdefghijklmnopqrstuvwxyz"
print(abc)
for letter in abc:
if letter in string:
abc = abc.replace(letter, "*")
string = string.replace(letter, "*", 1)
if letter in string:
abc = abc.replace(letter, "*")
string = string.replace(letter, "*", 1)
else:
abc = abc.replace(letter, "_")
print(abc)
return abc
output:
abcdefghijklmnopqrstuvwxyz
**______*___*_____________
**______*___*_____________
tried this also in the third edit ( among some other stuff, this seems like the closest:
abc = "abcdefghijklmnopqrstuvwxyz"
print(abc)
for letter in abc:
if letter in string:
abc = abc.replace(letter, "*")
string = string.replace(letter, "*", 1)
if letter in string:
print(abc)
abc = abc.replace(letter, "*")
string = string.replace(letter, "*", 1)
else:
abc = abc.replace(letter, "_")
print(abc)
return abc
abcdefghijklmnopqrstuvwxyz
*bcdefghijklmnopqrstuvwxyz
**______*___*_____________
**______*___*_____________
but still not good
from Recent Questions - Stack Overflow https://ift.tt/3zpQ5cJ
https://ift.tt/eA8V8J
Comments
Post a Comment