Define and test a function myRange. This function should behave like Python’s standard range function, with the required and optional arguments, but should return a list. Do not use the range function in your implementation!
Expert Answer
Explanation::
- Code for myRange() is provided below. Code runs without any error.
- Code is developed in pyCharm software.
- Every detail is provided in code itself.
- All important syntax and variable are explained well in comments format.
- myRange() has *args as parameter like this -> def myRange(*args) in its definition.
- *args is used when there are variable number of parameters depending on the calls. Any number of parameters can be passed.
- But for myRange() at maximum we have used three parameters.
- Read comments for better understanding of the code.
- If you get identation issues after copying the code, then I have provided screenshots of the code too. So refer them if any error occurs.
Code in python::
""" In function named myRange(*args), I have created a list called myList which is returned at the end of the function. There are three condtitions of if/elif/else for 1,2 and 3 number of arguments respectively. Every code below is explained well in comments format. """ def myRange(*args): myList=[] if len(args) is 1: """ This condtition is true when there is only one parameter passed into function. Variable k is created and it is initialized to 0. Now passed value can be either positive(>0) or negative(<0) or 0 itself(empty list returned). """ k=0 if args[0] > 0: """ If parameter is positive, a while loop is run starting from k=0 to args[0] args[0] itself is not added in the list. For eg if args[0]=5 then myList=[0,1,2,3,4] is returned. In every iteration of loop, variable k is added into the list and incremented by 1 i.e k=k+1. """ while k < args[0]: myList.append(k) k=k+1 else: """ If parameter is negative, a while loop is run starting from k=0 to args[0] args[0] itself is not added in the list. For eg if args[0]= -5 then myList=[0,-1,-2,-3,-4] is returned. In every iteration of loop, variable k is added into the list and decremented by 1 i.e k=k-1. """ while k > args[0]: myList.append(k) k=k-1 elif len(args) is 2: """ This condition is true when there are two parameters passed into the function. First parameter i.e args[0] is starting value of range. Second parameter i.e args[1] is ending value of range. Variable k is declared and initialized to args[0](Starting value of range) If both parametere are same i.e args[0]==args[1] then an empty list is returned. """ k = args[0] if args[0] < args[1]: """ If args[0] < args[1], here while loop run from k=args[0] upto args[1]. For eg args[0]=4 and args[1]=10, then myList=[4,5,6,7,8,9] is returned. In every iteration of while loop value k is appended into the list and k is incremented by 1 i.e k=k+1. Here args[1] itself is not added. """ while k < args[1]: myList.append(k) k=k+1 else: """ If args[0] > args[1], here while loop run from k=args[0] upto args[1]. For eg args[0]=10 and args[1]=4, then myList=[10,9,8,7,6,5] is returned. In every iteration of while loop value k is appended into the list and k is incremented by 1 i.e k=k+1. Here args[1] itself is not added. """ while k > args[1]: myList.append(k) k=k-1 else: """ This condition is for three parameters passed into function. It is similar to 2 parameter functioning, here instead of increasing value k by 1, it is incremented by third parameter i.e args[2]. """ k=args[0] if args[0] < args[1]: while k < args[1]: myList.append(k) k=k+args[2] else: while k > args[1]: myList.append(k) k=k+args[2] """ Finally at the end myList is returned """ return myList """ Calling myRange() function with different parameters as follows. """ print("=================One parameter function call===================") print(myRange(10)) print(myRange(-10)) print("=================Two parameter function call===================") print(myRange(10,20)) print(myRange(10,-10)) print(myRange(10,10)) #same parameters print("=================Three parameter function call===================") print(myRange(2,25,3)) print(myRange(25,2,-3))
Output::
Screenshots of the code for identation….refer line number of the code on every pictures.
Picture 1:
Picture 2:
Picture 3:
Picture 4:
Picture 5:
Picture 6: