Question

6. Write Ruby recognizer methods limited? and sorted? that expand the Ruby class Array. The expression array.limited? amin,amax) should return true if amin Sali] S amax for all values of i. The expression array.sorted? should return 0 if the array is not sorted 1 if a[0] S a[1]Sa12] S... (increasing sequence) if a a[1] a[2] (decreasing sequence) Show examples of the use of this method.

In Ruby, also please take a screenshot of your output and how to run the code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution class Array #checks amin <-: n and n <-amax for all values in the array #n for number def limited? (amin, amax) self#display the output for array is increasing sequence array[1,2, 3,4,5] puts array: # { array} -array. limited? (0,10) : #{arCopyable Code:

class Array

    #Checks amin <= n and n <= amax for all values in the array

    #n for number

     def limited?(amin,amax)

          self.each do |n|

              return false unless amin <= n && n <= amax

          end

          return true

     end

#this is for checks if given array is sorted in increasing   or decreasing sequence.

     #'n' for number and 'i for index

     def sorted?

         self.each_with_index do |n, i|

             break unless n <= self[i+1] if i != self.length - 1

             return "+1" if i == self.length - 1

         end

        

         self.each_with_index do |n, i|

             break unless n >= self[i+1] if i != self.length - 1

             return "-1" if i == self.length - 1

         end

        

         return 0

     end

end

#display the output for array is increasing sequence

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

puts "array: #{array} - array.limited?(0,10): #{array.limited?(0,10)}, array.sorted?: #{array.sorted?}"

#display the output for array is decreasing sequence

array = [10,7,5,3,1]

puts "array: #{array} - array.limited?(5,11): #{array.limited?(5,11)}, array.sorted?: #{array.sorted?}"

#display the output for array is not sorted

array = [8, 3, 4 ,10, 5]

puts "array: #{array} - array.limited?(0,5): #{array.limited?(0,15)}, array.sorted?: #{array.sorted?}"

Add a comment
Know the answer?
Add Answer to:
In Ruby, also please take a screenshot of your output and how to run the code....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

  • PLEASE DO THIS IN RUBY # Write a method bigger_filter that accepts an array of numbers...

    PLEASE DO THIS IN RUBY # Write a method bigger_filter that accepts an array of numbers and a target number. The method should return a new array containing the elements that are greater than the given target. def bigger_filter(arr, tar) end print bigger_filter([7,3,2,8,12], 5) # => [7, 8, 12] #puts print bigger_filter([1,2,3], 100) # => [] #puts print bigger_filter([10,9,20,3], 9) # => [10, 20] MY ATTEMPT def bigger_filter(arr, tar)    new_arr = []    i = 0    while i...

  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

  • for the following code I need the source code and output screenshot (includes date/time) in a...

    for the following code I need the source code and output screenshot (includes date/time) in a PDF format. I keep getting an error for the #include "dayType.hpp" dayType.cpp #include"dayType.hpp" string dayType::weekday[7] = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" }; //set the day void dayType::setDay(string day){ cout << "Set day to: " ; cin >> dayType::day; for(int i=0; i<7; i++) { if(dayType::weekday[i]==dayType::day) { dayType::markDay = i; } } } //print the day void dayType::printDay() { cout << "Day = "...

  • java : here is a code that I have to implement. counting the occuence in an...

    java : here is a code that I have to implement. counting the occuence in an array /** * _Part 3: Implement this method._ * * Counts the items in the ordered array list that are equal to the item at * the specified index. Be sure to take advantage of the fact that the list * is sorted here. You should not have to run through the entire list to * make this count. * * @param index an...

  • Please help with a source code for C++ and also need screenshot of output: Step 1:...

    Please help with a source code for C++ and also need screenshot of output: Step 1: Create a Glasses class using a separate header file and implementation file. Add the following attributes. Color (string data type) Prescription (float data type) Create a default constructor that sets default attributes. Color should be set to unknown because it is not given. Prescription should be set to 0.0 because it is not given. Create a parameterized constructor that sets the attributes to the...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • CODE IN C++ PLEASE INCLUDE A SCREENSHOT OF YOUR CODE Write a program that outputs the...

    CODE IN C++ PLEASE INCLUDE A SCREENSHOT OF YOUR CODE Write a program that outputs the shortest distance from a given node to every other node in the graph. Do not change anything in the supplied code below that will be the Ch20_Ex3.cpp except to add documentation and your name. Please use the file names listed below since your file will have the following components: Note: Here are the files that I need graphType.h linkedList.h linkedQueue.h queueADT.h unorderedLinkedList.h weightedGraph.h #include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT