- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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