 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to encrypt a string using Vertical Cipher in Python
Suppose we have a string s and a number n, we have to rearrange s into n rows so that s can be selected vertically (top to down, left to right).
So, if the input is like s = "ilovepythonprogramming" n = 5, then the output will be ['ipnrn', 'lypag', 'otrm', 'vhom', 'eogi']
To solve this, we will follow these steps −
- L := empty list
- for i in range 0 to n - 1:- insert a string by taking each nth character starting from i, and insert into L
 
- return L
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, s, n): return [s[i::n] for i in range(n)] ob = Solution() s = "ilovepythonprogramming" n = 5 print(ob.solve(s, n))
Input
"ilovepythonprogramming", 5
Output
['ipnrn', 'lypag', 'otrm', 'vhom', 'eogi']
Advertisements
 