39
39
in
40
40
improve 1 iterations ;
41
41
# https://leetcode.com/problems/two-sum/
42
- twosum = { nums , target , debug ? false } :
42
+ twosum = { nums , target , debug ? false } :
43
43
let
44
44
impl = ( builtins . foldl'
45
45
( acc : num :
55
55
if compAddsToTarget
56
56
then acc // {
57
57
ans = [ acc . h . ${ complement } acc . i ] ;
58
- detected = true ;
59
58
}
60
59
else acc // {
61
60
h = acc . h // {
62
- ${ complement } = acc . i ;
61
+ ${ builtins . toString num } = acc . i ;
63
62
} ;
64
63
i = acc . i + 1 ;
65
- nums = acc . nums ++ [ num ] ;
66
- complements = acc . complements ++ [ complement ] ;
67
- sums = acc . sums ++ [ sum ] ;
68
- doesAddToTarget = acc . doesAddToTarget ++ [ compAddsToTarget ] ;
69
- everExists = acc . everExists ++ [ exists ] ;
70
-
71
64
}
72
65
)
73
66
{
74
67
i = 0 ;
75
- h = { } ;
68
+ h = { } ;
76
69
ans = [ ] ;
77
- nums = [ ] ;
78
- complements = [ ] ;
79
- detected = false ;
80
- sums = [ ] ;
81
- doesAddToTarget = [ ] ;
82
- everExists = [ ] ;
83
70
}
84
71
nums ) ;
85
72
in
89
76
# using builtins.toJSON to eagerly evaluate the tests
90
77
# jq then pretty prints the result
91
78
builtins . toJSON {
92
- twosum = twosum {
93
- nums = [ 2 7 11 15 ] ;
94
- target = 9 ;
95
- debug = true ;
96
- } ;
79
+
97
80
tests = [
98
81
( assert remainder 10 3 == 1 ; "remainder works" )
99
82
( assert perfectNumber 6 ; "six is perfect" )
@@ -107,14 +90,18 @@ builtins.toJSON {
107
90
( assert fib 6 == 8 ; "fib 6 is 8" )
108
91
( assert facTCO 15 == 1307674368000 ; "tail call optimized factorial works" )
109
92
( assert ftco 100 == 5050 ; "tail call optimized fibonacci works" )
110
- # # nums = [2,7,11,15], target = 9
111
- # (assert twosum [2 7 11 15] 9 == [0 1]; "two sum works")
112
- # # Input: nums = [3,2,4], target = 6
113
- # # Output: [1,2]
114
- # (assert twosum [3 2 4] 6 == [1 2]; "two sum works")
115
- # # Input: nums = [3,3], target = 6
116
- # # Output: [0,1]
117
- # (assert twosum [3 3] 6 == [0 1]; "two sum works")
93
+ (
94
+ assert twosum { nums = [ 2 7 11 15 ] ; target = 9 ; } == [ 0 1 ] ;
95
+ "two sum works"
96
+ )
97
+ (
98
+ assert twosum { nums = [ 3 2 4 ] ; target = 6 ; } == [ 1 2 ] ;
99
+ "two sum works"
100
+ )
101
+ (
102
+ assert twosum { nums = [ 3 3 ] ; target = 6 ; } == [ 0 1 ] ;
103
+ "two sum works"
104
+ )
118
105
119
106
] ;
120
107
}
0 commit comments