DEV Community

Discussion on: Day-19 Third Maximum Number

Collapse
 
alex_twittelive profile image
Alex • Edited

Thanks for your adivces ! It's helpful!
My new version, is it better?


def thirdMax(lister):
    result = None
    dicolist = set(lister)
    new_list = list(dicolist)
    if len(new_list) > 3:
        for i in range(0,2):
            new_list.remove(max(new_list))
        result = max(new_list)
    elif len(new_list)==3:
        result = min(new_list)
    else:
        result = max(new_list)
    del dicolist
    del new_list
    return result

Thread Thread
 
mridubhatnagar profile image
Mridu Bhatnagar

Cool! IMO still there is no need to create a new_list. Can you test the below code for some inputs? Should work.

def thirdMax(lister):
    lister = list(set(lister))
    if len(lister) > 3:
        for i in range(0,2):
            lister.remove(max(lister))
        result = max(lister)
    elif len(lister)==3:
        result = min(lister)
    else:
        result = max(lister)
    return result
Thread Thread
 
alex_twittelive profile image
Alex

Great !!! Thanks for your share...
I keep this. Best regards