ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
scramble sentence, how to better program?
by Globalrev other posts by this author
May 8 2008 1:05PM messages near this date
Re: wired result | Re: scramble sentence, how to better program?
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
Thread:
Globalrev
David A. Black
S2
S2
Globalrev
S2
7stud --
David A. Black
Rob Biedenharn
S2
S2

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved