tags:

views:

137

answers:

3

Hi,

I have a scenario where i need to generate all possible keystrokes using numbers 2 to 9. The possible keystrokes should generate 2-git, 3-digit etc upto 32-digit numbers. can anybody tell me what is the best way to solve this problem.

Thanks, Pdit

A: 

You're looking at quite a bit of computation towards the higher end of that.

One possible way to do it would be to have an array as a place-holder for your digit sequence, increment either the highest or lowest index, check if it overflows and then "carry" it to the next index and do that until you overflow the poistion "at the other end". Start with the array filled with 2.

Vatine
+5  A: 

Start with some simple analysis to consider feasibility. One digit has 8 possible values. Two digits have 8 x 8. Etc. Now grab your calculator and compute 8 ^ 32.

Hans Passant
yeah, i know..but i need to do it even if it runs for days.. :(
http://www.google.com/search?q=8+%5E+32+%2A+nanoseconds+in+years
Pete Kirkham
@Pete Kirkham, cool, I didn't know that you can use google as an calculator.
quinmars
A: 

I'd take a simple recursive approach, here with characters, but you could build your numbers using other means as well..

in C:

#include <stdio.h>
void keystrokes(int x, int lo, int hi, char array[]) {
  int i;
  if (x > hi)
    return;
  if (x >= lo) {
    array[x] = 0;
    printf ("%s\n", array);
  }
  for (i = 2; i < 10; i++) {
    array[x] = i+'0';
    keystrokes (x+1, lo, hi, array);
  }
}
int main(void)
{
  char array[33];
  keystrokes(0, 2, 32, array);

  return 0;
}

It can be optimized a bit (for example, for all 32-digit numbers, it'll still make 10 extra recursive calls), but it'll still take forever to compute all possible combinations.

roe