solve a newspaper quiz

Francesco Bochicchio bieffe62 at gmail.com
Mon May 10 03:40:17 EDT 2010


On 9 Mag, 11:20, superpollo <ute... at esempio.net> wrote:
> "if a b c are digits, solve ab:c=a*c+b"
>
> solved in one minute with no thought:
>
> for a in range(10):
>      for b in range(10):
>          for c in range(10):
>              try:
>                  if (10.*a+b)/c==a*c+b:
>                      print "%i%i:%i=%i*%i+%i" % (a,b,c,a,c,b)
>              except:
>                  pass
>
> any suggestion for improvement?
>
> bye

The obvious one-liner. Maybe not an improvement, but more compact (I
included the solutions for the really lazy ones).
But you need to think just one second to exclude 0 from the values of
c and avoid a divide by zero exception.


>>> [(a,b,c) for a in range(10) for b in range(10) for c in range(1,10) if (a*10+b)/c == a*c+b ]
[(0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 0, 5), (0, 0, 6), (0,
0, 7), (0, 0, 8), (0, 0, 9), (0, 1, 1), (0, 2, 1), (0, 3, 1), (0, 4,
1), (0, 5, 1), (0, 6, 1), (0, 7, 1), (0, 8, 1), (0, 9, 1), (1, 0, 3),
(1, 5, 2), (1, 6, 2), (2, 0, 3), (2, 1, 3), (3, 1, 3), (4, 1, 3), (4,
2, 3), (5, 2, 3), (6, 2, 3), (6, 3, 3), (7, 3, 3), (8, 3, 3), (8, 4,
3), (9, 4, 3)]


Ciao
---
FB



More information about the Python-list mailing list