So I came across an interesting problem awhile back, and I finally got around to solving it. Basically, it needs to allow the user to provide two words, and have them progressively splice together, like so:
Word 1: 123
Word 2: abc
Result: 123abc
12a3bc
1a2b3c
a1b2c3
ab1c23
abc123
The two words provided must maintain their order as they weave between eachother, and they should be arranged so that any two letters in one word are never separated by more than one letter from the other word.
I half expect to learn that I over-thought the problem, but I still think it's pretty slick. I'm open to all kinds of feedback.
EDIT: Here's the updated version. Old version is below:
!/usr/bin/ruby
class Array
def swap!(a,b)
self[a], self[b] = self[b], self[a]
self
end
end
class String
def phase(other)
if self.empty?
[other]
elsif other.empty?
[self]
else
@word1 = self.split("")
@word2 = other.split("")
@combined_words = []
@word2.each { |letter| @combined_words.push({:letter => nil, :word => nil}) }
@word1.each do |letter|
@combined_words.push({:letter => letter, :word => 1})
@combined_words.push({:letter => nil, :word => nil})
end
@word2.each { |letter| @combined_words.push({:letter => letter, :word => 2}) }
end
while @combined_words.include?({:letter => nil, :word => nil})
nil_loc = @combined_words.rindex{ |addr| addr[:word].nil? }.to_i
word2_subloc = @combined_words.drop(nil_loc).index{ |addr| addr[:word] == 2 }.to_i
if word2_subloc == 0
@combined_words.delete_at(nil_loc)
print "\n\n"
@combined_words.each do |addr|
if not addr[:letter].nil?
print addr[:letter]
end
end
print "\n\n"
else
@combined_words.swap!(nil_loc, word2_subloc + nil_loc)
end
end
end
end
puts "What is your first word?"
param1 = gets
puts "Cool, what is your second word?"
param2 = gets
puts param1.chomp.phase(param2.chomp)
Old version:
#!/usr/bin/ruby
class Array
def swap!(a,b)
self[a], self[b] = self[b], self[a]
self
end
end
class Phaser
def initialize(word1, word2)
raise unless word1.is_a?(String) && word2.is_a?(String)
@word1 = word1.split("")
@word2 = word2.split("")
@combined_words = []
@word2.each { |letter| @combined_words.push({:letter => nil, :word => nil}) }
@word1.each { |letter|
@combined_words.push({:letter => letter, :word => 1 })
@combined_words.push({:letter => nil, :word => nil})
}
@word2.each { |letter| @combined_words.push({:letter => letter, :word => 2}) }
end
def phase
if !@combined_words.include?({:letter => nil, :word => nil})
return
else
nil_loc = @combined_words.rindex{ |addr| addr[:word].nil? }.to_i
word2_subloc = @combined_words.drop(nil_loc).index{ |addr| addr[:word] == 2 }.to_i
if word2_subloc == 0
@combined_words.delete_at(nil_loc)
print "\n\n"
@combined_words.each do |addr|
if not addr[:letter].nil?
print addr[:letter]
end
end
else
@combined_words.swap!(nil_loc, word2_subloc + nil_loc)
end
phase
end
end
end
puts "What is your first word?"
param1 = gets
puts "Cool, what is your second word?"
param2 = gets
test_phase = Phaser.new(param1.chomp, param2.chomp)
test_phase.phase