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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:31:05+00:00 2026-06-13T02:31:05+00:00

I am trying to write an algorithm to rearrange the letters in a string

  • 0

I am trying to write an algorithm to rearrange the letters in a string in order of frequency and then alphabetical order. For instance, “apple” becomes “aelpp”. “banana” becomes “bnnaaa”.

I know a variety of languages, but I am using Python for now to code it. This is what I have so far, but it does not work because there is no frequency sorting.

def order(word):
  word = word.lower()
  storage = [0] * 26
  for c in word:
    storage[ord(c) - 97] += 1
  newWord = []
  for l, c in enumerate(storage):
    for i in range(0, storage[l]):
      newWord.append(chr(l + 97))
  return ''.join(newWord)

Any advice on how to most efficiently implement this algorithm correctly?

  • 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-13T02:31:06+00:00Added an answer on June 13, 2026 at 2:31 am

    Here is a (mostly) pythonic example of how to deal with this problem in python,
    I hope the comments are sufficient to explain what is happening:

    words = ["apples", "banannas", "oranges"]
    
    def main():
        # our list of jumbled words
        jumbled = []
        for word in words:
            # dictionary of letter / frequency pairs.
            letters = {};
            # get letter frequency
            for letter in word:
                if letter in letters:
                    letters[letter] += 1
                else:
                    letters[letter] = 1
            # sort the letter / frequency pairs on descending frequency
            jumbled_word = sorted(letters.items(), key = lambda x: x[1],
                                                                  reverse = True)
            # join the letters back together and add to our jumbled words
            jumbled.append(''.join([x[0] for x in jumbled_word]))
            letters = {}
    
        # print out the jumbled words in alphabetical order
        for x in sorted(jumbled):
            print x
    
    if __name__=="__main__":
        main()
    

    this implementation will keep capital letters capital.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an algorithm to generate the ceiling panel from a horiontally
i am trying to write an algorithm for finding a sub matrix in a
I'm trying to write an algorithm that will let me iterate over all desired
I am trying to write an efficient algorithm that will effectively let me merge
I have been trying to write a shortest path algorithm, dijkstras algorithm, finding the
I am trying to write a small application using bouncycastle algorithm, from the BouncyCastleProvider.java
I'm trying to write the Diamond-Square algorithm in Java to generate a random map
I'm trying to write a free text search algorithm for finding specific posts on
I'm trying to write an algorithm for finding out the number of ways n
I'm trying to write a substitution algorithm in Haskell. I have defined a polymorphic

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.