forked from course-work/week1
more
This commit is contained in:
41
06-binary-converter/README.md
Normal file
41
06-binary-converter/README.md
Normal file
@ -0,0 +1,41 @@
|
||||
Binary Converter
|
||||
================
|
||||
|
||||
In this exercise you will be making functions that convert between base 10 numbers and binary.
|
||||
|
||||
Binary notation is based on powers of 2. Each digit is a power of 2. The first digit is 2^0, second 2^1, etc... You then add all of the digits together.
|
||||
|
||||
For example, 101 is 5 (4 + 1). 110 is 6 (4 + 2). 1100 is 12 (8 + 4)
|
||||
Here is a chart of some base 2 vs base 10 notation numbers:
|
||||
|
||||
0.....0
|
||||
1.....1
|
||||
10.....2
|
||||
11.....3
|
||||
100.....4
|
||||
101.....5
|
||||
110.....6
|
||||
111.....7
|
||||
1000.....8
|
||||
1001.....9
|
||||
1010.....10
|
||||
1011.....11
|
||||
1100.....12
|
||||
1101.....13
|
||||
|
||||
####Step 1
|
||||
|
||||
Write a method that takes binary numbers and outputs a base 10 number.
|
||||
|
||||
binary_to_decimal(1011) ## returns 11
|
||||
|
||||
####Step 2
|
||||
|
||||
Write a method that takes decimal numbers and returns it in binary notation.
|
||||
|
||||
decimal_to_binary(12) ## 1100
|
||||
|
||||
Resources
|
||||
----------
|
||||
|
||||
[How to Convert Decimal to Binary](http://www.wikihow.com/Convert-from-Decimal-to-Binary)
|
16
06-binary-converter/binary-converter.py
Normal file
16
06-binary-converter/binary-converter.py
Normal file
@ -0,0 +1,16 @@
|
||||
def binary_to_decimal(num):
|
||||
pass
|
||||
|
||||
def decimal_to_binary(num):
|
||||
pass
|
||||
|
||||
|
||||
assert binary_to_decimal(0) == 0, "0 to decimal should return 0"
|
||||
assert binary_to_decimal(1011) == 11, "1011 to decimal should return 11"
|
||||
assert binary_to_decimal(101011) == 43, "101011 to decimal should return 43"
|
||||
assert binary_to_decimal(101011101) == 349, "101011101 to decimal should return 349"
|
||||
|
||||
assert decimal_to_binary(0) == 0, "0 to binary should return 0"
|
||||
assert decimal_to_binary(5) == 101, "5 to binary should return 101"
|
||||
assert decimal_to_binary(10) == 1010, "10 to binary should return 1010"
|
||||
assert decimal_to_binary(113) == 1110001, "113 to binary should return 1110001"
|
Reference in New Issue
Block a user