#๐Ÿ”’ MAX() algorithm bugs

87 messages ยท Page 1 of 1 (latest)

sweet fox
#

im tring to build max function i got bugs but i dont know where they are:

def max(*items):
# make sure it not empty
if items == ():
return "EmptyTuple"

# set the first item as highest one
HIGH = items[0]

for i in items:
    # Check if the current item is a list
    if isinstance(i, list):
        i = max(tuple(i))
    # Compare the current item with the current highest item
    if i > HIGH:
        HIGH = i

return HIGH

result = max([5, 11, 6], 1, 5)
print("the highest item is {}".format(result))

hasty currentBOT
#

@sweet fox

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

full heath
#

the first thing i would ask is why you want to deal with taking a list and individual integers

fresh tree
#

result = max([5, 11, 6], 1, 5)

here is the bug

sinful zodiac
#

Okay, you start by saying that your initial maximum is the first value of the list

#

That works only of the first value of the list is an integer

#

If it is a list, you set your initial HIGH with the value of the list

#

But you can't compare a list and an integers

sweet fox
sweet fox
sweet fox
sinful zodiac
#

Yep, so you need to avoid setting the first highest value to the first item, otherwise you have a problem

sweet fox
#

what i need to do insteade than

sinful zodiac
#

I'll let you think about it for a few minutes

sweet fox
#

you said i shouldn't keep asigning the first iteme to HIGH var

sinful zodiac
#

not the first item, indeed

hasty currentBOT
#

Hey @sweet fox!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
sweet fox
#
def max(*items):
    # make sure it not empty
    if items == ():
        return "EmptyTuple"

    # set the first item as highest one
    for l in items:
        # Check if the current item is a list
        if isinstance(l, list):
            l = tuple(l)
            l = max(l)

    for i in items:
        # Compare the current item with the current highest item
        if i > HIGH:
            HIGH = i
    
    return HIGH

result = max([5, 11, 6], 1, 5)
print("the highest item is {}".format(result))

sinful zodiac
#

so now you don't define HIGH before using it

#

which will lead to a problem

sweet fox
#

yeah sorry i forget it

ionic moon
#

Builtin max takes one argument or two arguments

#

If there is one argument, it iterates it. If there are two arguments, it compares them

rustic birch
#

When you call max([5, 11, 6], 1, 5), what do you think the result should be, and why should that be the result?

ionic moon
#

!e
max(1, 2, 3)

hasty currentBOT
ionic moon
#

Wait

sinful zodiac
#

you need to print

ionic moon
#

!e
print(max(1, 2, 3, 4))

hasty currentBOT
ionic moon
#

anyways apparently I was wrong

#

If one argument is passed then it iterates it, otherwise it finds the max of the arguments passed, comparing directly

#

So it looks that OP wants something different than builtin max

sinful zodiac
#

@sweet fox did you solve your problem ?

sweet fox
#

nooo im tring different way my brain almost to explose

#

def maax(*items):
# make sure it not empty
if items == ():
return "EmptyTuple"
# set the first item as highest one
# print(items)

# for my_tuple in items:
#     if isinstance(my_tuple, list):
#         my_tuple = tuple(my_tuple)

# print(items)

# for l in items:
#     # find the max of lists objects
#     if isinstance(l, tuple):
#         l = max(l)
# print(items)

# convert list into tuples and than make the neasted tuples one tuple
individual = ()
converted = ()
for i in items:
    if isinstance(i, list):
        i = tuple(i)
        converted = converted + (i,)
    else:
        individual = individual + (i,)
individual = maax(individual)
converted = maax(converted)

print(individual)
print(converted)

print(items)
# convert the singles items to one individual tuple
HIGH = items[0]


for i in items:
    # Compare the current item with the current highest item
    if i > HIGH:
        HIGH = i

return HIGH

result = maax([5, 11, 6], 1, 5)
print("the highest item is {}".format(result))

hasty currentBOT
#

Hey @sweet fox!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
charred cipher
#
    # set the first item as highest one
    HIGH = items[0]      # you're setting this to the index 0 to [5, 11, 6]

    for i in items:      # you iterate over the *items
        # Check if the current item is a list
        if isinstance(i, list):
            i = max(tuple(i))      # you set this to the max of i
        # Compare the current item with the current highest item
        if i > HIGH:     # here you try compare an int (i) with HIGH (list), so error
            HIGH = i

    return HIGH

the problem is here

#

you should add a check so that

if isinstance(items[0], list):
    HIGH = max(items[0])
else:
    HIGH = items[0]
sinful zodiac
#

it's a little spoonfeeding...

#

anyway, yes, we still have the problem of initializing the HIGH variable

#

it cannot be a list

sweet fox
#

i think im too close

charred cipher
#

or you can initialize

HIGH = None

for i in items:
    i = max(tuple(i))
    if HIGH is None or i > HIGH:
        HIGH = i

return HIGH
#

with this you can init HIGH to nothing, if its nothing, then assign it, but if its not None, then check if i > HIGH and assign

sinful zodiac
#

it's a little lot spoonfeeding...

sweet fox
#

hey guys

#

im working now only on integers values

charred cipher
#

OR if you want to skip all of that

return max([max(tuple(i)) for i in items)
sinful zodiac
#

it is it's a little lot way too much spoonfeeding...

#

@charred cipher can you let OP think ?

charred cipher
#

i didnt want his brain to explode

sinful zodiac
charred cipher
#

im working now only on integers values he can figure out the strings himself :D

sweet fox
#

soo like i said only integers values and neasted list my plan is like the following:

0 - check if the items tuple is not null otherwise return "EmptyTuple"

1 - i need to iterate over my tuple and any item is a lise convert it to a tuple than found the max of it

2 - when we found all max values of all item (lists, tuples) we still have other individuals we will found max of all becase we will have only one tuple our final items

charred cipher
#

yes we understand that, your current code does that, but the way you are initializing your HIGH, which in this case, initialises it to the list instead of the max of the list, meaning when you try compare your int (i) vs your HIGH, it returns an error

sweet fox
#

i got it but i dound another huge problem : what if after turning the list to a tuple and the first item of the tuple is another list

charred cipher
#

so you're essentially trying to get the max of items (that may contain a mix of ints and lists)

sweet fox
#

hhhhh

#

yes i because i did it with integers values only and it works but now i wanna level it up

#

i thaught at first it will be soo easy

sinful zodiac
#

(also you know you can find the max of a list without converting it to a tuple)

charred cipher
#

i think he does that for regular single values at the moment, which adds an additional step

charred cipher
#

oh yea

sweet fox
#

i stuck

charred cipher
#

what are you stuck on

sweet fox
#

no im thinking about finding a way to convert all the sub lists to tuples

sinful zodiac
#

do you need to ?

sweet fox
#

convert all lists into tuples

sinful zodiac
#

do you need to do that ?

sweet fox
#

yeah if i do that i can get the max of each tuple and you know the next

sinful zodiac
#

you can get the max of a list the same way you get the max of a tuple

#

!e

my_tuple = (1, 2, 3)
my_list = [1, 2, 3]
print(type(my_tuple))
print(type(my_list))
for x in my_tuple:
  print(x)

for x in my_list:
  print(x)
hasty currentBOT
sinful zodiac
#

look

#

you can iterate over them

#

whether they tuples or lists doesn't matter

sweet fox
#

okay im gonna try

sinful zodiac
#

also, do you need to take car of sublists ?

You are already using recursivity quite well, look:

#

!e

def my_function(items):
 for item in items:
  print("currently handeling", item)
  if isinstance(item, list):
    my_function(item)
  else:
    print("currently handeling the single item:", item)


my_function([1])
my_function([[2, 3]])
my_function([[[2, 3], 4]])
hasty currentBOT
# sinful zodiac !e ```py def my_function(items): for item in items: print("currently handelin...

:white_check_mark: Your 3.14 eval job has completed with return code 0.

001 | currently handeling 1
002 | currently handeling the single item: 1
003 | currently handeling [2, 3]
004 | currently handeling 2
005 | currently handeling the single item: 2
006 | currently handeling 3
007 | currently handeling the single item: 3
008 | currently handeling [[2, 3], 4]
009 | currently handeling [2, 3]
010 | currently handeling 2
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/PFMBAEZTWSLVDFAI3P7CMKRWZU

sinful zodiac
#

alright, take a look

hasty currentBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.