code confusion

Avi Gross avigross at verizon.net
Fri Apr 15 16:58:15 EDT 2022


As usual, without very clear and precise instructions and parameters, the answers may not quite fit.
It looks like you are asked two and only two questions.
The first is asking how many numbers you want. 
Before continuing, you need to make sure that is a valid number as many answer will throw an exception.
The next line, complex as it is, asks for one long answer and does not check anything and breaks rapidly unless you use just normal integer representation. Yes, it ignores any entry after the "i"th but if you want valid entries, you might want to evaluate them in a loop perhaps one at a time and keep going till you have 'i" valid ones.
I do suggest you not use the variable name of "i" for many reasons as modern languages allow more meaningful names like, well, "n"!
I understand using i, j, k in some nested loops but here I would haveused something like howMany and verified the number was an integer larger than 0.
As for getting the second largest number, there is nothing wrong with determining it the hard way. Of course for some people, it is more intuitive to sort the uniqued data and simply choose the 2nd entry from the end. Some python modules allow you tosee the ranks of various entries and you can simply choose the one of second rank. 
But if this is HW, you are being asked to do things the old-fashioned way! LOL!



-----Original Message-----
From: Dennis Lee Bieber <wlfraed at ix.netcom.com>
To: python-list at python.org
Sent: Fri, Apr 15, 2022 2:31 pm
Subject: Re: code confusion

On Fri, 15 Apr 2022 08:41:20 +0100, Tola Oj <ojomooluwatolami675 at gmail.com>
declaimed the following:

>i = int(input())

    Obtain a single /integer/ from stdin -- note: any extraneous characters
on the input line will result in a failure to convert from textual
representation to internal/binary integer

>lis = list(map(int,input().strip().split()))[:i]

    Obtain a line from stdin containing space separated /integer/
representations. Split the line at the spaces. Convert each "word" to
internal/binary integer. Keep up to at most "i" integers. Note that the
position of the [:i] could be at 
            ... .split()[:i]
The difference being that the provided code is converting all "words" on
the input into integers and then keeping the first "i"; putting the [:i]
after .split() means only the first "i" words are kept, and hence only that
many need to be converted to integer.

>z = max(lis)

    Determine largest value in the list of integers

>while max(lis) == z:
>lis.remove(max(lis))

    WHILE the largest value in the (current) list matches the initially
determined maximum value... remove that value from the list.

    Rather than repeat "max(lis)" in the .remove() invocation, just pass it
"z" (the WHILE has already confirmed that the maximum "z" is found in the
list, so why recompute the maximum).

    Note: Python indentation is significant -- the above .remove() line
needs to be indented. Presuming your code was properly indented please find
a posting client that doesn't reformat leading indentation.

>
>print (max(lis))
>

    Display the new list maximum value after removing all instances of the
initial maximum value.

>this is an answer to a question from the discussion chat in hackerrank. i
>didn't know the answer so i found an answer that fitted well to the
>question, however i struggle to understand the use of some of the methods
>and functions the person has used. my major questions are: 1. what does
>"[:i]" mean

    Learn the contents of the Library Reference Manual -- you don't need to
memorize it all, but should at least know the major groupings...

https://docs.python.org/3/library/stdtypes.html#common-sequence-operations


>                                                        2. is there
>another i could write this code using if statement?

    There are many ways to rewrite that...

UNTESTED

i = int(input("How many integers are to be considered?"))
ls = [int(wd) for wd in input(
                "enter space separated integers"
                ).split()[:i]]
maximum = max(ls)
while maximum in ls:
    ls.remove(maximum)
print(ls)

    Remove the first line, and the [:i], and the code will happily process
for however many integers are provided on the input line.

    The while/remove loop can be replaced with

ls = [itm for itm in ls if itm != maximum]

which only requires one pass through the list; while/remove has to scan the
list to see if maximum is in it, then has to scan it a second time to for
the .remove() to find where in the list it is found.

    Or...

for _ in range(ls.count(maximum)):
    ls.remove(maximum)

where _ is a "junk/temp" value that we don't care about -- we only want to
loop once for EACH maximum value

    Or...

while maximum in ls:
    del ls[ls.index(maximum)]



-- 
    Wulfraed                Dennis Lee Bieber        AF6VN
    wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list