Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need a python 3 help from operator import add, mul def square(x): return x * x def negate(x): return -x def identity(x): return x
I need a python 3 help
from operator import add, mul def square(x): return x * x def negate(x): return -x def identity(x): return x
def triple(x): return 3 * x
def increment(x): return x+1
def increment_by(x, step): return x + step
Q2.5 Higher Order Inputs
Write a function higher_order_input that takes inputs x and returns a function given the following conditions:
- If x is a string, then return a function that takes integer n and converts the first n characters of input x to uppercase and prints the new string n times. If n is greater than the length of the string, then return the original string.
- If the input is an integer, then return a function that takes function f and integer n and prepends the $ sign and appends n zeros to f(x). if n is negative, return the original number as a string with a prepended $ sign.
- If the input is a list, then return a function that takes function f as input and applies it to the first n elements of the given list and returns the list. If n is greater than the size of the list, then return a function that reverses the list.
- For any other input types, return a function that takes a function f and integer n and returns f(n)
For the case where the input is a list, you can assume that the list will not contains other lists. Please review the doctests for specifics. Note: each sub function that you write needs asserts and therefore you do not need asserts for x.
def higher_order_input(x): """ Return functions given the guidelines in the hw2 write-up >>> string_func = higher_order_input('Halicioglu') >>> string_func(6)
HALICIoglu
HALICIoglu
HALICIoglu
HALICIoglu
HALICIoglu
HALICIoglu
'HALICIoglu'
>>> string_func(12) 'Halicioglu'
>>> int_func = higher_order_input(15) >>> int_func(increment, 6) '$16000000'
>>> list_func = higher_order_input([1, 2, 3, 6, 9]) >>> list_func(square, 3) [1, 4, 9, 6, 9]
>>> rand_func = higher_order_input(True) >>> rand_func(increment, 5) 6 """ #Your code goes here#
Ps. You should assert statements for every input for every function
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