From f286f8fdbe4df5af9b84a7f727eab801a98cc426 Mon Sep 17 00:00:00 2001 From: Albert Hu Date: Tue, 30 May 2017 10:30:49 -0700 Subject: [PATCH 1/3] Day 49: start --- day49/README.md | 33 +++++++++++++++++++++++++++++++++ day49/heapsort.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 day49/README.md create mode 100644 day49/heapsort.py diff --git a/day49/README.md b/day49/README.md new file mode 100644 index 0000000..00ffe3b --- /dev/null +++ b/day49/README.md @@ -0,0 +1,33 @@ +Question of the day: implement heapsort +http://www.techiedelight.com/heap-sort-place-place-implementation-c-c/ + +## Ideas + +We can implement the sort in-place or out-of-place. + +In either case, the essential idea is that we take an input +array of unsorted elements and perform an `O(n)` heapify +operation on the array, and then collect the output of +successive pops from the heap until there are no more elements +left in the heap. The output of those pops, collected in +order, is the sorted result. Each pop is `O(logn)`, and there +are `n` pops, which brings the total runtime of this sort to +`O(n + nlogn)` which is asymptotically equivalent to the +runtimes of efficient searches such as mergesort and quicksort. + +In the out-of-place version, we could rearrange the input +array into a heap and collect the results of heap popping +in a separate array, which would take `O(n)` space. + +In the in-place version, you could use part of the input +array for storing the heap and part of the array for storing +the results of popping. One advantage that heapsort has +over both quicksort and mergesort is that it can be done +in-place like this, saving some memory. + +## Code + +[Python](./day49) + +## Follow up + diff --git a/day49/heapsort.py b/day49/heapsort.py new file mode 100644 index 0000000..12c39a6 --- /dev/null +++ b/day49/heapsort.py @@ -0,0 +1,38 @@ +def outofplace(arr): + return arr + +def inplace(arr): + return + +def testHeapsort(): + assert outofplace([1]) == [1] + assert outofplace([2,1]) == [1,2] + assert outofplace([1,6,4]) == [1,4,6] + assert outofplace([-1,6,4]) == [-1,4,6] + assert outofplace([3,3,3,8,1,6,9,-3,-5,-19]) == [-19,-5,-3,1,3,3,3,6,8,9] + + t1, o1 = [1], [1] + inplace(t1) + assert t1 == o1 + + t2, o2 = [2, 1], [1, 2] + inplace(t2) + assert t2 == o2 + + t3, o3 = [1,6,4], [1,4,6] + inplace(t3) + assert t3 == o3 + + t4, o4 = [-1,6,4], [-1,4,6] + inplace(t4) + assert t4 == o4 + + t5, o5 = [3,3,3,8,1,6,9,-3,-5,-19], [-19,-5,-3,1,3,3,3,6,8,9] + inplace(t5) + assert t5 == o5 + +def tests(): + testHeapsort() + +if __name__ == "__main__": + tests() From 15372c7d70b26667eb1f22d8ce818cf51847ccec Mon Sep 17 00:00:00 2001 From: Albert Hu Date: Tue, 30 May 2017 10:49:50 -0700 Subject: [PATCH 2/3] Day 49: finish out-of-place heapsort --- day49/heapsort.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/day49/heapsort.py b/day49/heapsort.py index 12c39a6..d60c9a9 100644 --- a/day49/heapsort.py +++ b/day49/heapsort.py @@ -1,8 +1,16 @@ +import heapq + def outofplace(arr): - return arr + heapq.heapify(arr) + result = [] + size = len(arr) + while size > 0: + result.append(heapq.heappop(arr)) + size -= 1 + return result def inplace(arr): - return + print arr def testHeapsort(): assert outofplace([1]) == [1] From 56c0a1d255769500ee058f544b7488a2b23d8358 Mon Sep 17 00:00:00 2001 From: Albert Hu Date: Thu, 1 Mar 2018 20:58:04 -0800 Subject: [PATCH 3/3] Do day 49 --- day49/Accumul.java | 17 +++++++++++++++++ day49/README.md | 28 ++++------------------------ day49/heapsort.py | 46 ---------------------------------------------- 3 files changed, 21 insertions(+), 70 deletions(-) create mode 100644 day49/Accumul.java delete mode 100644 day49/heapsort.py diff --git a/day49/Accumul.java b/day49/Accumul.java new file mode 100644 index 0000000..6230a2e --- /dev/null +++ b/day49/Accumul.java @@ -0,0 +1,17 @@ +public class Accumul { + + public static String accum(String s) { + StringBuilder newString = new StringBuilder(); + for (int i = 0; i < s.length(); i++) { + char currentChar = s.charAt(i); + newString.append(Character.toUpperCase(currentChar)); + for (int j = 0; j < i; j++) { + newString.append(Character.toLowerCase(currentChar)); + } + if (i != s.length() - 1) { + newString.append("-"); + } + } + return newString.toString(); + } +} \ No newline at end of file diff --git a/day49/README.md b/day49/README.md index 00ffe3b..d368922 100644 --- a/day49/README.md +++ b/day49/README.md @@ -1,33 +1,13 @@ -Question of the day: implement heapsort -http://www.techiedelight.com/heap-sort-place-place-implementation-c-c/ +Question of the day: Accumul Kata +http://www.codewars.com/kata/5667e8f4e3f572a8f2000039/solutions/java/all/clever ## Ideas -We can implement the sort in-place or out-of-place. - -In either case, the essential idea is that we take an input -array of unsorted elements and perform an `O(n)` heapify -operation on the array, and then collect the output of -successive pops from the heap until there are no more elements -left in the heap. The output of those pops, collected in -order, is the sorted result. Each pop is `O(logn)`, and there -are `n` pops, which brings the total runtime of this sort to -`O(n + nlogn)` which is asymptotically equivalent to the -runtimes of efficient searches such as mergesort and quicksort. - -In the out-of-place version, we could rearrange the input -array into a heap and collect the results of heap popping -in a separate array, which would take `O(n)` space. - -In the in-place version, you could use part of the input -array for storing the heap and part of the array for storing -the results of popping. One advantage that heapsort has -over both quicksort and mergesort is that it can be done -in-place like this, saving some memory. +Just do it ## Code -[Python](./day49) +[Java](./day49) ## Follow up diff --git a/day49/heapsort.py b/day49/heapsort.py deleted file mode 100644 index d60c9a9..0000000 --- a/day49/heapsort.py +++ /dev/null @@ -1,46 +0,0 @@ -import heapq - -def outofplace(arr): - heapq.heapify(arr) - result = [] - size = len(arr) - while size > 0: - result.append(heapq.heappop(arr)) - size -= 1 - return result - -def inplace(arr): - print arr - -def testHeapsort(): - assert outofplace([1]) == [1] - assert outofplace([2,1]) == [1,2] - assert outofplace([1,6,4]) == [1,4,6] - assert outofplace([-1,6,4]) == [-1,4,6] - assert outofplace([3,3,3,8,1,6,9,-3,-5,-19]) == [-19,-5,-3,1,3,3,3,6,8,9] - - t1, o1 = [1], [1] - inplace(t1) - assert t1 == o1 - - t2, o2 = [2, 1], [1, 2] - inplace(t2) - assert t2 == o2 - - t3, o3 = [1,6,4], [1,4,6] - inplace(t3) - assert t3 == o3 - - t4, o4 = [-1,6,4], [-1,4,6] - inplace(t4) - assert t4 == o4 - - t5, o5 = [3,3,3,8,1,6,9,-3,-5,-19], [-19,-5,-3,1,3,3,3,6,8,9] - inplace(t5) - assert t5 == o5 - -def tests(): - testHeapsort() - -if __name__ == "__main__": - tests()