I wrote this code but the result is wrong... why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I wrote this code but the result is wrong... why?

Write a program that reads 20 numbers from the input and at the end prints the number that has the largest number of divisors and also print the number of divisors in the output. If there are several numbers in this state, print the largest one. Input: 767 665 999 895 907 796 561 914 719 819 555 529 672 933 882 869 801 660 879 985 Output will be: 672 24 https://code.sololearn.com/cxxP8QVSsl0N/?ref=app

18th Oct 2022, 11:46 AM
Amateur
7 Answers
+ 2
#last lines corrections need : nr_divisors = nr_divisors[::-1] #need to store back idx = nr_divisors.index(max(nr_divisors)) #find index return idx,max(nr_divisors) #return index, max divisors largest, tedad = find_integer_with_most_divisors(*a) print(a[::-1][largest], tedad) #largest is the index of element from end
18th Oct 2022, 1:18 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 8
i have done a try not to correct the given code, but to simplify the complete approach. you can find it in the attached file, also a description how it is working. https://code.sololearn.com/cwqCTgoe5ULK/?ref=app
18th Oct 2022, 7:56 PM
Lothar
Lothar - avatar
+ 3
Jayakrishna๐Ÿ‡ฎ๐Ÿ‡ณ Wooow thank u so much ๐ŸŒน๐ŸŒน๐ŸŒน๐ŸŒน
18th Oct 2022, 1:24 PM
Amateur
+ 2
You are taking a = [] . It creates each time a empty in loop. So you will have only last input in list. All will get replaced. Took it before loop start.. edit: Ali return input_list[len(nr_divisors) - 1] , nr_divisors[-1] first one give you the original list last element. and nr_divisors[-1] will return list last element..
18th Oct 2022, 12:03 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 2
18th Oct 2022, 1:11 PM
Amateur
+ 2
Lothar Fabulous ๐Ÿ‘๐Ÿ‘๐Ÿ‘
19th Oct 2022, 12:40 AM
Amateur
0
reverse list nr_divisors by nr_divisors[::-1] and find max number then it's index. Index value is the index of largest divisors index from end to start. Ali
18th Oct 2022, 12:14 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ