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 3496388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:14:32+00:00 2026-05-18T12:14:32+00:00

I’ve had a look at similar topics, but no solution I can find exactly

  • 0

I’ve had a look at similar topics, but no solution I can find exactly compares to what I’m trying to achieve.

I have a cipher text that needs to undergo a simple letter substitution based on the frequency of each letter’s occurrence in the text. I already have a function to normalise the text (lowercase, no none-letter characters, no , count letter occurrences and then get the relative frequency of each letter. The letter is the key in a dictionary, and the frequency is the value.

I also have the expected letter frequencies for A-Z in a separate dictionary (k=letter, v=frequency), but i’m a bit befuddled by what to do next.

What I think I need to do is to take the normalised cipher text, the expected letter freq dict [d1] and the cipher letter freq dict [d2] and iterate over them as follows (part psuedocode):

for word in text:
    for item in word:
        for k,v in d2.items():
            if d2[v] == d1[v]:
                replace any instance of d2[k] with d1[k] in text
    decoded_text=open('decoded_text.txt', 'w')
    decoded_text.write(str('the decoded text')

Here, I want to take text and say “if the value in d2 matches a value in d1, replace any instance of d2[k] with d1[k] in text”.

I realise i must have made a fair few basic python logic errors there (I’m relatively new at Python), but am I on the right track?

Thanks in advance

Update:

Thank you for all the helpful suggestions. I decided to try Karl Knechtel’s method, with a few alterations to fit in my code. However, i’m still having problems (entirely in my implementation)

I have made a decode function to take the ciphertext file in question. This calls the count function previously made, which returns a dictionary (letter:frequency as a float). This meant that the “make uppercase version” code wouldn’t work, as k and v didn’t were floats and couldn’t take .upper as an attribute. So, calling this decode function returns the ciphertext letter frequencies, and then the ciphertext itself, still encoded.

def sorted_histogram(a_dict):
    return [x[1] for x in sorted(a_dict.items(), key=itemgetter(1))]

def decode(filename):
    text=open(filename).read()
    cipher=text.lower()

    cipher_dict=count(filename)

    english_histogram = sorted_histogram(english_dict)
    cipher_histogram = sorted_histogram(cipher_dict)

    mapping = dict(zip(english_histogram, cipher_histogram)

    translated = ''.join(
    mapping.get(c, c)
    for c in cipher
    )
    return translated
  • 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-05-18T12:14:33+00:00Added an answer on May 18, 2026 at 12:14 pm

    You don’t really want to do what you’re thinking of doing, because the frequencies of characters in the sample won’t, in general, match the exact frequency distribution in the reference data. What you’re really trying to do is find the most common character and replace it with ‘e’, the next most and replace it with ‘t’, and so on.

    So what we’re going to do is the following:

    1. (I assume you can already do this part) Construct a dictionary of actual letter frequency in the ciphertext.

    2. We define a function that takes a {letter: frequency} dictionary and produces a list of the letters in order of frequency.

    3. We get the letters, in order of frequency, in our reference (i.e., now we have an ordered list of the most common letters in English), and in the sample (similarly).

    4. On the assumption that the most common letter in the sample corresponds to the most common letter in English, and so on: we create a new dictionary that maps letters from the first list into letters from the second list. (We could also create a translation table for use with str.translate.) We’ll make uppercase and lowercase versions of the same dictionary (I’ll assume your original dictionaries have only lowercase) and merge them together.

    5. We use this mapping to translate the cipher text, leaving other characters (spaces, punctuation, etc.) alone.

    Thus:

    # 2.
    import operator
    def sorted_histogram(a_dict):
      return [
        x[1] # the value
        for x in sorted(a_dict.items(), key=operator.itemgetter(1))
        # of each dict item, sorted by value (i.e. the [1] element of each item).
      ]
    
    # 3.
    english_histogram = sorted_histogram(english_dict)
    cipher_histogram = sorted_histogram(cipher_dict)
    
    # 4.
    # Make the lowercase version
    mapping = dict(zip(english_histogram, cipher_histogram))
    # Make the uppercase version, and merge it in at the same time.
    mapping.update(dict(
      (k.upper(), v.upper()) for (k, v) in zip(english_histogram, cipher_histogram)
    ))
    
    # 5.
    translated = ''.join( # make this list of characters, and string them together:
      mapping.get(c, c) # the mapped result, if possible; otherwise the original
      for c in cipher
    )
    
    # 6. Do whatever you want with 'translated' - write to file, etc.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.