Codility Challenge 01 - No if, no buts

Problem

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

BDD (Behavior Driven Development)

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"

PseudoCode:

  1. Use the case statement
  2. Use when to check for greater, less and equal to 5 > 4 or 2 < 10 or 0 == 0
  3. Return the result of the comparison "5 is greater than 4" or "2 is less than 10" or "0 is equal than 0"

Code

The code is available in no_ifs.rb




Codility Challenge 02 - Ordered Count of Characters

Problem

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).

BDD (Behavior Driven Development)

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 ]]

PseudoCode:

  1. Split the string into an array of its characters, including spaces.
  2. Use the tally method to count the number of times each character appears
  3. Return the result as an array by calling the to_a method

Code

The code is available in count_characters.rb