Commit aa6d6eb9 authored by Phuengton Chummuel's avatar Phuengton Chummuel

add week03 assignment

parent ab2cc52a
......@@ -10,26 +10,37 @@ def value(v, p=4, n=3):
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))
def values(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)])
# print(value([0, 0, 0, 0, 1, 0, 1, 1]))
v = [1, 0, 1, 0, 1, 0, 1, 1]
print(v)
print(value(v))
# print(values('0000101000000', 5, 3))
# v = [0, 0, 0, 0, 1, 1, 0, 0]
# 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 = [1, 0, 1, 0, 1, 0, 1, 1]
# print(v)
# print(value(v))
v = '100000110000'
print(v)
print(value(v, 6, 5))
# v = [1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
# print(v)
# print(value(v, 6, 5))
v = '00001100'
print(v)
print(value(v))
# v = '100000110000'
# print(v)
# print(value(v, 6, 5))
v = '100000101010000'
print(v)
print(value(v, 9, 5))
# v = '10001100'
# print(v)
# print(value(v))
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