Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def decode_char(x, H):
agent = H
code, agent = agent.act(Message("what is the ASCII code of []?", x))
return chr(decode_int(code, H))
def encode_int(x):
if x == 0:
return Message("zero")
elif x < 0:
return Message("the additive inverse of []", encode_int(-x))
elif x%2 == 0:
return Message("two times []", encode_int(x/2))
elif x%2 == 1:
return Message("two times [] plus one", encode_int(x/2))
raise Exception()
sign = -1
x = -x
elif signm == Message("positive"):
sign = 1
elif signm == Message("zero"):
return 0
else:
raise Exception("sign is not 'negative', 'positive', or 'zero'")
paritym, agent = agent.act(Message("is it even or odd? (respond verbatim)"))
if paritym == Message("even"):
parity = 0
elif paritym == Message("odd"):
parity = 1
else:
raise Exception("Parity is not 'even' or 'odd'")
half, agent = agent.act(Message("what is half of it, rounded towards zero?"))
return sign * (2 * decode_int(half, H) + parity)
def encode_int(x):
if x == 0:
return Message("zero")
elif x < 0:
return Message("the additive inverse of []", encode_int(-x))
elif x%2 == 0:
return Message("two times []", encode_int(x/2))
elif x%2 == 1:
return Message("two times [] plus one", encode_int(x/2))
raise Exception()
def decode_int(x, H):
agent = H
signm, agent = agent.act(Message("is [] negative, zero, or positive? (respond verbatim)", x))
if signm == Message("negative"):
sign = -1
x = -x
elif signm == Message("positive"):
sign = 1
elif signm == Message("zero"):
return 0
else:
raise Exception("sign is not 'negative', 'positive', or 'zero'")
paritym, agent = agent.act(Message("is it even or odd? (respond verbatim)"))
if paritym == Message("even"):
parity = 0
elif paritym == Message("odd"):
parity = 1
else:
raise Exception("Parity is not 'even' or 'odd'")
half, agent = agent.act(Message("what is half of it, rounded towards zero?"))
return sign * (2 * decode_int(half, H) + parity)
def decode_int(x, H):
agent = H
signm, agent = agent.act(Message("is [] negative, zero, or positive? (respond verbatim)", x))
if signm == Message("negative"):
sign = -1
x = -x
elif signm == Message("positive"):
sign = 1
elif signm == Message("zero"):
return 0
else:
raise Exception("sign is not 'negative', 'positive', or 'zero'")
paritym, agent = agent.act(Message("is it even or odd? (respond verbatim)"))
if paritym == Message("even"):
parity = 0
elif paritym == Message("odd"):
parity = 1
else:
raise Exception("Parity is not 'even' or 'odd'")
half, agent = agent.act(Message("what is half of it, rounded towards zero?"))
return sign * (2 * decode_int(half, H) + parity)
def decode_list(x, H):
agent = H
empty, agent = agent.act(Message("is [] empty? (respond yes/no)", x))
if empty == Message("yes"):
return []
elif empty == Message("no"):
x, agent = agent.act(Message("what is its first element?"))
xs, agent = agent.act(Message("what is the list of remaining elements?"))
return [x] + decode_list(xs, H)
else:
raise Exception("is empty is not 'yes' or 'no'")
def decode_int(x, H):
agent = H
signm, agent = agent.act(Message("is [] negative, zero, or positive? (respond verbatim)", x))
if signm == Message("negative"):
sign = -1
x = -x
elif signm == Message("positive"):
sign = 1
elif signm == Message("zero"):
return 0
else:
raise Exception("sign is not 'negative', 'positive', or 'zero'")
paritym, agent = agent.act(Message("is it even or odd? (respond verbatim)"))
if paritym == Message("even"):
parity = 0
elif paritym == Message("odd"):
parity = 1
else:
raise Exception("Parity is not 'even' or 'odd'")
def encode_char(x):
code = encode_int(ord(x))
return Message("the character with ASCII code []", code)