What is the problem with my logic? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the problem with my logic?

Hello, I trying to write a small program with python 3 to find out the runner up from given number. What is the problem with my code? https://code.sololearn.com/cE2veg4thWGh/?ref=app

17th Jan 2019, 7:13 AM
Shifan
Shifan - avatar
19 Answers
+ 5
import math champ = -math.inf runner = -math.inf and your code without any changes.
17th Jan 2019, 9:27 AM
Խաչատուր Դալլաքյան
Խաչատուր Դալլաքյան - avatar
+ 11
This should work even for negative numbers: s = '-2 -3 -4' l = list(map(int, s.split())) largest = l[0] second_largest = min(l) # using min() feels like cheating for n in l[1:]: if n > largest: second_largest, largest = largest, n else: if n > second_largest: second_largest = n print('second largest number:', second_largest) If you don't want to sort the list, you could also just remove the largest number from the list and find the largest number from the remaining list, which will be the second largest number of the original list.
17th Jan 2019, 8:45 AM
Anna
Anna - avatar
+ 4
I don't know, how is it supposed to work, but when I give input: "2" #or any single digit it prints -1. When I give input: "1 2 4" #or any space separated sequence of digits, it gives the second last number.
17th Jan 2019, 7:22 AM
Seb TheS
Seb TheS - avatar
+ 4
Խաչատուր Դալլաքյան Why? That will result in an error because it'll try to convert spaces to integers. If there aren't any spaces, it will convert each digit into an integer, not the number as a whole. '1234' will be converted to [1, 2, 3, 4].
17th Jan 2019, 7:51 AM
Anna
Anna - avatar
+ 4
The purpose of his program, as I understood is to find second biggest digit in number. arr = [int(a) for a in n] this line of code just splits number into separate digits. I just thought he wants to do so. Because his code for separated numbers works.
17th Jan 2019, 7:57 AM
Խաչատուր Դալլաքյան
Խաչատուր Դալլաքյան - avatar
+ 3
What kind of errors do you get?
17th Jan 2019, 7:43 AM
Anna
Anna - avatar
+ 3
Shifan we are waiting for your response. Please explain once again how do you want program to work. And what is the problem with program.
17th Jan 2019, 8:05 AM
Խաչատուր Դալլաքյան
Խաչատուր Դալլաքյան - avatar
+ 3
Shifan For what kind of input?
17th Jan 2019, 8:19 AM
Seb TheS
Seb TheS - avatar
+ 3
You check whether i>champ, when there are numbers that are less then -1, they will go throw your code else part, although they shouldn't. And champ and runner will remain -1 unless positive number appears which is wrong. That's why we change this two variable to minus infinity and regardless in first step values of champ and runner will be change. For better understanding your code won't work for numbers less that 4 for example if you give champ and runner initial value 4.
17th Jan 2019, 9:38 AM
Խաչատուր Դալլաքյան
Խաչատուր Դալլաքյան - avatar
+ 2
Ok, that might be possible 🤔
17th Jan 2019, 7:58 AM
Anna
Anna - avatar
+ 2
Anna and Rewa Mathur Thanks for your input :)
17th Jan 2019, 9:11 AM
Shifan
Shifan - avatar
+ 2
The problem of your code is that you give your variable initial value -1, your code can work if you gave that variable value - infinity.
17th Jan 2019, 9:21 AM
Խաչատուր Դալլաքյան
Խաչատուր Դալլաքյան - avatar
+ 1
m = n.split() arr = map(int,m) change this two lines of code into this one: arr = [int(a) for a in n]
17th Jan 2019, 7:50 AM
Խաչատուր Դալլաքյան
Խաչատուր Դալլաքյան - avatar
+ 1
I would like to find the second largest number but my program is not work for all inputs..sometimes it fails to find exact answer
17th Jan 2019, 8:18 AM
Shifan
Shifan - avatar
+ 1
Seb TheS -2 -3 -4
17th Jan 2019, 8:37 AM
Shifan
Shifan - avatar
+ 1
Rewa Mathur I would like to do this without sorting.
17th Jan 2019, 8:39 AM
Shifan
Shifan - avatar
+ 1
Hi Խաչատուր Դալլաքյան Thanks again. Here is the complete code and it works fine: import math if __name__ == '__main__': n = input() m = n.split() arr = map(int,m) champ = -math.inf runner = -math.inf for i in arr: if(i > champ): runner = champ champ = i elif(i < champ): if(runner < i): runner = i print(runner)
17th Jan 2019, 9:37 AM
Shifan
Shifan - avatar
0
What should be the initial value ? Please put modified code.Thanks
17th Jan 2019, 9:23 AM
Shifan
Shifan - avatar
0
Խաչատուր Դալլաքյան Thanks for your input . Could you please explain the logic ?
17th Jan 2019, 9:29 AM
Shifan
Shifan - avatar