The method takes two arrays of integers a[n] and b[m]. Determine all integers that satisfy the following two conditions:
- The elements of the first array are all factors of the integer being considered.
- The integer being considered is a factor of all elements of the second array.
The method should return the numbers of integers that satisfy this condition.
def getTotalX(a: list, b: list) -> int:
return sum(1 for elem in range(min(a)*int(max(a)/min(a)), min(b)+1, min(a)) if (all(elem % n==0 for n in a) and all(n % elem == 0 for n in b)))
import codewars_test as test
from solution import getTotalX
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Simple tests")
def test_group():
@test.it("Test case 1")
def test_case_1():
test.assert_equals(getTotalX([2, 6], [24, 36]), 2)
@test.it("Test case 2")
def test_case_2():
test.assert_equals(getTotalX([3, 6], [24, 72, 144]), 3)
Given a vector, calculate it's numerical gradient.
Examples:
gradient([3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3])
= [-1, 1, 3, -2, -3, 1, 1, -1, -1, 1, 1]
gradient([1, 1, 2, -1, 7])
= [0, 1, -3, 8]
Important: If the given vector has a size equal to n
, the numerical derivative will be a vector with a size equal to n-1
.
std::vector<int> gradient(const std::vector<int> &v) {
std::vector<int> dv;
for (int i = 1; i < int(v.size()); i++)
dv.push_back(v[i]-v[i-1]);
return dv;
}
#include <vector>
Describe(general_tests)
{
It(should_do_the_examples)
{
Assert::That(gradient(std::vector<int> {3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3}),
Equals(std::vector<int> {-1, 1, 3, -2, -3, 1, 1, -1, -1, 1, 1}));
Assert::That(gradient(std::vector<int> {1, 1, 2, -1, 7}),
Equals(std::vector<int> {0, 1, -3, 8}));
}
};
int convert(char bin) {
int dec = 0;
for (int i = 7; i >= 0; i--) {
dec = dec << 1;
if ((bin & (1 << i)) != 0)
dec ++;
}
return dec;
}
Describe(test_cases)
{
It(main_tests)
{
Assert::That(convert(0b00000101), Equals(5));
Assert::That(convert(0b11111110), Equals(254));
Assert::That(convert(0b10101010), Equals(170));
Assert::That(convert(0b11001100), Equals(204));
}
It(min_max_tests)
{
Assert::That(convert(0b00000000), Equals(0));
Assert::That(convert(0b11111111), Equals(255));
}
};