Question

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 < tar
       old_arr = arr[i]
       if old_arr > tar
           new_arr << old_arr
       else old_arr < tar
       end

       i += 1
   end

   return new_arr
end

It only works with the first line of code but not the other 2 because it gives me an error regarding Syntax.

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

Hi,

Please find the answer below:

The bug is the while loop: Your condition should be i less than array length.

while i < arr.length

and NOT while i < tar

Also, i have removed unnecessary usage of the variable in the loop.

Steps

  • Just simplified your logic using Array.new()
  • Iterate over the input array.
  • If the condition is met push to new array.
  • return the new array

Fixed Code:

def bigger_filter(arr, tar)
   new_arr = []

   i = 0
   while i < arr.length
       if arr[i] > tar
           new_arr << arr[i]
       end

       i += 1
   end

   return new_arr
end

print bigger_filter([10,9,20,3], 9) # => [10, 20]

Screenshot:

-----------------------------------------------

Like the solution if you find it useful.

Hope this helps.

Add a comment
Know the answer?
Add Answer to:
PLEASE DO THIS IN RUBY # Write a method bigger_filter that accepts an array of numbers...
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
  • Assume that arr[] is an array of 5 values. Write code within the FOR loop below...

    Assume that arr[] is an array of 5 values. Write code within the FOR loop below so that it finds the min value in the array. You MUST use the x pointer within the FOR loop. It is the only pointer you can use. You are not allowed to made additional function calls or modify anything else within the function. Do not modify the FOR loop conditions or anything outside of the FOR loop. You must use complete C++ code...

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • Given an array of integer, please complete a function multiply(). The function accepts the first memory...

    Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array. Pseudo code example:   multiply([ 1, 2, 3], 3) → 6   multiply([1, 1, 4 ,2], 4) → 8   multiply([7, 0, 0, 7, 0, 7],...

  • Preferred in Java Write a method called print Array With Total that accepts a reference to...

    Preferred in Java Write a method called print Array With Total that accepts a reference to a two - dimensional array of integers and two primitive integers called number Of Rows and number OF Columns as parameters and displays the elements of two - dimensional array as a table with an extra column for the row total as the rightmost column and an extra row for the column totals as the bottom row. (Please do NOT call either definition of...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • Write a method named printReverse() that accepts an int array as its parameter and prints that...

    Write a method named printReverse() that accepts an int array as its parameter and prints that array in reverse order. This method should not have a return statement (void). - All elements should be printed on the same line, separated by a space - A new line should be printed after the entire array prints - If the array passed in held the values: {3,2,1} - Output: 1 2 3 //newline printed here

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

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