辅导CS1210留学生、讲解CS/Python语言、解析Python设计、讲解Python
- 首页 >> Python编程CS1210 Computer Science I: Foundations
Homework 1: Beggar My Neighbour
Due Friday, October 5 at 11:59PM
Introduction
Beggar my neighbour is a British card game for two players that dates back to at least 1840. Indeed,
Charles Dickens’ Great Expectations (1861) makes reference to this game. In Pip’s own words:
Miss Havisham beckoned her to come close, and took up a jewel from the table, and tried its effect
upon her fair young bosom and against her pretty brown hair. ‘‘Your own, one day, my dear,
and you will use it well. Let me see you play cards with this boy.’’
‘‘With this boy? Why, he is a common laboring boy!’’
I thought I overheard Miss Havisham answer,—only it seemed so unlikely,—‘‘Well? You can
break his heart.’’
‘‘What do you play, boy?’’ asked Estella of myself, with the greatest disdain.
‘‘Nothing but beggar my neighbour, miss.’’
‘‘Beggar him,’’ said Miss Havisham to Estella. So we sat down to cards.
Game play is extremely simple. A standard 52 card deck is divided in half, with each player placing his or
her half, face down, on the table before them. Players alternate taking the top card off of their stack and
placing it — face up — at the center of the table.
Play continues in alternating fashion until a player places an Ace, King, Queen, or Jack on the growing
pile at center table. These ‘‘penalty cards’’ stop the regular flow of the game. Before restarting regular
play, the opponent must ‘‘pay’’acertain number of cards by placing them on the pile. The number of
cards paid depends on the value of the penalty card:
Ace = 4 cards
King = 3 cards
Queen = 2 cards
Jack = 1 card
Note that in the event that the cards ‘‘paid in penalty’’ themselves contain a penalty card, payment is
halted, and the player who played the original penalty card must now pay a penalty to the other player.
When the last penalty is paid, the player who played the most recent penalty card collects all of the cards
from the center of the table and adds them to the bottom of their own stack (in the same order they were
originally played). This player then plays the top card of his/her stack to the center of the table to resume
regular play. The player who collects all of the cards wins the game.
Note that if there were no penalty cards the game would terminate quickly, with all the cards
accumulating at the center of the table. With the addition of the penalty cards, the game can last a very
long time! Just how long is open to question; Wikipedia claims Is thereanon-terminating game of
beggar-my-neighbour? is an unsolved mathematical problem (put more succinctly, the actual question is
is there an ordering of the individual player’s stacks that leads to a game of beggar-my-neighbour that
will never end?
Resources
Start from the Python template file attached to this assignment. The file contains the skeleton of the
system you will be implementing; your job is to complete each function in the skeleton as instructed in
1 Revised September 21, 2018
the template file itself.
You should use the TA office hours to your advantage. If a funciton you are to complete is unclear to you,
then ask the TAs to help you understand what the input/output behavior of the function should be.
As usual, you should start by completing the hawkid() function so that we may properly credit you for
your work. Test hawkid() to ensure it in fact returns your own hawkid as the only element in a single
element tuple. As you work on each function, test it independently to make sure your code functions as
expected. Feel free to upload versions of your code to ICON as you go; we only grade the last version
uploaded, so this practice allows you to "lock in" working partial solutions prior to the deadline. Finally,
some general guidance:
(1) You will be graded on both the correctness and the quality of your code, including the quality of
your comments!
(2) As usual, respect the function signatures provided.
(3) Be careful with iteration; always choose the most appropriate form of iteration
(comprehension, while, or for) as the function mandates. Poorly selected iterative forms may be
graded down, even if they work!
(4) Finally, spend time polishing your solutions; show us you know how to write good quality code.
Which of these two would you rather read?
de f swap (T) :
if l en (T) < 2 : # Special case for T=() and len(T)==1
re t urn(T ) # Return T unchanged
re t urn(T( (T[ - 1] , ) + T[1 : -1] + (T[ 0 ] , ) ) )
or
de f swap (T) :
if l en (T) < 2 :
re t urn(T )
el s e :
f = T[ 0 ]
l = T[ - 1 ]
m= T[1 : -1]
r= l ist ( [f])
r . ex t e nd( l ist (m) )
r . append( l ist ( l ) )
re t urn( t up l e( r ) )
2 Revised September 21, 2018