GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added first 2 solutions. · onowdev/coding-problems@d0a71a1

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • Strings
    • reverse_sentence.py
    • reverse_words_in_sentence.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

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 numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All solutions are written in [Python](https://www.python.org/) (more precisely,
77
- [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).
88

99
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). \
1011
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).\
1112
Also in this README you can find many resources for learning algorithms: [Tutorials (Courses & Videos)](#Tutorials), [Books](#Books), [Training Sites](#Training), [Other Resources](#Other).
1213

@@ -15,13 +16,13 @@ Also in this README you can find many resources for learning algorithms: [Tutori
1516

1617
Each solution/problem in this repo belongs to one of these categories:
1718

18-
1. [Lists](/)
19-
2. [Strings](/)
19+
1. [Strings](/)
20+
2. [Lists](/)
2021
3. [Linked Lists](/)
2122
4. [Trees](/)
22-
5. [Graphs](/)
23+
5. [Dynamic Programming](/)
2324
6. [Backtracking](/)
24-
7. [Dynamic Programming](/)
25+
7. [Graphs](/)
2526
8. [Math](/)
2627
9. [Other](/)
2728

@@ -36,8 +37,8 @@ Problem name
3637
3738
Problem explanation
3839
39-
Sample input: XXX
40-
Sample output: XXX
40+
Input: XXX
41+
Output: XXX
4142
Output explanation: XXX
4243
4344
=========================================
@@ -53,22 +54,25 @@ Solution 2 explanation
5354
############
5455
# Solution #
5556
############
56-
def name_of_function(params):
57+
def name_of_problem(params):
5758
# description of code
5859
pass
5960

6061
##############
6162
# Solution 2 #
6263
##############
63-
def name_of_function_2(params):
64+
def name_of_problem_2(params):
6465
# description of code
6566
pass
6667

6768
###########
6869
# Testing #
6970
###########
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'))
7276
```
7377

7478
## Tutorials
Collapse file

‎Strings/reverse_sentence.py‎

Copy file name to clipboard
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

‎Strings/reverse_words_in_sentence.py‎

Copy file name to clipboard
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.