Ruby Array Exercises: Check whether the number of 2's is greater than the number of 5's of a given array of integers
Ruby Array: Exercise-34 with Solution
Write a Ruby program to check whether the number of 2's is greater than the number of 5's of a given array of integers.
def check_array(nums) ctr = 0 i = 0 while i < nums.length if (nums[i] == 2) ctr = ctr + 1 elsif (nums[i] == 5) ctr= ctr - 1 end i += 1 end return (ctr > 0) end print check_array([3, 5, 3, 3]),"\n" print check_array([2, 3, 2, 5]),"\n" print check_array([2, 2, 5, 5]),"\n"
false true false
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check whether the sum of all the 3's of an given array of integers is exactly 9.
Next: Write a Ruby program to check whether every element is a 3 or a 5 in a given array of integers.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework