0

I failed this in a mock exam of python?

1. Write a program to calculate and return the sum of distances between the adjacent number in an array of positive integers. Note: You are expected to write code in the find total distance function only which receives the first parameter as the numbers of items in the array and the second parameter is the array itself. You are not required to take the input from the console. Example Finding the total distance between adjacent items of a list of 5 numbers. Input input 1: 5 input 2 : 10 11 7 12 14 Output #This is the inbuilt code and solution which i written and getting error for cls and input1&input2 which is already given in code inbuilt Code given in exam and solution i wrote: ------------ class UserMainCode(object): //inbuilt given def findTotalDistance(cls,input1, input2): #Write your code here total = 0 for i in range(n-1): total+= abs(numbers[i]-numbers[i+1]) return total n = int(input()) numbers= list(map(int, input().split())) print(findTotalDistance(n, numbers)) Solution2: This is same soluion of question but it is not giving right result as from the inbuilt code (cls,input1,input2) which is given applying in tjat this logic of solution it is giving wrong result?Please anyone help it out def findTotalDistance(n,numbers): total = 0 for i in range(n-1): total+= abs(numbers[i]-numbers[i+1]) return total n = int(input()) numbers = list(map(int, input().split())) print(findTotalDistance(n,numbers))

19th Jul 2021, 3:07 PM
praveen
praveen - avatar
4 Réponses
+ 2
Well, there are a lot of things wrong with this, but I'll only talk about the cls problem you have, the other is more of a mistake you can probably fix. That error you got with cls? That's the question maker's fault, if findTotalDistance(cls,input1, input2) was inbuilt . Why? because cls is the self reference for any instance of the class UserMainCode, which is typically defined within the magic method __init__(self, args), which is NOT defined within the class UserMainCode. In this case, they renamed self with cls, which is abbreviated "class", but the name doesn't really matter. There's two ways to fix it. First, define an __init__ method within the class with cls as the only parameter and pass as the body, or, make findTotalDistance a static method, ie remove cls from the method signature entirely. def __init__(cls): pass def findTotalDistance(cls,input1, input2): #class method #code here OR def findTotalDistance(input1, input2): #static #code DISCLAIMER: I have not tested this code, so I am unaware of any other bugs that might ensue from using this.
19th Jul 2021, 3:29 PM
BootInk
BootInk - avatar
0
class UserMainCode(object): def __init__(cls): def findTotalDistance(cls,input1, input2): total = 0 for i in range(n-1): total+= abs(numbers[i]-numbers[i+1]) return total n = int(input()) numbers= list(map(int, input().split())) print(findTotalDistance(n,numbers)) #Please try once this code and send me i am getting error findTotalDistance not found?
19th Jul 2021, 7:18 PM
praveen
praveen - avatar
0
You forgot to add "pass" to the body of the __init__ method. def __init__(cls): pass otherwise you have an incomplete method stub. Python can be picky about this.
19th Jul 2021, 7:55 PM
BootInk
BootInk - avatar
0
praveen How about this one: return sum(abs(arr[i]-arr[i-1]) for i in range(1, len(arr)))
20th Jul 2021, 5:54 AM
Calvin Thomas
Calvin Thomas - avatar