0% found this document useful (0 votes)
43 views2 pages

Python Question Solution For Q1

This document defines four functions (nim_minimal, nim, nim_best, nim_human) that represent different strategies for playing the game of Nim. It then defines a player pool containing these four functions. The document provides a select_players function to select two players from the pool for a game. It also defines a game function that uses the selected players to play a game of Nim by alternating turns and updating the heap size until it reaches zero.

Uploaded by

Saurav Basu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Python Question Solution For Q1

This document defines four functions (nim_minimal, nim, nim_best, nim_human) that represent different strategies for playing the game of Nim. It then defines a player pool containing these four functions. The document provides a select_players function to select two players from the pool for a game. It also defines a game function that uses the selected players to play a game of Nim by alternating turns and updating the heap size until it reaches zero.

Uploaded by

Saurav Basu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

###########################################################
#####
#
# three basic players
#
###########################################################
#####
def nim_minimal(n):
return 1 # 1 is always a legal move,
# unless we already lost
def nim(n):
# note: n is guaranteed to be at least 1
return random.choice(range(1, min(n,3)+1))
def nim_best(n):
taken = n % 4
if taken:
return taken
else: # taken is 0, we lose - just take randomly
# pick randomly 1 or more
# but never more than either limit of sticks, 3
# or available sticks, n
return random.choice(range(1, min(n,3)+1))
def nim_human(n):
while True: # get input until it's legal
taken = int(input("There are %d sticks. How many do you take? (1/2/3) " %
n))
if taken in range(1, min(n,3)+1):
return taken
print("Illegal move.")
###########################################################
#####
#
# player candidates
#
###########################################################
#####
# these are the functions
player_pool = [nim_minimal, nim, nim_best, nim_human]
# transform into a dictionary with function name mapping to
# function
player_pool = { p.__name__:p for p in player_pool }
def select_players():
players = [] # we need two
# select the players
while len(players) < 2:
# select more players
print("These are the players: %s" % "/".join(player_pool.keys()))
p = input("Name one: ")
if p not in player_pool.keys():
print("Not a valid player. Select again: ")
continue
players.append(p)

print("Player %s begins, player %s plays second." % tuple(players))


return players
def game():
while True:
n = int(input("Heap size? "))
if n > 0: break # accept only positive
current, other = tuple(select_players()) # tuple with two elements
# game runs
while n > 0: # as long as there are sticks in the heap
print("Heap has %d sticks." % n)
taken = player_pool[current](n) # carry out move; guaranteed legal
print("%s takes %d sticks.\n" % (current, taken))
n -= taken # update heap
current, other = other, current # now it's the other player's turn
print("%s has lost." % current)

Assignment 1.2
You have four numbers (integers or floats) x,y,z,u.
Write a function sort4(.) that takes 4 numerical arguments and returns a sorted tuple of
these
arguments. Do not use the list's sort() method.
def sort4(x,y,z,u):
# sort the two pairs
if x > y:
x,y = y,x
if z > u:
z,u = u,z
# after this, u is the largest of them all
if y > u:
y,u = u,y
# and x will now become the smallest
if x > z:
x,z = z,x
# the last pair is the middle one, sort it:
if y > z:
y,z = z,y
return x,y,z,u

Test your sorting algorithm with the permutation generator.

from sort4 import *


from perm import *
fourlist = list(range(1,4+1))
for unsorted in perm(fourlist):
s = list(sort4(*unsorted)) # unpack
print("Unsorted:", unsorted, "Sorting", "Ok" if s == fourlist else "Wrong!")

You might also like