|
|
|
|
|
by bmacho
1220 days ago
|
|
I tried to translate it to python, I translated (defn sum3
"Solve the 3SUM problem in O(n^2) time."
[s]
(def tab @{})
(def solutions @{})
(def len (length s))
(for k 0 len
(put tab (s k) k))
(for i 0 len
(for j 0 len
(def k (get tab (- 0 (s i) (s j))))
(when (and k (not= k i) (not= k j) (not= i j))
(put solutions {i true j true k true} true))))
solutions)
into def sum3(s) :
"""Solve the 3SUM problem in O(n^2) time."""
tab = {}
solutions = {}
l = len(s)
for k in range(0,l) :
tab[ s[k] ] = k
for i in range(0,l) :
for j in range(0,l) :
k = tab.get( -s[i]-s[j] )
if k and k != i and k != j and i != j :
solutions[ {i:True, j:True, k:True} ] = True
return solutions
pretty much the same. Python is not working because it can't hash dicts, while janet interprets {1:True, 2:True} the same as {2:True,1:True} when these are keys (I think?).In the example janet returns (map keys (keys solutions))
instead of the "solution" dict that converts dicts like {{1:True, 2:True} : True} into [[1,2]], but I don't get it how.But syntactically janet is not much worse(?). |
|