Question: Programming in Lua write the Lua module will contain functions dealing with tables, strings, and numbers. One of the functions will return an iterator. Another

Programming in Lua

write the Lua module will contain functions dealing with tables, strings, and numbers. One of the functions will return an iterator. Another will be a coroutine.

Write a Lua module pa2, contained in the file pa2.lua. Module pa2 should export the following four functions, and nothing else: filterArray, concatMax, collatz, substrings. Just write these functions

  • Function filterArray. This takes a function p and an array t. Function p can be assumed to be a one-parameter function that will accept any value in array t. filterArray returns an array containing every value v in t for which p(v) is truthy, in the same order as they appear in t. No other key-value pairs should appear.

    Example:

    function isBig(n) return n > 6 end arr = { 1, 7, 9, 3, 10, 6, 2, 7, 0 } arr2 = pa2.filterArray(isBig, arr) -- arr2 should be the array { 7, 9, 10, 7 }.
  • Function concatMax. This function takes a string and an integer. It returns a string, which is the concatenation of as many copies of the given string as possible, without the length exceeding the given integer. If the given integer is less than the length of the given string, then it returns the empty string.

    Examples:

    • pa2.concatMax("abc", 13) returns "abcabcabcabc". This is the concatenation of 4 copies of "abc". One more copy would result in length 15, which is greater than the given value 13.
    • pa2.concatMax("aardvark", 20) returns "aardvarkaardvark".
    • pa2.concatMax("aardvark", 10) returns "aardvark".
    • pa2.concatMax("aardvark", 5) returns "" (the empty string).
  • Function collatz. This takes an integer parameter k and returns an iterator that produces one or more integers; these are the entries in the Collatz sequence starting at k, as explained below.

    By returns an iterator I mean that function collatz should be usable as follows.

    for i in collatz(n) do ff(i) -- Do something with i end

    The Collatz function [L. Collatz 1937] is the following function c, defined on the positive integers.

    c(n)={3n+1,if n is odd;n/2,if n is even.

    The Collatz sequence starting at a positive integer k is

    k, c(k), c(c(k)), c(c(c(k))), , 1.

    That is, the sequence begins with k, each later entry is the result of applying function c to the entry preceding it, and the sequence stops when it reaches 1.

    Let us construct the Collatz sequence starting at 3.

    • 3 is odd, so c(3)=33+1=10.
    • 10 is even, so c(10)=10/2=5.
    • 5 is odd, so c(5)=35+1=16.
    • 16 is even, so c(16)=16/2=8.
    • 8 is even, so c(8)=8/2=4.
    • 4 is even, so c(4)=4/2=2.
    • 2 is even, so c(2)=2/2=1.
    • 1 has been reached; the sequence stops here.

    So the Collatz sequence starting at 3 is

    3,10,5,16,8,4,2,1.

    Therefore, this code

    for i in collatz(3) do io.write(i.." ") end io.write(" ")
    should produce the following output:
    3 10 5 16 8 4 2 1 

    If collatz(3) is replaced by collatz(2), then the output will be the following:

    2 1 
  • Coroutine substrings. This takes a single parameter s, which must be a string. It yields all substrings of s: first the unique length-zero substring, then all length-one substrings, and so on, ending with s itself.

    For example, if substrings("abca") is called as a coroutine, then it should yield the following, in order:

    "" "a" "b" "c" "a" "ab" "bc" "ca" "abc" "bca" "abca"

Note

  • You do not need to do any type checking. You may assume that the test program will pass reasonable values to your code. For example, the parameter to collatz will always be a positive integer.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!