Advertisement
SimeonTs

SUPyF Lists - Extra 06. * Winecraft

Jun 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. """
  2. Lists
  3. Check your answer: https://judge.softuni.bg/Contests/Practice/Index/425#5
  4.  
  5. 06. * Winecraft
  6.  
  7. Problem:
  8. You will be given a sequence of integers, which will represent grapes. On the next line,
  9. you will be given N - an integer, indicating the growth days. You must increment every integer in the list by 1 N times.
  10. However, if one of the grapes’ value is greater than the grape to its left and is also greater than the one to his
  11. right, it becomes Greater grape.
  12. The Greater grape steals the values, which would have been incremented to its neighbors, and adds them to itself,
  13. instead of being incremented by 1 like normal. On top of that the grapes, which are neighbors of the Greater grape
  14. are decremented by 1 (note: if any of the greater grapes’ neighboring grapes have a value of 0,
  15. DON’T decrement it and DON’T add its value to the greater grape).
  16. Example: If we the list 1 12 4. The element at position 1 is greater grape, because it is bigger than the elements
  17. on the left and on the right:
  18. - First iteration: The Greater grape increases with 1 by default and takes 2 from its neighbors. The new list look like:
  19. 0 15 3
  20. - Second iteration: The Greater grape increases with 1 by default and takes only 1 from its neighbors. This is because
  21. the grape on left is 0 and the Greater grape takes only from the left one. The list now looks like this: 0 16 2
  22. Lesser grapes don’t get incremented when they have as neighbor Greater grape , but instead they have their values
  23. decremented by 1 by their neighboring Greater grapes (if there are such), therefore their values get added to the
  24. Greater grapes.
  25. After you're done with the growing (processed the grapes N times), every grape which has a value, lower than N
  26. should be set to a value of 0 and you should not increment them or steal values from them.
  27. The process should then repeat, again incrementing everything N times, where the Greater grapes steal from the
  28. lesser grapes, until your list contains less than N grapes with value more than N.
  29. After that, print the remaining grapes on the console (one line, space-separated).
  30.  
  31. Examples:
  32.  
  33. Input:                  Output:
  34. 4 4 15 4                7 24
  35. 3
  36.  
  37. Input:                  Output:
  38. 10 11 12 13 19 13       20 35 44
  39. 5
  40.  
  41. Input:                  Output:
  42. 6 7 6 2                 16 5
  43. 3
  44. """
  45.  
  46.  
  47. def growing(grapes, n):
  48.     for times in range(n):
  49.         grapes_copy = grapes.copy()
  50.         for i in range(1, len(grapes)-1):
  51.             if grapes[i-1] < grapes[i] > grapes[i+1]:       # Classified as greater grape
  52.                 grapes[i] += 1
  53.                 if grapes[i-1] > 0:
  54.                     grapes[i-1] -= 1
  55.                     grapes[i] += 1
  56.                 if grapes[i+1] > 0:
  57.                     grapes[i+1] -= 1
  58.                     grapes[i] += 1
  59.  
  60.     # Incrementing all grapes but the greater grapes by 1
  61.     # ______________________________________________________
  62.         for e in range(len(grapes)):
  63.             if grapes_copy[e] == grapes[e]:     # the value has not been changed
  64.                 if grapes[e] > 0:
  65.                     grapes[e] += 1
  66.     # ______________________________________________________
  67.  
  68.  
  69. def solve(grapes, n):
  70.     grapes_copy = grapes.copy()
  71.     while len(grapes_copy) >= n:
  72.         growing(grapes, n)
  73.         grapes_copy = [e for e in grapes if e > n]      # filtering the list by removing the numbers less or equal to n
  74.         for e in range(len(grapes)):
  75.             if grapes[e] < n:
  76.                 grapes[e] = 0
  77.     return ' '.join(map(str, grapes_copy))
  78.  
  79.  
  80. def main():
  81.     grapes = [int(e) for e in input().split()]
  82.     n = int(input())
  83.     print(solve(grapes, n))
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement