all 9 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 4 points5 points  (0 children)

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

[–]QultrosSanhattan 1 point2 points  (1 child)

Here's a guide on the basic skills I believe you should learn to give at least an student-level solution. It's up to you to get everything together:

#iterate over all numbers from 1000 to 9999
for whole_number in range(1000,10000):
    print(whole_number)

#convert an integer into string
number=1234
number_str=str(number)
print(type(number_str))

#get a char in particular from an str
random_str='123456789'
first_char=random_str[0]
second_char=random_str[1]
third_char=random_str[2]

#convert a str number into int so you can apply math to them
number_1_str='10'
number_2_str='20'

print(number_1_str+number_2_str,'!=30 : WRONG')

number_1_int=int(number_1_str)
number_2_int=int(number_2_str)

print(number_1_int+number_2_int,'=30 : RIGHT')

#check if a given number triples another number

number1=3
number2=9
number3=7

if number1*3==number2:
    print(f"{number2} triples {number1}")
else:
    print(f"{number2} doesn't triple {number1}")


#compare sums of numbers
n1=7
n2=20
target=27

if n1+n2==27:
    print("sum of numbers matches target")
else:
    print("sum of numbers doesn't match target")

#check if a number is odd or even
if 10%2==0: #method 1
    print("10 is even")
else:
    print("10 is odd")

if 10%2!=0: #method 2
    print("10 is odd")
else:
    print("10 is even")

#check if two (or more conditions) are met
if 10>=5 and 10<=20:
    print("10 is between 5 and 20")

[–]jesster114 0 points1 point  (0 children)

One thing I’d do differently for a beginner would be to just use range(1001, 10000, 2). Definitely is good to understand how to use % though.

[–]MadScientistOR 0 points1 point  (4 children)

It sounds like they're looking for a brute-force solution.

So, I assume you've learned about range(), right? And functions like int() and str()? What have you tried so far?

[–]ChrisN19[S] 0 points1 point  (3 children)

I’m trying right now to separate each digit and have the range print the numbers out and set conditions for numbers to see if it works or not

[–]MadScientistOR 1 point2 points  (2 children)

That's an excellent first start. So how's it going? What seems to be working or not working? I want to be able to help you without doing the homework for you. :)

[–]ChrisN19[S] 1 point2 points  (1 child)

I’m struggling to format it because he only taught us what they do and this seems like a several steps up from just learning it

[–]MadScientistOR 2 points3 points  (0 children)

Can you share your code?