I would like some feedback on the most wanted letter.
My idea to build this in a very simplistic way (iterate text over a string, sort by frequency, select position...basic stuff), both because I am just starting and I think other starting people might benefit in learning the basics routines. I wanted to stay away from lamba function for instance, because I still have to learn it (I could copy the solution from the net, but that feels like cheating).
My framework to tackle the problem
1: #define what is a lower letter (WORKS)
2: #make new string with only lower case letters in given text, this will remove spaces, capital and all none alphabetical characters. (WORKS)
3: #sort lower case letters by frequency (WORKS AS LONG AS THERE IS AN UNEQUAL DISTRIBUTION)
4: for equal frequency of elements, pick first in alphabet (STUCK)
5: select position [0] of the sorted list as answer. (WORKS FOR THE ASSERTS THAT CAN SKIP POINT 4)
This works for the first few asserts because there is no equal frequency (point 4). However, assert "all letter only once" doesn't sort the letters in alphabetical order (as i expected, so I tried: ''.join(sorted(inorder)) or inorder.sort(key=len) and it still gives 'n' as answer when asserting 'One'. It removes the O since it is capital but both sorting methods don't put the e before the n. When I disable this assert by putting a # in front, the program gives an error for assert "only letters" raises error (test print 2 in below program: ['o', 'o', 'o', 'a'], thus answer is 'o' but the assert should be 'a'. As you can see I put test prints (1-3) in there to see where it goes wrong and whether the program does what I would like it to do. I am sure there is an easy explanation or approach that will check by frequency and alphabet, and saw some of the solutions in stackoverflow...stubborn, those are yet too fancy for me, and i don't understand them yet.
print test 1: shows if the iteration over the lowercase letters works (.islower would be another solution probably, but it kept the spaces, so i defined the lowercase string)
print test 2: shows if the elements are ordered in descending frequency
print test 3: shows if the "return" function gives the right output.
#CODE I HAVE UPTO NOW (as you can see most lines are test lines, and code itself is very simplistic...embarrassing):
from collections import Counter
def checkio(text: str) -> str:
isalphalower = ("abcdefghijklmnopqrstuvwxyz")
y = []
for x in text:
if x in isalphalower:
y.extend(x)
print("test1:", y)
inorder = [item for items, c in Counter(y).most_common()
for item in [items] * c]
print("test2:", inorder)
print("test3:", inorder[0])
return inorder[0]
Created at: 2019/08/08 02:24; Updated at: 2019/08/08 05:59
The question is resolved.