Arbitrages: First working version

This commit is contained in:
2025-11-10 10:53:00 -05:00
parent 7739e18c7f
commit c6c7e667c3
4 changed files with 99 additions and 8 deletions

View File

@@ -0,0 +1,83 @@
"""
name: Nicholas Tamassia
Honor Code and Acknowledgments:
This work complies with the JMU Honor Code.
Comments here on your code and submission.
"""
import math
from collections import defaultdict
def find_arbitrage(graph: dict[str, dict[str, float]]) -> list[str] | None:
weights: dict[str, float] = {c: math.inf for c in graph}
prev: dict[str, str | None] = {c: None for c in graph}
start: str = list(graph.keys())[0]
weights[start] = 0
for _ in range(len(graph) - 1):
for src in graph:
for dest, rate in graph[src].items():
weight: float = -math.log(rate)
if weights[src] + weight < weights[dest]:
weights[dest] = weights[src] + weight
prev[dest] = src
node: str | None = None
for src in graph:
for dest, rate in graph[src].items():
weight: float = -math.log(rate)
if weights[src] + weight < weights[dest]:
node = dest
prev[dest] = src
break
if node is not None:
break
if node is None:
return
for _ in range(len(graph)):
node = prev[node]
path = [node]
while True:
node = prev[node]
path.append(node)
if node == path[0]:
break
path.reverse()
return path
def main():
n: int = int(input().strip())
graph: dict[str, dict[str, float]] = defaultdict(dict)
for _ in range(n):
vals: list[str] = input().strip().split()
src: str = vals[0]
dest: str = vals[1]
rate: float = float(vals[2])
graph[src][dest] = rate
path = find_arbitrage(graph)
if path is not None:
print("Arbitrage Detected")
print(" => ".join(path))
factor = 1.0
for i in range(len(path) - 1):
factor *= graph[path[i]][path[i + 1]]
print(f"{factor:.5f} factor increase")
else:
print("No Arbitrage Detected")
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,5 @@
4
USD EUR 0.82
EUR JPY 129.7
JPY TRY 12
TRY USD 0.0008

View File

@@ -0,0 +1,3 @@
2
USD GBP 1.0
GBP USD 1.0

View File

@@ -3,10 +3,10 @@
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1757570236,
"lastModified": 1762789285,
"owner": "cachix",
"repo": "devenv",
"rev": "c57bded76fa6a885ab1dee2c75216cc23d58b311",
"rev": "379dd49a2ae3470e216cf07bfee4326e3c5a5baf",
"type": "github"
},
"original": {
@@ -19,10 +19,10 @@
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1747046372,
"lastModified": 1761588595,
"owner": "edolstra",
"repo": "flake-compat",
"rev": "9100a0f413b0c601e0533d1d94ffd501ce2e7885",
"rev": "f387cd2afec9419c8ee37694406ca490c3f34ee5",
"type": "github"
},
"original": {
@@ -40,10 +40,10 @@
]
},
"locked": {
"lastModified": 1757588530,
"lastModified": 1762441963,
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "b084b2c2b6bc23e83bbfe583b03664eb0b18c411",
"rev": "8e7576e79b88c16d7ee3bbd112c8d90070832885",
"type": "github"
},
"original": {
@@ -74,10 +74,10 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1755783167,
"lastModified": 1761313199,
"owner": "cachix",
"repo": "devenv-nixpkgs",
"rev": "4a880fb247d24fbca57269af672e8f78935b0328",
"rev": "d1c30452ebecfc55185ae6d1c983c09da0c274ff",
"type": "github"
},
"original": {