Write the functionnbsparraydupn objnbspthat returns an


Problem 1:

Write the function array_dup(n, obj) that returns an array containing n copies of obj.

>> array_dup(5, "hi")

=> ["hi", "hi", "hi", "hi", "hi"]

Ensure the array that gets returned contains independent copies of obj:

>> c = array_dup(5, [1,2])

=> [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]

>> c[0].delete(1)

=> 1

>> c

=> [[2], [1, 2], [1, 2], [1, 2], [1, 2]]

Problem 2:

Write a function count_occurrences(a, obj) that counts the number of items in array a that are equal to obj.

>> a

=> [1, 2, 1, 3, 1, 4, 2, 5]

>> count_occurrences(a, 1)

=> 3

Problem 3:

Write a function insert(x, a) that takes an integer x and an array aof integers in nondecreasing order and returns a new sorted array that includes x and the integers of a. Your function should not modify a.

>> a = [2, 4, 5, 9, 12]

=> [2, 4, 5, 9, 12]

>> insert(5, a)

=> [2, 4, 5, 5, 9, 12]

>> insert(10, a)

=> [2, 4, 5, 9, 10, 12] >> insert(20, a)

=> [2, 4, 5, 9, 12, 20]

>> a

=> [2, 4, 5, 9, 12]

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Write the functionnbsparraydupn objnbspthat returns an
Reference No:- TGS01564405

Expected delivery within 24 Hours