Can anyone code this in Python 3 Write a function sumtriax t

Can anyone code this in Python 3?

Write a function, sum_tri(a,x), that takes a list , a, as input and returns True if there exists indices i, j and k (where i and j and k are not necessarily distinct) such that a[i]+a[j]+a[k]=x. Otherwise it returns False. For example, if a=[1, 5, 8, 2, 6, 55, 90] and x=103, then sum_tri(a, x) would return True since a[1]+a[2]+a[6]=5+8+90=103. If a=[-1, 1, 5, 8, 2, 6] and x=-3, sum_tri(a, x) would return True since a[0]+a[0]+a[0]=-1+ -1 + -1 = -3. If a=[-10,2] and x=-18, sum_tri(a, x) would return True since a[0]+a[0]+a[1]=-10+-10+2=18. If a=[1, 1, 5, 8, 2, 6] and x=1000 would return False, since there are not indices i, j and k such that a[i]+a[j]+a[k]=1000.

Solution

def sum_trai(a,x):
   for i in range(len(a)):
   for j in range(len(a)):
   for k in range(len(a)):
   if(a[i]+a[j]+a[k]==x):
   return true
   return false

Can anyone code this in Python 3? Write a function, sum_tri(a,x), that takes a list , a, as input and returns True if there exists indices i, j and k (where i a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site