Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Python 10.33 In the simple coin game you are given an initial number of coins and then, in every iteration of the game, you

Using Python

10.33 In the simple coin game you are given an initial number of coins and then, in every iteration of the game, you are required to get rid of a certain number of coins using one of the following rules. If n is the number of coins you have then: If n is divisible by 10, then you may give back 9 coins. If n is even, then you may give back exactly n/2 1 coins. If n is divisible by 3, then you may give back 7 coins. If n is divisible by 4, then you may give back 6 coins. If none of the rules can be applied, you lose. The goal of the game is to end up with exactly 8 coins. Note that more than one rule may be applied for some values of n. If n is 20, for example, rule 1 could be applied to end up with 11 coins. Since no rule can be applied to 11 coins, you would lose the game. Alternatively, rule 4 could be applied to end up with 14 coins, and then rule 2 could be applied to end up with 8 coins and win the game. Implement a function coins() that takes as input the initial number of coins and returns True if there is some way to play the game and end up with 8 coins. The function should return False only if there is no way to win. >>> coins(7) False >>> coins(8) True >>> coins(20) True >>> coins(66) False >>> coins(99) True 10.34 Using linear recursion, implement function recDup() that takes a list as input and returns a copy of it in which every list item has been duplicated. >>> recDup(['ant', 'bat', 'cat', 'dog']) ['ant', 'ant', 'bat', 'bat', 'cat', 'cat', 'dog', 'dog']

10.35 Using linear recursion, implement function recReverse() that takes a list as input and returns a reversed copy of the list. >>> lst = [1, 3, 5, 7, 9] >>> recReverse(lst) [9, 7, 5, 3, 1]

10.36 Using linear recursion, implement function recSplit() that takes, as input, a list lst and a nonnegative integer i no greater than the size of lst. The function should split the list into two parts so that the second part contains exactly the last i items of the list. The function should return a list containing the two parts. >>> recSplit([1, 2, 3, 4, 5, 6, 7], 3) [[1, 2, 3, 4], [5, 6, 7]]

10.37 Implement a function that draws patterns of squares like this: (a) To get started, first implement function square() that takes as input a Turtle object and three integers x, y, and s and makes the Turtle object trace a square of side length s centered at coordinates (x, y). >>> from turtle import Screen, Turtle >>> s = Screen() >>> t = Turtle() >>> t.pensize(2) >>> square(t, 0, 0, 200) #draws the square (b) Now implement recursive function squares() that takes the same inputs as function square plus an integer n and draws a pattern of squares. When n = 0, nothing is drawn. When n = 1, the same square drawn by square(t, 0, 0, 200) is drawn. When n = 2 the pattern is: Each of the four small squares is centered at an endpoint of the large square and has length 1/2.2 of the original square. When n = 3, the pattern is:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions