Between Two Sets

HackerRank 問題: Between Two Sets

Question

HackerRank 問題: Between Two Sets

Answer

PHP

<?php

function getTotalX($number_factor_original_list, $number_factor_target_list) {
    // calculate check range
    $max_original = max($number_factor_original_list);
    $min_target = min($number_factor_target_list);

    $factor_count = 0;

    // check range number is factor of each array
    for ($check_number = $max_original; $check_number <= $min_target; $check_number++) {
        // check original list is factor of check number
        $is_original_factor_of_number = true;
        $is_number_factor_of_target = true;

        // is original number factor of check number
        foreach ($number_factor_original_list as $original_number) {
            if ($check_number % $original_number != 0) {
                $is_original_factor_of_number = false;
                // not factor number
                break;
            }
        }

        // is check number factor of target number
        foreach ($number_factor_target_list as $target_number) {
            if ($target_number % $check_number != 0) {
                $is_number_factor_of_target = false;
                // not factor number
                break;
            }
        }

        if ($is_original_factor_of_number && $is_number_factor_of_target) {
            // is all factor, add factor count
            $factor_count++;
        }
    }

    return $factor_count;
}

Python

def getTotalX(a, b):
    maxA = max(a)
    minB = min(b)
    count = 0
    for num in range(maxA, minB + 1):
        left = all([num % numA == 0 for numA in a])
        right = all([numB % num == 0 for numB in b])
        count += left * right
    return count

Reference