LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-02-2017, 02:41 PM   #1
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Rep: Reputation: 31
Would like some understand on a ruby skills challenge I failed


A while back, I was confronted with a skills challenge for potential employment. I failed it miserably. Its been quite some time since then and it is still haunting me.

So I tried to run through it again today. I figured without any time limit and for as much as I thought about it maybe I would have a better shot at getting it right or at least completed. Unfortunately not the case, so now I am looking for understanding on what I am lacking in my knowledge to get past this problem.

The challenge:
Get the minimum number of keystrokes to change one word to another word.

Example:
Code:
Original Word: "clarity"
New Word: "singularity"

The minimum number of keystrokes to change "clarity" to "singularity" is six (one to delete the "c" and five to type "singu").
The code I am working with to try and solve this challenge.
Code:
#
=begin
variabls:
  original_word
  new_word
processes:
  Get a list of matching letters
  Remove matching letters from both the original and new words
  Get a count of remaining letters from the original word (count of letters to delete)
  Get a count of remaining letters from the new word (count of letters to add)
  Add both counts to get total number of keystrokes
output:
  The minimum number of keystrokes to change "---" to "---" is six (n to delete the "-" and n to type "-----").
=end

original_word = 'clarity'
new_word = 'singularity'

original_word_array = original_word.split("")
new_word_array = new_word.split("")
ow_index = original_word_array.length - 1
nw_index = new_word_array.length - 1
i = 0
x = 0

matches = []

#### Having trouble incrementing through the characters of each correctly to separate the matching and non matching characters
while i <= nw_index do
  if new_word_array[i] == original_word_array[x]
    puts "MATCH"
    matches << new_word_array[i]
    i += 1
    original_word_array.slice!(x)
    x = 0
  else
    x = x == ow_index ? 0 : (x + 1)
    i = 
  end
 
Old 04-02-2017, 09:36 PM   #2
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
Sorry, didn't read that problem statement properly, ignore previous version of my reply.

Last edited by cyent; 04-02-2017 at 09:38 PM.
 
Old 04-02-2017, 10:17 PM   #3
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
ruby -e 'p ["clarity".tr( "singularity",""),"singularity".tr("clarity","")]'
["c", "sngu"]

ruby -e 'p "singularity".tr( "sngu","")'
"ilarity"

ruby -e 'p "clarity".tr( "c","")'
"larity"
 
Old 04-04-2017, 09:10 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,667
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Also: don't get too up-tight about any job that gives you a "skills challenge" as a pre-condition of employment.

You can find much better jobs.

You should be able to describe to them programs that you have written, either in school or in previous employment, and you should be able to give references of "people who will speak well of you."

"People who will speak well of you" is the single most important thing that you must nurture and curry. Even an a*shole can be "a good, competent code-slinger," but (s)he will still remain "an a*shole," and in a team of people who are doing very exacting intellectual work under conditions of high pressure, they're a detriment to the team that must be avoided. A "skills challenge" won't tell you this.

If you've done any programming at all, I can teach you how to "sling code" the way we do. What I can't discern is whether or not you are an a*shole. Whether or not you will be a social(!) fit, and therefore an asset to the team.

- - -

Pondering this particular "challenge," I would consider it to be open to very broad interpretation and not at all obvious. I'm not persuaded that the algorithm proposed really does lead to "the right answer," and I can think of several disjoint possibilities of just what "the right answer" might be considered to be. It's fairly unreasonable to expect a candidate to just sit down and bang-out this thing in an interview situation.

Last edited by sundialsvcs; 04-04-2017 at 09:12 AM.
 
1 members found this post helpful.
Old 04-04-2017, 09:19 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Code:
#!/usr/bin/env ruby

old='clarity'
new='singularity'

take = old.split(//).select{|c| new.sub!(c,'').nil?}.size

puts "Required number of moves is :- #{take + new.length}"
 
Old 04-04-2017, 12:54 PM   #6
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
@sundialsvcs I appreciate the insight. That actually makes me feel a lot better about it. This has been eating away at me for some time now.
 
Old 04-04-2017, 01:06 PM   #7
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
@cyent and @grail thanks for taking a stab at the challenge. The results that you both have written are specific to the 2 words that are in the example.

If I wanted to use 2 completely different words I don't think it would work. On a quick test in irb for @grail's code:

Code:
irb(main):009:0> old = 'car'
=> "car"
irb(main):010:0> new = 'bile'
=> "bile"
irb(main):011:0> take + new.length
=> 5
If my manual counting is right, the count should be 7 (3 to delete 'car', 4 to create 'bile')

But to be honest, even though I know my code was not right, I did the same thing. I wrote the code to satisfy the example words and not any 2 words.

I guess that goes into another thing I seem to run into which is having challenges that aren't directly clear about the expected results.

Last edited by lmcilwain; 04-04-2017 at 01:07 PM.
 
Old 04-04-2017, 07:27 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,667
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
This so-called "challenge" appears to be quite specific to the two words given. It is certainly not a general statement of the problem, nor of a general solution to "all problems of this class."

The general class is text differencing algorithms, and if you look at (for example) this paper by Neil Fraser, or Dr. Meyers' paper on the subject, you will immediately see that in the general case it is anything but trivial.

Whether your solution – whatever it was – was "correct," would have been very much "in the eye of the beholder." You could not be reasonably expected to take on such a "challenge" in a pre-employment code-slinging contest.

Last edited by sundialsvcs; 04-04-2017 at 07:33 PM.
 
Old 04-04-2017, 10:34 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Not sure if you copied all of my data, but it does in fact return 7 for me using your new words. I did actually use half a dozen other combos including words with repeated characters.
I don't think it should matter, but I am on version 2.4

Actually having a quick look at what you have written, did you set 'take' variable again or is the 1 stored in that from previous run??

btw. I agree 100% with sundialsvcs that a test of this nature in an interview on the spot tells me little to nothing about you as an individual which I hold higher than your skills
which can be taught
 
Old 04-05-2017, 08:37 AM   #10
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
@grail, I don't think it should matter. Your probably right, I might not have copied everything I apologize. The 'take' variable is a newly set variable from what I remember but honestly its been a couple of days and I should have posted the full IRB session for clarification .

@sundialsvcs, I am in full agreement with you on your thoughts. I will certainly read those links. I am very curious what they have to say.

As far as if the challenge was specific to those 2 words, that is part of what get me overthinking about these types of challenges. When seeing something as simple as the difference between 2 words, my first thought is to make it not just about those 2 words but any 2 words.

Just to be fair, this code challenge was part of the interview process where a cultural evaluation conversation was had. It wasn't a case of me showing up and them throwing me in a room to code. I have had other places start and end with code challenges before having any sort of conversation. At the time this challenge was given, that was not the case.

Last edited by lmcilwain; 04-05-2017 at 08:41 AM.
 
Old 04-05-2017, 08:58 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
With regards to the general problem, https://en.wikipedia.org/wiki/Edit_distance might be of interest. The OP sounds like Longest common subsequence (which is a particular form of edit distance).
 
1 members found this post helpful.
  


Reply

Tags
ruby



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RVM + local install ruby + system ruby maintenance Corpus-Khu Linux - Software 0 02-13-2014 01:45 AM
LXer: Jan 2014 IT Skills Watch report: three IT skills on the rise LXer Syndicated Linux News 0 01-22-2014 01:00 PM
LXer: Understand Representational State Transfer (REST) in Ruby LXer Syndicated Linux News 0 08-14-2012 09:40 AM
Any issues installing Ruby Gems and Ruby on Rails in Slackware? Lufbery Slackware 8 02-09-2011 07:22 PM
LXer: Hot skills: could Ruby be the jewel in the crown of scripting? LXer Syndicated Linux News 0 05-30-2006 10:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration