How to make this code loop again and again? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to make this code loop again and again?

arr = [1,2,3] Count = 0 step-1 sort the array Step-2 check if min(arr)==max(arr): break else: Step-3- iterate over the arr in range (len(arr)-1): arr[i]+=1 Count+=1 step-4 now again check if min(arr) == max(arr): break else: go to step 1 and repeat until all the elements become equal... For eg: [1,2,3] #here 1(min) and 3(max)..so by leaving the max element..we have to make the min element equal to to 3..so this is how we do it.. 1 2 3. 2 3 3. 1step 3 4 3. 2step 4 4 4. < Every element is equal now...and it was a 3 step process..so the Count = 3 I am having problems in implementation..so i need a lil help.. I made a working solution, but it take too long to give a solution for a very large array..like in range(1000) Link> https://code.sololearn.com/c1W10i4qNI3w/?ref=app

9th Apr 2020, 6:13 AM
Amit Dubey
Amit Dubey - avatar
12 Answers
+ 2
Oh...but new version is better
9th Apr 2020, 7:25 AM
Oma Falk
Oma Falk - avatar
+ 1
Well check out the previous question i asked, i tried a recursive solution but recursion is a new thing for me right now..Lily Mea
9th Apr 2020, 6:45 AM
Amit Dubey
Amit Dubey - avatar
+ 1
are all numbers different?
9th Apr 2020, 7:06 AM
Oma Falk
Oma Falk - avatar
0
do u mean u want to increase other values in the list till it equal the max value in the list?
9th Apr 2020, 6:22 AM
durian
durian - avatar
0
Yes..Lily Mea ...but the max value keeps changing..but if the code is looping over the previous max value..the loop doesn't care about the new max value until it is finished...i don't how to change the max value while in a loop..
9th Apr 2020, 6:30 AM
Amit Dubey
Amit Dubey - avatar
0
i mean let say u have a list [1,2,4,5] u want to keep increasing 1,2,4 till it becomes 5 and stop the loop?
9th Apr 2020, 6:37 AM
durian
durian - avatar
0
why dont u try recursion
9th Apr 2020, 6:38 AM
durian
durian - avatar
0
See how it counts: 1 2 4 5 2 3 5 5. (added 1 to every elem..except the max) 3 4 6 5. (+1) 4 5 7 5 (+1) 5 6 8 5 < the min element became equal to max..but the max changed to '8', so i need to make them equal again.. after sorting.. 5 5 6 8 6 6 7 8 7 7 8 8 8 8 9 8...again max changed to 9, so again Sort 8 8 8 9 9 9 9 9 it took 10 steps to equalize..so the count = 10 Lily Mea
9th Apr 2020, 6:51 AM
Amit Dubey
Amit Dubey - avatar
0
in recursion u put the algorithm to a function and keep calling it till it break for example arr = [1,2,3,8,2,3] def func(arr) : if(min(arr) == max(arr)): return; #when this if statement true,the recursion break, else ind = arr.index(min(arr)) #get the index of min value arr[ind] += 1 #adding 1 func(arr) #call this function back func(arr) #call the func print(arr) #print the result but still,the larger the list, the larger time it takes to run... im new to this programming thing too,but i think that probably work
9th Apr 2020, 6:59 AM
durian
durian - avatar
0
Well, it did make every element equal to 5..but that's not a solution for the question..Great effort tho!👍 Lily Mea
9th Apr 2020, 7:07 AM
Amit Dubey
Amit Dubey - avatar
0
The solution that you gave, got accepted..and it was really fast..thanks again.. Oma Falk
9th Apr 2020, 7:09 AM
Amit Dubey
Amit Dubey - avatar
0
So can you implement the new version? And paste your solution here? Oma Falk
9th Apr 2020, 8:44 AM
Amit Dubey
Amit Dubey - avatar