Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #The below code snippet inputs a list of integers and
  2. #Find all pairs of numbers in the list whose product is even and whose sum is odd.
  3.  
  4. #User input
  5. user_list = input("Enter the list of numbers in a comma seperated way. eg: 1,3,5,67,8 \n")
  6.  
  7. # Try catch to handle the case, if the user doesn't input correctly
  8. try:
  9. num_list = [int(x.strip()) for x in user_list.split(',')]
  10. except:
  11. print("\n\nThe list of number is not entered correctly. Please run again.")
  12. print("The code is running on a dummy list for now [1,2,3,4,5]")
  13. num_list = [1,2,3,4,5]
  14.  
  15. #Initialise lists to store results
  16. prod_list = []
  17. sum_list = []
  18.  
  19. #Iterate over the list to find the pairs
  20. for i in range(len(num_list)):
  21. for j in range(i, len(num_list)):
  22. if (num_list[i] * num_list[j]) % 2 == 0:
  23. prod_list.append((num_list[i],num_list[j]))
  24. if (num_list[i] + num_list[j]) % 2 != 0:
  25. sum_list.append((num_list[i],num_list[j]))
  26.  
  27.  
  28. # Print the results
  29. print("\n\nPair of numbers having even product:",prod_list)
  30. print("\nPair of numbers having odd sum:",sum_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement