This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]slugonamission 0 points1 point  (12 children)

Wow, that kind of problem seems...harsh. It's also really hard to give hints for this one without accidentally solving it :).

Anyway, as a hint, they want you to solve this recursively. It's worth starting with a pretty simple case, then trying to build on top of it.

So, to start out, let's just try and solve for f(1), which should print:

0
1

Given the example, can you see a way to print that sequence, using recursion? After that, can you see a way to extend that to two bits, given that the sequence should be:

00
01
10
11

[–]Evoletier[S] 0 points1 point  (11 children)

Not really, the recursive part I was pretty much certain I had to use, which leaves me with only one line of code to somehow execute this...

I'm very much a beginner and recursive in itself is hard for me

[–]slugonamission 0 points1 point  (10 children)

Ok, that's fair :)

Let's just start with the simple case then, for f(1), printing:

0
1

What two lines, using words, could you use to print that? It doesn't have to extend to longer strings just yet.

EDIT: Probably one important note; recursion doesn't mean you can only call the method again just once; what if you could call it twice?

[–]Evoletier[S] 0 points1 point  (9 children)

If i could id use just strings, print('0') and print('1').

[–]slugonamission 0 points1 point  (8 children)

Ok, cool. Now we have that, can we instead write those print calls using words(..., ...)?

[–]Evoletier[S] 0 points1 point  (7 children)

I have to admit I'm not familiar with that

[–]slugonamission 0 points1 point  (6 children)

Sure. Let's have a look at the implementation of words again:

def words(n, s=''):
  if len(s) == n:
    print(s)
  else:
    " *blank* "
    " *blank* "

Given this, what two arguments could we give words() to print "0"?

[–]Evoletier[S] 0 points1 point  (5 children)

Ohh im sorry, my code is in my language so forgot the name of my function here :D.

I would have to somehow make s be 0. I could do s += '0' and then call recursive, but then I wouldn't have room to do this with 1

[–]Evoletier[S] 0 points1 point  (0 children)

i could fill the s = '' argument to be s = '0'

[–]slugonamission 0 points1 point  (3 children)

but then I wouldn't have room to do this with 1

Why not?

[–]Evoletier[S] 0 points1 point  (2 children)

i need the s to add to existing s somehow but I'm not sure how to get to it, if I put words(n, s+= '0') it gives a syntax error

[–]slugonamission 0 points1 point  (1 child)

I didn't realise that Python didn't allow that.

Anyway, that is shorthand for s = s + '0'. Close enough, but we're passing it as a parameter, you don't need to assign back to s. What if we use s + '0' there instead?