Take a list of integers (n) and return a new list with n times each integer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Take a list of integers (n) and return a new list with n times each integer

Write a function named n_copies that accepts a list of integers a as a parameter and returns a new list a2, with each element value n from a replaced by n consecutive copies of the value n at the same relative location in the list. For example, if a list named a stores the following element values: [3, 5, 0, 2, 2, -7, 0, 4] Then the call of a2 = n_copies(a) should return a new list a2 containing the following elements. The idea is that the value 3 was replaced by three 3s the 5 was replaced by five 5s and so on. [3, 3, 3, 5, 5, 5, 5, 5, 2, 2, 2, 2, 4, 4, 4, 4] Any element whose value is 0 or negative should not be kept in the returned list (as with 0 and -7 above). Constraints: In solving this problem, you must create a single new list to be returned, but aside from that, do not create any other data structures such as temporary lists or strings. You may use as many simple variables (such as ints) as you like. def n_copies(a): i = 0 while i < len(a)-1: a.insert(i, (a[i] * i)) i += 2 return a this code just multiplies each integer by n...

5th Apr 2018, 10:01 PM
Taylor Deal
Taylor Deal - avatar
10 Answers
5th Apr 2018, 11:55 PM
John Wells
John Wells - avatar
+ 3
Updated my code to make the while loop change so there is no question of it fitting the constraints. It now has 3 variables: the resulting new list, the current integer number from the original list, and the integer count of how many times the number has been added to the new list.
6th Apr 2018, 2:29 AM
John Wells
John Wells - avatar
+ 3
Based on your constraints, range might be illegal. You are asked to make a new list so I wouldn't change a (1st it isn't needed & 2nd screws with your indexing.) a2.append(val) adds the value at the end of the new list.
7th Apr 2018, 1:59 AM
John Wells
John Wells - avatar
+ 2
Xan it states you must create a new list: Constraints: In solving this problem, you must create a single new list to be returned
6th Apr 2018, 2:00 AM
John Wells
John Wells - avatar
+ 2
yes. My only question is my use of range(n). That might not be allowed. Might need to replace the for statement on line 26 with: i = 0 while i < n: i += 1
6th Apr 2018, 2:06 AM
John Wells
John Wells - avatar
+ 1
Oh yes! Whoops. 3am coding dizziness on my part! I take my hat off to you ☺ Python is fun though, right? ☺
6th Apr 2018, 2:01 AM
Emma
+ 1
You've mastered this one ☺ Spot on.
6th Apr 2018, 2:08 AM
Emma
0
I've updated my code now. I realised John's isn't quite right. He creates an empty list... it specifies in the instructions that you can't create a temporary list: "do not create any other data structures such as temporary lists or strings" def n_copies(a): # Start at the beginning of the list. i = 0 # While we haven't finished processing the list, do... while (i < len(a)): # val is the current integer we're working on in the original list. val = a[i] # Remove values zero or less. if val <= 0: a.pop(i) # Add val (val - 1) times in the correct place. else: for j in range(1, val): a.insert(i, val) # Move our pointer to the correct place, for the next integer. i += val # Return the completed list. return a This code is here: https://code.sololearn.com/chUAB6j2976a/#py
6th Apr 2018, 1:39 AM
Emma
0
I missed that at first too! The exceptions (what we weren't allowed to use), were hidden way down in the wads of text! It must have been for educational purposes, rather than to solve the problem! I wasted hours getting caught out too!
6th Apr 2018, 1:58 AM
Emma
0
How do you make it into a new list named a2? i'm trying to do something like adding a2 += a but i know this is not the correct format. def n_copies(a): # Start at the beginning of the list. i = 0 a2 = [] # While we haven't finished processing the list, do... while (i < len(a)): # val is the current integer we're working on in the original list. val = a[i] # Remove values zero or less. if val <= 0: a.pop(i) a2 += a # Add val (val - 1) times in the correct place. else: for j in range(1, val): a.insert(i, val) a2 += a # Move our pointer to the correct place, for the next integer. i += val # Return the completed list. return a
6th Apr 2018, 11:00 PM
Taylor Deal
Taylor Deal - avatar