We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defsqrt(l);Math.sqrtlenddefencryption(s)# strip off any whitespacess.gsub!/\s+/,''# length of the string without whitespaceslen=s.length# let us set the (rows, cols) based on floor & ceil of the string lengthrows,cols=sqrt(len).floor,sqrt(len).ceil# if the product of rows and cols is less than string length, set both to samerows=cols=sqrt(len).ceilifrows*cols<len# each index of this array will hold chars (c..row), where for each c,# we get all the values until rowencrypted=[](0...rows).eachdo|i|encrypted<<s[((i%rows)*cols)..((i%rows)*cols)+cols-1].charsend# we use the first entry and zip it with the rest of entries,# and join them to make a complete stringencrypted[0].zip(*encrypted[1..-1]).mapdo|cipher|cipher.compact.join+' 'end.joinend
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Encryption
You are viewing a single comment's thread. Return to all comments →
How about Ruby?