Home > Software design >  My code does not work on some cases. What is the problem?
My code does not work on some cases. What is the problem?

Time:11-02

I'm having a problem here. This is a program finding the maximum and minimum value in a custom list. Some cases work while other cases don't work so well.




#find the maximum and minimum number in an integer list
def Custom_List(MyList):
    for i in range(len(MyList)):
        if MyList[i] >= MyList[i-1]:
            myMax = MyList[i]
        if MyList[i] <= MyList[i-1]:
            myMin = MyList[i]

    return myMax,myMin

#inputing numbers to a list
MyList = []                  #first an empty list
length = int(input("List length: ))       #the length of a list
for i in range(length):
    num= int(input())       #inputing our value
    MyList.append(num)      #and inserting them into our list
print(Custom_List(MyList))

'''Example of erroneous cases
List length: 8
1645
12
-465
325
0
134
78
664
Output: (664, 78)'''

Could you tell me what is the problem here? I will be very much appreciated

I expect the code to work on every case, but the error seems to appear sooner than I expected

CodePudding user response:

try to start your loop from 1 so you can copare first and second element in the list at the first one instead of first one with the list index -1

   def Custom_List(MyList):
        for i in range(1,len(MyList)):
            if MyList[i] >= MyList[i-1]:
                myMax = MyList[i]
            if MyList[i] <= MyList[i-1]:
                myMin = MyList[i]
    
        return myMax,myMin

CodePudding user response:

we can do this task without using any indexes. we use 2 variables, that will finally hold the min / max values, and they get preset with one element from the list [0]. we run a for loop and compare the loop variable with the current value of the min / max.

def Custom_List(MyList):
    # preset min, max with a value from the list
    myMax = MyList[0]
    myMin = MyList[0]
    for num in MyList:
        if num > myMax:
            myMax = num
        if num < myMin:
            myMin = num

    return myMax,myMin

MyList = [1645, 12, -465, 325, 0, 134, 78, 664]
print(Custom_List(MyList)) 

result: (1645, -465)

CodePudding user response:

You forgot to end the string with quotes:

length = int(input("List length: ))       #the length of a list

Try this:

#find the maximum and minimum number in an integer list
def Custom_List(MyList):
    for i in range(len(MyList)):
        if MyList[i] >= MyList[i-1]:
            myMax = MyList[i]
        if MyList[i] <= MyList[i-1]:
            myMin = MyList[i]

    return myMax,myMin

#inputing numbers to a list
MyList = []                  #first an empty list
length = int(input("List length: "))       #the length of a list
for i in range(length):
    num= int(input())       #inputing our value
    MyList.append(num)      #and inserting them into our list
print(Custom_List(MyList))

'''Example of erroneous cases
List length: 8
1645
12
-465
325
0
134
78
664
Output: (664, 78)'''
  • Related