0

For the following I have to convert an int to a binary string. I have a few constraints, I can't change the first two lines and moreover, I have to return a binary representation for 10^a, where a is the input int. I tried the following, but it doesn't work the way it should. Also, the solution needs to be recursive. Please help someone.

{int} -> {str} return a binary string for 10^a

def converttobin(a: int) -> str:
    if a == 1:
        return "0b1010"
    else:
        return converttobin(10^a//2) + str(a % 2)
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 3
    Where does `n` come from? What is `p2b`? Sorry, this looks as if you just added some code lines you found somewhere. – Matthias Nov 03 '20 at 15:28
  • I'm really sorry about that, I have two functions todo and I mixed them up, really sorry about that – gofobrmglnlxfblacl Nov 03 '20 at 15:32
  • "but it doesn't work the way it should." How so? Does it produce an error? Does it produce unexpected output? – MisterMiyagi Nov 03 '20 at 15:39
  • Is this related to your [earlier, very similar question](https://stackoverflow.com/questions/64664022/convert-decimal-to-binary-python) which has since been deleted after getting answered? – MisterMiyagi Nov 03 '20 at 15:40
  • yes, it returns an unexpected output. The output that I get from this function is wrong. – gofobrmglnlxfblacl Nov 03 '20 at 15:42
  • 1
    *What* unexpected output do you get for *what* input and *what* output did you expect instead? – MisterMiyagi Nov 03 '20 at 15:43
  • yes it is related – gofobrmglnlxfblacl Nov 03 '20 at 15:43
  • for converttobin(3) I should get '0b1111101000', but the program returns '0b10101' instead. – gofobrmglnlxfblacl Nov 03 '20 at 15:47
  • 1
    The power operator is ``**``, not ``^``. – MisterMiyagi Nov 03 '20 at 15:48
  • like no matter what operator I would have used, I would have ended up at reaching the max recursion depth or the code wouldn't execute and end up in an endless loop – gofobrmglnlxfblacl Nov 03 '20 at 15:54
  • You might want to [edit] your question then to clarify what exactly you are asking about. Provide a [mcve], and make sure to clarify the restrictions of your task. Mind the [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) for this. Consider to restore your [previous question](https://stackoverflow.com/questions/64664022/convert-decimal-to-binary-python) and accept its answer if it solved your problem; it is not inviting for volunteers to help if their effort just gets erased. – MisterMiyagi Nov 03 '20 at 16:00
  • Please take a moment to read through the help center; specifically [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers), but the other help pages as well. If you feel a question is not worth people's time, do not ask it in the first place. Be aware that both the system *and the volunteers answering questions* will remember questions they have seen before. – MisterMiyagi Nov 03 '20 at 16:09

1 Answers1

0

Recursive - check, includes the first two lines - check. It is somewhat silly, but the constraints are as well...

def converttobin(a: int) -> str:
    if a == 1:
        return "0b1010"
    else:
        converttobin(1)
        return bin(10**a)