Question
Answer these methods in python please.? 1.) A function change() that accepts a number of U.S. cents and returns a tuple containing, respectively, the smallest
Answer these methods in python please.? 1.) A function change() that accepts a number of U.S. cents and returns a tuple containing, respectively, the smallest number of U.S. quarters, dimes, nickels, and pennies that equal the given amount.
2.) A function strip_quotes() that accepts a string and returns a new string equivalent to the argument but with all single quotes and double quotes removed.
3.) A function scramble() that randomly permutes a string. What does random mean? It means that each time you call the function for a given argument, all possible permutations are equally likely. Random is not the same as arbitrary.
4.) A generator function powers() that yields successive powers of a base starting at 1 and going up to some limit.
5.) A function triples() returning a list of all (positive) integer Pythagorean triples for all hypotenuse values up to, and including, some value.
6.) A chainable function say() that accepts one string per call, but when called without arguments, returns the words previously passed, in order, separated by a single space. ** Please implement the above six functions in Python and name the source file as h1.py. A test file h1_test.py is provided above! You need to install the pytest before compiling the files.
Solutions:
def change(amt):
if amt < 0:
raise ValueError('amount cannot be negative')
quarters, other = divmod(amt, 25)
dimes, other = divmod(other, 10)
nickels, pennies = divmod(other, 5)
return (quarters, dimes, nickels, pennies)
import re
def strip_quotes(s):
return re.sub(r'\'|\"', '', s)
import random
def scramble(s):
return ''.join(random.sample(s, len(s)))
def say(first_word=None):
words = []
def say_more(word=None):
if word is None:
return ' '.join(words)
words.append(word)
return say_more
return say_more(first_word)
def triples(n):
return [(x, y, z) for x in range(1, n + 1)
for y in range(x, n + 1)
for z in range(y, n + 1)
if x * x + y * y == z * z]
def powers(base, limit):
result = 1
while result <= limit:
yield result
result *= base
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started