What if we need we need to check which number is bigger between 2 numbers without using if
or the ternary
operator?
Write a function that accepts two numbers a and b and returns whether a is smaller than, bigger than, or equal to b, as a string.
Note: Assume the function will always take 2 parameters
Given: 2 numbers a and b;
When: The numbers are compared
Then: Return which number is bigger without the use of if
or the ternary
operator
Example1
Given: (5, 4)
Return: "5 is greater than 4"
Example2
Given: (2, 10)
Return: "2 is less than 10"
Example3
Given: (0, 0)
Return: "0 is eaqul to 0"
- Use the
case
statement - Use
when
to check for greater, less and equal to5 > 4
or2 < 10
or0 == 0
- Return the result of the comparison
"5 is greater than 4"
or"2 is less than 10"
or"0 is equal than 0"
The code is available in no_ifs.rb
Count the number of occurrences of each character and return it as a (list of arrays) in order of appearance. For empty output return (an empty list).
Given: A string s
When: The number of times each character appears in the string is calculated
Then: Return an array of a list of arrays in order of the character's appearance
Example1
Given: "abracadabra"
Return: [['a', 5], ['b', 2], ['r', 2], ['c', 1], ['d', 1]]
Example2
Given: "Code Wars"
Return: [['C', 1], ['o', 1], ['d', 1], ['e', 1], [' ', 1], ['W', 1], ['a', 1], ['r', 1], ['s', 1]]
Example3
Given: "233312"
Return: [['2', 2], ['3', 3], ['1', 1 ]]
- Split the string into an array of its characters, including spaces.
- Use the
tally
method to count the number of times each character appears - Return the result as an array by calling the
to_a
method
The code is available in count_characters.rb