Advertisement
GeorgiLukanov87

Python Advanced Exam - 19 February 2022

Sep 20th, 2022 (edited)
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.84 KB | None | 0 0
  1. # Python Advanced Exam - 19 February 2022
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/3374#0
  4.  
  5.  
  6. # 01. Flowers Finder
  7. # 02. Pawn Wars
  8. # 03. Springtime
  9.  
  10. -----------------------------------------------------------------------------------------------------------
  11.  
  12. # 01. Flowers Finder
  13. # NEW VERSION !
  14.  
  15. from collections import deque
  16.  
  17.  
  18. def is_letter_inside_func(letter):
  19.     for flower in flowers_dict:
  20.         if letter in flower and letter not in flowers_dict[flower]:
  21.             x = flower.count(letter)
  22.             flowers_dict[flower].append(letter)
  23.             if x > 1:
  24.                 flowers_dict[flower].append(letter)
  25.  
  26.  
  27. def is_word_found_func(flowers):
  28.     for flower in flowers:
  29.         if len(flower) == len(flowers[flower]):
  30.             print(f'Word found: {flower}')
  31.             return True
  32.     return False
  33.  
  34.  
  35. vowels = deque(input().split())
  36. consonants = deque(input().split())
  37.  
  38. flowers_dict = {'rose': [], 'tulip': [], 'lotus': [], 'daffodil': []}
  39. word_found = False
  40.  
  41. while True:
  42.     if not vowels or not consonants:
  43.         break
  44.  
  45.     first_vowel = vowels.popleft()
  46.     last_consonant = consonants.pop()
  47.  
  48.     is_letter_inside_func(first_vowel)
  49.     is_letter_inside_func(last_consonant)
  50.  
  51.     if is_word_found_func(flowers_dict):
  52.         word_found = True
  53.         break
  54.  
  55. if not word_found:
  56.     print('Cannot find any word!')
  57.  
  58. if vowels:
  59.     print(f'Vowels left: {" ".join(vowels)}')
  60. if consonants:
  61.     print(f'Consonants left: {" ".join(consonants)}')
  62.  
  63.    
  64. =============================================================================================================
  65.  
  66. # 01. Flowers Finder
  67. # OLD VERSION !
  68.  
  69.  
  70. from collections import deque
  71.  
  72. vowels = deque(input().split())
  73. consonants = deque(input().split())
  74.  
  75. word_found = False
  76. last_word = None
  77. word_dict = {'rose': [], 'tulip': [], 'lotus': [], 'daffodil': []}
  78. word_id_access = {1: 'rose', 2: 'tulip', 3: 'lotus', 4: 'daffodil'}
  79.  
  80. while vowels and consonants:
  81.     v = vowels.popleft()
  82.     c = consonants.pop()
  83.  
  84.     for index, word in enumerate(word_dict):
  85.         if v in word:
  86.             if v not in word_dict[word_id_access[index+1]]:
  87.                 word_dict[word_id_access[index+1]].append(v)
  88.  
  89.         if c in word:
  90.             if c not in word_dict[word_id_access[index+1]]:
  91.                 word_dict[word_id_access[index+1]].append(c)
  92.                 if c in ['f', 'd']:
  93.                     word_dict[word_id_access[index+1]].append(c)
  94.  
  95.         if len(word) == len(word_dict[word_id_access[index + 1]]):
  96.             word_found = True
  97.             last_word = word
  98.             break
  99.  
  100.     if word_found:
  101.         break
  102.  
  103. if word_found:
  104.     print(f'Word found: {last_word}')
  105. else:
  106.     print('Cannot find any word!')
  107.    
  108. if vowels:
  109.     print(f'Vowels left: {" ".join(vowels)}')
  110. if consonants:
  111.     print(f'Consonants left: {" ".join(consonants)}')
  112.    
  113.  
  114. -----------------------------------------------------------------------------------------------------------
  115.  
  116. # 02. Pawn Wars
  117.  
  118.  
  119. from collections import deque
  120.  
  121.  
  122. def get_next_position_func(color, row, col):
  123.     if color[0] == 'white':  # UP !
  124.         return row - 1, col
  125.     elif color[0] == 'black':  # DOWN !
  126.         return row + 1, col
  127.  
  128.  
  129. def check_diagonals_func(color, row, col):  # CHECK IF PLAYER CAN ATTACK
  130.     if color[0] == 'white':
  131.         if is_inside_func(row - 1, col - 1) and matrix[row - 1][col - 1] == 'b':
  132.             return True
  133.         elif is_inside_func(row - 1, col + 1) and matrix[row - 1][col + 1] == 'b':
  134.             return True
  135.  
  136.     elif color[0] == 'black':
  137.         if is_inside_func(row + 1, col + 1) and matrix[row + 1][col + 1] == 'w':
  138.             return True
  139.         elif is_inside_func(row + 1, col - 1) and matrix[row + 1][col - 1] == 'w':
  140.             return True
  141.  
  142.     return False
  143.  
  144.  
  145. def is_inside_func(r, c):
  146.     return 0 <= r < SIZE and 0 <= c < SIZE
  147.  
  148.  
  149. SIZE = 8
  150. matrix = []
  151. board = {0: "a", 1: "b", 2: "c", 3: "d", 4: "e", 5: "f", 6: "g", 7: "h", }
  152. w_row, w_col = 0, 0
  153. b_row, b_col = 0, 0
  154.  
  155. for row_index in range(SIZE):  # CREATE THE BOARD !
  156.     matrix.append(input().split())
  157.     for col_index in range(SIZE):
  158.         if matrix[row_index][col_index] == 'w':
  159.             w_row, w_col = row_index, col_index
  160.         if matrix[row_index][col_index] == 'b':
  161.             b_row, b_col = row_index, col_index
  162.  
  163. turns = deque(['black', 'white'])
  164. while True:
  165.     turns.rotate()
  166.    
  167.     if turns[0] == 'white':
  168.         w_fist_row, w_first_col = get_next_position_func(turns, w_row, w_col)
  169.         if w_fist_row == 0 and not check_diagonals_func(turns, w_row, w_col):  # IF 1ST MOVE WIN !
  170.             print(f'Game over! White pawn is promoted to a queen at {board[w_first_col]}8.')
  171.             break
  172.         if check_diagonals_func(turns, w_row, w_col):  # WIN BY ATTACKING !
  173.             print(f'Game over! White win, capture on {board[b_col]}{8 - b_row}.')
  174.             break
  175.         matrix[w_row][w_col] = '-'
  176.         w_row, w_col = get_next_position_func(turns, w_row, w_col)
  177.         matrix[w_row][w_col] = 'w'
  178.         if w_row == 0:  # WIN BY PROMOTE A QUEEN !
  179.             print(f'Game over! White pawn is promoted to a queen at {board[w_col]}8.')
  180.             break
  181.     # -----------------------------------------------------------------------------------------------------
  182.     elif turns[0] == 'black':
  183.         b_fist_row, b_first_col = get_next_position_func(turns, b_row, b_col)
  184.         if b_fist_row == 7 and not check_diagonals_func(turns, b_row, b_col):  # IF 1ST MOVE WIN !
  185.             print(f'Game over! Black pawn is promoted to a queen at {board[b_first_col]}1.')
  186.             break
  187.         if check_diagonals_func(turns, b_row, b_col):  # WIN BY ATTACKING !
  188.             print(f'Game over! Black win, capture on {board[w_col]}{8 - w_row}.')
  189.             break
  190.         matrix[b_row][b_col] = '-'
  191.         b_row, b_col = get_next_position_func(turns, b_row, b_col)
  192.         matrix[b_row][b_col] = 'b'
  193.         if b_row == 7:  # WIN BY PROMOTE A QUEEN !
  194.             print(f'Game over! Black pawn is promoted to a queen at {board[b_col]}1.')
  195.             break
  196.  
  197.  
  198. -----------------------------------------------------------------------------------------------------------
  199.  
  200. # 03. Springtime
  201.  
  202.  
  203. def start_spring(**param):
  204.     elements_dict = {}
  205.     for key, value in param.items():
  206.         if value not in elements_dict.keys():
  207.             elements_dict[value] = [key]
  208.         else:
  209.             if key not in elements_dict.values():
  210.                 elements_dict[value].append(key)
  211.     final_print = ""
  212.     for key, value in sorted(elements_dict.items(), key=lambda x: (-len(x[1]), x[0])):
  213.         final_print += f'{key}:\n'
  214.         for sub_value in sorted(value):
  215.             final_print += f'-{sub_value}\n'
  216.  
  217.     return final_print
  218.  
  219.  
  220. -----------------------------------------------------------------------------------------------------------
  221.  
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement