Regular Expressions Revisted

Difficulty:
Medium

Tags:
regular expressions, strings, arrays

Instructions:
Parse this list into a two dimensional array where the inner array has a number and name.

Code:
doctor_map = "One: William Hartnell
Two:Patrick Troughton
Three:: Jon Pertwee
Four: Tom Baker (longest run)
5: Peter Davison
six: Colin Baker
Seven:   Sylvester McCoy
Eight Paul McGann
Nine: Christopher Eccleston *series reboot*
Ten: David Tennant <- personal fav
Eleven: Matthew Robert Smith - the current doctor"

doctors = doctor_map.scan(/___/)

assert_equal doctors[0][1], "William Hartnell"
assert_equal doctors[4][1], "Peter Davison"
assert_equal doctors[3][1], "Tom Baker"
assert_equal doctors[4][0], "5"
assert_equal doctors[7][0], "Eight"
assert_equal doctors[9][1], "David Tennant"
assert_equal doctors[10][1], "Matthew Robert Smith"

Your Solution

Ruby 1.9.3

Back to Problems