阅读有关网球比赛的资料。通则问题

2024-06-17 11:04:33 发布

您现在位置:Python中文网/ 问答频道 /正文

示例文件和预期结果:

> file_1.txt
A. Aardvark d. C. Capybara
C. Capybara d. B. Badger
C. Capybara d. F. Flamingo
A. Aardvark d. G. Goat
F. Flamingo d. Bye
E. Echidna d. D. Dolphin
A. Aardvark d. E. Echidna

> python spoon.py file_1.txt
No one wins the wooden spoon.

示例文件和预期结果:

> file_2.txt
C. Capybara d. A. Aardvark
C. Capybara d. B. Badger
C. Capybara d. F. Flamingo
A. Aardvark d. G. Goat
F. Flamingo d. Bye
E. Echidna d. D. Dolphin
A. Aardvark d. E. Echidna

> python spoon.py file_2.txt
D. Dolphin wins the wooden spoon!

QF: E. Echidna d. D. Dolphin
SF: A. Aardvark d. E. Echidna
F: C. Capybara d. A. Aardvark

信息:

  • 编写一个从文件中读取数据的脚本。你知道吗
  • 这些数据包括在同一地点举行的个人比赛的结果 网球锦标赛。你知道吗
  • 数据是随机排列的。包含 比赛并不一定意味着这是第一场 播放了。你知道吗
  • 用剧本来决定谁赢了木勺。你知道吗
  • 如果有人真的赢了木勺-一定要把所有的照片都打印出来 决定勺子结果的比赛。你知道吗

我所做的:

  • 我想出了如何为每一轮 如果总共有三组“回合”的话。 意思:四分之一决赛,半决赛,决赛。你知道吗
  • 我基本上写了一个脚本,它贯穿了每一行 数据和查找匹配的地方失败者(球员二)的名字 “正在分析的行”与获胜者的姓名不符 (玩家一)。你知道吗
  • 我用输家和赢家这两个词,因为d指的是“失败”。你知道吗
  • 一旦我创建了“三个”列表,我就会逆向工作 使用决赛中失败者的名字,找到他/她的对手 在半决赛中。然后我把那场比赛的失败者找出来 他/她在四分之一决赛中的比赛。你知道吗
  • 基本上,第一场比赛的输家 最后)拿到木勺。你知道吗
  • 如果那场比赛的失败者是“再见”,那么就没有人会得到木勺。你知道吗

问题:

  • 我的剧本很棒,只要有三个“回合”的剧本 比赛。你知道吗
  • 如果匹配的数量增加或减少超过 三轮“它不能提供正确的数据。你知道吗
  • 我想得到一些帮助来概括我的解决方案,使它工作 不管文件中的数据有多大或多小。你知道吗
  • 理想情况下,我希望脚本能够遍历所有的数据 把所有的事情都弄清楚而不需要我编写多个代码 包括一个可能上升到第50轮的场景。你知道吗

问题场景和预期结果示例:

> file_3.txt
A. Aardvark d. C. Capybara
A. Aardvark d. E. Echidna
C. Capybara d. F. Flamingo

> python spoon.py file_2.txt
F. Flamingo wins the wooden spoon!
SF: C. Capybara d. F. Flamingo
F: A. Aardvark d. C. Capybara

当前结果:

- Traceback (most recent call last):
-   File "woodenspoon.py", line 56, in <module>
-     winner, loser = final.split(' d. ')
- NameError: name 'final' is not defined

我当前的代码/进度:

import sys

if len(sys.argv) < 2:
    sys.exit("No file specified")
try:
    filename = sys.argv[1]
    file = open(filename, 'r')
except:
    sys.exit("Could not read file")

outcome = []
for line in file:
    outcome.append(line.strip())

element_index = len(outcome)-1
round_1 = []

for i in range(0, element_index):
    isSame = False
    for d in range(i+1, element_index):
        org_player_1, org_player_2 = outcome[i].split(' d. ')
        comp_player_1, comp_player_2 = outcome[d].split(' d. ')
        if org_player_2 == comp_player_1:
            isSame = True
    if isSame == False:
        round_1.append(outcome[i])

for i in range(len(round_1)):
    try:
        outcome.remove(round_1[i])
    except:
        pass

element_index = len(outcome)
round_2 = []

for i in range(0, element_index):
    isSame = False
    for d in range(i+1, element_index):
        org_player_1, org_player_2 = outcome[i].split(' d. ')
        comp_player_1, comp_player_2 = outcome[d].split(' d. ')
        if org_player_2 == comp_player_1:
            isSame = True
    if isSame == False:
        round_2.append(outcome[i])

for i in range(len(round_2)):
    try:
        outcome.remove(round_2[i])
    except:
        pass

for e in outcome:
    final = e

winner, loser = final.split(' d. ')

for e in round_2:
    if loser in e:
        semifinal = e

winner, loser = semifinal.split(' d. ')

for e in round_1:
    if loser in e:
        quaterfinal = e

winner, loser = quaterfinal.split(' d. ')

if "Bye" in loser:
    print("No one wins the wooden spoon.")
else:
    print("{} wins the wooden spoon!\n".format(loser))
    print("QF: {}".format(quaterfinal))
    print("SF: {}".format(semifinal))
    print("F: {}".format(final))

注意:

  • 我为这篇冗长的帖子/问题道歉。我想试着提供 尽可能多的细节。你知道吗
  • 我希望我没有错过任何关于剧本的重要细节 它是如何运作的,以及预期的结果。你知道吗
  • 我对Python有相当基本的了解。我仍然在学习我的方法。你知道吗
  • 谢谢你的时间和帮助。非常感谢!你知道吗

Tags: intxtforiffilesplitplayerechidna