Question

In: Computer Science

In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...

In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ] , Three sum = 0 List 4: [8,1] , Three sum = 9 List 5: [1,3,5,7,4,2,9] , Three sum = 16

Solutions

Expert Solution

(for any doubts pls menion in the comments section, if you are pleased with the answer , please do give a like:)

Steps:

  1. Define a function threesum
  2. Define a functio main
  3. In three sum do , the following steps from 4 till 7
  4. Find the largest value and its position
  5. if length of the list is less than or equal to 3 then add all the elements in the list   
  6. if length of list is greater than 3 add, the max value, and the 2 values near to the max values .
  7. if list is empty , return sum as zero.
  8. print the result sum from main

Source Code(# comments are given)(screen shot of code is given after the code, refer it for indentation checking)

#function threesum with argument as list_1

def threeSum(list_1):

#max will store the threesums of all the list

#large will store the largest element in each list
max=[]
large=0

#n contains the length of list passed
n=len(list_1)

#checking conditions
if n<3:
sum=0
for i in list_1:
sum+=i
return sum
elif n<1:
sum=0
return sum
else:
for item in range(n-2):
pos=0
sum=0
if pos==0:
sum=list_1[item]
pos+=1
if pos==1:
sum+=list_1[item+1]
pos+=1
if pos==2:
sum+=list_1[item+2]
pos+=1
max.append(sum)
for l in max:
if l>large:
large=l
return large

#main function to call threeSum and print the sum

def main():
list1=[4,5,4,5]
list2=[7]
list3=[]
list4=[8,1]
list5=[1,3,5,7,4,2,9]
print(list1)
sum=threeSum(list1)
print("ThreeSum = "+str(sum))
print(list2)
sum=threeSum(list2)
print("ThreeSum = "+str(sum))
print(list3)
sum=threeSum(list3)
print("ThreeSum = "+str(sum))
print(list4)
sum=threeSum(list4)
print("ThreeSum = "+str(sum))
sum=threeSum(list5)
print(list5)
print("ThreeSum = "+str(sum))

main()

Screenshot of code

Screenshot of output


Related Solutions

This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
In python write a program that gets a list of integers from input, and outputs non-negative...
In python write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If the input is: 10 -7 4 39 -6 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space. Do not end with newline
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the numbers in values (i.e., the numbers raised to the third power). This version of the function may not use recursion.
Please use Python for both Concatenate List Elements Write a function called concat_list that accepts a...
Please use Python for both Concatenate List Elements Write a function called concat_list that accepts a list of strings as an argument and returns a string made up of all elements in the list concatenated together in order. For example, if the argument is ['one', 'two', 'three'], the return value would be 'onetwothree'. Remove List Duplicates Write a function called remove_duplicates that accepts a list and returns a list containing the same elements in the same order but with duplicates...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should...
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should output an array, b, that is computed as: b=3a+5. Write a MATLAB function called “fit_line” that accepts 2 input arguments: a column vector of x data and a column vector of y data. The nth element in the input arguments should correspond to the nth Cartesian data point i.e. (xn,yn). The function should compute and return 2 outputs: the slope, m, and the y...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and returns only the odd index entries. Do this by first setting the even entries to 0, and then removing the 0 entries by using a logical array. The first line of your code should read function p = ReturnOddEntries(p) For example, if you run in the command window p = ReturnOddEntries([1.2 7.1 8.4 -42 100.1 7 -2 4 6]), then you should get p...
Write a function in Python to compute the sample mean of a list of numbers. The...
Write a function in Python to compute the sample mean of a list of numbers. The return value should be the sample mean and the input value should be the list of numbers. Do not use a built-in function for the mean. Show an example of your function in use.    Thank You
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT