0
What is error in it??
class Plan: r = [] re = [] def __init__(self,arr): self.arr=arr def func(self): for j in range(len(self.arr)): for k in range(j,len(self.arr)): Plan.r.append(sum(self.arr[j:k+1])) for p in Plan.r: if p[::-1]==p: Plan.re.append(p) return Plan.re ab = Plan(list(map(int,input("Enter the values with the spaces:").split(" ")))) print(ab.func()) In this i need to print the minimum sum of two numbers in a arr
8 Réponses
+ 10
Ravi King ,
you mentioned that you are going to find the *minimum sum of 2 numbers in an array*.
so this means for me that we have to find the *two smallest numbers of the array* and build the sum of them. i am just wondering if you are not allowed to use built-in functions like sorted() to do this task.
by sorting the array (list) and using a slice for the *first 2 elements*, we can get less lines of code to accomplish this task.
using this list: [3 7 -8 -12 9], *- 20* would be the minimum sum.
or did i get you wrong?
+ 9
Ravi King ,
so you could do :
▪︎find min element in list and store it in var
▪︎remove the min element from the list
▪︎find again min element in the list (which is the second smallest number of the original list) and store it in var2
▪︎sum var1 and var2
+ 6
Ravi King ,
are you allowed to use min() function?
+ 5
The second for loop is using r in a way that implies it should be an array of iterables. But what I gather from the code leading up to this point, r is an array of single values. Ravi King can you explain this?
for p in Plan.r:
if p[::-1]==p:
Plan.re.append(p)
+ 3
Please give more info. We don't know what the program is meant for, what you're trying to do with it, what the input may be, what the expected output for that input is, what r and re are there for, and please copy and paste code in the playground so we can run it and help faster.
+ 1
Thanks
0
In this question we are not allowed to use the sort() function
0
Yes