[ create a new paste ] login | about

Link: http://codepad.org/7OmYpCca    [ raw code | output | fork ]

aaronla - Python, pasted on Feb 20:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def g(f, y, all_legal_inputs_to_f):
  "returns x for y = f(x)"
  for x in all_legal_inputs_to_f:
    if y == f(x): 
      return x
  raise "y is not an output of f(x)"

def f(x):
  if x >= 11 or x < 0: raise "invalid input"
  return (x*3 + 5) % 11

valid_inputs = range(11)
for x in valid_inputs:
    fx = f(x); gfx = g(f, fx, valid_inputs)
    print "f(%s) = %s; g(%s) = %s" % (x, fx, fx, gfx)


Output:
1
2
3
4
5
6
7
8
9
10
11
f(0) = 5; g(5) = 0
f(1) = 8; g(8) = 1
f(2) = 0; g(0) = 2
f(3) = 3; g(3) = 3
f(4) = 6; g(6) = 4
f(5) = 9; g(9) = 5
f(6) = 1; g(1) = 6
f(7) = 4; g(4) = 7
f(8) = 7; g(7) = 8
f(9) = 10; g(10) = 9
f(10) = 2; g(2) = 10


Create a new paste based on this one


Comments: