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

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...
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...
All functions are written in python Write a function cube_evens_lc(values) that takes as input a list...
All functions are written in python Write a function cube_evens_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 even numbers in values (i.e., the even numbers raised to the third power). For example: >>> cube_evens_lc([2, 5, 6, 4, 1]) result: [8, 216, 64] This version of the function may not use recursion. Write a function cube_evens_rec(values) that takes as input a...
PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
Write a python function to fulfill the requirements. The function accepts a string, a current state,...
Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.             ### Finite State Machine Simulator in Python ### Provide s1 and s2 that are both accepted, but s1 != s2. s1 = "bdf" s2 = "bdgbdf" edges = {(1,'a') : 2,                (1,'b') : 3,       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT