div24ever's blog

By div24ever, history, 4 years ago, In English,

I solved TopBiologist by recursion. I want to know if there is any iterative approach? We will need to run loop 6 times to generate sequence of length 6. Is there any shorter way to do this?

My code

 
 
 
 
  • Vote: I like it
  • -6
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It's similar to converting numbers into the radix 4.

for num in [0 .. 4**length)
  s = ""
  x = num
  for i in [0 .. length)
    s += "ACGT"[x % 4]
    x /= 4
  // use s
  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks. Can we write i th element of an integer array like you wrote for string?

    You wrote s += "ACGT"[x % 4]

    I wrote this and got compilation error

    for(int i=0;i<4;i++) cout<<{1,2,3,4}[i];

»
3 years ago, # |
  Vote: I like it -8 Vote: I do not like it

I have found an interesting solution without recursion.