23.1k views
20 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.

User Jatin Jha
by
5.9k points

1 Answer

6 votes

Answer:

The solution in Python is as follows:

class myClass:

def doubleUp(self,doubleList):

doubleList[:0] = doubleList[::2] = doubleList[1::2] = doubleList[:]

newlist = []

n = int(input("Number of elements: "))

for i in range(n):

num = int(input(": "))

newlist.append(num)

list = myClass()

list.doubleUp(newlist)

print(newlist)

Step-by-step explanation:

Start by creating a class

The solution in Python is as follows:

class myClass:

Then define the method doubleUp

def doubleUp(self,doubleList):

The duplicates is done here

doubleList[:0] = doubleList[::2] = doubleList[1::2] = doubleList[:]

The program main begins here

This defines an empty list

newlist = []

This prompts user for number of elements

n = int(input("Number of elements: "))

The following iteration get the elements of the list

for i in range(n):

num = int(input(": "))

newlist.append(num)

This defines the instance of the class

list = myClass()

This calls the method

list.doubleUp(newlist)

Lastly, this prints the duplicate list

print(newlist)

User Amrith
by
5.6k points