-2

See my code below. I keep getting an error code, and I don't understand what it means too. Where in the code can I look?

def list_true(n):
    return [False for x in range(2)] + [x for x in range (2, n+1)]

assert len(list_true(20)) == 21
assert list_true(20)[0] is False
assert list_true(20)[1] is False


def mark_false(bool_list, p):
    mark_false = []
    for x in bool_list:
        if x/p == 1:
            mark_false.append(True)
        elif x % p == 0:
            mark_false.append(False)
        else:
            mark_false.append(True)
    return mark_false

assert mark_false(list_true(6), 2) == [False, False, True, True, False, True, False]

def find_next(bool_list, p):
    x = 0
    cleared = False
    for bool in bool_list:
        if cleared:
            if bool:
                return x
        if x == p and bool:
            cleared = True
        x += 1
    return None

assert find_next([True, True, True, True], 2) == 3
assert find_next([True, True, True, False], 2) is None

def prime_from_list(bool_list):
    y = [x for x, i in enumerate(bool_list) if i]
    prime_from_list = []
    for element in bool_list:
        if element == True:
            return y
    return prime_from_list

assert prime_from_list([False, False, True, True, False]) ==  [2, 3]

def sieve(n):
    bool_list = list_true(n)
    p = 2
    while p is not None:
        bool_list = mark_false(bool_list, p)
        p = find_next(bool_list, p)
    return prime_from_list(bool_list)

Then I get an error message after the below code.

assert sieve(1000) == get_primes(0, 1000)
--------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-50-c49169fabbae> in <module>()
----> 1 assert sieve(1000) == get_primes(0, 1000)

AssertionError: 

Why am I getting the error and is there a possible way I can amend it?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • your assert doesn't work. if an assert can't assert correctly, then you get that error. you are also very vague. please stop being so vague here, ppl will bully you – austanss Jul 11 '20 at 23:14
  • sieve(1000) is not equal to get_primes(0,1000), is what it means ... maybe check some smaller values like `print(sieve(5),"==",get_primes(0,5))` – Joran Beasley Jul 11 '20 at 23:18

1 Answers1

0

This is a strange question. First, you didn't supply get_primes(0, 1000) so we can't compare the results. Second, you, as programmer, put in the assert statements to test for situations you know shouldn't happen, so you shouldn't question the assert itself.

I believe the reason the assert is failing is that your tortured code doesn't produce primes (it includes composite odd numbers) E.g. sieve(20) returns:

[2, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Furthermore, your code isn't actually a sieve! The mark_false() routine should simply strike out multiples of the most recently discovered prime, but instead it tests all the numbers to see if they are divisible by the prime! This is brute force prime searching in the guise of a sieve.

Below is my rework and simplification of your code which should pass the assertion in question:

def list_true(n):
    return [False for _ in range(2)] + [True for _ in range(2, n + 1)]

assert len(list_true(20)) == 21
assert list_true(20)[0] is False
assert list_true(20)[19] is True

def mark_false(bool_list, prime):

    for index in range(prime * prime, len(bool_list), prime):
        if bool_list[index] is False:
            continue

        bool_list[index] = False

    return bool_list

assert mark_false(list_true(6), 2) == [False, False, True, True, False, True, False]

def find_next(bool_list, index):
    while index + 1 < len(bool_list):
        index += 1

        if bool_list[index]:
            return index

    return None

assert find_next([True, True, True, True], 2) == 3
assert find_next([True, True, True, False], 2) is None

def prime_from_list(bool_list):
    return [index for index, boolean in enumerate(bool_list) if boolean]

assert prime_from_list([False, False, True, True, False]) == [2, 3]

def sieve(number):
    bool_list = list_true(number)
    prime = 2

    while prime is not None:
        bool_list = mark_false(bool_list, prime)
        prime = find_next(bool_list, prime)

    return prime_from_list(bool_list)

assert sieve(1000) == get_primes(0, 1000)

Note that bool is a Python class, don't use it as a variable name.

cdlane
  • 40,441
  • 5
  • 32
  • 81