matt simpson
matt simpson

Reputation: 1

Can anyone help me with my python code for this list, not working how I think it should

So I have very, very limited coding knowledge and half the code I used here I learned today but what I was attempting to do was to create a list that was 2020 positive whole numbers long and which excluded all numbers that were divisible by 3 or 4 but not by 5. I was trying to figure out what the 2020th number was but I noticed that in my final list with the numbers excluded, it was still leaving them in towards the end!

This is the code I wrote:

numbers = [*range(1, 2165, 1)]
#print(numbers)

list1 = [ x for x in range(1, 2165) if x % 3 == 0 and x % 4 == 0 and x % 5 != 0 ]
#print(list1)

for x in list1:
    numbers.remove(x)
print(len(numbers))
print(numbers)

Like I said I'm not very good at coding but it did seem to be working early on in the list as 12 was excluded and 60 was left in but towards the end, 2139 was left, which is divisible by 3. I would appreciate any help.

Upvotes: 0

Views: 53

Answers (2)

Samwise
Samwise

Reputation: 71454

It might simplify your debugging if you just built the list directly by explicitly declaring the condition you're trying to match on rather than doing it in multiple steps. You can generate numbers in a single comprehension:

[x for x in range(1, 2165) if not(x % 3 == 0 and x % 4 == 0 and x % 5 != 0)]

or the logical equivalent:

[x for x in range(1, 2165) if x % 3 != 0 or x % 4 != 0 or x % 5 == 0]

If you're not convinced this is the same, you can test it against the numbers list generated by your original code:

>>> numbers == [x for x in range(1, 2165) if not(x % 3 == 0 and x % 4 == 0 and x % 5 != 0)]
True

2139 appears in this list because 2139 % 4 != 0.

If this doesn't match the condition you're trying to capture, having the code simplified should make it easier to find and fix the problem.

Upvotes: 1

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

2020 positive whole numbers long

For things like this, rather than iterating over a fixed range (in your case, from 1 to 2165, which doesn't produce 2020 numbers matching your conditions), it's usually more straightforward to build a generator that gives you numbers that you want, and take numbers from it via next() until you have as many as you need. In this case, given your conditions

def my_generator():
  x = 0
  while True:
    x += 1
    # skip all numbers that are divisible by (3 or 4) but are not divisible by 5
    if ((x % 3 == 0) or (x % 4 == 0)) and (x % 5) != 0:
      continue
    yield x

# initialize the generator, and then grab exactly 2020 numbers from it
gen = my_generator()
numbers = [next(gen) for _ in range(2020)]
print("The 2020th number meeting the conditions is", numbers[-1])
# 3367

Note that, in your original question, your if condition was coded incorrectly, and I've fixed it here.

Upvotes: 1

Related Questions