first want to say I am new to Python but I am eager to learn and have searched around for a solution, can't seem to figure this problem out without resorting to many lines of code.
We recently recieved an assignment for our course which looks this:
Write a program that, given a text, computes the frequency of every letter and outputs the most and the least frequent one.
Do not use:
• external libraries (no "import" statement is allowed)
• dictionaries
• sets
• predefined functions (e.g., max(), min() )
I have asked for clarification on the predefined functions but have not received a reply. Would be grateful for your input or feedback, I'll paste my code so far below, it's unfinished but gives an idea of what it will look like. I did use "count" in it but I can replace that with my own counters if we aren't allowed to use "count."
txt = input('Skriv en text: ').replace(" ","").lower()
counters = []
for c in txt:
counters.append(c)
a = counters.count("a")
b = counters.count("b")
c = counters.count("c")
d = counters.count("d")
e = counters.count("e")
f = counters.count("f")
g = counters.count("g")
h = counters.count("h")
i = counters.count("i")
j = counters.count("j")
k = counters.count("k")
l = counters.count("l")
m = counters.count("m")
n = counters.count("n")
o = counters.count("o")
p = counters.count("p")
q = counters.count("q")
r = counters.count("r")
s = counters.count("s")
t = counters.count("t")
u = counters.count("u")
v = counters.count("v")
x = counters.count("x")
y = counters.count("y")
z = counters.count("z")
if a >= b or c or d or e or f or g or h or i or j or k or l or m or n or o or p or q or r or s or t or u or v or x or y or z:
print ("A has the highest frequency.")
elif b >= a or c or d or e or f or g or h or i or j or k or l or m or n or o or p or q or r or s or t or u or v or x or y or z:
print()
# Will do this for each letter and for the minimum value as well
CodePudding user response:
For erach character in the string, add it and its count to a list if it's not already in the list. This requires nested lists and therefore has terrible performance implications for large strings.
def count(s):
counts = []
for c in s:
if not any(True for (k, _) in counts if k == c):
counts.append((c, s.count(c)))
return counts
>>> count("hello world")
[('h', 1), ('e', 1), ('l', 3), ('o', 2), (' ', 1), ('w', 1), ('r', 1), ('d', 1)]
CodePudding user response:
I believe Chris solution is the smallest possible, I however prefer readability, so here it's an alternative solution:
def count_char(word):
counter_list = []
for char in word:
index = get_index(char, counter_list)
if(index == -1):
counter_list.append((char, 1))
else:
char, value = counter_list[index]
value = 1
counter_list[index] = (char, value)
return counter_list
def get_index(character, haystack) -> int:
for index, char_tuple in enumerate(haystack):
char, _ = char_tuple
if char == character:
return index
return -1
count_char("myrepeatedword")
As already stated, due to the constraints of your problem, the performance here is far away from being optimal.