0

I'm out of mind! How can I write it program?

Hi I'm trying to solve this problem but I can't find out the algorithm. Imagine a list: [1,2,3,4,5] (The list must be Arithmetic progression! It can be any Arithmetic progression With any denominator  with limited elements for example 100 elements) Output: We should start from multiple of 1 and 2 and 3 and so on ... and the last one is multiple of len(list) Multiple of 1: 1,2,3,4,... Multiple of 2: 2,4,6,8,.. Multiple of 3: 3,6,9,... And so on.. Now for multiple of 1 in our given list: [1,2,3,4,5] Multiple of 2 in our given list: [2,4] Multiple of 3 in our given list:[3] ... Multiple of 5 (the last one) in list: [5] Now first of all we change all elements to 0: [0,0,0,0,0] Now we start from the multiple of 1 and all the elements will be one: [1,1,1,1,1] Second we we start from multiple of 2 in our new [1,1,1,1,1] list and elements will be zero: [1,0,1,0,1] And now multiple of 3 in our new list: [1,0,0,0,1] And now for 4: [1,0,0,1,1] And for last one (5): [1,0,0,1,0] So [1,0,0,1,0] is output

9th Oct 2021, 8:15 PM
Amirreza
Amirreza - avatar
8 Respostas
0
Simon Sauter so like this? a=[0,0,0,0] b=len(a) def main(m): for i in range(m,b,m+1): if a[i]==0: a[i]=1 elif a[i]==1: a[i]=0 for i in range(len(a)): main(i) print(a)
9th Oct 2021, 10:16 PM
Amirreza
Amirreza - avatar
+ 2
Where's your attempt?
9th Oct 2021, 8:27 PM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
+ 2
I don't see the connection between those lists of multiples and and the lists of ones and zeroes. But I can tell you how the lists of ones and zeroes work. (I'm not sure if this is a point where you're stuck.) In every step you flip the numbers at certain positions from zero to one or one to zero. Step 0: You start with a list of zeroes. Step 1: You flip every number. Step 2: You flip every second number. Step 3: You flip every third number. Step 4: You flip every fourth number. Step 5 You flip every fifth number. ... Step n: You flip every nth number.
9th Oct 2021, 8:55 PM
Simon Sauter
Simon Sauter - avatar
+ 2
I guess the connection between the lists of multiples and the lists of ones and zeroes is that you can use the lists of multiples to determine which numbers to flip. Although you don't really have to create those lists to do that.
9th Oct 2021, 9:03 PM
Simon Sauter
Simon Sauter - avatar
0
Dino Wun (Use the search bar plz!) Deleted my code! I tried two code it in two different ways: 1: in a for loops trying to code it but it wasn't true because it was working for all elements 2: I wrote a program to to say our multiples we need so we can make four list from it: a=[1,2,3,4] v=[] def main(m, count): for i in range(m,count*m,m): v.append(i) for i in range(1,len(a)+1): main(i,len(a)+1) print(v) But I'm out of mind
9th Oct 2021, 8:41 PM
Amirreza
Amirreza - avatar
0
Dino Wun (Use the search bar plz!) Output of that code: [1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16] I call it s Ok it has 16 elements and each list must have has 4 elements what if we say like this that s//len(list) and print it on a loops so we will have 4 lists ! I don't have any idea now
9th Oct 2021, 8:45 PM
Amirreza
Amirreza - avatar
0
Let me debug this code and analyze how the code compiled the wrong output.
9th Oct 2021, 8:47 PM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
0
Dino Wun (Use the search bar plz!) It's not complete it's only one step of the question that came to my mind but don't have any idea for the rest of it
9th Oct 2021, 8:48 PM
Amirreza
Amirreza - avatar