Create a ruby script that defines a function

Assignment Help C/C++ Programming
Reference no: EM131295386

Programming Assignment: Getting your feet wet with Ruby

Overview

The key to working with Ruby is understanding the underlying protocol for the different data types. You will experience this in this assignment by implementing a set of functions that add additional functions for the array data type in Ruby. Note that you will need to write additional helper functions for each of these problems and that you should take advantage of predefined methods instead of trying to implement these functions by hand.

Problem 1

Create a Ruby script that defines a function occurrences(list,key) that takes an array list and object key and returns the number of elements in list that are equal to key. Include sufficient test code to test your program (either by hand or using Ruby's unit test framework).

Problem 2

Create a Ruby script that defines a function numSmaller(list,item) that takes an array list of size 1 or larger and integer item as parameters. It should return the number of elements in the list that are strictly less than item. Again, include sufficient test code for your solution.

Problem 3

Crate a Ruby script that defines a funciton secondSmallest(list) that takes an array and returns the position of the second smallest element in the list. In case the smallest entry occurs more than once, the second occurrance of the item would be considered the second smallest entry. For example, in the list [1,1,1,2,3] , the position returned would be 1.

Problem 4

Implement Selection Sort for Ruby arrays. Make certain to include both mutable and immutable versions of the sorting function (i.e. if you are using ssort for your function name, then you should implement a ssort!(list) and sort(list) versions of the functions.

Problem 5

A list can be recursively defined as: the empty list () a pair (value rest), where value is some value (which may be another list), and a list rest that is the rest of the list.

Define a class List in Ruby that implements this definition. Your class needs to provide three methods: (1) insert which inserts a value into a list, (2) delete which removes a value from the list, and (3) traverse which traverses and prints the values in a list.

Notes:

Use a Ruby arrays as your backing store for the list. Note that value can be any valid type in Ruby, including being another List. Remember that recursion is your friend.

Verified Expert

In this assignment ruby scripts are written for 5 problems. In problem 1, using ruby script the number of occurrences of the number in the list is written. In problem 2 the number of items that are less than the given value is returned. In problem 3 second smallest number in the list is returned. In problem 4 Selection sort program is implemented with both mutable and immutable version. In problem 5 various list operation such as insertion, deletion and traversal on the list is implemented.

Reference no: EM131295386

Questions Cloud

Identify the forced and natural components in the output : The circuit in Figure is in the zero state when the input vS (t) = 150u (t) is applied. If C = 0.022 μF and R = 82 kΩ, find vO(t) for t ≥ 0.-  Identify the forced and natural components in the output.
How statistics be misleading to a decision-maker : Why has risk modeling become pervasive in the security management professions? Why do you believe that MSRAM has become a favored Risk Management tool? Just because it was first?
Calculate the time value of money problems : Calculate the following time value of money problems. What is the future value of 24 periodic payments of $4,620 each made at the beginning of each period and compounded at 8% per period? What would you pay for a $200,000 face value bond that matures..
Employment relationship from union member rights : Describe the benefits have nonunion employees have gained in their employment relationship from union member rights?
Create a ruby script that defines a function : Create a Ruby script that defines a function occurrences(list,key) that takes an array list and object key and returns the number of elements in list that are equal to key - Create a Ruby script that defines a function numSmaller(list,item) that t..
Find the devices capacitance : The device undergoes a constant current discharge of iD = 3 mA.- After 1 hour, the voltage remaining on the capacitor is 3 V. Find the device's capacitance.
Female assistant manager : Review "Clippings" on pages 607-608 of your textbook about the McDonald's strip search incident. Imagine a scenario that is not quite as egregious as the one referenced. Instead, imagine that a female assistant manager performed the same cavity se..
Explain how the attitude-rationalization condition : Research the Bernie Madoff ponzi scheme case. Explain how the “attitude/rationalization” condition of the fraud triangle is incorporated into the scheme. What do you think are some of the challenges currently facing the public accounting profession?
Write a summary of a business or company : Write a summary of a business or company i.e. related to the Chase Bank.

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Economy of kenya has a budget deficit

The economy of Kenya has a budget deficit of KSH 500B. This deficit is likely to be funded through domestic borrowing and taxation. Using an appropriate model, explain the macroeconomic implications of such a move.

  What is y after the given switch statement is executed

What is y after the following switch statement is executed? What is the value in count after the following loop is executed? What is the printout of the following code?

  Locate an article about object modeling

Search the Web for online "trade magazines" related to computers and technology. Locate an article about object modeling. To be considered current, the article must have been published within the last 90 days. Anything older is considered out-date..

  How to implement member functions in c++

I am having a dificult time understanding Classes in C++ and how to implement member functions. Given the attached code I need to:

  Name of the employee with a given ssn

We are about to run a query on this Emp table to find the name of the employee with a given ssn, say 1000; i.e., in SQL, "select name from Emp where ssn=1000". In a worst-case scenario, how long this operation will take? Express your answer in bot..

  Task 1 fill out surveyplease take the 122223 survey at

task 1 fill out surveyplease take the 122223 survey at survey.osble.orgindex.php?sid97282. please treat the question

  Write a program with two functions- main and swap

Write a program with two functions: main and swap. The main function will read two integer numbers.

  Program to calculate word ladders where single-character

Write down a program in C++ to calculate word ladders where single-character substitutions have the cost of 1, and single-character additions or deletions.

  What are abstract classes

What are abstract classes

  Describes in detail how you would reform the current law

Assignment topic -TABL5541 critically analyses the statement that "[o]ur current insolvency laws put too much focus on penalising and stigmatising the failures" and describes in detail how you would reform the current law and why.

  Create a funcion called max

Create a funcion called max().

  Write the class declaration for a class named complex

Write the class declaration for a class named Complex using the class diagram

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