Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Solution Review: Sum of Lists

Sumit Rawal answered on May 23, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • python sum comprehension
  • sum nodes in linkedlist
  • how to calculate the sum of a list in python
  • Target Can Be Sum Of List Elements?
  • how to sum all the numbers in a list in python
  • Summing Elements of Two Lists
  • Find the total of a List in Python Without sum() and with for loop in python
  • how to sum all the values in a list in python
  • Sum of list of numbers
  • sum of a list prolog
  • Write a prolog program to find the sum of elements in given list.
  • accumulate sum of elements in list
  • Write a prolog program to find the sum of elements in given list.
  • write a prolog program to find the sum of elements in given list.
  • sum of item in list python using list comprehension
  • Exercise: Sum Two Linked Lists
  • Solution Review: Appending an Element to a List
  • Challenge: Sum of Lists
  • how to search through a list for two numbers that sum to make an external input in python
  • Calculate the Sum of List Elements
  • how to do the sum of list in python
  • how to add all values in a list python without using sum function

  • Solution Review: Sum of Lists

    0

    In the following lesson, we will go over the solution of the challenge: Sum of Lists.

    We'll cover the following

    Task

    Solution

    Alternate Solution

    Task

    In this challenge, you had to create a recursive function sum which sums together all the integers in a List.

    Solution

    A skeleton of the function was already provided for you. Let’s look it over.

    def sum(numberList: List[Int]): Int = numberList match {

    }

    sum takes a single parameter of type List[Int] and returns a value of type Int.

    The function body of sum consists of a match expression. You needed to write the cases required for sum to execute correctly. The first case would be our base case which is if the List is empty. In this case, sum would return 0.

    case Nil => 0

    The second case is if the List is not empty. In this case, we separate the first element of the List from the rest of the elements using the built-in :: operator. Let’s see how it will work

    case x :: tail

    Here the x will contain the first element of the list and the tail will have all the element of the list except the first element (x). Then we will recursively pass the tail (a list) to the sum() method.

    case x :: tail => sum(tail)

    We will continue this process until the base case is reached i.e., the tail list becomes empty.

    You can find the complete solution below:

    You were required to write the code from line 1 to line 4.

    Popularity 1/10 Helpfulness 1/10 Language scala
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on May 23 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.