0

I wrote this code, and I'm struggling to replace using break since it says in the homework that we are not allowed to use continue/break statements in our loops.

The code's goal is:

finds the character that appears most frequently in the input parameter string and returns it. For Example, if the string is "Canada day" the function returns the string "a"

If there are two characters or more with the same frequency, the function should return the first of many values with the same frequency.

def find_frequent(s):
    """
    -------------------------------------------------------
    description
    Use:
    -------------------------------------------------------
    Parameters:
        name - description (type)
    Returns:
        name - description (type)
    ------------------------------------------------------
    """
    L = []
    count = 0
    char = 0
    i = 0
    words = True
    while i < 1 and words:
        x = s[i]
        for j in range(len(L)):
            if x == L[j]:
                words = False
                L[j + 1] = L[j + 1] + 1
        else:
            L.append(x)
            L.append(1)

    for i in range(1, len(L), 2):
        if L[i] > count:
            count = L[i]
            char = L[i - 1]
    return char
the output should look like this 

`Enter a string: Hello Hi`

output should be

`Most frequent characters: H`



I'm getting this output

Enter a string: canada

Most frequent character: c
  • 2
    A workaround to avoid using `break` is to change the `for` loop in a `while condition:` and instead of `break`ing you set the `condition` to false and the loop ends. – Pietro Mar 26 '21 at 16:34

2 Answers2

0

You can replace the break statement in a for loop with a while loop.

print("using for loop + break")

for i in range(0, 5):
    print(i)
    if i == 3:
        break


print("using while")

i = 0
keep_searching = True
while i < 5 and keep_searching:
    print(i)
    if i == 3:
        keep_searching = False
    i += 1

The output is:

using for loop + break
0
1
2
3
using while
0
1
2
3

I think you can figure it out from here, but if you need help with finding the most frequent character (a different issue from the break), take a look here and here.

miquelvir
  • 1,748
  • 1
  • 7
  • 21
  • Can you kindly apply what you mentioned to my code? I tried to apply what you said, but I keep getting errors, and I'm confused about why I'm getting errors? It's been a long time I didn't use while loop, so I'm struggling to remember the fundamentals. your help is highly appreciated – sara miller Mar 26 '21 at 18:35
  • Hi! Since it's a school assignment I can't directly solve it (https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) but if you update your code showing what you have tried we can work it from there – miquelvir Mar 26 '21 at 18:38
  • def find_frequent(s): L = [] count = 0 char = 0 i = 0 words = True while i < 1 and words: x = s[i] for j in range(len(L)): if x == L[j]: words = False L[j + 1] = L[j + 1] + 1 else: L.append(x) L.append(1) for i in range(1, len(L), 2): if L[i] > count: count = L[i] char = L[i - 1] return char – sara miller Mar 26 '21 at 19:07
  • please update the question, indentation here is lost! – miquelvir Mar 26 '21 at 19:22
  • sorry about that, I just updated my question. – sara miller Mar 26 '21 at 19:29
-1

No need for a break if you don't use a loop in the first place :-)

def find_frequent(s):
    return max(s, key=s.count)

(I'm actually serious... forbidding break sounds to me like your teacher didn't want you to write it with such nested loops.)

Or if you want to teach your teacher a lesson (the lesson being that they shouldn't forbid stuff), you could fake the break:

    for i in range(0, len(s), 1):
        x = s[i]
        it = iter(range(len(L)))            # make it an iterator
        for j in it:
            if x == L[j]:
                L[j + 1] = L[j + 1] + 1
                *it,                        # exhaust the iterator
                it = None                   # make it false
        if it:                              # instead of `else`
            L.append(x)
            L.append(1)
Manuel
  • 912
  • 4
  • 11
  • Unfortunately, the two methods you offered I can't use them coz we still didn't take it in the course so the teacher will take off marks if I use smth they didn't teach us yet. Also, another note the teacher didn't let us use break/continue statements coz we still haven't gotten there yet. It's just that I personally know it coz I've learned it from high school. But this course, they assume you know nothing about programming, that's why they forbade using techniques, we didn't learn yet. However, thank you for your answers. I appreciate your help. – sara miller Mar 26 '21 at 17:37