如何解决链接堆栈记分牌
我被这个问题困住了,我不知道如何为此实现代码,我对 python 有点陌生,所以对我来说是裸露的。我需要实现一个记分牌来记录分数最高的游戏的名称和分数。
我需要在骨架中实现的代码是:
构造函数,它保持对第一个节点的引用,即链表的头部。为方便起见,它保留了记分板的容量限制以及列表中可能的当前节点数。
addscore 对我来说有点大,addscore 函数可以找到新分数的当前位置,其中计分板中分数最高的节点位于列表的最前面,随后的节点按分数递减排序订购。
辅助函数 getNode 返回记分板中特定位置的节点
函数 score 和 playerName 分别返回特定位置的分数和玩家名称,并使用 getNode 函数
printHighSchoolscoreList 打印记分板的所有内容。这不应使用 getNode、score 或 playername 函数,而是遍历列表中的节点一次以打印内容。
class Linkedscoreboard:
"""scoreboard implementation using a singly linked list for storage."""
#-------------------------- nested _Node class --------------------------
class _Node:
"""Lightweight,nonpublic class for storing a singly linked node."""
__slots__ = '_playerName','_score','_next' # streamline memory usage
def __init__(self,playerName,score,next): # initialize node's fields
self._playerName = playerName # name
self._score = score # score
self._next = next # reference to next node
#------------------------------- stack methods -------------------------------
def __init__(self,capacity):
"""Create an empty scoreboard."""
# Todo: Implement the constructor.
# Keep a reference to the first node in the list,the head.
# Initially the scoreboard is empty and the head reference None.
# Keep the capacity limit,to determine when to discard scores.
# Possibly keep the current number of nodes in the list if you want to
# avoid going through the list to count.
self.head = None
self.size = 0
def __len__(self):
"""Return the number of elements in the scoreboard."""
# Todo: implement
return self.size
def isEmpty(self):
"""Return True if the scoreboard is empty."""
# Todo implement
return self.size == 0
def addscore(self,score):
"""Add element with playerName and score at the right place of the scoreboard."""
# Todo implement
# The head node contains the node with the highest score.
# The list consists of at most capacity number of nodes,in order of
# decreasing scores.
# When adding a score you need to:
# - Find the correct position for the new score.
# - If it is within the scoreboard capacity,create a new Node.
# - Insert it at the right position (adjust next/ head references).
# - If the scoreboard capacity is exceeded remove the reference to the
# node that is pushed out
# - Adjust the current number of nodes in the list if you keep it as a
# member variable.
return
def _getNode(self,position):
"""Helper function to get the node at a position in the list,to be used
by playerName() and score(). Raise Exception if there is no node at position.
position 1 refers to the first element in the list with the highest score."""
# Todo: Implement
# Walk through the nodes in the list until the correct position
return None
def playerName(self,position):
"""Returns the player name at a position in the list"""
return ""
"""Returns the score at a position in the list"""
def score(self,position):
return 0
def printHighscoreList(self):
"""Print the high score list
"""
# Todo Print the contents of the scoreboard.
# Start with printing a scoreboard header
# Go through each node in the list,and print the position number,# player name and score.
# Do not use the _getNode,playerName and score functions,because
# it is more efficient to go through the nodes in the linked list once.
return
scoreboard = Linkedscoreboard(5)
testReport(scoreboard.isEmpty(),"scoreboard empty")
scoreboard.printHighscoreList()
scoreboard.addscore("Donald Duck",10)
scoreboard.addscore("Pluto",12)
testReport(not scoreboard.isEmpty(),"scoreboard not empty")
testReport(len(scoreboard) == 2,"scoreboard len 2")
testReport(scoreboard.playerName(1) == "Pluto","scoreboard playerName 1")
testReport(scoreboard.score(1) == 12,"scoreboard score 1")
testReport(scoreboard.playerName(2) == "Donald Duck","scoreboard playerName 2")
testReport(scoreboard.score(2) == 10,"scoreboard score 2")
scoreboard.printHighscoreList()
scoreboard.addscore("Mickey Mouse",7)
scoreboard.addscore("Donald Duck",19)
scoreboard.addscore("Goofy",5)
scoreboard.addscore("Pluto",9)
testReport(len(scoreboard) == 5,"scoreboard len 5")
testReport(scoreboard.playerName(1) == "Donald Duck","scoreboard playerName 1")
testReport(scoreboard.score(1) == 19,"scoreboard score 1")
testReport(scoreboard.playerName(2) == "Pluto","scoreboard playerName 2")
testReport(scoreboard.score(2) == 12,"scoreboard score 2")
testReport(scoreboard.playerName(3) == "Donald Duck","scoreboard playerName 3")
testReport(scoreboard.score(3) == 10,"scoreboard score 3")
testReport(scoreboard.playerName(4) == "Pluto","scoreboard playerName 4")
testReport(scoreboard.score(4) == 9,"scoreboard score 4")
testReport(scoreboard.playerName(5) == "Mickey Mouse","scoreboard playerName 5")
testReport(scoreboard.score(5) == 7,"scoreboard score 5")
scoreboard.printHighscoreList()
scoreboard.addscore("Daisy Duck",8)
testReport(scoreboard.playerName(5) == "Daisy Duck","scoreboard playerName 5")
testReport(scoreboard.score(5) == 8,"scoreboard score 5")
testReport(len(scoreboard) == 5,"scoreboard len 5")
scoreboard.printHighscoreList()
要查看记分板分数是通过还是失败,我们需要此测试报告
def testReport(test,testInfo):
if test:
print("PASS: " + testInfo)
else:
print("FAIL: " + testInfo)
这个问题的整个部分是让所有的分数和玩家名都通过。
谢谢//
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。