首页 > 编程问答 >我的 Python 程序中解决 UVa 860 的运行时错误 - 熵文本分析器

我的 Python 程序中解决 UVa 860 的运行时错误 - 熵文本分析器

时间:2024-07-29 04:34:11浏览次数:10  
标签:python python-3.x runtime-error

我正在尝试为 UVa 860 编写一个解决方案,但是当我通过 vJudge 发送它时,它一直显示“运行时错误”。

from sys import stdin

import math

def main():

end_of_input = False
lambda_words = 0
dictionary = {}
text_entropy = 0
relative_entropy = 0

    while not end_of_input:
        line = stdin.readline().strip()
        if line == "****END_OF_INPUT****":
            end_of_input = True
    else:
        if line == "****END_OF_TEXT****":
            print("%s %.1f %d" % (lambda_words, text_entropy, relative_entropy))
            # print(lambda_words, round(text_entropy, 1), round(relative_entropy))
            dictionary = {}
            lambda_words = 0
            text_entropy = 0
            relative_entropy = 0
        else:
            line = line.replace(',', ' ')
            line = line.replace('.', ' ')
            line = line.replace(':', ' ')
            line = line.replace(';', ' ')
            line = line.replace('!', ' ')
            line = line.replace('?', ' ')
            line = line.replace('"', ' ')
            line = line.replace('(', ' ')
            line = line.replace(')', ' ')
            line = line.lower()
            words = line.split()
            lambda_words += len(words)
            for word in words:
                if word in dictionary:
                    dictionary[word] += 1
                else:
                    dictionary[word] = 1
            text_entropy = 0
            for word in dictionary:
                text_entropy += dictionary[word]*(math.log10(lambda_words) - math.log10(dictionary[word]))
                
            text_entropy = text_entropy / lambda_words
            max_entropy = math.log10(lambda_words)
            relative_entropy = (text_entropy / max_entropy) * 100
return 0

main()

我几乎尝试了所有方法,我检查了 Python 中的不同类型的错误,但没有任何结果似乎有帮助。 我需要帮助,希望你能帮助我! :)


错误在于你尝试将 relative_entropy (一个浮点数)舍入到打印语句中的一个整数。 %d 格式说明符用于整数。要解决这个问题,你需要使用 %f relative_entropy 打印为一个浮点数,或者将其舍入为一个整数,并在打印语句中使用 %d

以下是你的代码的修复版本:

from sys import stdin
import math

def main():
    end_of_input = False
    lambda_words = 0
    dictionary = {}
    text_entropy = 0
    relative_entropy = 0

    while not end_of_input:
        line = stdin.readline().strip()
        if line == "****END_OF_INPUT****":
            end_of_input = True
        else:
            if line == "****END_OF_TEXT****":
                # 使用 %f 打印 relative_entropy 为浮点数
                print("%s %.1f %.1f" % (lambda_words, text_entropy, relative_entropy))
                dictionary = {}
                lambda_words = 0
                text_entropy = 0
                relative_entropy = 0
            else:
                line = line.replace(',', ' ')
                line = line.replace('.', ' ')
                line = line.replace(':', ' ')
                line = line.replace(';', ' ')
                line = line.replace('!', ' ')
                line = line.replace('?', ' ')
                line = line.replace('"', ' ')
                line = line.replace('(', ' ')
                line = line.replace(')', ' ')
                line = line.lower()
                words = line.split()
                lambda_words += len(words)
                for word in words:
                    if word in dictionary:
                        dictionary[word] += 1
                    else:
                        dictionary[word] = 1
            text_entropy = 0
            for word in dictionary:
                text_entropy += dictionary[word]*(math.log10(lambda_words) - math.log10(dictionary[word]))

            text_entropy = text_entropy / lambda_words
            max_entropy = math.log10(lambda_words)
            relative_entropy = (text_entropy / max_entropy) * 100
    return 0

main()

此更正将 print 语句中的 relative_entropy 的格式说明符更改为 %.1f ,这将以一位小数的精度打印它作为一个浮点数。这应该可以解决 UVa 860 上的运行时错误。

标签:python,python-3.x,runtime-error
From: 78804760

相关文章