Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to encrypt a string using Vertical Cipher in Python
The Vertical Cipher is an encryption technique that rearranges a string by distributing characters into columns and reading them vertically. Given a string and number of rows, we arrange the string into a grid and extract each column to form encrypted segments.
For example, with string "ilovepythonprogramming" and n = 5 rows, we arrange characters into 5 rows and read each column from top to bottom, left to right.
How Vertical Cipher Works
The algorithm distributes characters across n rows in a round-robin fashion ?
Implementation Using List Comprehension
We use Python's slicing notation s[i::n] to extract every nth character starting from position i ?
def vertical_cipher(s, n):
return [s[i::n] for i in range(n)]
# Test the function
text = "ilovepythonprogramming"
rows = 5
result = vertical_cipher(text, rows)
print(f"Original text: {text}")
print(f"Number of rows: {rows}")
print(f"Encrypted segments: {result}")
Original text: ilovepythonprogramming Number of rows: 5 Encrypted segments: ['ipnrn', 'lypag', 'otrm', 'vhom', 'eogi']
Step-by-Step Process
Let's trace through the algorithm to understand how it works ?
def vertical_cipher_detailed(s, n):
print(f"Input string: '{s}'")
print(f"Number of rows: {n}")
print("\nExtracting characters for each row:")
result = []
for i in range(n):
segment = s[i::n]
print(f"Row {i}: s[{i}::{n}] = '{segment}'")
result.append(segment)
return result
# Demonstrate the process
text = "ilovepythonprogramming"
encrypted = vertical_cipher_detailed(text, 5)
print(f"\nFinal result: {encrypted}")
Input string: 'ilovepythonprogramming' Number of rows: 5 Extracting characters for each row: Row 0: s[0::5] = 'ipnrn' Row 1: s[1::5] = 'lypag' Row 2: s[2::5] = 'otrm' Row 3: s[3::5] = 'vhom' Row 4: s[4::5] = 'eogi' Final result: ['ipnrn', 'lypag', 'otrm', 'vhom', 'eogi']
Alternative Implementation
Using a more explicit approach with loops ?
def vertical_cipher_loops(s, n):
segments = [''] * n
for i, char in enumerate(s):
row = i % n
segments[row] += char
return segments
# Test both approaches
text = "hello world"
rows = 3
method1 = vertical_cipher(text, rows)
method2 = vertical_cipher_loops(text, rows)
print(f"Text: '{text}' with {rows} rows")
print(f"List comprehension: {method1}")
print(f"Loop method: {method2}")
print(f"Results match: {method1 == method2}")
Text: 'hello world' with 3 rows List comprehension: ['hlwr', 'eool', 'l d'] Loop method: ['hlwr', 'eool', 'l d'] Results match: True
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| List Comprehension | O(len(s)) | O(len(s)) | High (concise) |
| Loop Method | O(len(s)) | O(len(s)) | Medium (explicit) |
Conclusion
The vertical cipher encrypts text by arranging characters in rows and reading columns vertically. Python's slicing notation s[i::n] provides an elegant solution for extracting every nth character starting from position i.
