mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
parent
d488e4f355
commit
dce6b6aa67
|
@ -92,9 +92,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bit(object):\n",
|
||||
|
@ -145,40 +143,38 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_bit.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBit(object):\n",
|
||||
"class TestBit(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_bit(self):\n",
|
||||
" number = int('10001110', base=2)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" assert_equal(bit.get_bit(index=3), True)\n",
|
||||
" self.assertEqual(bit.get_bit(index=3), True)\n",
|
||||
" expected = int('10011110', base=2)\n",
|
||||
" assert_equal(bit.set_bit(index=4), expected)\n",
|
||||
" self.assertEqual(bit.set_bit(index=4), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('10000110', base=2)\n",
|
||||
" assert_equal(bit.clear_bit(index=3), expected)\n",
|
||||
" self.assertEqual(bit.clear_bit(index=3), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('00000110', base=2)\n",
|
||||
" assert_equal(bit.clear_bits_msb_to_index(index=3), expected)\n",
|
||||
" self.assertEqual(bit.clear_bits_msb_to_index(index=3), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('10000000', base=2)\n",
|
||||
" assert_equal(bit.clear_bits_index_to_lsb(index=3), expected)\n",
|
||||
" self.assertEqual(bit.clear_bits_index_to_lsb(index=3), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" assert_equal(bit.update_bit(index=3, value=1), number)\n",
|
||||
" self.assertEqual(bit.update_bit(index=3, value=1), number)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('10000110', base=2)\n",
|
||||
" assert_equal(bit.update_bit(index=3, value=0), expected)\n",
|
||||
" self.assertEqual(bit.update_bit(index=3, value=0), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('10001111', base=2)\n",
|
||||
" assert_equal(bit.update_bit(index=0, value=1), expected)\n",
|
||||
" self.assertEqual(bit.update_bit(index=0, value=1), expected)\n",
|
||||
" print('Success: test_bit')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -217,9 +213,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -179,9 +179,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def validate_index(func):\n",
|
||||
|
@ -196,9 +194,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bit(object):\n",
|
||||
|
@ -260,9 +256,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -274,34 +268,34 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_bit.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBit(object):\n",
|
||||
"class TestBit(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_bit(self):\n",
|
||||
" number = int('10001110', base=2)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" assert_equal(bit.get_bit(index=3), True)\n",
|
||||
" self.assertEqual(bit.get_bit(index=3), True)\n",
|
||||
" expected = int('10011110', base=2)\n",
|
||||
" assert_equal(bit.set_bit(index=4), expected)\n",
|
||||
" self.assertEqual(bit.set_bit(index=4), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('10000110', base=2)\n",
|
||||
" assert_equal(bit.clear_bit(index=3), expected)\n",
|
||||
" self.assertEqual(bit.clear_bit(index=3), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('00000110', base=2)\n",
|
||||
" assert_equal(bit.clear_bits_msb_to_index(index=3), expected)\n",
|
||||
" self.assertEqual(bit.clear_bits_msb_to_index(index=3), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('10000000', base=2)\n",
|
||||
" assert_equal(bit.clear_bits_index_to_lsb(index=3), expected)\n",
|
||||
" self.assertEqual(bit.clear_bits_index_to_lsb(index=3), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" assert_equal(bit.update_bit(index=3, value=1), number)\n",
|
||||
" self.assertEqual(bit.update_bit(index=3, value=1), number)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('10000110', base=2)\n",
|
||||
" assert_equal(bit.update_bit(index=3, value=0), expected)\n",
|
||||
" self.assertEqual(bit.update_bit(index=3, value=0), expected)\n",
|
||||
" bit = Bit(number)\n",
|
||||
" expected = int('10001111', base=2)\n",
|
||||
" assert_equal(bit.update_bit(index=0, value=1), expected)\n",
|
||||
" self.assertEqual(bit.update_bit(index=0, value=1), expected)\n",
|
||||
" print('Success: test_bit')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -317,9 +311,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -350,9 +342,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestBit(object):
|
||||
class TestBit(unittest.TestCase):
|
||||
|
||||
def test_bit(self):
|
||||
number = int('10001110', base=2)
|
||||
bit = Bit(number)
|
||||
assert_equal(bit.get_bit(index=3), True)
|
||||
self.assertEqual(bit.get_bit(index=3), True)
|
||||
expected = int('10011110', base=2)
|
||||
assert_equal(bit.set_bit(index=4), expected)
|
||||
self.assertEqual(bit.set_bit(index=4), expected)
|
||||
bit = Bit(number)
|
||||
expected = int('10000110', base=2)
|
||||
assert_equal(bit.clear_bit(index=3), expected)
|
||||
self.assertEqual(bit.clear_bit(index=3), expected)
|
||||
bit = Bit(number)
|
||||
expected = int('00000110', base=2)
|
||||
assert_equal(bit.clear_bits_msb_to_index(index=3), expected)
|
||||
self.assertEqual(bit.clear_bits_msb_to_index(index=3), expected)
|
||||
bit = Bit(number)
|
||||
expected = int('10000000', base=2)
|
||||
assert_equal(bit.clear_bits_index_to_lsb(index=3), expected)
|
||||
self.assertEqual(bit.clear_bits_index_to_lsb(index=3), expected)
|
||||
bit = Bit(number)
|
||||
assert_equal(bit.update_bit(index=3, value=1), number)
|
||||
self.assertEqual(bit.update_bit(index=3, value=1), number)
|
||||
bit = Bit(number)
|
||||
expected = int('10000110', base=2)
|
||||
assert_equal(bit.update_bit(index=3, value=0), expected)
|
||||
self.assertEqual(bit.update_bit(index=3, value=0), expected)
|
||||
bit = Bit(number)
|
||||
expected = int('10001111', base=2)
|
||||
assert_equal(bit.update_bit(index=0, value=1), expected)
|
||||
self.assertEqual(bit.update_bit(index=0, value=1), expected)
|
||||
print('Success: test_bit')
|
||||
|
||||
|
||||
|
@ -35,4 +35,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -80,9 +80,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -109,23 +107,21 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_bits_to_flip.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_bits_to_flip(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" a = int('11101', base=2)\n",
|
||||
" b = int('01111', base=2)\n",
|
||||
" expected = 2\n",
|
||||
" assert_equal(bits.bits_to_flip(a, b), expected)\n",
|
||||
" self.assertEqual(bits.bits_to_flip(a, b), expected)\n",
|
||||
" print('Success: test_bits_to_flip')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -164,9 +160,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -91,9 +91,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -119,9 +117,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -133,17 +129,17 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_bits_to_flip.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_bits_to_flip(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" a = int('11101', base=2)\n",
|
||||
" b = int('01111', base=2)\n",
|
||||
" expected = 2\n",
|
||||
" assert_equal(bits.bits_to_flip(a, b), expected)\n",
|
||||
" self.assertEqual(bits.bits_to_flip(a, b), expected)\n",
|
||||
" print('Success: test_bits_to_flip')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -159,9 +155,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -192,9 +186,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestBits(object):
|
||||
class TestBits(unittest.TestCase):
|
||||
|
||||
def test_bits_to_flip(self):
|
||||
bits = Bits()
|
||||
a = int('11101', base=2)
|
||||
b = int('01111', base=2)
|
||||
expected = 2
|
||||
assert_equal(bits.bits_to_flip(a, b), expected)
|
||||
self.assertEqual(bits.bits_to_flip(a, b), expected)
|
||||
print('Success: test_bits_to_flip')
|
||||
|
||||
|
||||
|
@ -18,4 +18,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -81,9 +81,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class BitsScreen(object):\n",
|
||||
|
@ -110,16 +108,14 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_draw_line.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBitsScreen(object):\n",
|
||||
"class TestBitsScreen(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_draw_line(self):\n",
|
||||
" bits_screen = BitsScreen()\n",
|
||||
|
@ -127,13 +123,13 @@
|
|||
" for _ in range(20):\n",
|
||||
" screen.append(int('00000000', base=2))\n",
|
||||
" bits_screen.draw_line(screen, width=32, x1=68, x2=80)\n",
|
||||
" assert_equal(screen[8], int('00001111', base=2))\n",
|
||||
" assert_equal(screen[9], int('11111111', base=2))\n",
|
||||
" assert_equal(screen[10], int('10000000', base=2))\n",
|
||||
" self.assertEqual(screen[8], int('00001111', base=2))\n",
|
||||
" self.assertEqual(screen[9], int('11111111', base=2))\n",
|
||||
" self.assertEqual(screen[10], int('10000000', base=2))\n",
|
||||
" bits_screen.draw_line(screen, width=32, x1=2, x2=6)\n",
|
||||
" assert_equal(screen[0], int('00111110', base=2))\n",
|
||||
" self.assertEqual(screen[0], int('00111110', base=2))\n",
|
||||
" bits_screen.draw_line(screen, width=32, x1=10, x2=13)\n",
|
||||
" assert_equal(screen[1], int('00111100', base=2))\n",
|
||||
" self.assertEqual(screen[1], int('00111100', base=2))\n",
|
||||
" print('Success: test_draw_line')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -172,9 +168,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -151,9 +151,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class BitsScreen(object):\n",
|
||||
|
@ -200,9 +198,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -214,10 +210,10 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_draw_line.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBitsScreen(object):\n",
|
||||
"class TestBitsScreen(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_draw_line(self):\n",
|
||||
" bits_screen = BitsScreen()\n",
|
||||
|
@ -225,13 +221,13 @@
|
|||
" for _ in range(20):\n",
|
||||
" screen.append(int('00000000', base=2))\n",
|
||||
" bits_screen.draw_line(screen, width=32, x1=68, x2=80)\n",
|
||||
" assert_equal(screen[8], int('00001111', base=2))\n",
|
||||
" assert_equal(screen[9], int('11111111', base=2))\n",
|
||||
" assert_equal(screen[10], int('10000000', base=2))\n",
|
||||
" self.assertEqual(screen[8], int('00001111', base=2))\n",
|
||||
" self.assertEqual(screen[9], int('11111111', base=2))\n",
|
||||
" self.assertEqual(screen[10], int('10000000', base=2))\n",
|
||||
" bits_screen.draw_line(screen, width=32, x1=2, x2=6)\n",
|
||||
" assert_equal(screen[0], int('00111110', base=2))\n",
|
||||
" self.assertEqual(screen[0], int('00111110', base=2))\n",
|
||||
" bits_screen.draw_line(screen, width=32, x1=10, x2=13)\n",
|
||||
" assert_equal(screen[1], int('00111100', base=2))\n",
|
||||
" self.assertEqual(screen[1], int('00111100', base=2))\n",
|
||||
" print('Success: test_draw_line')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -247,9 +243,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -280,9 +274,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestBitsScreen(object):
|
||||
class TestBitsScreen(unittest.TestCase):
|
||||
|
||||
def test_draw_line(self):
|
||||
bits_screen = BitsScreen()
|
||||
|
@ -9,13 +9,13 @@ class TestBitsScreen(object):
|
|||
for _ in range(20):
|
||||
screen.append(int('00000000', base=2))
|
||||
bits_screen.draw_line(screen, width=32, x1=68, x2=80)
|
||||
assert_equal(screen[8], int('00001111', base=2))
|
||||
assert_equal(screen[9], int('11111111', base=2))
|
||||
assert_equal(screen[10], int('10000000', base=2))
|
||||
self.assertEqual(screen[8], int('00001111', base=2))
|
||||
self.assertEqual(screen[9], int('11111111', base=2))
|
||||
self.assertEqual(screen[10], int('10000000', base=2))
|
||||
bits_screen.draw_line(screen, width=32, x1=2, x2=6)
|
||||
assert_equal(screen[0], int('00111110', base=2))
|
||||
self.assertEqual(screen[0], int('00111110', base=2))
|
||||
bits_screen.draw_line(screen, width=32, x1=10, x2=13)
|
||||
assert_equal(screen[1], int('00111100', base=2))
|
||||
self.assertEqual(screen[1], int('00111100', base=2))
|
||||
print('Success: test_draw_line')
|
||||
|
||||
|
||||
|
@ -25,4 +25,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -82,9 +82,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -111,31 +109,29 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_flip_bit.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_flip_bit(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_raises(TypeError, bits.flip_bit, None)\n",
|
||||
" assert_equal(bits.flip_bit(0), 1)\n",
|
||||
" assert_equal(bits.flip_bit(-1), bits.MAX_BITS)\n",
|
||||
" self.assertRaises(TypeError, bits.flip_bit, None)\n",
|
||||
" self.assertEqual(bits.flip_bit(0), 1)\n",
|
||||
" self.assertEqual(bits.flip_bit(-1), bits.MAX_BITS)\n",
|
||||
" num = int('00001111110111011110001111110000', base=2)\n",
|
||||
" expected = 10\n",
|
||||
" assert_equal(bits.flip_bit(num), expected)\n",
|
||||
" self.assertEqual(bits.flip_bit(num), expected)\n",
|
||||
" num = int('00000100111011101111100011111011', base=2)\n",
|
||||
" expected = 9\n",
|
||||
" assert_equal(bits.flip_bit(num), expected)\n",
|
||||
" self.assertEqual(bits.flip_bit(num), expected)\n",
|
||||
" num = int('00010011101110111110001111101111', base=2)\n",
|
||||
" expected = 10\n",
|
||||
" assert_equal(bits.flip_bit(num), expected)\n",
|
||||
" self.assertEqual(bits.flip_bit(num), expected)\n",
|
||||
" print('Success: test_print_binary')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -174,9 +170,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -112,9 +112,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -186,9 +184,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -200,25 +196,25 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_flip_bit.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_flip_bit(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_raises(TypeError, bits.flip_bit, None)\n",
|
||||
" assert_equal(bits.flip_bit(0), 1)\n",
|
||||
" assert_equal(bits.flip_bit(-1), bits.MAX_BITS)\n",
|
||||
" self.assertRaises(TypeError, bits.flip_bit, None)\n",
|
||||
" self.assertEqual(bits.flip_bit(0), 1)\n",
|
||||
" self.assertEqual(bits.flip_bit(-1), bits.MAX_BITS)\n",
|
||||
" num = int('00001111110111011110001111110000', base=2)\n",
|
||||
" expected = 10\n",
|
||||
" assert_equal(bits.flip_bit(num), expected)\n",
|
||||
" self.assertEqual(bits.flip_bit(num), expected)\n",
|
||||
" num = int('00000100111011101111100011111011', base=2)\n",
|
||||
" expected = 9\n",
|
||||
" assert_equal(bits.flip_bit(num), expected)\n",
|
||||
" self.assertEqual(bits.flip_bit(num), expected)\n",
|
||||
" num = int('00010011101110111110001111101111', base=2)\n",
|
||||
" expected = 10\n",
|
||||
" assert_equal(bits.flip_bit(num), expected)\n",
|
||||
" self.assertEqual(bits.flip_bit(num), expected)\n",
|
||||
" print('Success: test_print_binary')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -234,9 +230,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -267,9 +261,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
from nose.tools import assert_equal, assert_raises
|
||||
import unittest
|
||||
|
||||
|
||||
class TestBits(object):
|
||||
class TestBits(unittest.TestCase):
|
||||
|
||||
def test_flip_bit(self):
|
||||
bits = Bits()
|
||||
assert_raises(TypeError, bits.flip_bit, None)
|
||||
assert_equal(bits.flip_bit(0), 1)
|
||||
assert_equal(bits.flip_bit(-1), bits.MAX_BITS)
|
||||
self.assertRaises(TypeError, bits.flip_bit, None)
|
||||
self.assertEqual(bits.flip_bit(0), 1)
|
||||
self.assertEqual(bits.flip_bit(-1), bits.MAX_BITS)
|
||||
num = int('00001111110111011110001111110000', base=2)
|
||||
expected = 10
|
||||
assert_equal(bits.flip_bit(num), expected)
|
||||
self.assertEqual(bits.flip_bit(num), expected)
|
||||
num = int('00000100111011101111100011111011', base=2)
|
||||
expected = 9
|
||||
assert_equal(bits.flip_bit(num), expected)
|
||||
self.assertEqual(bits.flip_bit(num), expected)
|
||||
num = int('00010011101110111110001111101111', base=2)
|
||||
expected = 10
|
||||
assert_equal(bits.flip_bit(num), expected)
|
||||
self.assertEqual(bits.flip_bit(num), expected)
|
||||
print('Success: test_print_binary')
|
||||
|
||||
|
||||
|
@ -26,4 +26,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -78,9 +78,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -111,35 +109,33 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_get_next_largest.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_get_next_largest(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_raises(Exception, bits.get_next_largest, None)\n",
|
||||
" assert_raises(Exception, bits.get_next_largest, 0)\n",
|
||||
" assert_raises(Exception, bits.get_next_largest, -1)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_largest, None)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_largest, 0)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_largest, -1)\n",
|
||||
" num = int('011010111', base=2)\n",
|
||||
" expected = int('011011011', base=2)\n",
|
||||
" assert_equal(bits.get_next_largest(num), expected)\n",
|
||||
" self.assertEqual(bits.get_next_largest(num), expected)\n",
|
||||
" print('Success: test_get_next_largest')\n",
|
||||
"\n",
|
||||
" def test_get_next_smallest(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_raises(Exception, bits.get_next_smallest, None)\n",
|
||||
" assert_raises(Exception, bits.get_next_smallest, 0)\n",
|
||||
" assert_raises(Exception, bits.get_next_smallest, -1)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_smallest, None)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_smallest, 0)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_smallest, -1)\n",
|
||||
" num = int('011010111', base=2)\n",
|
||||
" expected = int('011001111', base=2)\n",
|
||||
" assert_equal(bits.get_next_smallest(num), expected)\n",
|
||||
" self.assertEqual(bits.get_next_smallest(num), expected)\n",
|
||||
" print('Success: test_get_next_smallest')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
@ -178,9 +174,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -108,9 +108,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -178,9 +176,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -192,29 +188,29 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_get_next_largest.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_get_next_largest(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_raises(Exception, bits.get_next_largest, None)\n",
|
||||
" assert_raises(Exception, bits.get_next_largest, 0)\n",
|
||||
" assert_raises(Exception, bits.get_next_largest, -1)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_largest, None)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_largest, 0)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_largest, -1)\n",
|
||||
" num = int('011010111', base=2)\n",
|
||||
" expected = int('011011011', base=2)\n",
|
||||
" assert_equal(bits.get_next_largest(num), expected)\n",
|
||||
" self.assertEqual(bits.get_next_largest(num), expected)\n",
|
||||
" print('Success: test_get_next_largest')\n",
|
||||
"\n",
|
||||
" def test_get_next_smallest(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_raises(Exception, bits.get_next_smallest, None)\n",
|
||||
" assert_raises(Exception, bits.get_next_smallest, 0)\n",
|
||||
" assert_raises(Exception, bits.get_next_smallest, -1)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_smallest, None)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_smallest, 0)\n",
|
||||
" self.assertRaises(Exception, bits.get_next_smallest, -1)\n",
|
||||
" num = int('011010111', base=2)\n",
|
||||
" expected = int('011001111', base=2)\n",
|
||||
" assert_equal(bits.get_next_smallest(num), expected)\n",
|
||||
" self.assertEqual(bits.get_next_smallest(num), expected)\n",
|
||||
" print('Success: test_get_next_smallest')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
|
@ -230,9 +226,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -264,9 +258,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
from nose.tools import assert_equal, assert_raises
|
||||
import unittest
|
||||
|
||||
|
||||
class TestBits(object):
|
||||
class TestBits(unittest.TestCase):
|
||||
|
||||
def test_get_next_largest(self):
|
||||
bits = Bits()
|
||||
assert_raises(Exception, bits.get_next_largest, None)
|
||||
assert_raises(Exception, bits.get_next_largest, 0)
|
||||
assert_raises(Exception, bits.get_next_largest, -1)
|
||||
self.assertRaises(Exception, bits.get_next_largest, None)
|
||||
self.assertRaises(Exception, bits.get_next_largest, 0)
|
||||
self.assertRaises(Exception, bits.get_next_largest, -1)
|
||||
num = int('011010111', base=2)
|
||||
expected = int('011011011', base=2)
|
||||
assert_equal(bits.get_next_largest(num), expected)
|
||||
self.assertEqual(bits.get_next_largest(num), expected)
|
||||
print('Success: test_get_next_largest')
|
||||
|
||||
def test_get_next_smallest(self):
|
||||
bits = Bits()
|
||||
assert_raises(Exception, bits.get_next_smallest, None)
|
||||
assert_raises(Exception, bits.get_next_smallest, 0)
|
||||
assert_raises(Exception, bits.get_next_smallest, -1)
|
||||
self.assertRaises(Exception, bits.get_next_smallest, None)
|
||||
self.assertRaises(Exception, bits.get_next_smallest, 0)
|
||||
self.assertRaises(Exception, bits.get_next_smallest, -1)
|
||||
num = int('011010111', base=2)
|
||||
expected = int('011001111', base=2)
|
||||
assert_equal(bits.get_next_smallest(num), expected)
|
||||
self.assertEqual(bits.get_next_smallest(num), expected)
|
||||
print('Success: test_get_next_smallest')
|
||||
|
||||
def main():
|
||||
|
@ -30,4 +30,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -81,9 +81,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -110,23 +108,21 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_insert_m_into_n.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBit(object):\n",
|
||||
"class TestBit(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_insert_m_into_n(self):\n",
|
||||
" n = int('0000010000111101', base=2)\n",
|
||||
" m = int('0000000000010011', base=2)\n",
|
||||
" expected = int('0000010001001101', base=2)\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_equal(bits.insert_m_into_n(m, n, i=2, j=6), expected)\n",
|
||||
" self.assertEqual(bits.insert_m_into_n(m, n, i=2, j=6), expected)\n",
|
||||
" print('Success: test_insert_m_into_n')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -165,9 +161,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -108,9 +108,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -140,9 +138,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -154,17 +150,17 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_insert_m_into_n.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBit(object):\n",
|
||||
"class TestBit(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_insert_m_into_n(self):\n",
|
||||
" n = int('0000010000111101', base=2)\n",
|
||||
" m = int('0000000000010011', base=2)\n",
|
||||
" expected = int('0000010001001101', base=2)\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_equal(bits.insert_m_into_n(m, n, i=2, j=6), expected)\n",
|
||||
" self.assertEqual(bits.insert_m_into_n(m, n, i=2, j=6), expected)\n",
|
||||
" print('Success: test_insert_m_into_n')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -180,9 +176,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -213,9 +207,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestBit(object):
|
||||
class TestBit(unittest.TestCase):
|
||||
|
||||
def test_insert_m_into_n(self):
|
||||
n = int('0000010000111101', base=2)
|
||||
m = int('0000000000010011', base=2)
|
||||
expected = int('0000010001001101', base=2)
|
||||
bits = Bits()
|
||||
assert_equal(bits.insert_m_into_n(m, n, i=2, j=6), expected)
|
||||
self.assertEqual(bits.insert_m_into_n(m, n, i=2, j=6), expected)
|
||||
print('Success: test_insert_m_into_n')
|
||||
|
||||
|
||||
|
@ -18,4 +18,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -81,9 +81,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -110,24 +108,22 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_pairwise_swap.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_pairwise_swap(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_equal(bits.pairwise_swap(0), 0)\n",
|
||||
" assert_equal(bits.pairwise_swap(1), 1)\n",
|
||||
" self.assertEqual(bits.pairwise_swap(0), 0)\n",
|
||||
" self.assertEqual(bits.pairwise_swap(1), 1)\n",
|
||||
" num = int('0000100111110110', base=2)\n",
|
||||
" expected = int('0000011011111001', base=2)\n",
|
||||
" assert_equal(bits.pairwise_swap(num), expected)\n",
|
||||
" self.assertEqual(bits.pairwise_swap(num), expected)\n",
|
||||
" print('Success: test_pairwise_swap')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -166,9 +162,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -108,9 +108,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -135,9 +133,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -149,19 +145,19 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_pairwise_swap.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_pairwise_swap(self):\n",
|
||||
" bits = Bits()\n",
|
||||
" assert_equal(bits.pairwise_swap(0), 0)\n",
|
||||
" assert_equal(bits.pairwise_swap(1), 1)\n",
|
||||
" self.assertEqual(bits.pairwise_swap(0), 0)\n",
|
||||
" self.assertEqual(bits.pairwise_swap(1), 1)\n",
|
||||
" num = int('0000100111110110', base=2)\n",
|
||||
" expected = int('0000011011111001', base=2)\n",
|
||||
" assert_equal(bits.pairwise_swap(num), expected)\n",
|
||||
" assert_raises(TypeError, bits.pairwise_swap, None)\n",
|
||||
" self.assertEqual(bits.pairwise_swap(num), expected)\n",
|
||||
" self.assertRaises(TypeError, bits.pairwise_swap, None)\n",
|
||||
" \n",
|
||||
" print('Success: test_pairwise_swap')\n",
|
||||
"\n",
|
||||
|
@ -178,9 +174,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -211,9 +205,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
from nose.tools import assert_equal, assert_raises
|
||||
import unittest
|
||||
|
||||
|
||||
class TestBits(object):
|
||||
class TestBits(unittest.TestCase):
|
||||
|
||||
def test_pairwise_swap(self):
|
||||
bits = Bits()
|
||||
assert_equal(bits.pairwise_swap(0), 0)
|
||||
assert_equal(bits.pairwise_swap(1), 1)
|
||||
self.assertEqual(bits.pairwise_swap(0), 0)
|
||||
self.assertEqual(bits.pairwise_swap(1), 1)
|
||||
num = int('0000100111110110', base=2)
|
||||
expected = int('0000011011111001', base=2)
|
||||
assert_equal(bits.pairwise_swap(num), expected)
|
||||
assert_raises(TypeError, bits.pairwise_swap, None)
|
||||
self.assertEqual(bits.pairwise_swap(num), expected)
|
||||
self.assertRaises(TypeError, bits.pairwise_swap, None)
|
||||
|
||||
print('Success: test_pairwise_swap')
|
||||
|
||||
|
@ -21,4 +21,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
|
@ -82,9 +82,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Bits(object):\n",
|
||||
|
@ -111,27 +109,25 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_print_binary.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_print_binary(self):\n",
|
||||
" bit = Bits()\n",
|
||||
" assert_equal(bit.print_binary(None), 'ERROR')\n",
|
||||
" assert_equal(bit.print_binary(0), 'ERROR')\n",
|
||||
" assert_equal(bit.print_binary(1), 'ERROR')\n",
|
||||
" self.assertEqual(bit.print_binary(None), 'ERROR')\n",
|
||||
" self.assertEqual(bit.print_binary(0), 'ERROR')\n",
|
||||
" self.assertEqual(bit.print_binary(1), 'ERROR')\n",
|
||||
" num = 0.625\n",
|
||||
" expected = '0.101'\n",
|
||||
" assert_equal(bit.print_binary(num), expected)\n",
|
||||
" self.assertEqual(bit.print_binary(num), expected)\n",
|
||||
" num = 0.987654321\n",
|
||||
" assert_equal(bit.print_binary(num), 'ERROR')\n",
|
||||
" self.assertEqual(bit.print_binary(num), 'ERROR')\n",
|
||||
" print('Success: test_print_binary')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -170,9 +166,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -91,9 +91,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from __future__ import division\n",
|
||||
|
@ -130,9 +128,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -144,21 +140,21 @@
|
|||
],
|
||||
"source": [
|
||||
"%%writefile test_print_binary.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"import unittest\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestBits(object):\n",
|
||||
"class TestBits(unittest.TestCase):\n",
|
||||
"\n",
|
||||
" def test_print_binary(self):\n",
|
||||
" bit = Bits()\n",
|
||||
" assert_equal(bit.print_binary(None), 'ERROR')\n",
|
||||
" assert_equal(bit.print_binary(0), 'ERROR')\n",
|
||||
" assert_equal(bit.print_binary(1), 'ERROR')\n",
|
||||
" self.assertEqual(bit.print_binary(None), 'ERROR')\n",
|
||||
" self.assertEqual(bit.print_binary(0), 'ERROR')\n",
|
||||
" self.assertEqual(bit.print_binary(1), 'ERROR')\n",
|
||||
" num = 0.625\n",
|
||||
" expected = '0.101'\n",
|
||||
" assert_equal(bit.print_binary(num), expected)\n",
|
||||
" self.assertEqual(bit.print_binary(num), expected)\n",
|
||||
" num = 0.987654321\n",
|
||||
" assert_equal(bit.print_binary(num), 'ERROR')\n",
|
||||
" self.assertEqual(bit.print_binary(num), 'ERROR')\n",
|
||||
" print('Success: test_print_binary')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
@ -174,9 +170,7 @@
|
|||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
@ -207,9 +201,9 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
"version": "3.7.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
from nose.tools import assert_equal
|
||||
import unittest
|
||||
|
||||
|
||||
class TestBits(object):
|
||||
class TestBits(unittest.TestCase):
|
||||
|
||||
def test_print_binary(self):
|
||||
bit = Bits()
|
||||
assert_equal(bit.print_binary(None), 'ERROR')
|
||||
assert_equal(bit.print_binary(0), 'ERROR')
|
||||
assert_equal(bit.print_binary(1), 'ERROR')
|
||||
self.assertEqual(bit.print_binary(None), 'ERROR')
|
||||
self.assertEqual(bit.print_binary(0), 'ERROR')
|
||||
self.assertEqual(bit.print_binary(1), 'ERROR')
|
||||
num = 0.625
|
||||
expected = '0.101'
|
||||
assert_equal(bit.print_binary(num), expected)
|
||||
self.assertEqual(bit.print_binary(num), expected)
|
||||
num = 0.987654321
|
||||
assert_equal(bit.print_binary(num), 'ERROR')
|
||||
self.assertEqual(bit.print_binary(num), 'ERROR')
|
||||
print('Success: test_print_binary')
|
||||
|
||||
|
||||
|
@ -22,4 +22,4 @@ def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user