Skip to content

Commit 0581ae6

Browse files
author
rw
committed
add twosum
1 parent a5fdbd5 commit 0581ae6

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

leetcode.nix

+16-29
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let
3939
in
4040
improve 1 iterations;
4141
# https://leetcode.com/problems/two-sum/
42-
twosum = { nums, target, debug ? false}:
42+
twosum = { nums, target, debug ? false }:
4343
let
4444
impl = (builtins.foldl'
4545
(acc: num:
@@ -55,31 +55,18 @@ let
5555
if compAddsToTarget
5656
then acc // {
5757
ans = [ acc.h.${complement} acc.i ];
58-
detected = true;
5958
}
6059
else acc // {
6160
h = acc.h // {
62-
${complement} = acc.i;
61+
${builtins.toString num} = acc.i;
6362
};
6463
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-
7164
}
7265
)
7366
{
7467
i = 0;
75-
h = {};
68+
h = { };
7669
ans = [ ];
77-
nums = [ ];
78-
complements = [ ];
79-
detected = false;
80-
sums = [ ];
81-
doesAddToTarget = [];
82-
everExists = [];
8370
}
8471
nums);
8572
in
@@ -89,11 +76,7 @@ in
8976
# using builtins.toJSON to eagerly evaluate the tests
9077
# jq then pretty prints the result
9178
builtins.toJSON {
92-
twosum = twosum {
93-
nums = [ 2 7 11 15 ];
94-
target = 9;
95-
debug = true;
96-
};
79+
9780
tests = [
9881
(assert remainder 10 3 == 1; "remainder works")
9982
(assert perfectNumber 6; "six is perfect")
@@ -107,14 +90,18 @@ builtins.toJSON {
10790
(assert fib 6 == 8; "fib 6 is 8")
10891
(assert facTCO 15 == 1307674368000; "tail call optimized factorial works")
10992
(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+
)
118105

119106
];
120107
}

0 commit comments

Comments
 (0)