Question

Write a function repeat_elem(values, index, num_times) that takes as inputs a list values, an integer index...

  1. 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]
    

Homework Answers

Answer #1

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 :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the...
In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the elements of an input list in pairs. That is the first element of the resulting list is the sum of the first two elements of the input, the second element of the resulting list is the sum of the 3rd and 4th elements of the input, and so on. If there is an odd number of elements, then the last element remains unchanged. As...
Write a java program that creates an integer array with 50 random values, prompts the user...
Write a java program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. “Out of Bounds”) and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle invalid inputs,...
Write an Oz program (having an Oz recursive function/procedure) which takes in the input a list,...
Write an Oz program (having an Oz recursive function/procedure) which takes in the input a list, say L, and two positive integers, M and N. The function should return another list L’, which represents the sub-list of L containing all the elements from index M to index N in reverse order. For example, if the input list is [a 2 g 5 4 k] and M is 2 and N is 5, then the output list should be [4 5...
Create a function called, convert. This function receives a string parameter called word which only contains...
Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works: - This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times,...
Write a function that takes, as an argument, a list, identified by the variable aList. If...
Write a function that takes, as an argument, a list, identified by the variable aList. If the list only contains elements containing digits (either as strings as integers), return the string formed by concatenating all of the elements in the list (see the example that follows). Otherwise, return a string indicating the length of the list, as specified in the examples that follow. Name this function AmIDigits(aList). For example, >>> AmIDigits([“hello”, 23]) should return the string “The length of the...
Write the function most_factors(numbers) that returns the integer from the list numbers that has the most...
Write the function most_factors(numbers) that returns the integer from the list numbers that has the most factors (divisors without remainder). For example: >>> most_factors([5,10,16,20,25]) 20 # because 20 has the most factors of any of these numbers # 6 factors, i.e., [1, 2, 4, 5, 10, 20] >>> most_factors([1, 2, 3, 4, 5]) 4 # because 4 has the most factors of any of these numbers # 3 factors, i.e., [1, 2, 4] Hints: For each element in numbers, call...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function returns an empty matrix. Otherwise, the function doubles every odd element of A and returns the resulting matrix. Notice that the output matrix will have all even elements. For example, the call B = matrix_problem([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. The function should work...
Write a method that takes an integer array and returns a copy of this array, without...
Write a method that takes an integer array and returns a copy of this array, without the middle (two) elements. result of removing two values from the array { 2, 6, 2, 4 } [ 2 4 ] The result of removing one value from the array { 4, 6, 2, 3, 4, 5, 6 } [ 4 6 2 4 5 6 ] Using Java.
A Queue is a linked list with pointers to both the head of the list as...
A Queue is a linked list with pointers to both the head of the list as well as the tail (or the last element) of the list. Given a queue Q, Q.head gives the head of the queue and Q.tail gives the tail of the queue. Give O(1) time algorithms for the following tasks. Enqueue • Input: A queue Q of distinct integers and a queue element a not in Q. 1 • Output: Q with the element a added...
IN SML Write a function dupList of type 'a list -> 'a list whose output list...
IN SML Write a function dupList of type 'a list -> 'a list whose output list is the same as the input list but with each element of the input list repeated twice in a row. For example, if the input is [1, 2, 3], the output list should produce [1, 1, 2, 2, 3, 3]. If the input list [], the output list should be []. Do not use explicit recursion but use one of the fold functions. Do...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT