Write a function repeat_elem(values, index, num_times) that takes as inputs a list values, an integer index (which you may assume is a valid index for one of the elements in values), and a positive integer num_times, and that returns a new list in which the element of values at position index has been repeated num_times times. For example:
>>> repeat_elem([10, 11, 12, 13], 2, 4) result: [10, 11, 12, 12, 12, 12, 13]
In the above example, the second input is 2 and the third input is 4, so we take the element at position 2 of the list (the 12) and repeat it 4 times.
Other examples:
>>> repeat_elem([10, 11, 12, 13], 2, 6) # repeat element 2 six times result: [10, 11, 12, 12, 12, 12, 12, 12, 13] >>> repeat_elem([5, 6, 7], 1, 3) # repeat element 1 three times result: [5, 6, 6, 6, 7]
CODE :
SIMPLIFIED BROKEN DOWN VERSION
#FUNCTION NAME : repeat_elem
#INPUT PARAMETERS :
#values = A list of numbers
#index = A valid index within the list[Assumption is given in the
question]
#num_times = Number of times the value needs to
copied.
#OUTPUT : A list with all the changes.
FUNCTION
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
def repeat_elem(values,index,num_times):
#Here we define an empty list to store the final result.
new_values = []
#Then we are storing values into the new_value list before the
given index.
#Example of this synatx : If values = [1,2,3,4,5], and index = 3,
then new_values = [1,2,3] i.e. upto index-1
new_values = values[:index]
#Then we are storing the repeated values into an temporary
list.
#Example of this syntax : [50] * 5 = [50, 50, 50, 50, 50]
temp_list = [values[index]] * num_times #Here values[index] gives
the value to be repeated, and num_times specifies number of
times
#Then we are extending the previous list to attach the list of
repeated values.
new_values.extend(temp_list)
#Finally we are again extending the previously modified list to
attach the rest of the list elements.
new_values.extend(values[index+1:])
#Then we return the new_list
return new_values
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
FUNCTION CALL
#Here we are calling the function with one of the specified
input in the question :
result = repeat_elem([10, 11, 12, 13], 2, 6)
print("The new list is : ",result)
Function can be written down in one line : (More compact and clean way)
#FUNCTION NAME : repeat_elem
#INPUT PARAMETERS :
#values = A list of numbers
#index = A valid index within the list[Assumption is given in the
question]
#num_times = Number of times the value needs to
copied.
#OUTPUT : A list with all the
changes.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
def repeat_elem(values,index,num_times):
new_values = values[:index] + [values[index]] * num_times +
values[index+1:] #All operation in one line
return new_values
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
FUNCTION CALL
#Here we are calling the function with one of the specified
input in the question :
result = repeat_elem([10, 11, 12, 13], 2, 6)
print("The new list is : ",result)
SCREENSHOTS :
Get Answers For Free
Most questions answered within 1 hours.