Skip to content

Instantly share code, notes, and snippets.

@fallwith
Last active December 20, 2020 02:59
Show Gist options
  • Save fallwith/00aa5f7a0d5c192e91fa6468e7434048 to your computer and use it in GitHub Desktop.
Save fallwith/00aa5f7a0d5c192e91fa6468e7434048 to your computer and use it in GitHub Desktop.
Ruby Challenge

Ruby Challenge

Introduction

This is a simple test of basic Ruby programming concepts. To complete this challenge, you will need to install Ruby 1.8.6 or later on your system. Rails is not required for this test.

This test is open book, and you may use whatever resources you need to complete it.

Instructions

Create a challenge.rb file. It can optionally have a shebang line and be made executable. If you forego a shebang line, execute the code by running ruby challenge.rb.

Work through the steps of the challenge, and satisfy their given instructions in code within the challenge.rb file. The steps will call for output to STDOUT when desired. You are welcome to produce additional output if it helps you confirm that things are working.

Once you have completed the challenge, upload the contents of your challenge.rb file to a location that can be accessed by someone to review it. A GitHub Gist would work well for this.

Challenge

  1. Define a delimited string of elements that are out of alphabetical order. There should be at least one set of duplicate values in the string.
Charlie|Snoopy|Linus|Lucy|Linus|Pigpen|Snoopy|Linus|Woodstock|Frieda
  1. Split the string on the delimiter into an array of its individual elements. ([Charlie, Snoopy, Linus, etc.])
  2. Append two more elements to the end of the array.
  3. Remove the last element from the array.
  4. Determine the number of items in the array that begin with a certain character (say "L" for example).
  5. Determine the number of items in the array that are longer than 4 characters.
  6. Remove any elements from the array that contain a certain string (for example: "ie") anywhere within them. NOTE: Be sure to include at least one item in the array that matches this string so that the removal process succeeds in removing at least one item.
  7. Alphabetically sort the items in the array.
  8. Iterate over the array and create a hash, where the key is the element of the array, and the value is the number of times that element has appeared in the array. Using our example, the hash would end up something like this:
{
  'Charlie' => 1,
  'Snoopy'  => 2,
  'Linus'   => 3,
  'Lucy'    => 1,
  'Pigpen'  => 1,
  ...
}
  1. Remove any duplicate values from the array ("dedupe").
  2. Iterate over each item in the deduped array and print each item to STDOUT with an English label prefix. Make it so the names line up vertically.
Item One:    Charlie
Item Two:    Frieda
Item Three:  Linus
Item Four:   Lucy
Item Five:   Pigpen
Item Six:    Snoopy
Item Seven:  Woodstock
  1. Create a new delimited string from the deduped, sorted array with a new delimiter. NOTE: Do not including a trailing delimiter.
Charlie;Frieda;Linus;Lucy;Pigpen;Snoopy;Woodstock
  1. Print the following to STDOUT. NOTE: Add extra information/spacing/etc. to make the output sensible.
  • The start date/time of the program, formatted as "1/1/10 at 1:23 AM"
  • The version of the program (your choice to use whatever format you want)
  • The original delimited string
  • The results of step 5
  • The results of step 6
  • The results of step 7
  • The results of step 9
  • What you had printed already for step 11
  • The new delimited string from step 12
  • The elapsed time of the program, formatted as "3.724 seconds" (3 decimal places)
  1. Modify your program so that it accepts the list of elements as arguments on the command line. If the list of arguments is not supplied on the command line, then a default list (from step 1) is used. If a list is supplied but the list is less than 3 elements long or more than 10 elements long, an error should be printed to STDERR and the program should exit
ruby challenge.rb Charlie Snoopy Linus Lucy Linus Pigpen Snoopy Woodstock Frieda
  1. (BONUS) Structure the code into a class so that it can be reused. Calling "new" on the class should give you a properly initialized object, but there should be a separate method to perform the above steps on the object. The class should accept a list of strings as an array (like step 14). Unlike the complete program, the class should require a list of elements to be passed in and not have a default, so you will need to decide where the appropriate place to handle the default list is elsewhere within the program
m = MyClass.new("Charlie", "Snoopy", "Linus", "Lucy", "Linus", "Pigpen", "Snoopy", "Woodstock", "Frieda")
m.whatever_you_want
  1. (BONUS) Add option handling so that your program accepts both the "-h" and "-v" flags, to print a help (usage) message and version information, respectively. If an unrecognized option is passed in, the program should print an error to STDERR and exit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment