Why my code fails to get the last item on a list while using a for loop?

Question:

I wrote a program to compare numbers and get the minimum one of each couple. First number on input is count of couples and the other ones are the numbers just to be compared with each other 2 by 2. When i execute and give the input the last number on the list is not included. I wanna know why and how to fix it.

There is my code.

class Solution:

    def __init__(self):
        self.iput = input('Enter the numbers: ')
        self.num_list = self.iput.split() 

    def min_of_two(self):
        result = ''
        for i in range(0,int(self.num_list.pop(0)) * 2 ,2):
            result += str(min(self.num_list[i], self.num_list[i+1])) + ' '
        return result
x = Solution()
x.min_of_two()

When i execute this and gave the input

3
5 3
2 8
100 15

The output is

'3 2 100 '

instead of

'3 2 15 '
Asked By: frouLet

||

Answers:

Manual input isn’t needed in sample code..
there is also no class-related issues, so skip both and just define your input as a list of integers:

inp = [3, 5, 3, 2, 8, 100, 15]

Try to keep functions simple, ideally only do "one" thing, e.g. return the list of miniums of pairs:

def min_of_two(length, pairs):
    res = []
    for i in range(length):
        res.append(min(pairs[i*2], pairs[i*2+1]))
    return res

Then prepare your input in the format the function needs them, here, split the length out before calling the function:

print(min_of_two(length=inp[0], pairs=inp[1:]))

(I’m also naming the parameters since inp[0] etc. is rather cryptic)

If you want the output as a string, you should convert it after running the function:

result = min_of_two(length=inp[0], pairs=inp[1:])
text_result = ' '.join(result)
print(text_result)

For completeness…

Input functionality is best located in their own function that does all the user interaction and converts the input to the correct format before handing it off to the rest of the program.
If you’re not using the actual length of the list of pairs, but rely on what the user entered, it might be useful to verify the input:

def ask_user_for_list():
    while True:
        iput = input('Enter the numbers: ')
        lst = [int(val) for val in iput.split()]
        if lst[0] == len(lst[1:]) / 2:
            return lst
        print("That doesn't seem right, try again.."

ps: it is perfectly fine to use ranges with steps as well:

    for i in range(0, length*2, 2):
        res.append(min(pairs[i], pairs[i+1])
Answered By: thebjorn

I get the correct result by simply modifying the for loop with these

def min_of_two(self):
        result = ''
        for i in range(0,int(self.num_list.pop(0)) * 2 ,2):
            result += str(min(int(self.num_list[i]), int(self.num_list[i+1]))) + ' '
        return result

Notice the result with a small str to int change. I don’t why but it worked.

Answered By: frouLet

I don’t if this is true but my guess is that because the type if number are str python check digit by digit

for example:
‘100’ & ’15’

1 is equal to 1

moves to the second digit

5 is greater than 0

so 15 is greater

like I said I don’t if my theory is true or not but it makes sense

here is your code:

class Solution:

    def __init__(self):
        self.iput = input('Enter the numbers: ')
        self.num_list = self.iput.split() 
        
    def min_of_two(self):
        result = ''
        for i in range(0,int(self.num_list.pop(0)) * 2 ,2):

            # change self.num_list[i] & self.num_list[i + 1] from str to int
            result += str(min(int(self.num_list[i]), int(self.num_list[i+1]))) + ' '
            
        return result
x = Solution()
print(x.min_of_two())
Answered By: Mustang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.