Advertisement
tollybobs

Coding Interview Problem

Apr 1st, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #Coding interview problem for today.
  2. #This problem was recently asked by Google.
  3. #Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
  4. #For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17
  5.  
  6. #Collect Input
  7. first_num = int(input("Please enter the first number: "))
  8. second_num = int(input("Please enter the second number: "))
  9. third_num = int(input("Please enter the third number: "))
  10. fourth_num = int(input("Please enter the fourth number: "))
  11. #num_list_copy = [10, 15, 3, 7]
  12. exp_total = 17
  13.  
  14. #Save Input in an array
  15. num_list = [first_num, second_num, third_num, fourth_num]
  16. #Create a copy of array so that we can compare 2 numbers one from each list.
  17. num_list_new = num_list.copy()
  18.  
  19. #Loop condition to compare and add up a number from each array list and print out [true] result if equals to expected total
  20. for num in num_list:
  21. for num_copy in num_list_new:
  22. if num + num_copy == exp_total:
  23. print("{} and {} = {}".format(num, num_copy, exp_total))
  24. print(True)
  25. break
  26. else:
  27. print("(False) the numbers don't add up to the expected total")
  28. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement