code confusion

Dennis Lee Bieber wlfraed at ix.netcom.com
Fri Apr 15 14:31:33 EDT 2022


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/


More information about the Python-list mailing list