Commit ab2cc52a authored by Phuengton Chummuel's avatar Phuengton Chummuel

add week03 assignment

parent 32781c3b
......@@ -235,6 +235,7 @@
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
......@@ -289,6 +290,7 @@
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "skip"
}
......@@ -336,6 +338,75 @@
"$0.1 \\times 0.1 = 0.01 \\approx 0.0$ \n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = [0 ,0,0,0,1 ,1,0,0]\n",
"\n",
"v = [0 , 0,0,0,0,0,0, 0,0,0,0,0]\n",
"\n",
"sign = -1 if v[0] == 1 else 1\n",
"value = 0\n",
"v_len = len(v)\n",
"\n",
"[ i for i in range(1,8)]\n",
"\n",
"# [ 2 ** (4 - i) for i in range(1,8)]\n",
"# sum([ v[i] * 2 ** (4-i) for i in range(1,8) ])"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-2.375\n"
]
}
],
"source": [
"def value(v):\n",
" sign = -1 if v[0] == 1 else 1\n",
" return sign * sum([ v[i] * 2 ** (4-i) for i in range(1,8) ])\n",
"\n",
"v = [1 ,0,0,1,0, 0,1,1]\n",
"\n",
"print(value(v))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def value(v , a, b):\n",
" sign = -1 if v[0] == 1 else 1\n",
" \n",
" \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -472,7 +543,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.6.3"
}
},
"nbformat": 4,
......
def value(v, p=4, n=3):
'''
รับค่า v มาเพื่อหาค่าเป็นเลขฐานสิบ
p เป็นจำนวนหลักที่ใช้เป็นบวก 4 หลัก
n เป็นจำนวนหลักที่ใช้เป็นลบ 3 หลัก
sign bit เป็นหลักแรกเสมอ
'''
tmp = [int(i) for i in v]
sign = -1 if tmp[0] == 1 else 1
return sign * sum([tmp[i] * 2 ** (p-i) for i in range(1, p+n+1)])
v = [0, 0, 0, 0, 1, 1, 0, 0]
print(v)
print(value(v))
v = [1, 0, 1, 0, 1, 0, 1, 1]
print(v)
print(value(v))
v = [1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
print(v)
print(value(v, 6, 5))
v = '100000110000'
print(v)
print(value(v, 6, 5))
v = '00001100'
print(v)
print(value(v))
v = '100000101010000'
print(v)
print(value(v, 9, 5))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment