GeezerVison is showing github.com — Added first 2 solutions. · onowdev/coding-problems@d0a71a1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +14-10Lines changed: 14 additions & 10 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,7 @@ All solutions are written in [Python](https://www.python.org/) (more precisely, | 7 | 7 | - [heapq](https://docs.python.org/3/library/heapq.html) (used when there is a need for [Priority Queue](https://en.wikipedia.org/wiki/Priority_queue) data structure). | 8 | 8 |
| 9 | 9 | So, to execute these solutions there is no need from installing any external packages. \ | | 10 | +Coding style and name conventions are described in this file [PEP8](https://www.python.org/dev/peps/pep-0008). \ | 10 | 11 | These problems are from sites like [LeetCode](https://leetcode.com/) (you can find more than 30 sites like this in the [Training Sites](#Training) section).\ | 11 | 12 | Also in this README you can find many resources for learning algorithms: [Tutorials (Courses & Videos)](#Tutorials), [Books](#Books), [Training Sites](#Training), [Other Resources](#Other). | 12 | 13 |
| @@ -15,13 +16,13 @@ Also in this README you can find many resources for learning algorithms: [Tutori | 15 | 16 |
| 16 | 17 | Each solution/problem in this repo belongs to one of these categories: | 17 | 18 |
| 18 | | -1. [Lists](/) | 19 | | -2. [Strings](/) | | 19 | +1. [Strings](/) | | 20 | +2. [Lists](/) | 20 | 21 | 3. [Linked Lists](/) | 21 | 22 | 4. [Trees](/) | 22 | | -5. [Graphs](/) | | 23 | +5. [Dynamic Programming](/) | 23 | 24 | 6. [Backtracking](/) | 24 | | -7. [Dynamic Programming](/) | | 25 | +7. [Graphs](/) | 25 | 26 | 8. [Math](/) | 26 | 27 | 9. [Other](/) | 27 | 28 |
| @@ -36,8 +37,8 @@ Problem name | 36 | 37 |
| 37 | 38 | Problem explanation | 38 | 39 |
| 39 | | -Sample input: XXX | 40 | | -Sample output: XXX | | 40 | +Input: XXX | | 41 | +Output: XXX | 41 | 42 | Output explanation: XXX | 42 | 43 |
| 43 | 44 | ========================================= | @@ -53,22 +54,25 @@ Solution 2 explanation | 53 | 54 | ############ | 54 | 55 | # Solution # | 55 | 56 | ############ | 56 | | -def name_of_function(params): | | 57 | +def name_of_problem(params): | 57 | 58 | # description of code | 58 | 59 | pass | 59 | 60 |
| 60 | 61 | ############## | 61 | 62 | # Solution 2 # | 62 | 63 | ############## | 63 | | -def name_of_function_2(params): | | 64 | +def name_of_problem_2(params): | 64 | 65 | # description of code | 65 | 66 | pass | 66 | 67 |
| 67 | 68 | ########### | 68 | 69 | # Testing # | 69 | 70 | ########### | 70 | | -print(name_of_function('example') == 'result') | 71 | | -print(name_of_function_2('example2') == 'result2') | | 71 | +# result = 'result' | | 72 | +print(name_of_problem('example')) | | 73 | + | | 74 | +# result = 'result2' | | 75 | +print(name_of_problem_2('example2')) | 72 | 76 | ``` | 73 | 77 |
| 74 | 78 | ## Tutorials | |
Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Reverse words in sentence | | 3 | +
| | 4 | +Reverse words in a given string, in constant space and linear time complexity. | | 5 | +
| | 6 | +Input: 'i like this program very much' - In Python this should be a list (the string operations are slow/linear time) | | 7 | +Output: 'hcum yrev margorp siht ekil i' | | 8 | +
| | 9 | +Input: 'how are you' | | 10 | +Output: 'uoy era woh' | | 11 | +
| | 12 | +========================================= | | 13 | +Reverse the whole sentence by swapping pair letters in place (first with last, second with second from the end, etc). | | 14 | + Time Complexity: O(N) | | 15 | + Space Complexity: O(1) | | 16 | +''' | | 17 | + | | 18 | + | | 19 | +############ | | 20 | +# Solution # | | 21 | +############ | | 22 | + | | 23 | +def reverse_sentence(sentence): | | 24 | + start = 0 | | 25 | + end = len(sentence) | | 26 | + | | 27 | + for i in range((end - start) // 2): | | 28 | + local_start = start + i | | 29 | + local_end = end - i - 1 | | 30 | + # swap each char with the pair from the other part of the string | | 31 | + temp = sentence[local_start] | | 32 | + sentence[local_start] = sentence[local_end] | | 33 | + sentence[local_end] = temp | | 34 | + | | 35 | + | | 36 | +########### | | 37 | +# Testing # | | 38 | +########### | | 39 | + | | 40 | +# Test 1 | | 41 | +s = ['i', ' ', 'l', 'i', 'k', 'e', ' ', 't', 'h', 'i', 's', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', ' ', 'v', 'e', 'r', 'y', ' ', 'm', 'u', 'c', 'h'] | | 42 | +reverse_sentence(s) | | 43 | +# correct result = ['h', 'c', 'u', 'm', ' ', 'y', 'r', 'e', 'v', ' ', 'm', 'a', 'r', 'g', 'o', 'r', 'p', ' ', 's', 'i', 'h', 't', ' ', 'e', 'k', 'i', 'l', ' ', 'i'] | | 44 | +print(s) | | 45 | + | | 46 | +# Test 2 | | 47 | +s = ['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u'] | | 48 | +reverse_sentence(s) | | 49 | +# correct result = ['u', 'o', 'y', ' ', 'e', 'r', 'a', ' ', 'w', 'o', 'h'] | | 50 | +print(s) |
Collapse file Copy file name to clipboard +70Lines changed: 70 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +''' | | 2 | +Reverse words in sentence | | 3 | +
| | 4 | +Reverse words in a given string, in constant space and linear time complexity. | | 5 | +
| | 6 | +Input: 'i like this program very much' - In Python this should be a list (the string operations are slow/linear time) | | 7 | +Output: 'much very program this like i' | | 8 | +
| | 9 | +Input: 'how are you' | | 10 | +Output: 'you are how' | | 11 | +
| | 12 | +========================================= | | 13 | +First, find each word and reverse it (in place, by swapping the letters), | | 14 | +after all words are reversed reverse the whole sentence (in place, by swapping the letters) | | 15 | +and the first word will be last and in the original form. | | 16 | +In total, there will be only 2 iterations through the whole string. | | 17 | + Time Complexity: O(N) | | 18 | + Space Complexity: O(1) | | 19 | +''' | | 20 | + | | 21 | + | | 22 | +############ | | 23 | +# Solution # | | 24 | +############ | | 25 | + | | 26 | +def reverse_words_in_sentence(sentence): | | 27 | + n = len(sentence) | | 28 | + start = 0 | | 29 | + | | 30 | + # reverse all words | | 31 | + for i in range(n): | | 32 | + if sentence[i] == ' ': | | 33 | + # in this moment you're sure that the word is complete | | 34 | + reverse_word(sentence, start, i) | | 35 | + start = i + 1 | | 36 | + # reverse the last word | | 37 | + reverse_word(sentence, start, n) | | 38 | + | | 39 | + # reverse the whole sentence | | 40 | + reverse_sentence(sentence) | | 41 | + | | 42 | +def reverse_sentence(sentence): | | 43 | + reverse_word(sentence, 0, len(sentence)) | | 44 | + | | 45 | +def reverse_word(sentence, start, end): | | 46 | + for i in range((end - start) // 2): | | 47 | + local_start = start + i | | 48 | + local_end = end - i - 1 | | 49 | + # swap each char with the pair from the other part of the string | | 50 | + temp = sentence[local_start] | | 51 | + sentence[local_start] = sentence[local_end] | | 52 | + sentence[local_end] = temp | | 53 | + | | 54 | + | | 55 | + | | 56 | +########### | | 57 | +# Testing # | | 58 | +########### | | 59 | + | | 60 | +# Test 1 | | 61 | +s = ['i', ' ', 'l', 'i', 'k', 'e', ' ', 't', 'h', 'i', 's', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', ' ', 'v', 'e', 'r', 'y', ' ', 'm', 'u', 'c', 'h'] | | 62 | +reverse_words_in_sentence(s) | | 63 | +# correct result = ['m', 'u', 'c', 'h', ' ', 'v', 'e', 'r', 'y', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', ' ', 't', 'h', 'i', 's', ' ', 'l', 'i', 'k', 'e', ' ', 'i'] | | 64 | +print(s) | | 65 | + | | 66 | +# Test 2 | | 67 | +s = ['h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u'] | | 68 | +reverse_words_in_sentence(s) | | 69 | +# correct result = ['y', 'o', 'u', ' ', 'a', 'r', 'e', ' ', 'h', 'o', 'w'] | | 70 | +print(s) |
|