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.
- Prepare
- Algorithms
- Implementation
- Encryption
- Discussions
Encryption
Encryption
+ 1 comment "Life is a Race. And we all are racist."
s2='' for i in s: if(i!=' '): s2+=i l=len(s2) root=l**(0.5) p=math.ceil(root) k=math.floor(root) innerlist=[] mainlist=[] ans='' for i in range(len(s2)): if(((i+1)%p)==0): innerlist.append(s2[i]) mainlist.append(innerlist) innerlist=[] else: innerlist.append(s2[i]) if(innerlist): mainlist.append(innerlist) for i in range(len(mainlist[0])): for j in range(len(mainlist)): if(i>=len(mainlist[j])): continue ans+=mainlist[j][i] ans+=' ' return ans
+ 0 comments Simple pypy3 solution using itertools lib
Row / Column tweak logic:
Comparison: L ^ 0.5 <= row <= col <= L ^ 0.5
- In the case where L is not a perfect square num, you would increase col by 1
- After incresement, if row x col is smaller than L, just increase row by 1 as well.
- Then you should get row, col pair based on challenge's rules.
zip_longest():
from itertools import zip_longest # Use it to combine 2 iterables: wordlist = ['apple', 'grape', 'rank'] print(*zip_longest(*wordlist, fillvalue = '_')) # Output: ('a', 'g', 'r') ('p', 'r', 'a') ('p', 'a', 'n') ('l', 'p', 'k') ('e', 'e', '_')
import math from itertools import zip_longest def encryption(s): # Write your code here # Remove spaces and calculate the length new_s = "".join(s.split(" ")) l = len(new_s) row = col = int(math.sqrt(l)) if not math.sqrt(l).is_integer(): col = row + 1 if row * col < l: row = col grid = [new_s[i: i + col] for i in range(0, len(new_s), col)] new_grid = zip_longest(*grid, fillvalue="") encrypted = " ".join("".join(items) for items in new_grid) return encrypted
+ 0 comments function encryption(s: string): string { const newS = s.replace(/ /g, ""); let result = ""; let rows = Math.floor(Math.sqrt(newS.length)); let columns = Math.ceil(Math.sqrt(newS.length)); if (rows * columns < newS.length) { rows = Math.max(rows, columns); columns = Math.max(rows, columns); } for (let i = 0; i < columns; i++) { for (let j = 0; j < rows; j++) { if (i + columns * j < newS.length) { result += newS[i + columns * j]; } } result += " "; } return result; }
+ 0 comments python
def encryption(s): p=''.join(s.split(' ')) a=list(map(list,p)) f='' col=math.ceil(math.sqrt(len(a))) for d in range(col): for j in range(d,len(a),col): f+=p[j] f+=' ' return f
+ 0 comments Java solution
public static String encryption(String s) throws IOException { String output = ""; //empty string if (s.isEmpty()) return output; //Remove spaces s = s.replaceAll("\\s+", ""); //Calculate square root of message length double sqroot = Math.sqrt(s.length()); int cols = (int) Math.ceil(sqroot); int rows = (int) Math.floor(sqroot); if(cols*rows < s.length()) rows++; String subtmp; int reminder ; List<String> finList = new ArrayList<>(); //iterate over string for (int i = 0; i < s.length(); i ++) { reminder = i % cols; if(finList.size()==cols) { subtmp = finList.get(reminder); subtmp=subtmp.concat(String.valueOf(s.charAt(i))); finList.set(reminder, subtmp); }else { finList.add(reminder, String.valueOf(s.charAt(i))); } } for (String st: finList) { output += st+" "; } return output.trim(); }
Load more conversations
Sort 1391 Discussions, By:
Please Login in order to post a comment