Skip to content

Commit db510f7

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Between Two Sets solved ✓
1 parent 7338f3d commit db510f7

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [Between Two Sets](https://www.hackerrank.com/challenges/between-two-sets)
2+
3+
- Difficulty: #easy
4+
- Category: #implementation
5+
6+
There will be two arrays of integers. Determine all integers that satisfy
7+
the following two conditions:
8+
9+
1. The elements of the first array are all factors of the integer being
10+
considered
11+
2. The integer being considered is a factor of all elements of the second
12+
array
13+
14+
These numbers are referred to as being between the two arrays. Determine
15+
how many such numbers exist.
16+
17+
## Example
18+
19+
$ a = [2, 6] $
20+
21+
$ b = [24, 36] $
22+
23+
There are two numbers between the arrays: $ 6 $ and $ 12 $.
24+
$ 6 \bmod 2 = 0 $, $ 6 \bmod 6 = 0 $, $ 24 \bmod 6 = 0 $ and $ 36 \bmod 6 = 0 $
25+
for the first value.
26+
$ 12 \bmod 2 = 0 $, $ 12 \bmod 6 = 0 $, and $ 24 \bmod 12 = 0 $,
27+
$ 36 \bmod 12 = 0 $ for the second value. Return $ 2 $.
28+
29+
## Function Description
30+
31+
Complete the getTotalX function in the editor below. It should return the
32+
number of integers that are betwen the sets.
33+
34+
getTotalX has the following parameter(s):
35+
36+
- int a[n]: an array of integers
37+
- int b[m]: an array of integers
38+
39+
## Returns
40+
41+
- int: the number of integers that are between the sets
42+
43+
## Input Format
44+
45+
The first line contains two space-separated integers, n and m, the number
46+
of elements in arrays a and b.
47+
The second line contains n distinct space-separated integers $ a[i] $ where
48+
$ 0 \leq i < n $.
49+
The third line contains m distinct space-separated integers $ b[j] $ where
50+
$ 0 \leq j < m $.
51+
52+
## Constraints
53+
54+
- $ 1 \leq n, m < 10 $
55+
- $ 1 \leq a[i] \leq 100 $
56+
- $ 1 \leq b[j] \leq 100 $
57+
58+
## Sample Input
59+
60+
```text
61+
2 3
62+
2 4
63+
12 32 96
64+
```
65+
66+
## Sample Output
67+
68+
```text
69+
3
70+
```
71+
72+
## Explanation
73+
74+
2 and 4 divide evenly into 4, 8, 12 and 16.
75+
76+
4, 8 and 16 divide evenly into 16, 32, 96.
77+
78+
4, 8 and 16 are the only three numbers for which each element of a is a factor
79+
and each is a factor of all elements of b.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import logging
2+
3+
LOGGER = logging.getLogger(__name__)
4+
5+
6+
def is_factor(_n: int, group: list[int]) -> bool:
7+
result: bool = True
8+
i: int = 0
9+
10+
if len(group) == 0:
11+
return False
12+
13+
while (i < len(group) and result):
14+
if _n % group[i] != 0:
15+
result = False
16+
17+
i += 1
18+
19+
return result
20+
21+
22+
def factor_of(_n: int, group: list[int]):
23+
result: bool = True
24+
i: int = 0
25+
26+
if len(group) == 0:
27+
return False
28+
29+
while (i < len(group) and result):
30+
if group[i] % _n != 0:
31+
result = False
32+
33+
i += 1
34+
35+
return result
36+
37+
38+
def get_total_x(_a: list[int], _b: list[int]) -> int:
39+
_max_ = 0
40+
for _, j in enumerate(_b):
41+
if j > _max_:
42+
_max_ = j
43+
44+
result: list[int] = []
45+
46+
for i in range(2, _max_):
47+
if is_factor(i, _a) and factor_of(i, _b):
48+
result.append(i)
49+
50+
output = len(result)
51+
LOGGER.info('Between Two Sets result: %i', output)
52+
53+
return output
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import unittest
2+
from .between_two_sets import get_total_x, is_factor, factor_of
3+
4+
5+
class TestProblemBetweenTwoSets(unittest.TestCase):
6+
7+
def test_get_total_x(self):
8+
9+
tinput: list[int] = [16, 32, 96]
10+
solution_found: int = 0
11+
12+
calculated_a = get_total_x([], tinput)
13+
14+
self.assertEqual(
15+
calculated_a, solution_found,
16+
f"get_total_x([], tinput) must be "
17+
f"=> {solution_found}")
18+
19+
calculated_b = get_total_x(tinput, [])
20+
21+
self.assertEqual(
22+
calculated_b, solution_found,
23+
f"get_total_x({tinput}, {[]}) must be "
24+
f"=> {solution_found}")
25+
26+
calculated_c = get_total_x([], [])
27+
28+
self.assertEqual(
29+
calculated_c, solution_found,
30+
f"get_total_x({[]}, {[]}) must be "
31+
f"=> {solution_found}")
32+
33+
calculated_d = is_factor(1, [])
34+
35+
self.assertEqual(
36+
calculated_d, solution_found,
37+
f"is_factor({1}, {[]}) must be "
38+
f"=> {False}")
39+
40+
calculated_e = factor_of(1, [])
41+
42+
self.assertEqual(
43+
calculated_e, solution_found,
44+
f"factor_of({1}, {[]}) must be "
45+
f"=> {False}")
46+
47+
def test_get_total_x_case_0(self):
48+
49+
_a_ = [2, 4]
50+
_b_ = [16, 32, 96]
51+
_b_reverse_ = [96, 32, 16]
52+
53+
solution_found = 3
54+
55+
self.assertEqual(
56+
get_total_x(_a_, _b_), solution_found,
57+
f"get_total_x({_a_}, {_b_}) must be "
58+
f"=> {solution_found}")
59+
60+
self.assertEqual(
61+
get_total_x(_a_, _b_reverse_), solution_found,
62+
f"get_total_x({_a_}, {_b_reverse_}) must be "
63+
f"=> {solution_found}")

0 commit comments

Comments
 (0)