Skip to content

Commit e2eec9b

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

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Challenge 19 - Count Servers that Communicate
2+
# You are given a map of a server center, represented as a m * n integer matrix grid,
3+
# where 1 means that on that cell there is a server and 0 means that it is no server.
4+
# Two servers are said to communicate if they are on the same row or on the same column.
5+
# Return the number of servers that communicate with any other server.
6+
7+
# Example 1:
8+
# Input: grid = [[1,0],[0,1]]
9+
# Output: 0
10+
# Explanation: No servers can communicate with others.
11+
12+
# Example 2:
13+
# Input: grid = [[1,0],[1,1]]
14+
# Output: 3
15+
# Explanation: All three servers can communicate with at least one other server.
16+
17+
# Example 3:
18+
# Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
19+
# Output: 4
20+
# Explanation: The two servers in the first row can communicate with each other.
21+
22+
23+
def count_servers(grid: list[list[int]]) -> int: ...
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Challenge 20 - Integer to Roman
2+
# Seven Roman numerals are represented by the following symbols: I, V, X, L, C, D, and M.
3+
#
4+
# Symbol Value
5+
# I 1
6+
# V 5
7+
# X 10
8+
# L 50
9+
# C 100
10+
# D 500
11+
# M 1000
12+
13+
# Your task is to convert an integer to a Roman numeral.
14+
15+
# Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
16+
17+
# If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
18+
# If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol, for example, 4 is 1 (I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Only the following subtractive forms are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM).
19+
# Only powers of 10 (I, X, C, M) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (V), 50 (L), or 500 (D) multiple times. If you need to append a symbol 4 times use the subtractive form.
20+
21+
22+
def integer_to_roman(num: int) -> str: ...
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Challenge 21 - Interval List Intersections
2+
# Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
3+
# Return the intersection of these two interval lists.
4+
# A closed interval [a, b] (with a < b) denotes the set of real numbers x with a <= x <= b.
5+
# The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval.
6+
# For example, the intersection of [1, 3] and [2, 4] is [2, 3].
7+
#
8+
# Example 1:
9+
# Input: first_list = [[0,2],[5,10],[13,23],[24,25]],
10+
# second_list = [[1,5],[8,12],[15,24],[25,26]]
11+
# Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
12+
#
13+
# Example 2:
14+
# Input: first_list = [[1,3],[5,9]],
15+
# second_list = []
16+
# Output: []
17+
18+
19+
def interval_intersection(
20+
first_list: list[list[int]], second_list: list[list[int]]
21+
) -> list[list[int]]: ...

ninja_challenges/tests/test_ninja_challenges.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from ..exercises.challenge_16 import is_word_in_matrix
1717
from ..exercises.challenge_17 import compare_versions
1818
from ..exercises.challenge_18 import NestedIterator
19+
from ..exercises.challenge_19 import count_servers
20+
from ..exercises.challenge_20 import integer_to_roman
21+
from ..exercises.challenge_21 import interval_intersection
1922

2023

2124
def test_challenge_01():
@@ -155,3 +158,32 @@ def test_challenge_18():
155158
assert iterator2.next() == 8
156159
assert iterator2.next() == 9
157160
assert iterator2.hasNext() is False
161+
162+
163+
def test_challenge_19():
164+
assert count_servers([[1, 0], [0, 1]]) == 0
165+
assert count_servers([[1, 0], [1, 1]]) == 3
166+
assert count_servers([[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) == 4
167+
assert count_servers([[1, 0, 0], [0, 0, 1], [0, 0, 1]]) == 3
168+
assert count_servers([[1, 0, 0], [0, 1, 1], [0, 0, 1]]) == 4
169+
assert count_servers([[1, 0, 0], [0, 1, 1], [0, 0, 0]]) == 2
170+
171+
172+
def test_challenge_20():
173+
assert integer_to_roman(3) == "III"
174+
assert integer_to_roman(4) == "IV"
175+
assert integer_to_roman(9) == "IX"
176+
assert integer_to_roman(58) == "LVIII"
177+
assert integer_to_roman(1994) == "MCMXCIV"
178+
assert integer_to_roman(3999) == "MMMCMXCIX"
179+
assert integer_to_roman(39999) == "MMM"
180+
181+
182+
def test_challenge_21():
183+
assert interval_intersection(
184+
[[0, 2], [5, 10], [13, 23], [24, 25]], [[1, 5], [8, 12], [15, 24], [25, 26]]
185+
) == [[1, 2], [5, 5], [8, 10], [15, 23], [24, 24], [25, 25]]
186+
assert interval_intersection([[1, 3], [5, 9]], []) == []
187+
assert interval_intersection([[1, 7]], [[3, 10]]) == [[3, 7]]
188+
assert interval_intersection([[1, 7]], [[8, 10]]) == []
189+
assert interval_intersection([[1, 7]], [[7, 10]]) == [[7, 7]]

0 commit comments

Comments
 (0)