Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- A Little program that prints every pair of numbers that has an even product and an odd sum.
- Assumes that pairs such as (3, 7) and (7, 3) are the same thing.
- Also assumes that their are no re
- Note this is definitely not an efficient solution to the problem. It is a
- simpler approach with the intentions of demonstrating fundamental programming concepts.
- I recognise that not all of you may be beginners and I encourage those of you that aren't to think about how
- you could improve this code to make it more efficient. I would love to see how you approach this.
- """
- data = input("please enter the numbers (separated by ',') : ")
- numbers = data.split(",") # splits the string into an array of strings. Splits the string wherever a "," occurs.
- count = len(numbers) # len(list) is a function that returns the number of items in a list aka how "long" a list is.
- # A nested loop, since we need to compare every item to every other item in the list.
- # Complexity : O(N^2) Don't worry if you don't know what this is. You will learn about this in FIT1045 and FIT1008.
- # Ask me about it if you're curious.
- for i in range(count):
- # The reason j only goes from i to count is to prevent numbers from all being compared twice.
- # Since (7,3) and (3,7) are the same pair.
- #
- # Try removing "i+1" and see how this changes the output : "range(0, count)" instead of "range(i+1, count)"
- for j in range(i + 1, count):
- num1 = int(numbers[i]) # numbers need to be converted into integers since they were input as a string.
- num2 = int(numbers[j]) # this is an important concept, please ask for help if you are confused.
- product = num1 * num2
- summation = num1 + num2 # sum is a reserved keyword in python so "summation" was used instead.
- # if the product(mod 2) = 0 and the summation(mod 2) = 1 then the product is even and the summation is odd.
- # the modulo operator is simply the remainder of a division eg: 5 % 4 = 1 and 8 % 2 = 0.
- if product % 2 == 0 and summation % 2 == 1:
- print(num1, ",", num2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement