explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

Problem Solving: Random Rules & Regexp (JavaScript)

Leave a comment

[disclaimer]
My intent is not to provide an answer per se, rather it’s to provide insight into the way I think and to foster discussion.  Copy & paste at your own risk.
[end]

This is one of 3 tests that I recently took in applying for a developer position.  Due to copyright restrictions I cannot post the problem here verbatim.  I’ve substituted certain values and names.  I can give you the gist (both :) here:

the problem

parameters

  • the input string P can have a length of 0 – 50000
  • the input string P will only consist of the characters X, Y and/or Z

task

Keep in mind that this is my interpretation of the task at hand.  While it’s unfortunate that I can’t recreate the test verbatim here, I can say it was very explicit, but did leave room for interpretation.  Without further ado…

Write a function

function solveIt(S);

..such that, if any transformation applies, pick one at random, transform the string, and continue the process until no transformations are applicable.  Now where there is some room for interpretation is in the transformation rules.  An example of a transformation rule looks like this:

substitute some occurrences of “XY” with “XX”

As you can see, the word “some” could be interpreted differently.  Rather than overthink that part of the test, I assumed they meant to apply the transformation to any & all occurrences.  I think the coding difference is rather trivial at this point.

transformations

  • XY -> XX
  • YX -> XX
  • YZ -> ZZ
  • ZY -> ZZ
  • XX -> X
  • ZZ -> Z

the solution

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.