Question: Q 2 Groups of four bits [ 2 0 points ] A group of four bits is called a nibble. In this question your task

Q2 Groups of four bits [20 points]
A group of four bits is called a nibble. In this question your task is to write a function make_nibbles that takes a string containing bits (digits 0 & 1) and returns a new string where bits are grouped into nibbles separated by a space, adding extra zeros on left, if necessary, to make the string length a multiple of 4.
Example: "101100"->"00101100"
nibbles = make_nibbles("101100")
print(nibbles)
print(type(nibbles))
Output:
00101100
(Note that padding a binary number with zeros on left does not change its value. We are padding zeros just to make the length a multiple of 4 so that we can make groups of four bits. This makes reading a long binary number a bit easier.)
Logic
The function make_nibbles should do the following:
First add extra zeros on the left of the argument string bits to create a padded_string whose length is a multiple of 4. Hint: you can use len() function and remainder operator % to figure out how many zeros should be added. For example, for "11011",3 zeros are need to make its length 8: "00011011" but for "1010111" only 1 zero is needed: "01010111".
Build a new string result using a loop basically copy each bit from padded_string to result but after every 4th bit, add a space to result string. Hint: you can use remainder operator % here as well for the "every 4th bit" part of the logic.
Return the result string
More examples
print(make_nibbles("11011")) # 00011011
print(make_nibbles("1101001011")) # 001101001011
# No zero padding needed as length is already a multiple of 4:
print(make_nibbles("1101")) # 1101
Assume that the length of argument string will be at least one i.e. it won't be an empty string. But there is no upper limit on the length.
It is okay if there are extra spaces at beginning or end of the result string e.g."00110100" or "00110100" is fine. But extra spaces or incorrect spaces between bits are not acceptable e.g."00110100" or "001101001" are incorrect.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!