Question
Write code that prompts for and reads a list of integers. It splits the list in half and outputs the half with the larger sum.
Write code that prompts for and reads a list of integers. It splits the list in half and outputs the half with the larger sum. If the list is of odd length, it includes one more number in the second half then in the first half. The following are example runs. Enter a list of numbers: [1, 3, 5, 6, 4, 2] The larger half is [6, 4, 2] Enter a list of numbers: [6, 5, 3, 4, 1, 2] The larger half is [6, 5, 3] Enter a list of numbers: [2, 3, 2, 4, 2] The larger half is [2, 4, 2] Regarding the implementation, note that function sum(), passed a list of numbers, returns the sum of the number. Also note that the eval() function evaluates a string containing a list of numbers into exactly that string of numbers. For example, eval('[1, 2, 3]') is the list of numbers [1, 2, 3]. This means that when we execute code like lst = eval(input(Enter a list of numbers)) and the user enters, say [1, 2, 3] lst is set to the list of numbers [1, 2, 3]. To split the list of numbers in the middle, recall that, where lst is a list, len(lst) is its length. The operator // (two slashes) is integer division: where x and y are integers, x // y is the integer that is the truncated quotient of x by y. For example, 9 // 2 is 4. So, use len() and // in finding where to split the list.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started