-1

This program is to find the second min and second max. What's the bug in the algorithm? It's not printing the second min and second max. It's printing only min and max.

a = list(map(int, input().split()))

m1 = m2 = s1 = s2 = a[0]

for i in range(len(a)):
    if a[i] > m1:
        m1 = a[i]
    if a[i] > m2 and a[i] < m1:
        m2 = a[i]
    if a[i] < s1:
        s1 = a[i]
    if a[i] < s2 and s2 > s1:
        s2 = a[i]
    print(m2, s2)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Sandeep
  • 39
  • 6
  • Its in python. Its printing max and min but its not printing second max and second min – Sandeep Aug 13 '19 at 16:06
  • go through the logic: if you set `m1=a[i]`, how should `a[i] – FObersteiner Aug 13 '19 at 16:09
  • could you edit code and send where i am doing mistake – Sandeep Aug 13 '19 at 16:12
  • 2
    @Sandeep Have you tried using a debugger? – ds_secret Aug 13 '19 at 16:22
  • There was no error to debug. – Sandeep Aug 13 '19 at 16:24
  • you could compare your logic to the one you find [here](https://stackoverflow.com/questions/19697504/how-to-find-second-largest-number-in-a-list) (and there's more...). It's about assigning new and previous value at the same time - otherwise things like I commented above happen. – FObersteiner Aug 13 '19 at 16:50
  • 1
    @Sandeep If your program does not generate expected output then it has a bug. You need to learn to use a debugger (or simple `print` statements) to trace the execution to pinpoint where your program deviates from the expected behaviour. – GZ0 Aug 13 '19 at 17:17
  • [Python Tutor](http://pythontutor.com/visualize.html) is a simple online debugger you could use for single-stepping through your code's execution. – wjandrea Aug 13 '19 at 17:18

1 Answers1

1

using an answer given in this Q&A (credits to here!) , you could do

a = [12,7,5,3,8,5,2,3,42]
# assuming no duplicates in a and len(a) > 1:
if a[0] > a[1]: # make sure that m1 != m2 and s1 != s2
    m1, m2 = a[0], a[1]
    s1, s2 = a[0], a[1]
else:
    m1, m2 = a[1], a[0]
    s1, s2 = a[1], a[0]

for i in a:
    # maximum and second maximum
    if i > m1:
        m1, m2 = i, m1
    elif i > m2:
        m2 = i
    # minimum and second minimum
    if i < s1:
        s1, s2 = i, s1
    elif i < s2:
        s2 = i

print(m1, m2, s1, s2)
# 42 12 2 3

comparing to the code in question

  • initialize correctly so that the logic works. setting everything to a[0] could work e.g. for the maxima if a[0] is not the maximum - or for the minima if a[0] is not the minimum. Not both.
  • modify the logic so that values are updated simultaneously, not sequentially. Otherwise, the conditionals won't work as I commented.

Note: I assume the whole point of this exercise is to do it all with conditionals. Otherwise, you could simplify to

a = list(set([1,7,5,3,8,5,2,3,1])) # remove duplicates with set
m1, s1 = max(a), min(a)
_, _ = a.remove(m1), a.remove(s1)
m2, s2 = max(a), min(a)
print(m1, m2, s1, s2)
# 8 7 1 2

or even simpler:

a_srt = sorted(list(set(a)))
m1, m2, s1, s2 = a_srt[-1], a_srt[-2], a_srt[0], a_srt[1]

...and I bet there's even more possibilities.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72