Write the function array_dup

Assignment Help Programming Languages
Reference no: EM131182168

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]

Reference no: EM131182168

Questions Cloud

Determine the conditions on the coefficients : Using the rref command on your calculator, determine the conditions on the coefficients in order for there to be a unique solution.
Improve the survey if it were to be repeated : a. Using complete sentences, list three things wrong with the way the survey was conducted. b. Using complete sentences, list three ways that you would improve the survey if it were to be repeated.
Discuss the differences between legitimate and reward : Discuss the differences between legitimate, reward, coercive, and referent power. Give an example of each or the appropriate uses of these expert powers
Markov analysis and forecasting : 1. According to Markov Analysis and Forecasting describe the internal labor market of the company in terms of job stability, promotion paths and rates, transfer paths and rates, demotion paths and rates, and turnover rates?
Write the function array_dup : Write the function array_dup(n, obj) that returns an array containing n copies of obj - Write a function count_occurrences(a, obj) that counts the number of items in array a that are equal to obj.
Did any of the other goals figure in this decision : Which of the goals listed in Table 1.1 was most important to you (and perhaps to your family or community, if they were involved) in making this decision? Did any of the other goals figure in this decision? If you were to write up a list of your o..
Identify and describe a business crisis : Identify and describe a business crisis situation and the main leaders involved. It could be one that you have experienced or have read about. Be sure to include a discussion of ethical implications
Dosages resembles a normal distribution reasonably closely : In a sample of 100 syringes, the hospital's quality control officer finds the average dosage to be 0.5mL. The standard deviation of the dosage is 0.02 mL and the distribution of the dosages resembles a normal distribution reasonably closely.
How will you sell the product in that country : What is the product that you plan to sell? -  What foreign country do you plan to target?  -  How will you sell the product in that country?

Reviews

Write a Review

Programming Languages Questions & Answers

  Program to compute total price of items before tax

Program is to compute total price of items before tax and then tax payable on those items. Tax payable is computed by applying tax rate percentage to total price.

  Write a program that calculates how much an employee earn

Write a program that calculates how much an employee would earn over a period of time (in months), if, every month, the employee' pay-per-hour rate is one dollar

  Modify the program to enter any amount of numbers

Modify the program in Exercise 2a so that the user can enter any amount of numbers up to 12 until a sentinel value is entered.

  Program to calculate sum of squares using for loop

Given int variables k and total that have already been declared, use for loop to calculate sum of squares of first 50 counting numbers, and store this value in total.

  Abebooks is an online bookseller it carries a

abebooks is an online bookseller. it carries a product-specific amount of safety stock for each of its products. the

  Write a program that will allow a user to enter in a

write a program that will allow a user to enter in a sentence of up to 100 characters. then take that sentence and

  Program to calculate monthly gross pay for super supermarket

Owners of Super Supermarket would like to have program which calculates monthly gross pay of their employees. Input for this program is employee ID number, hourly rate of pay.

  Explaining law of diminishing returns

As computer word size gets larger and larger, there is a law of diminishing returns; speed of execution of application programs doesn't increase and may, in fact, decrease.

  Write a paper discussing threads vs processes

Write a paper discussing Threads vs Processes. What is a Thread? What is a Process? How are they different? How are they the same? What types of problems lend themselves to Threads?

  Program development cycle

As the text explains, there are seven (7) steps in the Program Development Cycle. Identify which of the steps that you believe takes the most effort. Provide a rationale to support your response

  Write a script that accepts a part of a process name

Using what we know about shell scripts, write a script that accepts a part of a process name as the first argument (like pgrep), which prints out the PID of all matching processes in blue, which is color code 4. Then print out the number of proces..

  Create a program for telephone lookups

Create a program for telephone lookups. Your tasks are to allow the user find a number that is associated with a name and if given a number, find the name associated with it (normal and reverse lookups).

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd