Showing posts with label colorize. Show all posts
Showing posts with label colorize. Show all posts

Thursday, December 8, 2016

Arrays of Animals

From Exercise 34 of Learn Ruby the Hard Way.  I am sure this could be done a lot more elegantly, but I'm satisfied with what I got from this, and I know it works too.
# everything pertaining to the quiz is contained in the function below

def quiz

  # clear the screen
  puts "\e[H\e[2J"

  # gem for colorising text, very cool stuff
  require 'colorize'

  # the array of animals
  animals = ['bear', 'ruby', 'peacock', 'kangaroo', 'whale', 'platypus']

  # display the list of animals, it's not a memory test
  puts "These are the animals in our array: #{animals}"

  # preparing the ordinal and cardinal arrays for the quiz
  ordinal = ["first", "second", "third", "fourth", "fifth", "sixth"]
  cardinal = [0, 1, 2, 3, 4, 5]

  # generic question for choosing the ordinal position
  chooseOrdinal = """
  What is the ordinal position of this animal?
  first
  second
  third
  fourth
  fifth
  sixth


  """

  # generic question for choosing the cardinal position
  chooseCardinal = """
  What is the cardinal position of this animal?
  0
  1
  2
  3
  4
  5


  """

  puts """
  Type the name of an animal:
  - bear
  - ruby
  - peacock
  - kangaroo
  - whale
  - platypus

  """
  print "> "

  userSelection = $stdin.gets.chomp.downcase
  case userSelection
  when "#{animals[0]}"
    puts "You chose a #{userSelection}.\n"
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[0]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[0]}"
        puts "Correct!\n".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!".colorize(:red)
    end
  when "#{animals[1]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[1]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[1]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!"
    end
  when "#{animals[2]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[2]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[2]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!"
    end
  when "#{animals[3]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[3]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[3]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!"
    end
  when "#{animals[4]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[4]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[4]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!"
    end
  when "#{animals[5]}"
    puts "You chose a #{userSelection}."
    puts chooseOrdinal
    answerOrdinal = $stdin.gets.chomp.downcase
    if answerOrdinal == "#{ordinal[5]}"
      puts "Correct!\n".colorize(:green)
      puts chooseCardinal
    print "> "
      answerCardinal = $stdin.gets.chomp
      if answerCardinal == "#{cardinal[5]}"
        puts "Correct!".colorize(:green)
      else
        puts "Incorrect cardinal number!".colorize(:red)
      end
    else
      puts "Incorrect ordinal number!".colorize(:red)
    end
  else
    puts "I know no such animal."
  end

end

quiz()

First Small Step Into Testing

I am working on Exercise 28 of Learn Ruby the Hard Way and this is my first concrete example of using testing in the course of coding.

The exercise is a test of boolean logic: I am given a statement e.g.

true == true

And must work out whether it is "true" or "false".

As you can see, I have organised the statements in a two-dimensional array in which each sub-array is composed of three elements: a string denoting the statement, the statement itself, and my prediction.

# List of boolean statements to test my knowledge thereof, organised in a two-dimensional array
booleanTest = [
  ["true && true", true && true, true],
  ["false && true", false && true, false],
  ["1 == 1 && 2 == 1", 1 == 1 && 2 == 1, false],
  ['"test" == "test"', "test" == "test", true],
  ["1 == 1 || 2 != 1", 1 == 1 || 2 != 1, true],
  ["true && 1 == 1", true && 1 == 1, true],
  ["false && 0 != 0", false && 0 != 0, false],
  ["true || 1 == 1", true || 1 == 1, true],
  ['"test" == "testing"', "test" == "testing", false],
  ["1 != 0 && 2 == 1", 1 != 0 && 2 == 1, false],
  ['"test" != "testing"', "test" != "testing", true],
  ['"test" == 1', "test" == 1, false],
  ["!(true && false)", !(true && false), true],
  ["!(1 == 1 && 0 != 1)", !(1 == 1 && 0 != 1), false],
  ["!(10 == 1 || 1000 == 1000)", !(10 == 1 || 1000 == 1000), false],
  ["!(1 != 10 || 3 == 4)", !(1 != 10 || 3 == 4), true]
]
I test my prediction by running the following testing loop, and in keeping with the practice of "green" for good and "red" for error, I installed the colorize gem and applied it to the output of the testing loop.
# Required in order to highlight the text in the testing loop green or red.

# Colorize gem to mark the result green or red.
require 'colorize'

# Testing loop
puts "Here are the results of my test:\n\n"
booleanTest.each do|description,result,prediction|
  puts "PREDICTION: #{description} GIVES #{prediction}, RESULT: #{result}"
  if prediction == result
    puts "Correct!".colorize(:green)
  else
    puts "Incorrect!".colorize(:red)
  end
end

And here are the results:



1,050 hours

It took me 13 working days to complete my first 100 "work" pomodoros as a Junior Software Tester at Profectus Group.  Much of ...