ThrowCards in python
Given is an ordered deck of n cards numbered 1 to n with card 1 at the top
and card n at the bottom.
The following operation is performed as long as there are at least two
cards in the deck:
Throw away the top card and move the card that is now on the top of the
deck to the bottom of the deck.
My task is to find the sequence of the last k discarded cards and the last
remaining card.
Each line of input contains two non-negative numbers
n, where n ¡Ü 5000
k, where k < n
For each input line produce two lines of output.
The sequence of k discarded cards
The last remaining card.
See the sample for the expected format.
Sample input
7 2
19 4
10 5
6 3
4000 7
Output for sample input
Last 2 cards discarded: [4, 2]
Remaining card: 6
Last 4 cards discarded: [2, 10, 18, 14]
Remaining card: 6
Last 5 cards discarded: [9, 2, 6, 10, 8]
Remaining card: 4
Last 3 cards discarded: [5, 2, 6]
Remaining card: 4
Last 7 cards discarded: [320, 1344, 2368, 3392, 832, 2880, 1856]
Remaining card: 3904
My code will keep printing out the exact answers but with None on the
following line.
I am so confused why it will print None after each output.
Here is my code:
def throw_card(n,k):
lst=[]
bst=[]
for i in range(1,n+1):
lst.append(i)
while lst[0]!=lst[1] and len(lst)>1 and n<=5000 and k<n:
bst.append(lst.pop(0))
if len(lst)==1:
break
else:
lst.append(lst[0])
lst.remove(lst[0])
print('Last',k,'cards discarded: ',bst[n-(k+1):])
print('Remaining card: ',lst.pop())
print(throw_card(7,2))
print(throw_card(19,4))
print(throw_card(10,5))
print(throw_card(6,3))
print(throw_card(4000,7))
my output:
Last 2 cards discarded: [4, 2]
Remaining card: 6
None
Last 4 cards discarded: [2, 10, 18, 14]
Remaining card: 6
None
Last 5 cards discarded: [9, 2, 6, 10, 8]
Remaining card: 4
None
Last 3 cards discarded: [5, 2, 6]
Remaining card: 4
None
Last 7 cards discarded: [320, 1344, 2368, 3392, 832, 2880, 1856]
Remaining card: 3904
None
No comments:
Post a Comment