• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Stuck on a Problem

 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm stuck on a problem. Can anyone please give a hint of how to solve it or please solve this problem. I want to take an input(N) from user and produce the output as follows:

And 0 < N < 101

• For N = 1:

"a", "b", ..., "z".


• For N = 2:

"a", "b", ..., "z",


"aa", "ab", ..., "az",
"ba", "bb", ..., "bz",
.
.
.
"za", "zb", ...,"zz"


• For N = 3:

"a", "b", ..., "z",


"aa", "ab", ..., "az",
"ba", "bb", ..., "bz",
.
.
.
"za", "zb", ...,"zz"


"aaa", "aab", ..., "aaz",
"aba", "abb", ..., "abz"
.
.
.
"baa", "bab", ..., "baz", ...,
.
.
"zaz", "zbz", ..., "zzz".

Since the answer can be quite large I should output it as modulo 1000000007 (10E9 + 7).

How will I loop through these conditions?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Khattri wrote:I'm stuck on a problem. Can anyone please give a hint of how to solve it or please solve this problem. I want to take an input(N) from user and produce the output as follows:

And 0 < N < 101

• For N = 3:

"a", "b", ..., "z",


"aa", "ab", ..., "az",
"ba", "bb", ..., "bz",
..
.
"zaz", "zbz", ..., "zzz".

Since the answer can be quite large I should output it as modulo 1000000007 (10E9 + 7).

How will I loop through these conditions?




Well, if N was fixed, the easy answer would be to use nested loops -- meaning for N=3, you will need a loop within a loop within a loop. However, since N is not fixed, then using recursion is probably the best option -- meaning to recursively do the next inner loop until N is reached.

Henry
 
Sidharth Khattri
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry, I'm having a hard time thinking of solving the problem recursively. I solved it using iteration at first but the input(N) has to be taken from the user. Can you help?
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sidharth Khattri wrote:Hi Henry, I'm having a hard time thinking of solving the problem recursively. I solved it using iteration at first but the input(N) has to be taken from the user. Can you help?



Of course, that is what the ranch is for. Let us know what issues you are having. And what have you done so far. And we can give you some pointers in the right direction.


A word of warning though. How comfortable are you with recursion? Recursion is something that is difficult to teach, even in a classroom setting -- so this will work better if you are already comfortable with it.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic