Forum: Ruby [QUIZ] Text Munger (#76)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E2148159cc245f2335bb9f333c5ce449?d=identicon&s=25 Alex Barrett (Guest)
on 2006-04-22 18:46
(Received via mailing list)
Might as well send it in seperately:

puts gets.gsub(/\B[a-z]+\B/i){|s|l=s.length
;l.times{|i|r=rand(l);s[i],s[r]=s[r],s[i]};s}

88 characters in total.
E2148159cc245f2335bb9f333c5ce449?d=identicon&s=25 Alex Barrett (trib4lmaniac)
on 2006-04-22 18:53
Alex Barrett wrote:
> Might as well send it in seperately:
>
> puts gets.gsub(/\B[a-z]+\B/i){|s|l=s.length
> ;l.times{|i|r=rand(l);s[i],s[r]=s[r],s[i]};s}
>
> 88 characters in total.

Hellfire and damnations. Just read the rules. I didn't realize about the
spoiler period. Sorry guys.
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-04-22 19:01
(Received via mailing list)
On Apr 22, 2006, at 18:45, Alex Barrett wrote:

> Might as well send it in seperately:
>
> puts gets.gsub(/\B[a-z]+\B/i){|s|l=s.length
> ;l.times{|i|r=rand(l);s[i],s[r]=s[r],s[i]};s}

There was some golf in ruby-talk when that study was mentioned in
Slashdot back in 2003, it was an intensive day :-) and I blogged some
solutions:

     http://www.advogato.org/person/fxn/diary.html?start=238

The Perl one-liner is just a start, I am sure a Perl golf tournament
would squeeze that one quite a lot.

-- fxn
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-04-22 19:01
(Received via mailing list)
On Apr 22, 2006, at 18:53, Alex Barrett wrote:

> spoiler period. Sorry guys.
Argh, I assumed it was over after reading that email, I am very sorry.

-- fxn
E2148159cc245f2335bb9f333c5ce449?d=identicon&s=25 Alex Barrett (trib4lmaniac)
on 2006-04-22 19:18
Xavier Noria wrote:
> On Apr 22, 2006, at 18:53, Alex Barrett wrote:
>
>> spoiler period. Sorry guys.
> Argh, I assumed it was over after reading that email, I am very sorry.
>
> -- fxn

Looks I've caused trouble :/
Nice touch with the rand sort though. And the $&!
E2148159cc245f2335bb9f333c5ce449?d=identicon&s=25 Alex Barrett (trib4lmaniac)
on 2006-04-22 19:25
Xavier Noria wrote:
> The Perl one-liner is just a start, I am sure a Perl golf tournament
> would squeeze that one quite a lot.
I can take off 3. "sort_by" can simply be "sort" as Array overwrites
Enumberable's implementation.
E2148159cc245f2335bb9f333c5ce449?d=identicon&s=25 Alex Barrett (trib4lmaniac)
on 2006-04-22 19:49
Alex Barrett wrote:
> I can take off 3.

But now, sadly, I have to put them back on :/

rand on it's own doesn't scramble the letters properly. You get the same
gibberish every time. rand(9) fixes this.
E2148159cc245f2335bb9f333c5ce449?d=identicon&s=25 Alex Barrett (trib4lmaniac)
on 2006-04-22 19:53
Alex Barrett wrote:
> rand(9) fixes this.

But not 100% satisfactorily. I'll stick with my first version :)
0aca516785b12ea4ffd4fec43d69f586?d=identicon&s=25 Albert Vernon Smith (Guest)
on 2006-04-23 14:47
(Received via mailing list)
Here is my solution.  I didn't focus on trying to make a one-liner,
as others have, but made a couple of methods (shuffle and swap) for
Array which I might find useful in another context.

-albert


'scramble.rb':
==

class Array
   def shuffle
     # Fisher Yates shuffling of an array
     self.length.step(1,-1) do |i|
       j = rand(i)
       next if j == i - 1
       self.swap!(j,i-1)
     end
     self
   end

   def swap!(a,b)
       # Swap array elements in place.
       self[a], self[b] = self[b], self[a]
       self
   end
end

if ARGV[0] == nil
    abort("Usage: scramble.rb file")
else
    file = ARGV[0]
end

f = File.open(file)
while line = f.gets
   # Using a regex, shuffle all alphabetic characters which are
'internal' within a word.
   line.gsub!(/([A-z])([A-z]{2,})(?=[A-z])/) {$1 + $2.split
(//).shuffle.join}
   puts(line)
end
This topic is locked and can not be replied to.