71.2k views
23 votes
doubleUp 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 Kbang
by
3.1k points

1 Answer

3 votes

Answer:

The solution in Python is as follows:

class myClass:

def doubleUp(self,myList):

myList[:0] = myList[::2] = myList[1::2] = myList[:]

mylist = [1, 3, 2, 7]

list = myClass()

list.doubleUp(mylist)

print(mylist)

Step-by-step explanation:

To create a method in Python, the first step is to create a Class.

This is done in the following line:

class myClass:

Then this line defines the method

def doubleUp(self,myList):

This line duplicates the elements of the list

myList[:0] = myList[::2] = myList[1::2] = myList[:]

This defines the list

mylist = [1, 3, 2, 7]

This creates an instance of the class

list = myClass()

This passes the list to the doubleUp method

list.doubleUp(mylist)

This prints the duplicated list

print(mylist)

User Stanimirovv
by
3.6k points