Skip to content

Commit 682daa9

Browse files
author
Shehab Abdel-Salam
committed
Add 3 more challenges
1 parent bad5ed6 commit 682daa9

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Challenge 16 - Word Search
2+
# Write a function that takes a 2D matrix of letters and a word
3+
# and returns True if the word is present in the matrix.
4+
5+
# The word can be constructed from letters of sequentially adjacent cell,
6+
# where "adjacent" cells are those horizontally or vertically neighboring.
7+
# The same letter cell may not be used more than once.
8+
9+
# Example:
10+
# Given the following matrix:
11+
# [
12+
# ['A', 'B', 'C', 'E'],
13+
# ['S', 'F', 'C', 'S'],
14+
# ['A', 'D', 'E', 'E'],
15+
# ]
16+
#
17+
# word_search(matrix, "ABCCED") -> True
18+
# word_search(matrix, "SEE") -> True
19+
# word_search(matrix, "ABCB") -> False
20+
21+
22+
def is_word_in_matrix(matrix: list[list[str]], word: str) -> bool: ...
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Challenge 17 - Compare Version Numbers
2+
# Write a function that compares two version numbers version1 and version2.
3+
# To compare version numbers, compare their components in left-to-right order.
4+
# Components are separated by a dot '.' and each component may contain
5+
# multiple digits. Every component contains non-negative integers except
6+
# for the first component. The first component is a non-negative integer
7+
# and the rest of the components are separated by a dot '.'.
8+
# For example, 2.5.33 and 0.1 are valid version numbers.
9+
# To compare version numbers:
10+
# If version1 > version2 return 1,
11+
# If version1 < version2 return -1,
12+
# Otherwise return 0.
13+
14+
15+
def compare_versions(version1: str, version2: str) -> int: ...
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Challenge 18 - Flatten Nested List Iterator
2+
# Write an iterator that will flatten a nested list of integers.
3+
# The iterator should have two methods:
4+
# next() and hasNext().
5+
#
6+
# For example, given the following nested list:
7+
# [
8+
# 1,
9+
# [4, [6]]
10+
# ]
11+
#
12+
# The iterator should return 1, 4, 6.
13+
14+
15+
class NestedIterator:
16+
def __init__(self, nested_list: list[list[int]]): ...
17+
18+
def next(self) -> int: ...
19+
20+
def hasNext(self) -> bool: ...

ninja_challenges/tests/test_ninja_challenges.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from ..exercises.challenge_13 import is_monotonic
1414
from ..exercises.challenge_14 import add_matrices
1515
from ..exercises.challenge_15 import transpose_matrix
16+
from ..exercises.challenge_16 import is_word_in_matrix
17+
from ..exercises.challenge_17 import compare_versions
18+
from ..exercises.challenge_18 import NestedIterator
1619

1720

1821
def test_challenge_01():
@@ -75,3 +78,80 @@ def test_challenge_14():
7578

7679
def test_challenge_15():
7780
assert transpose_matrix([]) == []
81+
82+
83+
def test_challenge_16():
84+
assert is_word_in_matrix([], "") is False
85+
assert (
86+
is_word_in_matrix(
87+
[["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], "ABCCED"
88+
)
89+
is True
90+
)
91+
assert (
92+
is_word_in_matrix(
93+
[["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], "SEE"
94+
)
95+
is True
96+
)
97+
assert (
98+
is_word_in_matrix(
99+
[["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], "ABCB"
100+
)
101+
is False
102+
)
103+
assert (
104+
is_word_in_matrix(
105+
[["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]],
106+
"ABCESEEDAS",
107+
)
108+
is True
109+
)
110+
assert (
111+
is_word_in_matrix(
112+
[["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]],
113+
"ABCESEDAS",
114+
)
115+
is False
116+
)
117+
118+
119+
def test_challenge_17():
120+
assert compare_versions("1.01", "1.001") == 0
121+
assert compare_versions("1.0", "1.0.0") == 0
122+
assert compare_versions("0.1", "1.1") == -1
123+
assert compare_versions("1.0.1", "1") == 1
124+
assert compare_versions("1.0.1", "1.0.10") == -1
125+
assert compare_versions("1.0.1", "1.0.1") == 0
126+
assert compare_versions("1.0.1", "1.0.2") == -1
127+
assert compare_versions("1.0.1", "1.0.0") == 1
128+
129+
130+
def test_challenge_18():
131+
iterator0 = NestedIterator([[1, [2, [3, [4]]]]])
132+
assert iterator0.next() == 1
133+
assert iterator0.next() == 2
134+
assert iterator0.next() == 3
135+
assert iterator0.next() == 4
136+
assert iterator0.hasNext() is False
137+
138+
iterator1 = NestedIterator([[1, 2], [3], [4, 5, 6]])
139+
assert iterator1.next() == 1
140+
assert iterator1.next() == 2
141+
assert iterator1.next() == 3
142+
assert iterator1.next() == 4
143+
assert iterator1.next() == 5
144+
assert iterator1.next() == 6
145+
assert iterator1.hasNext() is False
146+
147+
iterator2 = NestedIterator([[1, 2, 3], [4, [5, 6]], [7, 8, 9]])
148+
assert iterator2.next() == 1
149+
assert iterator2.next() == 2
150+
assert iterator2.next() == 3
151+
assert iterator2.next() == 4
152+
assert iterator2.next() == 5
153+
assert iterator2.next() == 6
154+
assert iterator2.next() == 7
155+
assert iterator2.next() == 8
156+
assert iterator2.next() == 9
157+
assert iterator2.hasNext() is False

0 commit comments

Comments
 (0)