Skip to content

Instantly share code, notes, and snippets.

@mbklein
Created December 12, 2011 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbklein/1468557 to your computer and use it in GitHub Desktop.
Save mbklein/1468557 to your computer and use it in GitHub Desktop.
Randomize string contents while retaining the flow
def randomize(str)
vowels = ['a','e','i','o','u']
consonants = ('a'..'z').to_a - vowels
digits = ('0'..'9').to_a
replacements = {}
[vowels, consonants, digits].each { |set|
order = set.sort { rand(2) == 0 ? -1 : 1 }
set.each_with_index { |char,i| replacements[char] = order[i] }
}
tokens = str.split(/\b/)
tokens.collect { |token|
key = token.downcase
result = key.gsub(/(.)/) { |c| replacements[c] || c }
(0..result.length-1).to_a.collect { |i| token[i..i] =~ /[A-Z]/ ? result[i..i].upcase : result[i..i].downcase }.join('')
}.join('')
end
@mbklein
Copy link
Author

mbklein commented Dec 13, 2011

Before:

The NASA Space Shuttle Challenger disaster occurred on January 28, 1986 when Space Shuttle Challenger broke apart 73 seconds into its flight, leading to the deaths of its seven crew members. Disintegration of the entire vehicle began after an O-ring seal in its right solid rocket booster failed at liftoff. The disaster resulted in the formation of the Rogers Commission, a special commission appointed by United States President Ronald Reagan to investigate the accident. The Presidential Commission found that NASA's organizational culture and decision-making processes had been a key contributing factor to the accident. NASA managers had known that contractor Morton Thiokol's design of the solid rocket boosters contained a potentially catastrophic flaw in the O-rings, but they failed to address it properly. They also disregarded warnings from engineers about the dangers of launching posed by the low temperatures of that morning.

After:

Zha GIWI Wmika Whozzta Khittagjaf xewiwzaf ukkoffax ug Yigoifl 35, 0257 qhag Wmika Whozzta Khittagjaf dfuna imifz 46 wakugxw egzu ezw vtejhz, taixegj zu zha xaizhw uv ezw warag kfaq babdafw. Xewegzajfizeug uv zha agzefa rahekta dajig ivzaf ig U-fegj wait eg ezw fejhz wutex fuknaz duuwzaf vietax iz tevzuvv. Zha xewiwzaf fawotzax eg zha vufbizeug uv zha Fujafw Kubbewweug, i wmakeit kubbewweug immuegzax dl Ogezax Wzizaw Mfawexagz Fugitx Faijig zu egrawzejiza zha ikkexagz. Zha Mfawexagzeit Kubbewweug vuogx zhiz GIWI'w ufjigecizeugit kotzofa igx xakeweug-binegj mfukawwaw hix daag i nal kugzfedozegj vikzuf zu zha ikkexagz. GIWI bigijafw hix nguqg zhiz kugzfikzuf Bufzug Zheunut'w xawejg uv zha wutex fuknaz duuwzafw kugziegax i muzagzeittl kiziwzfumhek vtiq eg zha U-fegjw, doz zhal vietax zu ixxfaww ez mfumaftl. Zhal itwu xewfajifxax qifgegjw vfub agjegaafw iduoz zha xigjafw uv tiogkhegj muwax dl zha tuq zabmafizofaw uv zhiz bufgegj.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment