SimeonTs

SUPyF2 P.-Mid-Exam/18 December 2018 - 03. Present Delivery

Oct 30th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. """
  2. Technology Fundamentals Retake Mid Exam - 18 December 2018
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1395#2
  4.  
  5. SUPyF2 P.-Mid-Exam/18 December 2018 - 03. Present Delivery
  6.  
  7. Problem:
  8. Santa has limited time to drop at least some presents for each house. Help him with his mission!
  9. You will receive a string with even integers separated by "@" representing each house with its
  10. number of members and a series of Jump commands until you receive "Merry Xmas!"
  11. Santa starts at the position of the first house and has to jump by a given length.
  12. The jump command will be in format: "Jump {length}".
  13. Each time he jumps from one house to another he drops 2 presents for that house and decreases the needed presents
  14. for that house. If Santa jumps on a house which doesn't need more presents (presents = 0) you should instead print
  15. "House {houseIndex} will have a Merry Christmas.".
  16. Keep in mind that Santa can have a bigger jump length than the size of the field and if he does jump outside
  17. of it he should start from the beginning again.
  18. For example we have a field of size 3 and each house has 6 members. Santa is at the start and jumps with length of 2.
  19. He will end up at index 2 and decrease the needed presents by 2 (6 – 2 = 4).
  20. Next he jumps again with length of 2 and ends up at index position 1 and again decreases the needed presents.
  21.  
  22. Input
  23. • On the first line you will receive a string with even integers separated by "@" – houses and their number of members
  24. • On the next lines until "Merry Xmas!" you will receive jump commands in format: "Jump {length}".
  25. Output
  26. At the end print Santa's last position and whether or not his mission was successful:
  27. • "Santa's last position was {lastPositionIndex}."
  28. • If all members of each house have presents print:
  29. o   "Mission was successful."
  30. • If not print the count of all houses that won't have a Merry Christmas:
  31. o   "Santa has failed {housesCount} houses."
  32. Constraints
  33. • The field can be of size [1…20]
  34. • Each house will have an even number of  members [2 … 10]
  35. • Each jump length will be an integer [1 … 20]
  36. Examples:
  37.  
  38. Input:          Output:
  39. 10@10@10@2      Santa's last position was 3.
  40. Jump 1          Santa has failed 3 houses.
  41. Jump 2
  42. Merry Xmas!
  43.  
  44. Input:          Output:
  45. 2@4@2           House 0 will have a Merry Christmas.
  46. Jump 2          Santa's last position was 1.
  47. Jump 2          Mission was successful.
  48. Jump 8
  49. Jump 3
  50. Jump 1
  51. Merry Xmas!
  52. """
  53. houses = [int(house) for house in input().split("@")]
  54. santa_position = 0
  55.  
  56. while True:
  57.     command = input().split()
  58.     if command[0] == "Merry":
  59.         break
  60.     jump = int(command[1])
  61.  
  62.     if santa_position + jump >= len(houses):
  63.         santa_position = (santa_position + jump) % len(houses)
  64.     else:
  65.         santa_position = santa_position + jump
  66.  
  67.     if houses[santa_position] == 0:
  68.         print(f"House {santa_position} will have a Merry Christmas.")
  69.     else:
  70.         houses[santa_position] -= 2
  71.  
  72.  
  73. print(f"Santa's last position was {santa_position}.")
  74. if sum(houses) == 0:
  75.     print(f"Mission was successful.")
  76. else:
  77.     print(f"Santa has failed {len([house for house in houses if house > 0])} houses.")
Advertisement
Add Comment
Please, Sign In to add comment