[QUIZ] Word Loop (#149)

P

Phrogz

def loopword word
matchinfo=
word.match(/(.*?)(.)(.(?:..)+?)\2(.*)/i)
if matchinfo
_,before,letter,looplets,after=matchinfo.to_a
pad=" "*before.size
after.reverse.split(//).each{|l| puts pad+l}
looplets=looplets.split(//)
puts before+letter+looplets.shift
until looplets.empty?
puts pad+looplets.pop+looplets.shift
end
else
puts "No loop."
end
end

Marvelously tight! However, it outputs this:

loopword "chinchilla"
#=> a
#=> l
#=> l
#=> i
#=> h
#=> ch
#=> ni

when I would have expected this:

loopword "chinchilla"
#=> ch
#=> ni
#=> l
#=> l
#=> a
 
A

Alex Shulgin

Here's a fun little challenge from the Educational Computing Organization of
Ontario.

Given a single word as input try to find a repeated letter inside of it such
that you can loop the text around and reuse that letter. For example:

$ ruby word_loop.rb Mississippi
i
p
p
Mis
ss
si

OK, here is my solution to this extremly fun quiz! :)

#!/usr/bin/ruby

class Quiz149
def initialize(word)
@word = word
@pos = 0 # currently observed char
@knots = [] # current knots positions
@combos = {} # a set of knot combos found so far
@size = @word.length*2 - 1
@arr = Array.new(@size) { Array.new(@size, ?.) } # a size x size
of dots
@hist = [] # position history
end

def [](x, y)
@arr[y][x]
end

def []=(x, y, c)
@arr[y][x] = c
end

def print
@arr.each { |line| puts line.map{ |c| c.chr }.join }
puts
end

def length
@word.length
end

def loop(x = self.length - 1, y = self.length - 1)
@hist.push([x, y])
c = self[x, y]
self[x, y] = @word[@pos]
@pos += 1
if @pos >= length # reached end of the word
if [email protected]?
self.print unless @combos[@knots]
@combos[@knots] = true
end
else
looptry(x + 1, y ) # right
looptry(x, y - 1) # up
looptry(x - 1, y ) # left
looptry(x, y + 1) # down
end
@pos -= 1
self[x, y] = c
@hist.pop()
end

def no_loop? # was there any solution?
@combos.empty?
end


######################################################################
private

def looptry(x, y)
# could not make this look any uglier ;-)
return if @hist.size >= 2 && x == @hist[-2][0] && y == @hist[-2]
[1]

c = @word[@pos]
f = self[x, y]
if f == c || f == ?.
@knots.push(@pos) if f == c
loop(x, y)
@knots.pop() if f == c
end
end
end

STDIN.each do |line|
quiz = Quiz149.new(line.chomp.downcase)
quiz.loop()
puts "No loop." if quiz.no_loop?
end


I think it finds all of the possible solutions. A set of already
known solutions is kept to track down the equivalent ones and do not
print them.

It outputs something like this:

$ ./loop.rb
mississippi
......................
......................
......................
......................
......................
......................
......................
......................
......................
...............ppi....
...........mississ....
......................
......................
......................
......................
......................
......................
......................
......................
......................
......................

......................
......................
......................
......................
......................
......................
......................
......................
......................
............ssi.......
...........miss.......
............ppi.......
......................
......................
......................
......................
......................
......................
......................
......................
......................

......................
......................
......................
......................
......................
......................
......................
......................
............pi........
............psi.......
...........miss.......
......................
......................
......................
......................
......................
......................
......................
......................
......................
......................

......................
......................
......................
......................
......................
......................
......................
......................
......................
.............si.......
...........miss.......
.............ippi.....
......................
......................
......................
......................
......................
......................
......................
......................
......................

markham
..............
..............
..............
..............
..............
.......ahk....
.......mar....
..............
..............
..............
..............
..............
..............

..............
..............
..............
..............
..............
........hk....
.......mar....
..............
..............
..............
..............
..............
..............

..............
..............
..............
..............
..............
........hk....
.......mar....
........m.....
..............
..............
..............
..............
..............

yummy
..........
..........
..........
.....mm...
.....yu...
..........
..........
..........
..........

dana
No loop.


Of course, it could be tweaked to remove the unneeded dots when
printing, however I pretty much love it this way. ;-)
 
A

Alex Shulgin

Yeah, and I've finally found one which can form a loop with some empty
space in it. :)

Antidisestablishmentarianism:

...........nemh..
...........t..s..
antidisestablism
...........rian..
 
J

James Gray

Yeah, and I've finally found one which can form a loop with some empty
space in it. :)

Antidisestablishmentarianism:

..........nemh..
..........t..s..
antidisestablism
..........rian..

Eric's code makes some fun loops with that word:

$ ruby word_loop.rb Antidisestablishmentarianism
Number of overlaps: 5
========================================
ant
ianism
r d
abli
tses
nemh
========================================
sm
antidish
n lem
a bse
iratn
========================================
m
s
antidish
n lem
a bse
iratn
========================================
ant
ianis
r dm
abli
tses
nemh
========================================
an
t
ianis
r dm
abli
tses
nemh
========================================
an
t
ianism
r d
abli
tses
nemh
========================================

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top