-2

Set-up

I'm looking for all the integer pairs (x,y) in 2 closed sets: [822,2000] and [506,1231] such that x/y=1.624.


Code so far

I tried,

a = [[(x,y)] for x in range(822,2001) and y in range(506,1232) if x/y = 1.624]

But this gives a SyntaxError: invalid syntax pointing to the = in the code.

And if I do,

a = [[(x,y)] for x in range(822,2001) and y in range(506,1232) if x/y <= 1.624]

I get NameError: name 'y' is not defined.

How do I solve this?

LucSpan
  • 1,831
  • 6
  • 31
  • 66
  • 1
    `=` is assignment operator, for comparison use `==` – Sohaib Farooqi Mar 10 '18 at 15:37
  • 2
    what @bro-grammer says for the first line, and for the second - you mean `for y in` not `and y in`... and you probably want `[(x, y)`... instead of `[[(x, y]`... unless you really want a list of one element lists instead of a list of tuples... – Jon Clements Mar 10 '18 at 15:39
  • @bro-grammer (cc JonClements) Hm... it looks like that the higher-rep users get an unfair (?) advantages of being able to give hints in comment? – user202729 Mar 10 '18 at 15:43
  • Thanks for helping this dummy out. Solved it. – LucSpan Mar 10 '18 at 15:43
  • @user202729 I believe hints should always be in comments. – Sohaib Farooqi Mar 10 '18 at 15:51
  • ... Then users with < 50 rep can't give hints? Well... no wonder why they keep complaining. – user202729 Mar 10 '18 at 15:52
  • 1
    @user202729 nobody hinders users with 1 rep to try to answer things - it is possible to gain 50 rep and then be able to comment - not easy, just possible :) and commments do not get you any rep as is, so experienced users giving hints might help less experienced ones to get to an answer and to rep. – Patrick Artner Mar 10 '18 at 15:57
  • @user202729 [have a look at this meta post](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) – Sohaib Farooqi Mar 10 '18 at 15:59
  • @PatrickArtner Good point. – user202729 Mar 10 '18 at 16:00

2 Answers2

4

Comparing float calculations with == is difficult due to the nature of float arithmetics.

It is often better to compare like this:

a = [(x,y) for x in range(822,2001) for y in range(506,1232) if abs(x/y - 1.624) < 0.00001] 
print(set(a)) 

By substracting the wanted value from your result and and comparing its absolute value against something kindof small you get better results.

Result (using a set):

{(1624, 1000), (1637, 1008), (1015, 625), (1611, 992), (1840, 1133), 
 (1814, 1117), (1827, 1125), (1408, 867), (1218, 750), (1434, 883), 
 (1421, 875)}

Python rounding error with float numbers

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 2
    There is also [math.isclose](https://docs.python.org/3/whatsnew/3.5.html#pep-485-a-function-for-testing-approximate-equality), but only available for `python>=3.5` – Sohaib Farooqi Mar 10 '18 at 15:47
2

For the first one you are using the assignment operator instead of the equivalent operator so it should be:

a = [[(x,y)] for x in range(822,2001) and y in range(506,1232) if x/y == 1.624]

And for the second you're probably better off using two for loops

a = [[(x,y)] for x in range(822,2001) for y in range(506,1232) if x/y <= 1.624]

The second one would not make sense as you said it because x and y are coming from lists that have an unequal number of elements so you cannot loop over them like that

quantik
  • 776
  • 12
  • 26