|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# Q1
---
## Learning objective
* Experience that the same problem can be solved by iteration and recursion
* Practice using recursion to solve a problem
---
## Instructions
We revisit the problem set 2 Q2 for which we convert an integer from the decimal representation to its 32-bit binary representation.
1. In [src/binary.py](src/binary.py), define:
* A function `to_32_bit_binary()` to take an `int` (in the decimal representation, >= 0 and <= 4294967295) as the argument and return the corresponding 32-bit binary representation (`str` with length 32)
* Another function `to_decimal()` to take a 32-bit binary representation (`str` with length 32) as the argument and returns the corresponding decimal representation (`int`)
For both functions, you _must_ use recursion. No `for` loops or `while` loops are allowed.
**Note**
* Unlike problem set Q2 which requires the program to ask users for input and printout the results, here you are required to define the function `to_32_bit_binary()` that takes an `int` as an _argument_ and _returns_ a _str_
* You should not use `input()` or `print()` in your answer
* If you are not sure how to write a function definition that takes "arguments" and "returns" some value, please have a look at the problem set 3 solution when it is available
* Remember to include an _abstract_ function docstring inside the function definition
2. Ensure your function works as intended by calling the functions with different arguments (like the ones in the `Examples` section). Please note down at least 3 function calls that you have tried for each function, their expected return values, and the actual return values. You should consider different cases, for example, when the value is very big, very small, etc.
* For `to_32_bit_binary()`:
* Function calls that you have tried: _________________
* Expected return values: _________________
* Actual return values: _________________
* For `to_decimal()`:
* Function calls that you have tried: _________________
* Expected return values: _________________
* Actual return values: _________________
**Note**
* When submitting the `binary.py`, it should only contain the function definitions of `to_32_bit_binary()` and `to_decimal()`
* Please remove any function calls you tried from the `binary.py` file before you submit your work
---
## Function argument assumptions
Assume the argument provided is always valid, i.e.:
* For `to_32_bit_binary()`, the argument provided is always an `int` with values in the range [0, 4294967295]
* For `to_decimal()`, the argument provided is always a string that only contains `'0'` or `'1'` with length 32
---
## Example
Example function calls on the required function `to_32_bit_binary()`:
```
>>> to_32_bit_binary(0)
'00000000000000000000000000000000'
>>> to_32_bit_binary(123)
'00000000000000000000000001111011'
>>> to_32_bit_binary(4294967295)
'11111111111111111111111111111111'
```
Example function calls on the required function `to_decimal()`:
```
>>> to_decimal('00000000000000000000000000000000')
0
>>> to_decimal('00000000000000000000000001111011')
123
>>> to_decimal('11111111111111111111111111111111')
4294967295
```
---
## Note
* Unlike problem set 2, you can use `list`, slicing, indexing, and anything else we have seen so far in the course
* This question is a modified version of a question from the 2020/21 ST101 summative problem set |
|