-1

My goal is to solve the USACO necklace problem. I try to use a yield function to return the red_num and blue_num to 0 my final_calculation is smaller than red_num or applying to else function. Input: 29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb Output(Expected): 5

However my code show False when I running the yield and return. I had iterate my function and pass it through a generator, what did I do wrong.

def calculation(letter):
    final_calculation =final_calculation_b=0
    red_num = 0
    blue_num = 0
    while True:
        for i in range(read_line - 1):
            z = i + 1
            if letter[i] == "r":
                red_num += 1
                if (letter[z] == "w"):
                    red_num += 1
                else:
                    if letter[z] == "b":
                        if final_calculation <= max(red_num, final_calculation):
                            final_calculation = max(red_num, final_calculation)
                            yield red_num == 0
                            return final_calculation

                        else:
                            return red_num==0
            elif letter[i] == "b":
                blue_num += 1
                if letter[z] == "w":
                    blue_num += 1
                else:
                    if letter[z] == "a":
                        if final_calculation_b <= max(blue_num, final_calculation):
                            final_calculation_b = max(blue_num, final_calculation)
                            yield blue_num==0
                            return final_calculation_b

                        else:
                            return blue_num==0
            else:
                red_num += 1
                blue_num += 1
        if final_calculation > final_calculation_b:
            return final_calculation
        else:
            return final_calculation_b
  • 2
    Don't use yield with return. Yield turns the function into a generator and all you are generating is a bool. – scrappedcola Aug 15 '23 at 14:35
  • 4
    `yield blue_num==0` this (and similar lines )will yield a boolean. – snakecharmerb Aug 15 '23 at 14:35
  • Does this answer your question? [What does the "yield" keyword do in Python?](https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python) – scrappedcola Aug 15 '23 at 14:35
  • 1
    Also don't return a bool in one place (ie `return blue_num==0`) and an int in others. Pick one thing for your function to do and create other functions for additional logic. – scrappedcola Aug 15 '23 at 14:38
  • `red_num == 0` is a boolean and you are yielding it. it is not an assignment. – beh-f Aug 15 '23 at 14:40
  • Welcome to Stack Overflow. Please read [ask]. We do not find the bug for you here; we require a **specific** question - which will come out of your best attempt to [understand](//meta.stackoverflow.com/q/261592/) and [locate](//ericlippert.com/2014/03/05) a specific problem, and showcase it in a [mre]. – Karl Knechtel Aug 15 '23 at 14:46

0 Answers0