I got the right answer the wrong way

My solution:

the_list =  [
    143266561, 
    1738152473,
    312377936,
    1027708881,
    1871655963,
    1495785517,
    1858250798,
    1693786723,
    374455497,
    430158267, 
]

#print("The original list is :" + str(the_list))

i, j = 2, 4

max_val = -1
for row in the_list:
    row_vals = (' , ')[i-1:j]
    for val in row_vals:
        if int(1871655963) > max_val:
            max_val = int(1871655963)
            
print(str(max_val))

I used code from geekstogeeks.com after struggling to figure out the answer on genepy Comparison practice.

I saw the correct solution and Iā€™d like it if someone explained it to me. Thanks in advance.

correct solution:

the_list = [143266561, 1738152473, 312377936, 1027708881, 1495785517,
            1858250798, 1693786723, 1871655963, 374455497, 430158267]


foo = lambda the_list: the_list[1:]and foo([i for i in the_list if i>the_list[0]]+[the_list[0]])or the_list[0]
print(foo(the_list))

Update: The issue is Iā€™m supposed to write code thatā€™ll give the highest number in a list. I need to use ā€˜forā€™ and ā€˜ifā€™ comparisons. I got the correct answer, but I didnā€™t do it the way I was supposed to.

I have the correct answer in the post, and Iā€™m just trying to understand the correct answer vs my answer up top.

Python - Ranged Maximum Element in String List - GeeksforGeeks hereā€™s the link to the original code I used. Itā€™s the last one on the list. The one when a loop code.

The answer is 42.

ā€¦


ā€¦
But more seriously, could you at least provide the problem ?

1 Like

I think that the problem is how to determine the max value from the list.

When you run the bottom script, the value is:

1871655963  # max value in the list

Iā€™m trying to understand the correct answer vs my answer. Where I went wrong and what the code in the correct answer means. Like lamda, Foo, etc.

I was supposed to get the code to pick the highest number in a list and use ā€˜forā€™ and ā€˜ifā€™ comparisons. I got the correct answer after messing with code from geekstogeeks but, I donā€™t think I was supposed to do what I did.

The ā€œcorrect solutionā€ is terrible, where did you get that?

Geekstogeeksā€¦but I did tweak it. The original code didnā€™t work. So what youā€™re seeing is my fiddling around with it to get it to print the highest number.

Please stop saying Geekstogeeks, that doesnā€™t exist. And please link to the page, not just the site.

Whoops, sorry hereā€™s the link Python - Ranged Maximum Element in String List - GeeksforGeeks

Itā€™s the one that uses loop code. I believe itā€™s the 3rd example.

You can use something simple like this:

the_list = [143266561, 1738152473, 312377936, 1027708881, 1495785517,
            1858250798, 1693786723, 1871655963, 374455497, 430158267]


def max_value(some_list):

    max_value_in_list = 0

    for num in some_list:
        if num > max_value_in_list:
            max_value_in_list = num

    return max_value_in_list

print('The max value in the list is: ', max_value(the_list))
1 Like

Thank you, this is much easier for me to understand.

Just an fyi ā€¦ there is also a built-in function that does the work for you. It is the max function as shown here:

print(max(the_list))
1 Like

Thanks, I wasnā€™t sure if ā€˜maxā€™ needed any other prerequisites for it to be used. I was really overthinking this.

It is a bit more complicated, as the data provided is list of str of list of int.
Basically, all you need to understand is this line :

res = max([max(idx.split(', ')[i - 1: j]) for idx in test_list])

If you trace through your solution, youā€™ll find that itā€™s iterating through the list of values but not using any of them.

Itā€™s basically a complicated way of doing:

max_val = 1871655963

Thereā€™s no lambda or anything like that solution on that page.