[QUIZ] Word Blender (#108)

J

James Edward Gray II

And finally, here's to you, Mr. Ruby Quiz creator guy, for coming
up with a Ruby quiz that really peaked my interest :)

Thank Ben Bleything. It was his idea.

James Edward Gray II
 
J

James Edward Gray II

I also enjoyed the opportunity to explore sets and things; more RQs
that can involve the standard library would be awesome.

You make the quizzes and I will run them:

(e-mail address removed)

James Edward Gray II
 
T

T. W. Urp

Vincent Fourmond wrote long time ago:
"Try transposing your matrix first, if that is what you need. Are you
dealing with transformation matrices (which are not real matrices) ?"
Yes indeed, SVG 2D transformation matrices. These matrices are 3x3.

They never taught me about transposing, just matrix multiplication, and
googel didnt help either. What is transposing (for transformation matrices)?


Cameron said:
"the included Matrix library in ruby is NOT FAST. There are other libs
that do this on the C level, such as NArray, that will allow much better
performance for some graphical uses."

Thanks. While my simple graphic
genny isnt time-critical, and a SVG file might take 2-3 seconds to render,
I will look into NArray. Is NArray what you would recommend?
 
P

Phrogz

T. W. Urp said:
Vincent Fourmond wrote long time ago:

They never taught me about transposing, just matrix multiplication, and
googel didnt help either. What is transposing (for transformation matrices)?

If you have a 3x3 matrix like:
a b c
d e f
g h i
then its transpose is:
a d g
b e h
c f i

If you have a 2x3 matrix like:
a b c
d e f
then its transpose is:
a d
b e
c f

In other words, swap rows and columns. (Excel even knows how to do
this, under Paste Special.)
 
P

Phrogz

T. W. Urp said:
They never taught me about transposing, just matrix multiplication, and
googel didnt help either. What is transposing (for transformation matrices)?

Also, RTFM :)

C:\>ri transpose
More than one method matched your request. You can refine
your search by asking for information on one of:

Array#transpose, Matrix#transpose


C:\>ri Array.transpose
--------------------------------------------------------
Array#transpose
array.transpose -> an_array
------------------------------------------------------------------------
Assumes that _self_ is an array of arrays and transposes the rows
and columns.

a = [[1,2], [3,4], [5,6]]
a.transpose #=> [[1, 3, 5], [2, 4, 6]]


C:\>ri Matrix.transpose
-------------------------------------------------------
Matrix#transpose
transpose()
------------------------------------------------------------------------
Returns the transpose of the matrix.

Matrix[[1,2], [3,4], [5,6]]
=> 1 2
3 4
5 6
Matrix[[1,2], [3,4], [5,6]].transpose
=> 1 3 5
2 4 6


(also known as t)
 
D

David Kastrup

T. W. Urp said:
Vincent Fourmond wrote long time ago:

They never taught me about transposing, just matrix multiplication,
and googel didnt help either. What is transposing (for
transformation matrices)?

Matthew 20:16 for the dimensions.
 
T

T. W. Urp

C:\>ri transpose
More than one method matched your request. You can refine
your search by asking for information on one of:

Array#transpose, Matrix#transpose
C:\>ri Array.transpose
C:\>ri Matrix.transpose

Thats very nice. I never realized such a great help tool exists! Thank you
Phrogz!! :)
 
W

William James

Ruby said:
The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Ben Bleything

This is a riff on the Jumble puzzle found in many (US) newspapers. More
specifically, it's based on the game TextTwist[1], made by GameHouse[2] and
published in various places around the web.

The mechanic of TextTwist is simple. The player is given six letters and is
tasked with unscrambling those letters into as many words as possible. If the
player can use all six letters in a word, they proceed to the next round.

Your task is to build the back-end engine to run a TextTwist clone. Effectively,
this means that you must generate a list of three- to six-letter words that can
all be constructed from the same six letters. This list must contain at least
one six-letter word.

Bonus points for building a completely functional game!

[1]: http://games.yahoo.com/games/texttwist.html (just one example, java)
[2]: http://www.gamehouse.com/

class String
def chars
split("")
end
def sorted
chars.sort.join
end
end

# Generate combinations.
def comb array, n, str = "", &blk
0.upto(array.size - n){|i|
if 1 == n
yield str + array
else
comb array[i+1..-1], n-1, str+array, &blk
end
}
end

word_groups = Hash.new {[]}
shorts = Hash.new {[]}
while word = gets do
next unless (word=word.downcase.delete('^a-z')).size.between?(3,6)
if 6 == word.size
word_groups[word.sorted] += [ word ]
else
shorts[word.sorted] += [ word ]
end
end

word_groups.each_key{|key|
3.upto(5){|n|
combinations = []
comb( key.chars, n ){|s| combinations << s}
combinations.uniq.each{|s| word_groups[key] += shorts }}}
 
M

M. Edward (Ed) Borasky

T. W. Urp said:
Vincent Fourmond wrote long time ago:


They never taught me about transposing, just matrix multiplication, and
googel didnt help either. What is transposing (for transformation matrices)?
Given a matrix "a" with "m" rows and "n" columns

for i = 1 to m do
for j = 1 to n do
atranspose[j, i] = a[i, j]
end
end
Thanks. While my simple graphic
genny isnt time-critical, and a SVG file might take 2-3 seconds to render,
I will look into NArray. Is NArray what you would recommend?
Yes!
 
M

Martin DeMello

This just solves the find-all-subwords problem:

target = ARGV[0]
dict = ARGV[1] || 'sowpods'

reduced = target.split(//).sort.uniq.join
primes = [2, 3, 5, 7, 11, 13]
factors = []
reduced.split(//).each_with_index {|e, i|
factors[e[0]] = primes
}

target_num = 1
target.each_byte {|i| target_num *= factors}

IO.foreach(dict) {|word|
word.chomp!
next unless (word =~ /^[#{reduced}]+$/) &&
(word.length < 7) && (word.length > 2)
p = 1
word.each_byte {|i| p *= factors}
puts word if target_num % p == 0
}
 
M

Martin DeMello

This just solves the find-all-subwords problem:

target = ARGV[0]
dict = ARGV[1] || 'sowpods'

reduced = target.split(//).sort.uniq.join
primes = [2, 3, 5, 7, 11, 13]
factors = []
reduced.split(//).each_with_index {|e, i|
factors[e[0]] = primes
}

target_num = 1
target.each_byte {|i| target_num *= factors}

IO.foreach(dict) {|word|
word.chomp!
next unless (word =~ /^[#{reduced}]+$/) &&
(word.length < 7) && (word.length > 2)
p = 1
word.each_byte {|i| p *= factors}
puts word if target_num % p == 0
}


That's neat, and is a good general way of checking for inclusion (so you can
extend it past characters in a string which might not have an .include?
method). You probably don't want that .uniq in there though as that excludes
you from matching 'hell' out of 'hello', for example.


No, I do need the uniq since I want one prime per unique letter in the
target word. I then calculate its signature by a loop over the entire
word, not the reduced word, so "hell" is indeed matched for "hello"
but not for, say, "whelps".

martin
 

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
474,222
Messages
2,571,140
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top