Question
CANNOT USE ANY LOOPS (CANNOT USE FOR, IF-ELSE, CONDITIONALS, WHILE, etc) NO LOOPS WHAT SO EVER My class has not gotten to loops yet so
CANNOT USE ANY LOOPS (CANNOT USE FOR, IF-ELSE, CONDITIONALS, WHILE, etc) NO LOOPS WHAT SO EVER
My class has not gotten to loops yet so we have to use the functions python has built for tuples. I am to write the code for the given function based on specifications given. I tried making tuple into a list. Then I made the list into a string of numbers then I searched the string for a and replaced a for b. But I know this is incorrect because when I test my code I get incorrect answers. Again I cannot use any type of loops
Below is my code. I tried to comment it
def replace_first(tup,a,b): """ Returns a copy of tup with the first value of a replaced by b Examples: replace_first((1,2,1),1,3) returns (3,2,1) replace_first((1,2,1),4,3) returns (1,2,1) Parameter tup: The tuple to copy Precondition: tup is a tuple of integers Parameter a: The value to replace Precondition: a is an int Parameter b: The value to replace with Precondition: b is an int """ #turns my tuple into a list lists = list(tup) print(lists) #makes my list into a string of numbers res = "".join(map(str,lists)) print(res) #makes a into a string str_a = str(a) print(str_a) #searches the string for a pos_a = res.find(str_a) print(pos_a) #gives me the position of a char_a = res[pos_a] print(char_a) #makes b into a string strb = str(b) #replaces a and b one time repl = res.replace(char_a,strb,1) print(repl)
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