Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python Advanced Exam - 19 February 2022
- # https://judge.softuni.org/Contests/Practice/Index/3374#0
- # 01. Flowers Finder
- # 02. Pawn Wars
- # 03. Springtime
- -----------------------------------------------------------------------------------------------------------
- # 01. Flowers Finder
- # NEW VERSION !
- from collections import deque
- def is_letter_inside_func(letter):
- for flower in flowers_dict:
- if letter in flower and letter not in flowers_dict[flower]:
- x = flower.count(letter)
- flowers_dict[flower].append(letter)
- if x > 1:
- flowers_dict[flower].append(letter)
- def is_word_found_func(flowers):
- for flower in flowers:
- if len(flower) == len(flowers[flower]):
- print(f'Word found: {flower}')
- return True
- return False
- vowels = deque(input().split())
- consonants = deque(input().split())
- flowers_dict = {'rose': [], 'tulip': [], 'lotus': [], 'daffodil': []}
- word_found = False
- while True:
- if not vowels or not consonants:
- break
- first_vowel = vowels.popleft()
- last_consonant = consonants.pop()
- is_letter_inside_func(first_vowel)
- is_letter_inside_func(last_consonant)
- if is_word_found_func(flowers_dict):
- word_found = True
- break
- if not word_found:
- print('Cannot find any word!')
- if vowels:
- print(f'Vowels left: {" ".join(vowels)}')
- if consonants:
- print(f'Consonants left: {" ".join(consonants)}')
- =============================================================================================================
- # 01. Flowers Finder
- # OLD VERSION !
- from collections import deque
- vowels = deque(input().split())
- consonants = deque(input().split())
- word_found = False
- last_word = None
- word_dict = {'rose': [], 'tulip': [], 'lotus': [], 'daffodil': []}
- word_id_access = {1: 'rose', 2: 'tulip', 3: 'lotus', 4: 'daffodil'}
- while vowels and consonants:
- v = vowels.popleft()
- c = consonants.pop()
- for index, word in enumerate(word_dict):
- if v in word:
- if v not in word_dict[word_id_access[index+1]]:
- word_dict[word_id_access[index+1]].append(v)
- if c in word:
- if c not in word_dict[word_id_access[index+1]]:
- word_dict[word_id_access[index+1]].append(c)
- if c in ['f', 'd']:
- word_dict[word_id_access[index+1]].append(c)
- if len(word) == len(word_dict[word_id_access[index + 1]]):
- word_found = True
- last_word = word
- break
- if word_found:
- break
- if word_found:
- print(f'Word found: {last_word}')
- else:
- print('Cannot find any word!')
- if vowels:
- print(f'Vowels left: {" ".join(vowels)}')
- if consonants:
- print(f'Consonants left: {" ".join(consonants)}')
- -----------------------------------------------------------------------------------------------------------
- # 02. Pawn Wars
- from collections import deque
- def get_next_position_func(color, row, col):
- if color[0] == 'white': # UP !
- return row - 1, col
- elif color[0] == 'black': # DOWN !
- return row + 1, col
- def check_diagonals_func(color, row, col): # CHECK IF PLAYER CAN ATTACK
- if color[0] == 'white':
- if is_inside_func(row - 1, col - 1) and matrix[row - 1][col - 1] == 'b':
- return True
- elif is_inside_func(row - 1, col + 1) and matrix[row - 1][col + 1] == 'b':
- return True
- elif color[0] == 'black':
- if is_inside_func(row + 1, col + 1) and matrix[row + 1][col + 1] == 'w':
- return True
- elif is_inside_func(row + 1, col - 1) and matrix[row + 1][col - 1] == 'w':
- return True
- return False
- def is_inside_func(r, c):
- return 0 <= r < SIZE and 0 <= c < SIZE
- SIZE = 8
- matrix = []
- board = {0: "a", 1: "b", 2: "c", 3: "d", 4: "e", 5: "f", 6: "g", 7: "h", }
- w_row, w_col = 0, 0
- b_row, b_col = 0, 0
- for row_index in range(SIZE): # CREATE THE BOARD !
- matrix.append(input().split())
- for col_index in range(SIZE):
- if matrix[row_index][col_index] == 'w':
- w_row, w_col = row_index, col_index
- if matrix[row_index][col_index] == 'b':
- b_row, b_col = row_index, col_index
- turns = deque(['black', 'white'])
- while True:
- turns.rotate()
- if turns[0] == 'white':
- w_fist_row, w_first_col = get_next_position_func(turns, w_row, w_col)
- if w_fist_row == 0 and not check_diagonals_func(turns, w_row, w_col): # IF 1ST MOVE WIN !
- print(f'Game over! White pawn is promoted to a queen at {board[w_first_col]}8.')
- break
- if check_diagonals_func(turns, w_row, w_col): # WIN BY ATTACKING !
- print(f'Game over! White win, capture on {board[b_col]}{8 - b_row}.')
- break
- matrix[w_row][w_col] = '-'
- w_row, w_col = get_next_position_func(turns, w_row, w_col)
- matrix[w_row][w_col] = 'w'
- if w_row == 0: # WIN BY PROMOTE A QUEEN !
- print(f'Game over! White pawn is promoted to a queen at {board[w_col]}8.')
- break
- # -----------------------------------------------------------------------------------------------------
- elif turns[0] == 'black':
- b_fist_row, b_first_col = get_next_position_func(turns, b_row, b_col)
- if b_fist_row == 7 and not check_diagonals_func(turns, b_row, b_col): # IF 1ST MOVE WIN !
- print(f'Game over! Black pawn is promoted to a queen at {board[b_first_col]}1.')
- break
- if check_diagonals_func(turns, b_row, b_col): # WIN BY ATTACKING !
- print(f'Game over! Black win, capture on {board[w_col]}{8 - w_row}.')
- break
- matrix[b_row][b_col] = '-'
- b_row, b_col = get_next_position_func(turns, b_row, b_col)
- matrix[b_row][b_col] = 'b'
- if b_row == 7: # WIN BY PROMOTE A QUEEN !
- print(f'Game over! Black pawn is promoted to a queen at {board[b_col]}1.')
- break
- -----------------------------------------------------------------------------------------------------------
- # 03. Springtime
- def start_spring(**param):
- elements_dict = {}
- for key, value in param.items():
- if value not in elements_dict.keys():
- elements_dict[value] = [key]
- else:
- if key not in elements_dict.values():
- elements_dict[value].append(key)
- final_print = ""
- for key, value in sorted(elements_dict.items(), key=lambda x: (-len(x[1]), x[0])):
- final_print += f'{key}:\n'
- for sub_value in sorted(value):
- final_print += f'-{sub_value}\n'
- return final_print
- -----------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement