From 5ec5a6da8cbfe3c35558fd2c17ef779b5d0ccb54 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Wed, 12 Feb 2020 15:53:21 +0000 Subject: Complete InterviewCake reverse-string-in-place Wrote a function in TypeScript to reverse an array of characters mutatively. --- deepmind/part_two/reverse-string-in-place.ts | 17 ++++++++++++----- deepmind/part_two/todo.org | 2 +- 2 files changed, 13 insertions(+), 6 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) { + 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 + } +} diff --git a/deepmind/part_two/todo.org b/deepmind/part_two/todo.org index 9a163e01672b..510073e6e2cd 100644 --- a/deepmind/part_two/todo.org +++ b/deepmind/part_two/todo.org @@ -1,6 +1,6 @@ * Array and string manipulation ** TODO Merging Meeting Times -** TODO Reverse String in Place +** DONE Reverse String in Place ** TODO Reverse Words ** TODO Merge Sorted Arrays ** TODO Cafe Order Checker -- cgit 1.4.1