- #1
- 7,377
- 11,342
Hi,
I have a Python 3 algorithm to output the "Runner Up", not originally mine:
We're given a list of numbers and we're expected to output the second largest of these.
It runs well, but I'm unclear as to what output is/are expected
So that, e.g., for [1,4,2,4], it would output 2, the 2nd largest value.
I (think) I get the idea:
We start with a list(?). We then use 'map" to iterate over int, which I assume
means we turn each element into an int data type, as map() takes input(fun, iter), i.e., it
iterates a function on a data structure and stores it as an array.
Then 'set' turns it into a set, eliminating repeated entries, turns it into a list,
sorts it, returns the second highest element through the [-2]
Thing is I'm prompted to enter an input, and I'm not clear on just what input that is.
I've tried the obvious, an integer, which works, but I get the error:
If I try to input a list instead, I get an error too :
---------------------------------------------------------------------------
Any ideas?
I have a Python 3 algorithm to output the "Runner Up", not originally mine:
We're given a list of numbers and we're expected to output the second largest of these.
It runs well, but I'm unclear as to what output is/are expected
Python:
n=int(input())
arr=map(int,input().split())
print(sorted(list(set(arr))[-2])
So that, e.g., for [1,4,2,4], it would output 2, the 2nd largest value.
I (think) I get the idea:
We start with a list(?). We then use 'map" to iterate over int, which I assume
means we turn each element into an int data type, as map() takes input(fun, iter), i.e., it
iterates a function on a data structure and stores it as an array.
Then 'set' turns it into a set, eliminating repeated entries, turns it into a list,
sorts it, returns the second highest element through the [-2]
Thing is I'm prompted to enter an input, and I'm not clear on just what input that is.
I've tried the obvious, an integer, which works, but I get the error:
Python:
AttributeError Traceback (most recent call last)
<ipython-input-12-57efaba15b2f> in <module>
1 n=int(input())
----> 2 arr=map(int, input.split())
3 print(sorted(list(set(arr)))[-2])
4
AttributeError: 'function' object has no attribute 'split'
If I try to input a list instead, I get an error too :
---------------------------------------------------------------------------
Python:
ValueError Traceback (most recent call last)
<ipython-input-13-57efaba15b2f> in <module>
1 ----> n=int(input())
2 arr=map(int, input.split())
3 print(sorted(list(set(arr)))[-2])
4
ValueError: invalid literal for int() with base 10: '[1,3,2,4]'
Any ideas?
Last edited by a moderator: