15-110 PA7 Sample Solutions - Spring 2018 1. def unsigned_value(bitstring): total = 0 for i in range(16): if bitstring[i] == '1': total = total + 2**(15-i) return total 2. def to_positive(bytestring): if bytestring[0] == "0": return bytestring bytelist = list(bytestring) i = 0 for i in range(len(bytelist)): if bytelist[i]=='1': bytelist[i] = '0' else: bytelist[i] = '1' i = 7 while bytelist[i]=='1': bytelist[i] = '0' i = i - 1 if i >= 0: bytelist[i] = '1' return "".join(bytelist) 3. def bin_to_hex(bitstring): bin = ['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111'] hex = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'] result = "" for i in range(0, 16, 4): fourbits = bitstring[i:i+4] j = bin.index(fourbits) result = result + hex[j] return result 4. def forward(c, i): position = ord(c) - 65 new_position = (position + i) % 26 return chr(new_position + 65) def capitalize(s): new_s = "" for i in range(len(s)): if i == 0 or s[i-1]==' ': new_s = new_s + chr(ord(s[i]) - 32) else: new_s = new_s + s[i] return new_s