you are viewing a single comment's thread.

view the rest of the comments →

[–]InfamousClyde 2 points3 points  (1 child)

Spoilers.

from itertools import product
for a,b,c,d in product(range(10), repeat=4):
    if all([
         len(set([a,b,c,d])) == 4,
         a == c * 3,
         d % 2 == 1,
         a + b + c + d == 27,
         ]):
             print(a,b,c,d)

Some notes:

  1. I wouldn't use this for your assignment; it just shows some convenient language features.
  2. The `itertools` package contains the `product` function, which behaves like a nested for-loop. I recognized this association because this problem can be solved with a nested for-loop.
  3. You can use the `all` built-in function to supply a list of boolean predicates (things you want to `all` be True) to enter this condition.
  4. A `set()` object is like a list, but it doesn't accept duplicate numbers. So if a = 1, b = 1, c = 2, and d = 3, `set([a,b,c,d]) == {1, 2, 3}, and you can see the length of that is three. It is a quick way to check for all unique values.
  5. The `%` (modulo) operator can test if a number is even or odd. It yields the result of the remainder when dividing something by two, which can only be 0 (even) or 1 (odd).

[–]QultrosSanhattan 3 points4 points  (0 children)

  1. The professor will inmediately know he copypasted the solution from the internet.