changeset 250:cbee252910f3

example: while loop modernized
author Sigurd Meldgaard <stm@daimi.au.dk>
date Wed, 06 Jan 2010 12:20:12 +0100
parents a210979cbd5d
children 95f0cfe90bd1
files examples/while-loop.py
diffstat 1 files changed, 10 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/examples/while-loop.py	Wed Jan 06 12:19:27 2010 +0100
+++ b/examples/while-loop.py	Wed Jan 06 12:20:12 2010 +0100
@@ -1,8 +1,9 @@
-from pysmcl.functions import get
+from pysmcl.functions import result, input, output
 from pysmcl.ideal_functionality import ideal_functionality
 from pysmcl.functions import precondition
 
-@ideal_functionality(range={'bids': (-4,4)})
+@ideal_functionality(secrets=['bids'],
+                     range={'bids': (-100, 100)})
 def search(bids):
     precondition("a >= b <=> bids[a] >= bids[b]")
     precondition("0 >= bids[0]")
@@ -10,8 +11,10 @@
     low = 0
     high = len(bids)
     while(low < high):
+        print low, high
         mid = (low + high) // 2
         r = output(bids[mid] >= 0)
+        print mid
         if r:
             high = mid
         else:
@@ -21,10 +24,10 @@
 
 @ideal_functionality()
 def main():
-    bids = [0] * 100
-    for i in range(100):
-        bids1 = get("Buy at price: " + str(i), 1, (0,2))
-        bids2 = get("Sell at price: " + str(i), 2, (0,2))
-        bids[i] = bids1[i] - bids2[i]
+    bids = [0] * 3
+    for i in range(3):
+        bids1 = input("Buy at price: " + str(i), 1, 0, 100)
+        bids2 = input("Sell at price: " + str(i), 2, 0, 100)
+        bids[i] = bids1 - bids2
     a = search(bids)
     print "Market clearing price: " + str(a)