0

I have been stuck with this question on finding out the second largest number in a list to be read from the console with:

n = size of the list(number of elements) AND arr = The list itself

One of the constraints is -100<=A[i]<=100 which I am unable to integrate within my code as I am getting a LIST INDEX OUT OF RANGE ERROR. For instance, this code fails for the custom input: 3 -10 0 10

but passes for 3 10 0 10

n = int(input())
arr = map(int, input().split())

sortedlist = sorted(arr)
temp = len(sortedlist)

emptylist = []

i = 0 
for temp in range(2, 11):
    if sortedlist[i]<sortedlist[i+1]:
        i+=1
        emptylist.insert(i, sortedlist[i-1])
b = max(emptylist)
print(emptylist)

Error:

Traceback (most recent call last):

File "solution.py", line 12, in if sortedlist[i]

IndexError: list index out of range

5
  • So you sort the list then check if sortedlist[i] < sortedlist[i+1], why don't you trust Python builtins to make their job ? And what is for temp in range(2, 11): supposed to accomplish ? Commented May 16, 2017 at 10:23
  • Because i is getting incremented every time and you have no checking whether it crossed the list length limit or not Commented May 16, 2017 at 10:23
  • @polku because it could be == Commented May 16, 2017 at 10:23
  • As for the second case it passes as second element is greater than the third, i is not incremented again. So i always stays 1 Commented May 16, 2017 at 10:29
  • Got it! I was unnecessarily putting a constraint on temp between 2 and 11 as the question asked but that wasn't needed. For i in range(0, temp-1) did the job. Commented May 16, 2017 at 10:36

3 Answers 3

2

if sortedlist[i] < sortedlist[i+1]:

The problem lies here. In your example n = 3 so i can only be 0, 1 or 2. But in your code, i+1 goes 1, 2 and 3 in the above line, which is out of the array bounds.

You can print i and see that i goes till 2 which is n-1 and so i+1 will be out of bounds. That is one easy way you could debug.

You have to ensure a condition where you do not compare with the next element for the last element.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Got it. Issue was with my constraints for i in the for loop. For i in range(0, temp-1) did the job.
Great! But just out of curiosity, why are you not using sortedList[-2] to get the second largest element? Would make your job much easier!
I guess I got too obsessed with trying to find the logic behind it.
0

you can just use sortedlist[-2] to get the second largest number.

In case of repeated numbers you have to use set method call over original/sorted list.

sortedlist = sorted(list(set(arr)))

Comments

0

try below snippet number_of_inputs = 4 # this is user defined i = 0 collections = list() while True: user_input = input("Enter your numbers \n") if 100 >= user_input >= -100: i += 1 collections.append(user_input) if i == number_of_inputs: print "second largest number is \t ",sorted(collections)[-2] break else: print"this Entry not allowed !!!"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.