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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:38:46+00:00 2026-05-10T23:38:46+00:00

Arising out of this question, I’m looking for an elegant (ruby) way to compute

  • 0

Arising out of this question, I’m looking for an elegant (ruby) way to compute the word signature suggested in this answer.

The idea suggested is to sort the letters in the word, and also run length encode repeated letters. So, for example ‘mississippi’ first becomes ‘iiiimppssss’, and then could be further shortened by encoding as ‘4impp4s’.

I’m relatively new to ruby and though I could hack something together, I’m sure this is a one liner for somebody with more experience of ruby. I’d be interested to see people’s approaches and improve my ruby knowledge.

edit: to clarify, performance of computing the signature doesn’t much matter for my application. I’m looking to compute the signature so I can store it with each word in a large database of words (450K words), then query for words which have the same signature (i.e. all anagrams of a given word, that are actual english words). Hence the focus on space. The ‘elegant’ part is just to satisfy my curiosity.

  • 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. 2026-05-10T23:38:47+00:00Added an answer on May 10, 2026 at 11:38 pm

    I’m not much of a Ruby person either, but as I noted on the other comment this seems to work for the algorithm described.

    s = 'mississippi' s.split('').sort.join.gsub(/(.)\1{2,}/) { |s| s.length.to_s + s[0,1] }  

    Of course, you’ll want to make sure the word is lowercase, doesn’t contain numbers, etc.

    As requested, I’ll try to explain the code. Please forgive me if I don’t get all of the Ruby or reg ex terminology correct, but here goes.

    I think the split/sort/join part is pretty straightforward. The interesting part for me starts at the call to gsub. This will replace a substring that matches the regular expression with the return value from the block that follows it. The reg ex finds any character and creates a backreference. That’s the ‘(.)’ part. Then, we continue the matching process using the backreference ‘\1’ that evaluates to whatever character was found by the first part of the match. We want that character to be found a minimum of two more times for a total minimum number of occurrences of three. This is done using the quantifier ‘{2,}’.

    If a match is found, the matching substring is then passed to the next block of code as an argument thanks to the ‘|s|’ part. Finally, we use the string equivalent of the matching substring’s length and append to it whatever character makes up that substring (they should all be the same) and return the concatenated value. The returned value replaces the original matching substring. The whole process continues until nothing is left to match since it’s a global substitution on the original string.

    I apologize if that’s confusing. As is often the case, it’s easier for me to visualize the solution than to explain it clearly.

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

Sidebar

Related Questions

No related questions found

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.