Skip to content

Commit af90c83

Browse files
committed
Added new problem.
1 parent 0c3fab9 commit af90c83

File tree

9 files changed

+208
-20
lines changed

9 files changed

+208
-20
lines changed

.vscode/launch.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal"
13+
},
714
{
815
"type": "java",
916
"name": "Debug (Launch) - Current File",
@@ -29,7 +36,7 @@
2936
"type": "python",
3037
"request": "launch",
3138
"stopOnEntry": true,
32-
"pythonPath": "${command:python.interpreterPath}",
39+
"python": "${command:python.interpreterPath}",
3340
"program": "${file}",
3441
"cwd": "${workspaceRoot}",
3542
"env": {},

.vscode/python.code-snippets

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
// ],
1616
// "description": "Log output to console"
1717
// }
18-
1918
"Solution": {
2019
"scope": "python",
2120
"prefix": "solution",
2221
"body": [
2322
"class Solution:",
2423
"\tdef method(self):",
2524
"\t\t",
26-
"def main():",
25+
"def main():",
2726
"\tsolution = Solution()",
2827
"",
2928
"if __name__ == '__main__':",
@@ -38,12 +37,12 @@
3837
"import unittest",
3938
"",
4039
"class TrivialCase(unittest.TestCase):",
41-
"\tdef testTrivialCase1(self):",
42-
"\t\t\"\"\"\"trivial case 1\"\"\"",
40+
"\tdef test_trivial_case_1(self):",
41+
"\t\t\"\"\"\"trivial case 1\"\"\"",
4342
"\t\texpected = ",
4443
"\t\tsol = solution.Solution()",
45-
"\t\tactual = sol.",
46-
"\t\tself.assertEqual(expected, actual)",
44+
"\t\tactual = sol.",
45+
"\t\tself.assertEqual(expected, actual)",
4746
"",
4847
"if __name__ == \"__main__\":",
4948
"\tunittest.main()"

scripts/run_unit_tests.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
import os
22
import unittest
3-
import traceback
43
import sys
54
import re
65

7-
def runTests():
6+
7+
def run_tests():
88
lsPaths = []
9-
cwd = os.getcwd()
9+
cwd = os.getcwd()
1010

1111
#Find all relevant subdirectories that contain unit tests
1212
root_dir = "src"
1313
for root, dirs, files in os.walk(root_dir):
1414
dirs.sort()
1515
for file in files:
16-
if "pycache" not in root and file.startswith("test_"):
16+
if "pycache" not in root and 'test_' in file:
1717
test_folder = os.path.join(cwd, root)
18-
lsPaths.append(test_folder)
18+
lsPaths.append(test_folder)
1919

2020
#loop through subdirectories and run individually
2121
for path in lsPaths:
2222
sys.path.append(path)
2323
loader = unittest.TestLoader()
24-
suite = unittest.TestSuite()
2524
suite = loader.discover(path)
2625
testresult = unittest.TextTestRunner().run(suite)
2726
sys.path.remove(path)
@@ -39,14 +38,16 @@ def runTests():
3938
if len(testresult.failures) > 0 or len(testresult.errors) > 0:
4039
raise Exception()
4140

41+
4242
def main():
4343
"""main method."""
44-
try:
44+
try:
4545
print("Running unit tests...")
46-
runTests()
46+
run_tests()
4747
print("Finished running unit tests.")
4848
except Exception as e:
4949
sys.exit(f"Failure in unit test run. Exception: {e}.")
5050

51+
5152
if __name__ == '__main__':
5253
main()

src/Glassdoor/src/Facebook/NumberOfStringsFromEncodedAlphabet/Python/solution.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ def find_strings(self, x):
77
if int(i) != 0:
88
total += 1
99
nums.append(int(i))
10-
if prev:
11-
if int(prev + i) != int(i) and int(prev+i) < 27:
12-
total += 1
13-
nums.append(int(prev+i))
10+
if prev and int(prev + i) != int(i) and int(prev + i) < 27:
11+
total += 1
12+
nums.append(int(prev + i))
1413
prev = i
1514
return total, nums
1615

16+
1717
def main():
1818
solution = Solution()
1919
print(solution.find_strings(23413259802))
2020

21+
2122
if __name__ == '__main__':
2223
main()
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import solution
22
import unittest
33

4+
45
class TrivialCase(unittest.TestCase):
5-
def testTrivialCase1(self):
6+
def test_trivial_case_1(self):
67
"""trivial case 1"""
78
expected = (13, [2, 3, 23, 4, 1, 3, 13, 2, 5, 25, 9, 8, 2])
89
sol = solution.Solution()
910
actual = sol.find_strings(23413259802)
1011
self.assertEqual(expected, actual)
1112

13+
1214
if __name__ == "__main__":
1315
unittest.main()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def method(self):
3+
4+
def main():
5+
solution = Solution()
6+
7+
if __name__ == '__main__':
8+
main()

src/HackerRank/src/Miscellaneous/DamDesign/Python/test_solution.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"type": "Coding",
3+
"name": "Dam Design",
4+
"origin": {
5+
"name": "HackerRank",
6+
"link": ""
7+
},
8+
"companies": ["", ""],
9+
"categories": [
10+
{
11+
"name": "",
12+
"children": [
13+
{
14+
"name": "",
15+
"children": []
16+
}
17+
]
18+
},
19+
{
20+
"name": "Difficulty",
21+
"children": [
22+
{
23+
"name": "Easy",
24+
"children": []
25+
}
26+
]
27+
}
28+
],
29+
"tags": ["Arrays", ""],
30+
"buckets": ["", ""]
31+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# Problem Definition
2+
3+
## Description
4+
5+
Your company is designing a dam to be built across a stream to create a small lake. To reduce materials cost, it will be made of one or more concrete walls with mud packed in between them. Determine the maximum height of the mud segments in the dam with the following restrictions:
6+
7+
- One unit width of the gap between walls will contain one segment of packed mud
8+
- The height of mud in a segment cannot exceed 1 unit more than an adjacent wall or mud segment.
9+
10+
Given the placement of a number of walls and their heights, determine the maximum height of a mud segment that can be built. If no mud segment can be built, return 0.
11+
12+
Example
13+
14+
```plaintext
15+
wallPositions = [1, 2, 4, 7]
16+
wallHeights = [4, 6, 8, 11]
17+
```
18+
19+
- There is no space between the first two walls.
20+
- Between positions 2 and 4, there is one unit open for mud. Heights of the surrounding walls are 6 and 8, so the maximum height of mud is 6 + 1 = 7.
21+
- Between positions 4 and 7 there are two units. The heights of surrounding walls are 8 and 11.
22+
- The maximum height mud segment next to the wall of height 8 is 9.
23+
- The maximum height mud next to a mud segment of height 9 is 10.
24+
- Overall, mud segment heights are 7, 9 and 10, and the maximum height is 10.
25+
26+
Function Description
27+
28+
Complete the function maxHeight in the editor below.
29+
maxHeight has the following parameter(s):
30+
int wallPositions[n]: an array of integers
31+
int wallHeights[n]: an array of integers
32+
Returns:
33+
int: the maximum height mud segment that can be build
34+
35+
Constraints
36+
37+
- 1 < n ≤ 10^5
38+
- 1 ≤ wallPositions[i], wallHeights[i] ≤ 109 (where 0 ≤ i < n)
39+
40+
### Input Format For Custom Testing
41+
42+
The first line contains an integer, n, the number of elements in wallPositions.
43+
44+
Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, wallPositions[i].
45+
46+
The next line contains the integer, n, the number of elements in wallHeights.
47+
48+
Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, wallHeights[i].
49+
50+
### Sample Case 0
51+
52+
Sample Input For Custom Testing
53+
54+
```plaintext
55+
STDIN Function
56+
----- --------
57+
3 → wallPositions[] size n = 3
58+
1 → wallPositions = [1, 3, 7]
59+
3
60+
7
61+
3 → wallHeights[] size n = 3
62+
4 → wallHeights = [4, 3, 3]
63+
3
64+
3
65+
```
66+
67+
Sample Output
68+
69+
5
70+
71+
Explanation
72+
73+
The wallPositions = [1, 3, 7] and wallHeights = [4, 3, 3]. There can be a segment of height 4 at position 2 supported by walls of heights 4 and 3. Between positions 3 and 7, there can be a segment of height 4 at positions 4 and 6. Between them, a segment can be built of height 5 at position 5.
74+
75+
### Sample Case 1
76+
77+
Sample Input For Custom Testing
78+
79+
```plaintext
80+
STDIN Function
81+
----- --------
82+
2 → wallPositions[] size n = 2
83+
1 → wallPositions = [1, 10]
84+
10
85+
2 → wallHeights[] size n = 2
86+
1 → wallHeights = [1, 5]
87+
5
88+
```
89+
90+
Sample Output
91+
92+
7
93+
94+
Explanation
95+
96+
The wallPositions = [1, 10] and wallHeights = [1, 5]. The heights of the mud segments from positions 2 through 9 are [2, 3, 4, 5, 6, 7, 7, 6].
97+
98+
## Discussion
99+
100+
### Approach
101+
102+
#### Time Complexity
103+
104+
#### Space Complexity
105+
106+
### Alternate Approach 1
107+
108+
#### Time Complexity - Alternate Approach 1
109+
110+
#### Space Complexity - Alternate Approach 1
111+
112+
## Notes
113+
114+
```java
115+
public static int maxHeight(List<Integer> wallPositions, List<Integer> wallHeights) {
116+
int globalMax = 0;
117+
for (int i = 0; i < wallPositions.size()-1; i++) {
118+
if (wallPositions.get(i+1) > wallPositions.get(i) + 1) {
119+
int heightDiff = Math.abs(wallHeights.get(i) - wallHeights.get(i+1));
120+
int gap = wallPositions.get(i + 1) - wallPositions.get(i) - 1;
121+
int maxValue = 0;
122+
if(gap > heightDiff) {
123+
int taller = Math.max(wallHeights.get(i), wallHeights.get(i+1));
124+
//get the shorter one to same height and then take the mid point value for increment
125+
maxValue = taller + ((gap - heightDiff - 1)/ 2) + 1;
126+
}else {
127+
//when the height diff is larger than the gap then max would be gap added to shorter wall
128+
maxValue = Math.min(wallHeights.get(i), wallHeights.get(i+1)) + gap;
129+
}
130+
globalMax = Math.max(globalMax, maxValue);
131+
}
132+
}
133+
return globalMax;
134+
}
135+
136+
}
137+
```
138+
139+
## References

0 commit comments

Comments
 (0)