about summary refs log tree commit diff
path: root/users/wpcarro/scratch/facebook/stock-price.py
diff options
context:
space:
mode:
Diffstat (limited to 'users/wpcarro/scratch/facebook/stock-price.py')
-rw-r--r--users/wpcarro/scratch/facebook/stock-price.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/users/wpcarro/scratch/facebook/stock-price.py b/users/wpcarro/scratch/facebook/stock-price.py
new file mode 100644
index 000000000000..8e42f8152311
--- /dev/null
+++ b/users/wpcarro/scratch/facebook/stock-price.py
@@ -0,0 +1,16 @@
+def max_profit(xs):
+    buy = xs[0]
+    profit = xs[1] - xs[0]
+    for price in xs[1:]:
+        profit = max(profit, price - buy)
+        buy = min(buy, price)
+    return profit
+
+xs = [([10,7,5,8,11,9], 6),
+      ([10,8,7,6,5], -1)]
+
+for x, expected in xs:
+    result = max_profit(x)
+    print(x, result)
+    assert result == expected
+    print("Success!")