Unary - puzzle discussion

Continuing the discussion from Chuck Norris puzzle discussion:

Hello coders, I am in a bit of a pickle and need some help. I am using python to solve this problem so far I was able to convert the string of characters into 8-bit binary notation using this code

def encodeMessage(message:str):
    int_binaryFormat =  int.from_bytes(message.encode(),"little")

    binaryFormat = bin(int_binaryFormat)

    #binaryFormat = 0b+'$binaryString' So next steb removes substrin 0b

    binaryFormat =binaryFormat[2:]

the problem is well it has to be converted to 7-bit. for the rest of the problem I created a simple automaton that takes each character then creates an output based on the order. So I was able to pass the first test which is great but the rest fail since their is an extra zero cuz its 8bits. Any suggestions would be appreciated.

LongBeard, Try to work not with the entire string, but with each character separately.

1 Like

Many options here, you can use:

bin(nb)[2:].zfill(7)

a more generic option thatā€™s good to know:

bin(nb)[2:].rjust(7, "0")

and since f strings were introduced thereā€™s a simpler way now:

f"{nb:07b}"
2 Likes

Thatā€™s perfect! Thanks!

Hi! I am having issue with the Test case 3 where it expect a very different result from the actual binary result. Is this a bug or am I missing something.

Here is my console output result :

Standard Output Stream:

%

0100101

00 0 0 00 00 0 0 00 0 0

Here is the app result :

Failure

Found:

00 0 0 00 00 0 0 00 0 0

Expected:

00 0 0 0 00 00 0 0 00 0 0 0

The error is in your code ā€¦
The correct encoding is
0 => 00 0, 1 => 0 0, 00 => 00 00

This is clearly not compatbile with your answer

1 Like

Hello,
Would you help me please i have an issue in the last test ā€œMessage de Chuck Norrisā€.
I canā€™t find where is the problem in my code.

Please , i will share with you my solution (c language) to find the problem.

Here is the result

Sortie standard :

0 0 00 0000 0 0000 00 0 0 0 00 000 0 000 00 0 0 0 00 0 0 000 00 000 0 0000 00 0 0 0 00 0 0 0000 00 00000 0 0 00 00 0 000 00 0 0 00 00 0 0 0000000 00 00 0 0 00 0 0 000 00 00 0 0 00 0 0 00 00 0 0 0 00 00 0 0000 00 00 0 0000 00 00 0 00000 00 00000 0 00 00 0 0 0 00 0 0 0000 00 00 0 0 00 0 0 00000 00 00 0 000 00 000 0 0 00 0 0 00 00 0 0 000000 00 0000 0 0000 00 00 0 0 00 0 0 00 00 00 0 0 00 00 0 00 00 00000 0 00 00 0 0 0 00 000 0 00 00 0000 0 0000 00 00 0 0000 00 00000 0 000 00 00 0 0 00 0 0 00 00 00000 0 00 00 0 0 0 00 0 0 0000 00 00 0 0 00 0 0 00000 00 00 0 0000 00 00 0 000000 00 0 0 0 00 0 0 00 00 00000 0 000 00 0000 0 00 00 00000 0 00 00 0000 0 000 00 0 0 000 00 0 0 00 00 00 0 0 00 00 0 00 00 00000 0 000 00 0 0 00000 00 0 0 0 00 000 0 00 00 0 0 0 00 00 0 0000 00 0 0 0 00 00 0 00 00 00 0 0 00 0 0 000 00 00000 0 000 00 00 0 00000 00 0000 0 00 00 0000 0 000 00 000 0 0000 00 00 0 0 00 0 0 000 00 0 0 000 00 0

Ɖchec

TrouvƩ :

0 0 00 0000 0 0000 00 0 0 0 00 000 0 000 00 0 0 0 00 0 0 000 00 000 0 0000 00 0 0 0 00 0 0 000ā€¦

Attendu :

0 0 00 0000 0 0000 00 0 0 0 00 000 0 000 00 0 0 0 00 0 0 000 00 000 0 0000 00 0 0 0 00 0 0 00 ā€¦

ā€œspaceā€ exists ā€¦ as wrtten in test ā€¦
but your code is not correct on it

2 Likes

I found the problem thank you :smiley:

indeed, good point!

THANKS!!! Did exactly the sameā€¦ didnā€™t find it myself as it was working correctly while debugging using a single space for exactly that reason :smiley:

Hey @MedAyoub I am having the same issue but not able to understand what the problem is. Can you please shade some light on it? Thanks!

hi @push44 ,
You have to check your function that convert the char to tab of binary.
The purpose of these puzzles is to find the solution in your own, take your time in it, that helps you to improve your coding skill and reflexion.
If not you can send to me your code to verfiy with you .
regards

1 Like

hi @MedAyoub for quick reply. I am using format(ord(token), ā€˜07bā€™) for converting characters into binary values. On individual level character conversion is perfect and my code is working fine with other three cases. So I am confused about what could be the problem?

@push44 I think you miss somthing , I use C and C++ so canā€™t help you , sorry

Are you failing the 4th test case? If so, you may consider checking whether your code handles spaces and special characters correctly.

No problem and thanks for offering the help.

Yes, I was confused because code is handling special characters correctly. The problem was with spaces which I realized later from your comment. Problem is solved now. Thanks!

1 Like

Salut ! Jā€™utilise ce code mais Ƨa coince au niveau de la conversion en unaire :confused: je sais que il y a un problĆØme avec mes espaces que jā€™ai pas encore corrigĆ© mais il y a un autre problĆØme, la fin de la chaine binaire est pas traduite jā€™ai lā€™impression cā€™est assez bizarre :frowning:

    import sys
import math
compteur0 = 0
compteur1 = 0
reponse = ""
message = input()
binary_converted = ' '.join(format(ord(c), 'b') for c in message)
print (binary_converted)
for c in binary_converted:
    if c == "0":
        compteur0 = compteur0 +1
        if compteur1 > 0:
            reponse = reponse + " 0 "
            while compteur1 > 0:
                compteur1 = compteur1 - 1
                reponse = reponse + "0"
    if c == "1":
        compteur1 = compteur1 +1
        if compteur0 > 0:
            reponse = reponse + " 00 "
            while compteur0 > 0:
                compteur0 = compteur0 - 1
                reponse = reponse + "0"
print (reponse)

Attention, lā€™ASCII doit ĆŖtre ajustĆ© Ć  7 bits !