CS 116: Introduction to Computer Science 2

CS 116 Tutorial 5: Abstract List Function and Accumulative Recursion


Reminders: Assignment 05 is due on Wed, Feb 26th at 10am


  1. Write an accumulatively recursive function record_digit(n) that returns a list of integers of length 10, with each index from 0 to 9 represents a corresponding digit's total appearances in the integer n.You cannot use L.count().

    For example,
    record_digit(19990514)=>[1,2,0,0,1,1,0,0,0,3]


  2. Write an accumulatively recursive function count_max that consumes a nonempty list of numbers alon and returns the number of times the largest number in alon appears.

    For example,
    count_max([1, 3, 5, 4, 2, 3, 3, 3, 5]) => 2
    since the largest element of the list, 5 appears twice. Your function should pass through the list only once.


  3. Write a function smaller consumes a non-empty string called s containing only numeric characters by repeatedly removing the larger of the first and last characters in s . if the first and the last number are the same, remove the last one.

    For example, starting from "5284, compare "5" and "4", and recurse on "284", which will compare "2" and "4", and recurse on "28". Comparing "2" and "8", leads to recursing on "2", which is the answer (since it is a string of length 1).

    Note: min is not allowed to use in this question.


    For example:

    smaller("4325") => "2"
    smaller("1") => "1"
    smaller("2325") => "2"


  4. Given a list L of positive integers, what is the skip-value of the list?

    • If L is empty,the skip value is 0.
    • If L is not empty:
      • Add one to the current skip value
      • Move ahead in the list by n (where n is the first element in the list) and repeat the process

    Write a function skip_value to calculate the skip value of the list L.

    For example:
    skip_value([]) => 0
    skip_value([1,1,1]) => 3
    skip_value([2,100,1]) => 2
    

  5. Develop an accumulatively recursive function list_to_num that consumes a nonempty list, digits, of integers between 0 and 9, and returns the number corresponding to digits.

    For example,
    list_to_num([9, 0, 8]) => 908
    list_to_num([8, 6]) => 86.
    list_to_num([0, 6, 0]) => 60.

Valid XHTML 1.0 Strict

Last modified on Wednesday, 12 February 2020, at 14:09.