Advertisement
GeorgiLukanov87

Python Advanced Retake Exam - 16 December 2020

Sep 13th, 2022 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. # Python Advanced Retake Exam - 16 December 2020
  2. #
  3. # https://judge.softuni.org/Contests/Practice/Index/2720#0
  4. #
  5. # 01. Matching
  6. # 02. Game of Words
  7. # 03. Magic Triangle
  8.  
  9. -----------------------------------------------------------------------------------------
  10.  
  11. # 01. Matching
  12.  
  13.  
  14. from collections import deque
  15.  
  16. males = deque([int(x) for x in input().split() if int(x) > 0])
  17. females = deque([int(x) for x in input().split() if int(x) > 0])
  18.  
  19. matches = 0
  20. while True:
  21.     if not females or not males:
  22.         break
  23.     last_male = males[-1]
  24.     first_female = females[0]
  25.  
  26.     if last_male % 25 == 0:
  27.         males.pop()
  28.         if males:
  29.             males.pop()
  30.         continue
  31.  
  32.     if first_female % 25 == 0:
  33.         females.popleft()
  34.         if females:
  35.             females.popleft()
  36.         continue
  37.  
  38.     if last_male == first_female:
  39.         matches += 1
  40.         males.pop()
  41.         females.popleft()
  42.     else:
  43.         females.popleft()
  44.         males[-1] -= 2
  45.         if males[-1] <= 0:
  46.             males.pop()
  47.  
  48. print(f"Matches: {matches}")
  49.  
  50. if not males:
  51.     print("Males left: none")
  52. else:
  53.     males = reversed(males)
  54.     print(f'Males left: {", ".join(str(x) for x in males)}')
  55.  
  56. if not females:
  57.     print("Females left: none")
  58. else:
  59.     print(f'Females left: {", ".join(str(x) for x in females)}')
  60.  
  61.  
  62. -----------------------------------------------------------------------------------------
  63.  
  64. # 02. Game of Words
  65.  
  66.  
  67. def is_inside(r, c):
  68.     return 0 <= r < SIZE and 0 <= c < SIZE
  69.  
  70.  
  71. def get_next_position(d, r, c):
  72.     if d == 'left':
  73.         return r, c - 1
  74.     elif d == 'right':
  75.         return r, c + 1
  76.     elif d == 'up':
  77.         return r - 1, c
  78.     elif d == 'down':
  79.         return r + 1, c
  80.  
  81.  
  82. word = input()
  83. SIZE = int(input())
  84. matrix = []
  85. row, col = 0, 0
  86. for row_index in range(SIZE):
  87.     matrix.append(list(input()))
  88.     for col_index in range(SIZE):
  89.         if matrix[row_index][col_index] == 'P':
  90.             row, col = row_index, col_index
  91.  
  92. matrix[row][col] = '-'
  93. total_steps = int(input())
  94.  
  95. for _ in range(total_steps):
  96.     step = input()
  97.     next_row, next_col = get_next_position(step, row, col)
  98.  
  99.     if not is_inside(next_row, next_col):
  100.         word = word[:-1]
  101.     else:
  102.         row, col = next_row, next_col
  103.         if matrix[row][col] not in ['P', '-']:
  104.             word += matrix[row][col]
  105.             matrix[row][col] = '-'
  106.  
  107. matrix[row][col] = 'P'
  108. print(word)
  109. for el in matrix:
  110.     print(*el, sep='')
  111.  
  112.    
  113. -----------------------------------------------------------------------------------------
  114.  
  115. # 03. Magic Triangle
  116.  
  117.  
  118.  
  119. def get_magic_triangle(n):
  120.     matrix = [[1], [1, 1]]
  121.     counter1 = 0
  122.     counter2 = 0
  123.     for row in range(2, n):
  124.         matrix.append([])
  125.         counter1 += 1
  126.        
  127.         for col in range(counter1):
  128.             if len(matrix) <= 3:
  129.                 matrix[row].append(sum(matrix[counter1]))
  130.             else:
  131.                 counter2 += 1
  132.                 matrix[row].append(sum(matrix[counter1][col:counter2 + 1]))
  133.                
  134.         counter2 = 0
  135.         matrix[row].append(1)
  136.         matrix[row].insert(0, 1)
  137.        
  138.     return matrix
  139.  
  140.  
  141. -----------------------------------------------------------------------------------------
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement