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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:19:41+00:00 2026-05-26T14:19:41+00:00

Assume I have a number with 7 digits. I need a function like this

  • 0

Assume I have a number with 7 digits.
I need a function like this

List<int> GetVariations(int input, int count)

which returns list of all numbers that can change to input with exact number of digit changes equals to count;

for example ( the example is in 2 digit due to simplicity):

GetVariations(20, 1) should return {00,10,30,40,50,60,70,80,90,21,22,23,24,25,26,27,28,29}
GetVariations(20, 2) should return {01,…,18,19,31,32,…,98,99}

This is not a homework and I’ve already implemented this by making all numbers and compare each of them with input and get number of changes but this approach has performance issue in numbers with bigger digits.

  • 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-26T14:19:42+00:00Added an answer on May 26, 2026 at 2:19 pm

    This looks unrelated to numbers to me. You have a string and you want to create variations with a limited http://en.wikipedia.org/wiki/Hamming_distance.

    It’s relatively nice to implement this recursively:

    IEnumerable<string> GetVariations(string input, int limit,char[] characters)
    {
      if(limit==0)
      {
        yield return input;
        yield break;
      }
      if(limit>input.Length)//Disallows fewer than `limit` changes.
      {
        yield break;
      }
      string remainder=input.SubString(1);
      foreach(char c in characters)
      {
        int remainingLimit;
        if(c==input[0])
          remainingLimit=limit;
        else
          remainingLimit=limit-1;
        foreach(string variedRemainder in GetVariations(remainder,remainingLimit,characters)
          yield return c+variedRemainder;
      }
    }
    
    List<int> GetVariations(int input, int count)
    {
      return GetVariations(input.ToString(),count,new char[]{'0',...,'9'}).Select(int.Parse).ToList();
    }
    

    (I didn’t test the code, so it probably contains a few minor bugs)

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

Sidebar

Related Questions

Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
Assume I have a list of words, and I want to find the number
Lets assume i have a bill number that has 12 numbers: 823 45678912 My
Assume you have some objects which have several fields they can be compared by:
I have a user table structured like this: id MEDIUMINT(7), username VARCHAR(15) Would it
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i <
Assume I have the following function: // Precondition: foo is '0' or 'MAGIC_NUMBER_4711' //
Assume I have some utility that can take a number of options, each followed
Assume you have n users and know that n grows by a fixed number

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.