Skip to content

Conversation

Copy link

Copilot AI commented Dec 24, 2025

Performance improvements for cipher functions that become significant with larger inputs.

Changes

  • Caesar cipher: Replace string concatenation (result += char) with list append + join pattern, reducing O(n²) to O(n)
  • Vigenère cipher: Fix keyword index tracking to only advance on alphabetic characters (was incorrectly advancing on spaces/punctuation)
  • Add .gitignore for Python bytecode

Before/After

# Caesar - O(n²) string concat
def caesar_cipher(data, shift):
    result = ""
    for char in data:
        ...
        result += char  # Creates new string each iteration
    return result

# Caesar - O(n) list join
def caesar_cipher(data, shift):
    result = []
    for char in data:
        ...
        result.append(char)
    return ''.join(result)
# Vigenère - keyword index bug
for i, char in enumerate(data):
    shift = ord(keyword[i % k_len]) - ord('a')  # Advances for non-alpha chars

# Vigenère - correct keyword tracking
k_idx = 0
for char in data:
    if char.isalpha():
        shift = ord(keyword[k_idx % k_len]) - ord('a')
        k_idx += 1  # Only advance for letters

Benchmarks show 5-22% speedup on the Caesar cipher depending on input size.

Original prompt

Identify and suggest improvements to slow or inefficient code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 24, 2025 14:19
- Caesar cipher: Changed from O(n²) string concatenation to O(n) list join pattern
- Vigenère cipher: Fixed keyword index tracking to properly handle non-alphabetic characters

Co-authored-by: prashanthyadavv <177535072+prashanthyadavv@users.noreply.github.com>
Co-authored-by: prashanthyadavv <177535072+prashanthyadavv@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements to slow code Optimize cipher functions: fix O(n²) string concat and Vigenère keyword indexing Dec 24, 2025
@prashanthyadavv prashanthyadavv marked this pull request as ready for review December 24, 2025 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants