151k views
10 votes
Write a method doubleUp that doubles the size of a list of integers by doubling-up each element in the list. Assume there's enough space in the array to double the size. Suppose a list stores the values [1, 3, 2, 7]. After calling list.doubleUp(), the list should store the values [1, 1, 3, 3, 2, 2, 7, 7].

User Hequ
by
4.0k points

1 Answer

7 votes

Answer:

def double_up(self, mylist):

doubled = list()

for item in mylist:

for i in range(2):

doubled.append(item)

return doubled

Step-by-step explanation:

The program method double_up gets the list argument, iterates over the list and double of each item is appended to a new list called doubled.

Methods are functions defined in classes. A class is a blueprint of a data structure. Each instance of the same class holds the same attributes and features.

User ISpark
by
3.9k points