Skip to content

Feature/between two sets #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/hackerrank/implementation/between_two_sets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# [Between Two Sets](https://www.hackerrank.com/challenges/between-two-sets)

- Difficulty: #easy
- Category: #implementation

There will be two arrays of integers. Determine all integers that satisfy
the following two conditions:

1. The elements of the first array are all factors of the integer being
considered
2. The integer being considered is a factor of all elements of the second
array

These numbers are referred to as being between the two arrays. Determine
how many such numbers exist.

## Example

$ a = [2, 6] $

$ b = [24, 36] $

There are two numbers between the arrays: $ 6 $ and $ 12 $.
$ 6 \bmod 2 = 0 $, $ 6 \bmod 6 = 0 $, $ 24 \bmod 6 = 0 $ and $ 36 \bmod 6 = 0 $
for the first value.
$ 12 \bmod 2 = 0 $, $ 12 \bmod 6 = 0 $, and $ 24 \bmod 12 = 0 $,
$ 36 \bmod 12 = 0 $ for the second value. Return $ 2 $.

## Function Description

Complete the getTotalX function in the editor below. It should return the
number of integers that are betwen the sets.

getTotalX has the following parameter(s):

- int a[n]: an array of integers
- int b[m]: an array of integers

## Returns

- int: the number of integers that are between the sets

## Input Format

The first line contains two space-separated integers, n and m, the number
of elements in arrays a and b.
The second line contains n distinct space-separated integers $ a[i] $ where
$ 0 \leq i < n $.
The third line contains m distinct space-separated integers $ b[j] $ where
$ 0 \leq j < m $.

## Constraints

- $ 1 \leq n, m < 10 $
- $ 1 \leq a[i] \leq 100 $
- $ 1 \leq b[j] \leq 100 $

## Sample Input

```text
2 3
2 4
12 32 96
```

## Sample Output

```text
3
```

## Explanation

2 and 4 divide evenly into 4, 8, 12 and 16.

4, 8 and 16 divide evenly into 16, 32, 96.

4, 8 and 16 are the only three numbers for which each element of a is a factor
and each is a factor of all elements of b.
53 changes: 53 additions & 0 deletions src/hackerrank/implementation/between_two_sets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import logging

LOGGER = logging.getLogger(__name__)


def is_factor(_n: int, group: list[int]) -> bool:
result: bool = True
i: int = 0

if len(group) == 0:
return False

while (i < len(group) and result):
if _n % group[i] != 0:
result = False

i += 1

return result


def factor_of(_n: int, group: list[int]):
result: bool = True
i: int = 0

if len(group) == 0:
return False

while (i < len(group) and result):
if group[i] % _n != 0:
result = False

i += 1

return result


def get_total_x(_a: list[int], _b: list[int]) -> int:
_max_ = 0
for _, j in enumerate(_b):
if j > _max_:
_max_ = j

result: list[int] = []

for i in range(2, _max_):
if is_factor(i, _a) and factor_of(i, _b):
result.append(i)

output = len(result)
LOGGER.info('Between Two Sets result: %i', output)

return output
63 changes: 63 additions & 0 deletions src/hackerrank/implementation/between_two_sets_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest
from .between_two_sets import get_total_x, is_factor, factor_of


class TestProblemBetweenTwoSets(unittest.TestCase):

def test_get_total_x(self):

tinput: list[int] = [16, 32, 96]
solution_found: int = 0

calculated_a = get_total_x([], tinput)

self.assertEqual(
calculated_a, solution_found,
f"get_total_x([], tinput) must be "
f"=> {solution_found}")

calculated_b = get_total_x(tinput, [])

self.assertEqual(
calculated_b, solution_found,
f"get_total_x({tinput}, {[]}) must be "
f"=> {solution_found}")

calculated_c = get_total_x([], [])

self.assertEqual(
calculated_c, solution_found,
f"get_total_x({[]}, {[]}) must be "
f"=> {solution_found}")

calculated_d = is_factor(1, [])

self.assertEqual(
calculated_d, solution_found,
f"is_factor({1}, {[]}) must be "
f"=> {False}")

calculated_e = factor_of(1, [])

self.assertEqual(
calculated_e, solution_found,
f"factor_of({1}, {[]}) must be "
f"=> {False}")

def test_get_total_x_case_0(self):

_a_ = [2, 4]
_b_ = [16, 32, 96]
_b_reverse_ = [96, 32, 16]

solution_found = 3

self.assertEqual(
get_total_x(_a_, _b_), solution_found,
f"get_total_x({_a_}, {_b_}) must be "
f"=> {solution_found}")

self.assertEqual(
get_total_x(_a_, _b_reverse_), solution_found,
f"get_total_x({_a_}, {_b_reverse_}) must be "
f"=> {solution_found}")