hi, i'm new. plus one question

T

travis laduke

I've been forced to work on some php lately and found myself
thinking: "man, this sucks. i wish it was more like supercollider"
then i remembered supercollider is ruby-influenced so i started
reading about ruby.

Here's my question: is there a common way to rotate a string or an
array?

I thought i could do some combination of .pop and .unshift or
something, but i ran into this problem:

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?
why is it different than:

x = idea.reverse

where idea is left alone and only x is the reversed array?



-travis
 
S

SHIGETOMI, Takuhiko

i am also a ruby-newbie.
so i am not sure that i could answer enough to your question but...
idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?

because pop is an explicit manipulator, even if it is not followed by '!'.

i guess you simply can use idea.last instead of pop.

q2hdp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618
 
S

SHIGETOMI, Takuhiko

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?

this is a known phenomenon, named 'decay'.
"ruby" stands for "radioactively unstable block yielder".
you should refrain from giving a shock on it,
and keep it away from any other highly radioactive matter.

i am sure that your supercollider did make a kind of side effect on your
ruby and make its decay period get accelerated.
hence, the rearmost particle in the array has vanished.
it would be worth to try again after powering off your supercollider.

moreover, gravitational equilibration is also important.
ruby prefers flat place.
if you can see huge mountain on your left side and sea on your right
side, ruby won't run as you expect, since it would be drifting to the
mountain side.

be careful.

qssp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618
 
T

travis laduke

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?

this is a known phenomenon, named 'decay'.
"ruby" stands for "radioactively unstable block yielder".
you should refrain from giving a shock on it,
and keep it away from any other highly radioactive matter.

i am sure that your supercollider did make a kind of side effect on
your
ruby and make its decay period get accelerated.
hence, the rearmost particle in the array has vanished.
it would be worth to try again after powering off your supercollider.

moreover, gravitational equilibration is also important.
ruby prefers flat place.
if you can see huge mountain on your left side and sea on your right
side, ruby won't run as you expect, since it would be drifting to the
mountain side.

be careful.

qssp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618

like playing golf near the ocean?
 
S

SHIGETOMI, Takuhiko

like playing golf near the ocean?

you've got the point!

googling with 'gravimetric survey' will help you.

xnfp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618
 
B

Brian Schröder

I've been forced to work on some php lately and found myself
thinking: "man, this sucks. i wish it was more like supercollider"
then i remembered supercollider is ruby-influenced so i started
reading about ruby.

Here's my question: is there a common way to rotate a string or an
array?

I thought i could do some combination of .pop and .unshift or
something, but i ran into this problem:

idea =3D ["a","b","c","d"]
x =3D idea.pop
puts idea

why is idea changed? how do i make it stay the same?
why is it different than:

x =3D idea.reverse

where idea is left alone and only x is the reversed array?



-travis


Hope this helps:

bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb
module Rotate
def rotate(steps =3D 1)
steps =3D steps % self.length
self[-steps..-1].concat self[0...-steps]
end
end

class String
include Rotate
end

class Array
include Rotate
end

string =3D "I am a String"
array =3D string.split

10.times do | i |
p string.rotate(i)
p array.rotate(i)
end

bschroed@black:~/svn/projekte/ruby-things$ ruby rotate.rb
"I am a String"
["I", "am", "a", "String"]
"gI am a Strin"
["String", "I", "am", "a"]
"ngI am a Stri"
["a", "String", "I", "am"]
"ingI am a Str"
["am", "a", "String", "I"]
"ringI am a St"
["I", "am", "a", "String"]
"tringI am a S"
["String", "I", "am", "a"]
"StringI am a "
["a", "String", "I", "am"]
" StringI am a"
["am", "a", "String", "I"]
"a StringI am "
["I", "am", "a", "String"]
" a StringI am"
["String", "I", "am", "a"]

regards,

Brian
 
D

David A. Black

--8323328-1822827234-1127390837=:3297
Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1822827234-1127390837=:3297"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-1822827234-1127390837=:3297
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb
module Rotate
def rotate(steps =3D 1)

returns self if length.zero? # :)
steps =3D steps % self.length
self[-steps..-1].concat self[0...-steps]
end
end


David

--=20
David A. Black
(e-mail address removed)
--8323328-1822827234-1127390837=:3297--
--8323328-1822827234-1127390837=:3297--
 
M

Michael Ehehalt

Hi,

the code:

idea = ["a","b","c","d"]
idea.push( idea.shift )

lets the array rotate to the left and returns ["b", "c", "d", "a"]

the code:

idea = ["a","b","c","d"]
idea.unshift( idea.pop )
p idea

lets rotate to the right and returns: ["d", "a", "b", "c"]

Is this what you mean?

Michael
 
D

David A. Black

Hi --

Hi,

the code:

idea = ["a","b","c","d"]
idea.push( idea.shift )

lets the array rotate to the left and returns ["b", "c", "d", "a"]

the code:

idea = ["a","b","c","d"]
idea.unshift( idea.pop )
p idea

lets rotate to the right and returns: ["d", "a", "b", "c"]

Is this what you mean?

I think Travis wanted to do that without changing the original array
-- something like:

idea = %w{a b c d}
new_idea = [idea[-1], *idea[0..-2]]

(Obviously not a full implementation, but just illustrating the
non-changingness.)


David
Michael








travis laduke said:
I've been forced to work on some php lately and found myself thinking:
"man, this sucks. i wish it was more like supercollider"
then i remembered supercollider is ruby-influenced so i started reading
about ruby.

Here's my question: is there a common way to rotate a string or an array?

I thought i could do some combination of .pop and .unshift or something,
but i ran into this problem:

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?
why is it different than:

x = idea.reverse

where idea is left alone and only x is the reversed array?



-travis
 
R

Robert Klemme

Michael Ehehalt said:
Hi,

the code:

idea = ["a","b","c","d"]
idea.push( idea.shift )

lets the array rotate to the left and returns ["b", "c", "d", "a"]

the code:

idea = ["a","b","c","d"]
idea.unshift( idea.pop )
p idea

lets rotate to the right and returns: ["d", "a", "b", "c"]

Is this what you mean?

As Travis wanted the original array unchanged, you'd have to insert a #dup
or #clone somewhere, e.g.

idea = ["a","b","c","d"]
cp = idea.dup
cp.push( cp.shift )

etc.

Kind regards

robert
 
T

travis laduke

Hope this helps:

bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb
module Rotate
def rotate(steps =3D 1)
steps =3D steps % self.length
self[-steps..-1].concat self[0...-steps]
end
end


regards,

Brian


you guys are magicians, thanks

i thought not being able to rotate was the only thing standing in my =20
way of finding all the anagrams (permutations) of a string so i can =20
enhance my chances of beating my girlfriend at online scrabble.
turns out its a lot harder than i first imagined.

oh, and how to i point ruby to a directory where i want to put all my =20=

modules and such?, or where should i put all my modules and such? i'm =20=

on an apple computer.


travis
 
L

Logan Capaldo

Hope this helps:

bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb
module Rotate
def rotate(steps =3D 1)
steps =3D steps % self.length
self[-steps..-1].concat self[0...-steps]
end
end


regards,

Brian


you guys are magicians, thanks

i thought not being able to rotate was the only thing standing in =20
my way of finding all the anagrams (permutations) of a string so i =20
can enhance my chances of beating my girlfriend at online scrabble.
turns out its a lot harder than i first imagined.

oh, and how to i point ruby to a directory where i want to put all =20
my modules and such?, or where should i put all my modules and =20
such? i'm on an apple computer.


travis

Yeah, thats O(n!) IIRC. What you can try is

#!/usr/bin/env ruby
my_hand =3D %w( a b c ) # Only an example, put the letters you actually =20=

have here
seed =3D 'br' # Something already on the board you have room to build =
off
# Note, put only the part that would be in your word
# eg. if the word is bread, going down and you want to =20
start at the e and go to the right
# use only the letter e as your seed

File.open('/usr/share/dict/words') do |dictionary|
dictionary.grep(/^#{seed}(?:#{my_hand.join('|')})+/) do |word|
word.chomp!
puts word if (word.split(//) - (seed.split(//) + =20
my_hand)).empty?
end
end

Note that its very, very limited and not smart at all. It'll narrow =20
down your choices somewhat, and may be helpful in a minor way, but =20
its just a heuristic and a poor one at that, not an alogirithm and =20
not all the answers it gives you will be right.

Seems like a good Ruby Quiz though, a scrabble bot.
 
M

Martin DeMello

travis laduke said:
i thought not being able to rotate was the only thing standing in my
way of finding all the anagrams (permutations) of a string so i can
enhance my chances of beating my girlfriend at online scrabble.
turns out its a lot harder than i first imagined.

Permutations are not an efficient way to find anagrams, note. Something
like this is better (untested):

dictfile = 'sowpods.txt'
anags = {}
IO.foreach(dictfile) {|word|
word.chomp!
word.upcase!
alpha = word.split(//).sort.join('')
anags[alpha] ||= [] # set the value to [] if it doesn't exist
anags[alpha] << word
}

puts "Enter rack"
rack = gets.chomp.upcase.sort
n = rack.length

# work through all subracks
max = 2**n - 1
max.downto(1) {|i|
s = ""
0.upto(n) {|j|
s << rack if i[j] == 1
}

puts anags
}

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top