【问题标题】:How can i remove the break from find_frequent(s) code since our teacher told us we can't use it on the test and I'm trying to practice not using it?我怎么能从 find_frequent(s) 代码中删除中断,因为我们的老师告诉我们我们不能在测试中使用它并且我正在尝试练习不使用它?
【发布时间】:2021-03-26 16:32:10
【问题描述】:

我编写了这段代码,但我正在努力替换使用 break,因为它在作业中说我们不允许在循环中使用 continue/break 语句。

代码的目标是:

查找输入参数字符串中出现频率最高的字符并返回。例如,如果字符串是“Canada day”,则函数返回字符串“a”

如果有两个或更多字符频率相同,则函数应返回多个频率相同的值中的第一个。

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

【问题讨论】:

  • 避免使用break 的解决方法是在while condition: 中更改for 循环,而不是breaking,而是将condition 设置为false,循环结束。

标签: python


【解决方案1】:

您可以将 for 循环中的 break 语句替换为 while 循环。

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

输出是:

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

我认为您可以从这里找出答案,但如果您需要帮助来查找最常见的字符(与 break 不同的问题),请查看 herehere

【讨论】:

  • 您能否将您提到的内容应用到我的代码中?我试图应用你所说的,但我不断收到错误,我对为什么会收到错误感到困惑?我已经很长时间没有使用 while 循环了,所以我很难记住基本原理。非常感谢您的帮助
  • 嗨!由于这是一项学校作业,我无法直接解决它 (meta.stackoverflow.com/questions/334822/…),但如果您更新代码以显示您已尝试过的内容,我们可以从那里解决它
  • def find_frequent(s): L = [] count = 0 char = 0 i = 0 words = True while i count: count = L[i] char = L[i - 1] return char
  • 请更新问题,这里的缩进丢失了!
  • 抱歉,我刚刚更新了我的问题。
【解决方案2】:

如果您一开始不使用循环,则不需要break :-)

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

(其实我是认真的...禁止break 听起来就像你的老师不希望你用这样的嵌套循环编写它。)

或者如果你想给你的老师上一课(教训是他们不应该禁止东西),你可以伪造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)

【讨论】:

  • 很遗憾,你提供的两种方法我不能用,因为我们还没有在课程中学过,所以如果我使用他们没有教我们的smth,老师会扣分.另外,老师没有让我们使用中断/继续语句的另一个说明,因为我们还没有到达那里。只是我个人知道,因为我是从高中学到的。但是这门课,他们假设你对编程一无所知,这就是为什么他们禁止使用我们还没有学过的技术。不过,谢谢你的回答。感谢您的帮助。
猜你喜欢
  • 2021-06-13
  • 2010-11-11
  • 2018-09-24
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多