Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:58:19+00:00 2026-06-17T21:58:19+00:00

I have coded Rail Fence Cipher in Python. I was wondering if there could

  • 0

I have coded Rail Fence Cipher in Python. I was wondering if there could be a better solution.

For those who don’t know what rail fence cipher is, it is basically a method of writing plain text in a way it creates linear pattern in a spiral way.
Example – when “FOOBARBAZ” rail-fenced using key of 3.

F . . . A . . . Z . . .
. O . B . R . A . Q . X
. . O . . . B . . . U .

Reading the above spiral line-by-line, the cipher text becomes “FAZOBRAQXOBU”. Read more at – Rail fence – Wikipedia.

def cipher(s, key, graph=False) :
    down=True
    raw_out=[]
    out=''
    i=0
    for x in range(key) :
        raw_out.append({})
    for pos in range(len(s)) :
        raw_out[i][pos]=s[pos]
        if i==key-1 :
            down=False
        if i==0 :
            down=True
        if down :
            i=i+1
        else :
            i=i-1
    for p in raw_out :
        for q in p :
            out+=p[q]
    if graph :
        return raw_out
    return out

def decipher(s, key) :
    map_list=cipher(s, key, True) #CREATING JUST FOR MAPPING - WHICHth CHARACTER OF THE STRING - IS WHICHth CHARACTER OF THE CIPHER
    new={}
    out=''
    s_counter=0
    for x in map_list :
        for y in x :
            new[y]=s[s_counter]
            s_counter+=1
    for p in new :
        out+=new[p]
    return map_list

I was wondering if there was any better way of doing this, since my procedure is very costly, it a uses couple of dictionaries.

Code in any language is welcomed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:58:20+00:00Added an answer on June 17, 2026 at 9:58 pm

    Just for the heck of it…

    def fence(lst, numrails):
        fence = [[None] * len(lst) for n in range(numrails)]
        rails = range(numrails - 1) + range(numrails - 1, 0, -1)
        for n, x in enumerate(lst):
            fence[rails[n % len(rails)]][n] = x
    
        if 0: # debug
            for rail in fence:
                print ''.join('.' if c is None else str(c) for c in rail)
    
        return [c for rail in fence for c in rail if c is not None]
    
    def encode(text, n):
        return ''.join(fence(text, n))
    
    def decode(text, n):
        rng = range(len(text))
        pos = fence(rng, n)
        return ''.join(text[pos.index(n)] for n in rng)
    
    z = encode('ATTACK.AT.DAWN', 3)    
    print z # ACTWTAKA.ANT.D
    y = decode(z, 3)
    print y # ATTACK.AT.DAWN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have coded a simple Java game where there are two rectangles on the
I have coded a simple 2D Java game where there is a overhead view
I have coded a simple Java game where there are two rectangles on the
I have coded myself into a corner. Does anyone know the answer? Thanks!
I have coded the following codes,but there was a problem char p[20]; int n;
I have coded an animation (in python) for a beach ball to bounce around
I have coded a server in Java that will have several clients connected to
I have coded a background logging thread for my program, if a class needs
I have coded my own tiny static DAL class for .NET Compact Framework and
I have coded the next function. But surely someone has a more elegant way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.