简体   繁体   中英

Am trying to write a code that will print the numbers within the range of 'a' that are not divisible by y

sample.py

y = [2,3,4,5,6,7,8,9] # array

Function is created below

def _div(a):       # Needed to create a function 
    z = 0          # constant

    for x in range(1 , a +1):   # To check for all the numbers within the range of a
        while z < 8:
            if x % y[z] != 0 :    # To check if the value of x is divisible by any of the value in the array
                j = [x]
                Print (j[0])

            z += 1

Calling function

div(15)  # not showing any result on Python

end Python

def not_dividable_by(factor, quotient_list, unique_output=True):
    """
    Args:
        factor(int): All numbers from 0-factor are calculated
        quotient_list(list): List of to test if factor is divisable with
        unique_output(bool): Numbers may occur multiple times if False

    Returns:
        list: with integers of numbers not divisable by any of 0-factor
    """
    result = []
    for i in range(0, factor + 1):
        for x in quotient_list:
            if i % x:
                if x not in result or not unique_output:
                    result.append(x)
    return sorted(result) if unique_output else result

print(not_dividable_by(15, [2,3,4,5,6,7,8,9]))

But most of the times the output will be all numbers in the list.

Loop over all values in the range from 1 to the given number. For each value check if the modulo operation gives a value other than 0 for all divisors. If yes, add the value to the list.

Now we can write that down in Python code.

def _div(number, divisors):
    result = []
    for value in range(1, number + 1):
        if all(value % divisor != 0 for divisor in divisors):
            result.append(value)

    return result


y = [2, 3, 4, 5, 6, 7, 8, 9]
print(_div(15, y))

This will give you [1, 11, 13] .

There is a possible optimization. Instead of checking if the current value is not divisible by all divisors we can check if the value is divisible by any divisor. This will end the check (shortcut) when a matching divisor is found.

So replace

if all(value % divisor != 0 for divisor in divisors):
    result.append(value)

with

if not any(value % divisor == 0 for divisor in divisors):
    result.append(value)

I replaced the variable names in the function by more meaningful ones. The function itself would deserve a better name too. At least I added the list of divisors as a parameter because it's a bad idea to work with global states in a function.

With a list comprehension you could even make a one-liner out of this but I prefer the more readable multiline version.

def _div(number, divisors):
    return [value for value in range(1, number + 1) if not any(value % divisor == 0 for divisor in divisors)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM