Skip to content

Instantly share code, notes, and snippets.

@mcne65
Forked from elle/code-problems.md
Created June 15, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcne65/60a032f7126ff624c54d427ae4e0b54c to your computer and use it in GitHub Desktop.
Save mcne65/60a032f7126ff624c54d427ae4e0b54c to your computer and use it in GitHub Desktop.

Coding Problems

Evaluation criteria

  • Code demonstrates knowledge of Ruby syntax, style, organisation, and refactoring
  • Code is divided into logical components and methods with clear responsibility.
  • Code meets all requirements as laid out per the specification.

In one sentence: we are looking for simplicity, readability, and good practices

Option 1: Scrabble Score

Given a word, compute the scrabble score for that word. If you can, use test-driven development to build this Scrabble-like game.

Letter Values

You will need these:

Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10

Example

"cabbage" should be scored as worth 14 points:

  • 3 points for C
  • 1 point for A, twice
  • 3 points for B, twice
  • 2 points for G
  • 1 point for E

Hints

  • Use the following hash:
{
  "A" => 1, "B" => 3, "C" => 3, "D" => 2,
  "E" => 1, "F" => 4, "G" => 2, "H" => 4,
  "I" => 1, "J" => 8, "K" => 5, "L" => 1,
  "M" => 3, "N" => 1, "O" => 1, "P" => 3,
  "Q" => 10, "R" => 1, "S" => 1, "T" => 1,
  "U" => 1, "V" => 4, "W" => 4, "X" => 8,
  "Y" => 4, "Z" => 10
}

Requirements:

  1. The solution should be insensitive to case
  2. An empty word or nil score 0
  3. Use the following interaction model:
> game = Scrabble.new
> game.score("hello")
=> 8
> game.score("cabbage")
=> 14
> game.score("")
=> 0
> game.score(nil)
=> 0

Bonus points:

  • Using Test-Driven Development with MiniTest or RSpec

Extensions

  • You can play a double or a triple letter.
  • You can play a double or a triple word.

Option 2: Credit Check

Write a program that can detect mistakes in a credit card number. The program will implement the Luhn algorithm to validate a credit card number.

Luhn Algorithm

The Luhn algorithm is a check-summing algorithm best known for checking the validity of credit card numbers.

You can check out the full description on Wikipedia: http://en.wikipedia.org/wiki/Luhn_algorithm

Description

The formula verifies a number against its included check digit, which is usually appended to a partial account number to generate the full account number. This full account number must pass the following test:

  • From the rightmost digit, which is the check digit, moving left, double the value of every second digit
  • If product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).
  • Take the sum of all the digits
  • If and only if the total modulo 10 is equal to 0 then the number is valid

Example

Calculating the Check Digit

Take an account identifier 7992739871. To make it an account number, we need to calculate and append a check digit. Calling the digit x, the full account number will look like 7992739871x.

We use the algorithm to calculate the correct checksum digit:

Account identifier:    7   9   9   2   7   3   9   8   7   1   x
2x every other digit:  7   18  9   4   7   6   9   16  7   2   x
Summed digits over 10: 7   9   9   4   7   6   9   7   7   2   x
Results summed:        7   9   9   4   7   6   9   7   7   2 = 67

With the result of 67, we take the ones digit (7) and subtract it from 10: 10 - 7 = 3. Thus 3 is the check digit.

The full account number with check digit is 79927398713.

Validating an Account Number

We can use the same process to validate an account number. Using 79927398713 as our sample input:

Account identifier:    7   9   9   2   7   3   9   8   7   1   3
2x every other digit:  7   18  9   4   7   6   9   16  7   2   3
Summed digits over 10: 7   9   9   4   7   6   9   7   7   2   3
Results summed:        7   9   9   4   7   6   9   7   7   2   3 = 70

Since the summed results modulo 10 is zero, the account number is valid according to the algorithm.

Requirements

Write a program that implements the Luhn algorithm to validate a credit card number.

> credit_check = CreditCheck.new("4929735477250543")
> credit_check.valid?
=> true

Bonus points:

  • Using Test-Driven Development with MiniTest or RSpec.
  • Don't use loops, iteration, or arrays

Hints

  • How to calculate the account number is not needed for this challenge
  • You can pull a character out of a string using my_string[X] where X is a numeric position number
  • You can convert a string to an integer by calling .to_i (ex: "4".to_i)
  • A nice reference for Luhn Algorithm: https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers

Sample Data

If helpful, you can use the following sample data:

  • Valid: 5541808923795240, 4024007136512380, 6011797668867828
  • Invalid: 5541801923795240, 4024007106512380, 6011797668868728

Extensions

  • Can you make it work for American Express numbers? 342804633855673 is valid but 342801633855673 is invalid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment