[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

scramble sentence, how to better program?

skanemupp

5/8/2008 8:05:00 PM

i have written a program that takes a sentence as input and outputs
the sentence with the words rearranged.
it is probably inefficient and can probably be done with less code.
it is also nondeterministic( while (inList(temp,scr)) could go on
for a long time if the user is unlucky) and im not sure how to get
around that or if it is even possible.
also, if there is 2 identical words in the sentence, lets say "the" is
in there twice, it will loop forever. i could do
..uniq in the beginning but lets say i want both "the" to be in there.
i have to write some if duplicate then ok up to nbrofduplicates. is
there an easy way to do this?

#scrambler
puts "Enter a sentence: "
sentence = gets
list = sentence.split()
scr = []

def inList(aword,alist)
inl=false
for i in (0..alist.length()-1)
if alist[i] == aword
inl=true
end
end
return inl
end

for x in (0..list.length()-1)
temp = list[rand(list.length())]
while (inList(temp,scr))
temp = list[rand(list.length())]
end
scr = scr + temp.split()
end

puts "Scrambled: "
puts scr
10 Answers

David A. Black

5/8/2008 8:12:00 PM

0

Hi --

On Fri, 9 May 2008, globalrev wrote:

> i have written a program that takes a sentence as input and outputs
> the sentence with the words rearranged.
> it is probably inefficient and can probably be done with less code.
> it is also nondeterministic( while (inList(temp,scr)) could go on
> for a long time if the user is unlucky) and im not sure how to get
> around that or if it is even possible.
> also, if there is 2 identical words in the sentence, lets say "the" is
> in there twice, it will loop forever. i could do
> .uniq in the beginning but lets say i want both "the" to be in there.
> i have to write some if duplicate then ok up to nbrofduplicates. is
> there an easy way to do this?
>
> #scrambler
> puts "Enter a sentence: "
> sentence = gets
> list = sentence.split()
> scr = []
>
> def inList(aword,alist)
> inl=false
> for i in (0..alist.length()-1)
> if alist[i] == aword
> inl=true
> end
> end
> return inl
> end

Much easier:

def in_list?(aword, alist)
alist.include?(word)
end

which means you don't really need your own method; you can just call
include? on your list.

> for x in (0..list.length()-1)
> temp = list[rand(list.length())]
> while (inList(temp,scr))
> temp = list[rand(list.length())]
> end
> scr = scr + temp.split()
> end

Please lose the ()'s. They don't do anything or add any information;
they're just visual clutter. Meanwhile, see below....

> puts "Scrambled: "
> puts scr

In general, you're working much too hard :-) Let Ruby do it for you:

print "Enter a sentence: "
puts gets.split.sort_by { rand }.join(" ")


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

S2

5/8/2008 9:24:00 PM

0

globalrev wrote:

> i have written a program that takes a sentence as input and outputs
> the sentence with the words rearranged.
>

sentence = "this is a sentence with the word sentence repeaed"

sentence = sentence.split(' ')
scr = []
scri = []

while scri.length != sentence.length
i = rand(sentence.length)
scri << i unless scri.include? i
end

scri.each {|i| scr << sentence[i]}
p scr.join(' ')

S2

5/8/2008 9:25:00 PM

0

David A. Black wrote:

> print "Enter a sentence: "
> puts gets.split.sort_by { rand }.join(" ")

cool, i did not look at your solution before posting mine, this is a lot
nicer :)

S2

5/8/2008 9:30:00 PM

0

David A. Black wrote:

> print "Enter a sentence: "
> puts gets.split.sort_by { rand }.join(" ")

but does this take into account that rand could generate the same numer two
times? while that is not likely, it could happen.

skanemupp

5/8/2008 9:31:00 PM

0

On 8 Maj, 22:12, "David A. Black" <dbl...@rubypal.com> wrote:
> Hi --
>
>
>
> On Fri, 9 May 2008, globalrev wrote:
> > i have written a program that takes a sentence as input and outputs
> > the sentence with the words rearranged.
> > it is probably inefficient and can probably be done with less code.
> > it is also nondeterministic( while (inList(temp,scr)) could go on
> > for a long time if the user is unlucky) and im not sure how to get
> > around that or if it is even possible.
> > also, if there is 2 identical words in the sentence, lets say "the" is
> > in there twice, it will loop forever. i could do
> > .uniq in the beginning but lets say i want both "the" to be in there.
> > i have to write some if duplicate then ok up to nbrofduplicates. is
> > there an easy way to do this?
>
> > #scrambler
> > puts "Enter a sentence: "
> > sentence = gets
> > list = sentence.split()
> > scr = []
>
> > def inList(aword,alist)
> > inl=false
> > for i in (0..alist.length()-1)
> > if alist[i] == aword
> > inl=true
> > end
> > end
> > return inl
> > end
>
> Much easier:
>
> def in_list?(aword, alist)
> alist.include?(word)
> end
>
> which means you don't really need your own method; you can just call
> include? on your list.
>
> > for x in (0..list.length()-1)
> > temp = list[rand(list.length())]
> > while (inList(temp,scr))
> > temp = list[rand(list.length())]
> > end
> > scr = scr + temp.split()
> > end
>
> Please lose the ()'s. They don't do anything or add any information;
> they're just visual clutter. Meanwhile, see below....
>
> > puts "Scrambled: "
> > puts scr
>
> In general, you're working much too hard :-) Let Ruby do it for you:
>
> print "Enter a sentence: "
> puts gets.split.sort_by { rand }.join(" ")
>
> David
>
> --
> Rails training from David A. Black and Ruby Power and Light:
> INTRO TO RAILS June 9-12 Berlin
> ADVANCING WITH RAILS June 16-19 Berlin
> INTRO TO RAILS June 24-27 London (Skills Matter)
> Seehttp://www.ruby... details and updates!



rofl aamazing, ty very much :)

had a feeling i was complicating things.


does it work as well for building bigger applications(not just
webapps), like building an editor like emacs. is the language as
suitable for that?
i know executionspeed is the issue with ruby and python but thats not
really an issue and will be even less of one in the future i guess.

must be some tradeoff no?


anyway ty for the help, the rubycommunity seems very nice and helpful!

David A. Black

5/8/2008 9:51:00 PM

0

Hi --

On Fri, 9 May 2008, S2 wrote:

> David A. Black wrote:
>
>> print "Enter a sentence: "
>> puts gets.split.sort_by { rand }.join(" ")
>
> but does this take into account that rand could generate the same numer two
> times? while that is not likely, it could happen.

I'm by no means a random number expert/theorist, but the above is a
very common idiom. I don't really know how it holds up under full
scrutiny. There's probably some discussion of that in the archives.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

S2

5/8/2008 10:11:00 PM

0

David A. Black wrote:

> I'm by no means a random number expert/theorist, but the above is a
> very common idiom. I don't really know how it holds up under full
> scrutiny. There's probably some discussion of that in the archives.
>

even if rand would generate 2 times the same number that would not be a
problem...

7stud --

5/8/2008 10:17:00 PM

0

S2 wrote:
> David A. Black wrote:
>
>> print "Enter a sentence: "
>> puts gets.split.sort_by { rand }.join(" ")
>
> but does this take into account that rand could generate the same numer
> two
> times? while that is not likely, it could happen.

So what?

--
Posted via http://www.ruby-....

Rob Biedenharn

5/8/2008 10:19:00 PM

0



S2

5/8/2008 10:21:00 PM

0

7stud -- wrote:

> So what?

so nothing. i found that out 10 minutes ago :)