about summary refs log tree commit diff
path: root/deepmind/part_two/reverse-string-in-place.ts
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-02-12T15·53+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-02-12T15·53+0000
commit5ec5a6da8cbfe3c35558fd2c17ef779b5d0ccb54 (patch)
tree88729728b670eb90d4b3f4dfc2c242c5a1d77b0b /deepmind/part_two/reverse-string-in-place.ts
parente1427dd8b1ec59f46d16d1c6591364d83e75b39e (diff)
Complete InterviewCake reverse-string-in-place
Wrote a function in TypeScript to reverse an array of characters mutatively.
Diffstat (limited to 'deepmind/part_two/reverse-string-in-place.ts')
-rw-r--r--deepmind/part_two/reverse-string-in-place.ts17
1 files changed, 12 insertions, 5 deletions
diff --git a/deepmind/part_two/reverse-string-in-place.ts b/deepmind/part_two/reverse-string-in-place.ts
index 628b95a6bf91..d714dfef997f 100644
--- a/deepmind/part_two/reverse-string-in-place.ts
+++ b/deepmind/part_two/reverse-string-in-place.ts
@@ -1,6 +1,13 @@
-function main(x: number) {
-  console.log(x + 1);
-  console.log("Hello, world.");
-}
+// Reverse array of characters, `xs`, mutatively.
+function reverse(xs: Array<string>) {
+  let i: number = 0;
+  let j: number = xs.length - 1;
 
-main(10);
+  while (i < j) {
+    let tmp = xs[i];
+    xs[i] = xs[j]
+    xs[j] = tmp
+    i += 1
+    j -= 1
+  }
+}