Guest User

Mock Exam 2 - Merging and Squashing

a guest
Jun 13th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. '''
  2. We have the following operations defined for two-digit numbers. There are two possible ways of merging them:
  3.    Merging ab and cd produces bc
  4.    42 merged with 17 produces 21
  5.    17 merged with 42 produces 74
  6.  
  7.    Squashing ab and cd produces a(b+c)d - the middle digit is the sum of b and c
  8.    42 squashed with 17 produces 437
  9.    39 squashed with 57 produces 347 (9 + 5 = 14, we use only the 4)
  10. You have a sequence of N two-digit numbers. Your task is to merge and squash each pair of adjacent numbers.
  11. Input
  12. All input data is read from the standard input
  13.    On the first line, you will receive an integer N
  14.    On the next N lines you will receive N two-digit numbers
  15.        Each number will be on a separate line
  16. Output
  17. The output data is printed on the standard output
  18.    On the first output line print the merged numbers
  19.        There should be N - 1 of them
  20.        Separate them by spaces
  21.    On the second output line print the squashed numbers
  22.        There should be N - 1 of them
  23.        Separate them by spaces
  24. Constraints
  25.    2 <= N <= 1000
  26.    Numbers will consist of two non-zero digits
  27.    The input data will always be correct and there is no need to check it explicitly
  28. '''
  29.  
  30. def merge_nums(i: str, j: str):
  31.     return i[1] + j[0]
  32.  
  33.  
  34. def squash_nums(i: str, j: str):
  35.     return i[0] + str((int(i[1]) + int(j[0])) % 10) + j[1]
  36.  
  37.  
  38. merge_l = []
  39. squash_l = []
  40.  
  41. n = int(input())
  42. all_nums = [input() for _ in range(n)]
  43.  
  44. for k in range(n - 1):
  45.     merge_l.append(merge_nums(all_nums[k], all_nums[k + 1]))
  46.     squash_l.append(squash_nums(all_nums[k], all_nums[k + 1]))
  47.  
  48. print(*merge_l, sep=' ')
  49. print(*squash_l, sep=' ')
  50.  
  51. # import random
  52. #
  53. # res = [str(random.randrange(1, 9, 1)) + str(random.randrange(1, 9, 1)) for _ in range(1000)]
  54. # print(*res, sep="\n")
  55.  
Advertisement
Add Comment
Please, Sign In to add comment