Skip to content

Instantly share code, notes, and snippets.

@kevinnio
Last active October 4, 2018 17:38
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 kevinnio/183ef44ab9137b6a118e79346d4c3a8d to your computer and use it in GitHub Desktop.
Save kevinnio/183ef44ab9137b6a118e79346d4c3a8d to your computer and use it in GitHub Desktop.
The OddEven kata

The OddEven Kata

- Write a program that prints numbers within specified range lets say 1 to 100. If number is odd print 'Odd'
  instead of the number. If number is even print 'Even' instead of number. Else print number [hint - if number is Prime].

## Steps :

Lets divide into following steps:
- Prints numbers from 1 to 100
- Print "Even" instead of number, if the number is even, means divisible by 2
- Print "Odd" instead of number, if the number is odd, means not divisible by 2 but not divisible by itself too [hint - it should not be Prime]
- Print number, if it does not meet above two conditions, means if number is Prime
require './oddeven'
oddeven = OddEven.new
(1..100).each { |number| puts oddeven.print_number(number) }
class OddEven
def print_number(number)
# Write your code here
end
def prime_number?(number)
# Write your code here
end
end
require './oddeven'
RSpec.describe OddEven do
subject { described_class.new }
describe '#print_number' do
context 'when the number is divisible by 2' do
it 'returns "EVEN"' do
[2, 4, 6, 8, 10].each do |number|
expect(subject.print_number(number)).to eq 'EVEN'
end
end
end
context 'when the number is not divisible by 2 but not a prime number' do
it 'returns "ODD"' do
[1, 15, 21, 25, 27].each do |number|
expect(subject.print_number(number)).to eq 'ODD'
end
end
end
context 'when the number is a prime number' do
it 'returns the same number' do
[3, 5, 7, 11, 13].each do |number|
expect(subject.print_number(number)).to eq number
end
end
end
end
describe '#prime_number?' do
context 'when given a prime number' do
it 'returns true' do
[2, 3, 5, 7, 11].each do |number|
expect(subject.prime_number?(number)).to eq true
end
end
end
context 'when given a non-prime number' do
it 'returns false' do
[1, 4, 6, 9, 10, 12, 14].each do |number|
expect(subject.prime_number?(number)).to eq false
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment