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
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...
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.
Design a Python script that accepts as input a user-provided list and transforms it into a...
Design a Python script that accepts as input a user-provided list and transforms it into a different list in preparation for data analysis, the transformed list replaces each numeric element in the original list with its base-10 order of magnitude and replaces string elements with blanks. Example: This script accepts as input a user-provided list expected to contain non-zero numbers and strings. It then prints a transformed list replacing numbers with their order of magnitude, and strings as blanks. Type...
In python of Jupiter notebook Write a python function called trng that takes three numbers x,...
In python of Jupiter notebook Write a python function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangleotherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
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