Advertisement
KNenov96

Lists Advanced - Exercise

Sep 19th, 2022
235
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.12 KB | Source Code | 1 0
  1. # ========== 01. Which Are In?
  2.  
  3. first_sequence = input().split(", ")
  4. second_sequence = input().split(", ")
  5.  
  6. substrings = []
  7.  
  8. for substring in first_sequence:
  9.     for string in second_sequence:
  10.         if substring in string:
  11.             if substring not in substrings:
  12.                 substrings.append(substring)
  13. print(substrings)
  14.  
  15.  
  16. # ========== 02. Next Version
  17.  
  18. version = list(map(int, input().split(".")))
  19. first_digit = version[0]
  20. second_digit = version[1]
  21. third_digit = version[2]
  22.  
  23. third_digit += 1
  24. if third_digit == 10:
  25.     second_digit += 1
  26.     third_digit = 0
  27.     if second_digit == 10:
  28.         first_digit += 1
  29.         second_digit = 0
  30.  
  31. print(f"{first_digit}.{second_digit}.{third_digit}")
  32.  
  33.  
  34. # ========== 03. Word Filter
  35.  
  36. texts = input().split()
  37. even_length = [x for x in texts if len(x) % 2 == 0]
  38.  
  39. for word in even_length:
  40.     print(word)
  41.    
  42.    
  43. # ========== 04. Number Classification
  44.  
  45. def filtering_numbers(nums):
  46.     positive_filter = ", ".join(f"{x}" for x in nums if x >= 0)
  47.     negative_filter = ", ".join(f"{x}" for x in nums if x < 0)
  48.     even_filter = ", ".join(f"{x}" for x in nums if x % 2 == 0)
  49.     odd_filter = ", ".join(f"{x}" for x in nums if x % 2 != 0)
  50.  
  51.     print(f"""Positive: {positive_filter}
  52. Negative: {negative_filter}
  53. Even: {even_filter}
  54. Odd: {odd_filter}""")
  55.  
  56.  
  57. numbers_for_filter = list(map(int, input().split(", ")))
  58. filtering_numbers(numbers_for_filter)
  59.  
  60.  
  61. # ========== 05. Office Chairs
  62.  
  63. number_of_rooms = int(input())
  64. game_on = True
  65. free_chairs = 0
  66.  
  67. for room in range(1, number_of_rooms +1):
  68.     chairs_and_visitors = input().split()
  69.     chairs = len(chairs_and_visitors[0])
  70.     visitors = int(chairs_and_visitors[1])
  71.     if chairs < visitors:
  72.         print(f"{visitors-chairs} more chairs needed in room {room}")
  73.         game_on = False
  74.     else:
  75.         free_chairs += chairs-visitors
  76. if game_on:
  77.     print(f"Game On, {free_chairs} free chairs left")
  78.    
  79.    
  80. # ========== 06. Electron Distribution
  81.  
  82. number_of_electrons = int(input())
  83. shells = []
  84. counter = 1
  85.  
  86. while number_of_electrons != 0:
  87.     formula = 2 * (counter ** 2)
  88.     if number_of_electrons >= formula:
  89.         shells.append(formula)
  90.     else:
  91.         shells.append(number_of_electrons)
  92.         break
  93.     number_of_electrons -= formula
  94.     counter += 1
  95. print(shells)
  96.  
  97.  
  98. # ========== 07. Group of 10's
  99.  
  100.  
  101. number_of_sequence = list(map(int, input().split(", ")))
  102. counter = 0
  103. tens = []
  104.  
  105. num_for_loop = (max(number_of_sequence) // 10) + 1
  106. if max(number_of_sequence) % 10 == 0:
  107.     num_for_loop -= 1
  108.  
  109. for index in range(num_for_loop):
  110.     counter += 10
  111.     print(f"Group of {counter}'s:", [x for x in number_of_sequence if x <= counter])
  112.     number_of_sequence = [x for x in number_of_sequence if x > counter]
  113.    
  114.    
  115. # ========== 08. Decipher This!
  116.  
  117. secret_message = input().split()
  118. decoded_word = []
  119. decoded_digits = ""
  120.  
  121. for decode in secret_message:
  122.     for letters in decode:
  123.         if letters.isdigit():
  124.             decoded_digits += letters
  125.         else:
  126.             decoded_word.append(letters)
  127.     decoded_word[0], decoded_word[-1] = decoded_word[-1], decoded_word[0]
  128.     decoded_word.insert(0, chr(int(decoded_digits)))
  129.     print("".join(decoded_word), end=" ")
  130.     decoded_word = []
  131.     decoded_digits = ""
  132.    
  133.    
  134. # ========== 09. Anonymous Threat
  135.  
  136. array_data = input().split()
  137. command = input().split()
  138.  
  139. while command[0] != "3:1":
  140.     current_command = command[0]
  141.  
  142.     if current_command == "merge":
  143.         start_index = int(command[1])
  144.         end_index = int(command[2])
  145.         if start_index < 0:
  146.             start_index = 0
  147.         if end_index >= len(array_data):
  148.             end_index = len(array_data)-1
  149.         for merging in range(start_index+1, end_index+1):
  150.             array_data[start_index] += array_data[merging]
  151.             array_data[merging] = ""
  152.  
  153.     elif current_command == "divide":
  154.         divided_words = []
  155.         element_for_divide = int(command[1])
  156.         amount_partitions = int(command[2])
  157.         one_partition = len(array_data[element_for_divide]) // amount_partitions
  158.         for parts in range(1, amount_partitions+1):
  159.             if parts == amount_partitions and len(array_data[element_for_divide]) % amount_partitions != 0:
  160.                 divided_words.append(array_data[element_for_divide][::])
  161.             else:
  162.                 divided_words.append(array_data[element_for_divide][:one_partition])
  163.                 array_data[element_for_divide] = array_data[element_for_divide][one_partition:]
  164.         array_data.pop(element_for_divide)
  165.         for elements in reversed(divided_words):
  166.             array_data.insert(element_for_divide, elements)
  167.     array_data = [x for x in array_data if x != ""]
  168.     command = input().split()
  169.  
  170. print(" ".join(array_data))
  171.  
  172.  
  173. # ========== 10. Pokemon Don't Go
  174.  
  175. sequence_of_pokemons = list(map(int, input().split()))
  176. summed_elements = 0
  177. while True:
  178.     indexes = int(input())
  179.     if indexes >= len(sequence_of_pokemons):
  180.         element_for_work = sequence_of_pokemons[-1]
  181.         sequence_of_pokemons[-1] = sequence_of_pokemons[0]
  182.     elif indexes < 0:
  183.         element_for_work = sequence_of_pokemons[0]
  184.         sequence_of_pokemons[0] = sequence_of_pokemons[-1]
  185.     else:
  186.         element_for_work = sequence_of_pokemons[indexes]
  187.         sequence_of_pokemons.pop(indexes)
  188.     for index, pokemon in enumerate(sequence_of_pokemons):
  189.         if element_for_work >= pokemon:
  190.             sequence_of_pokemons[index] += element_for_work
  191.         else:
  192.             sequence_of_pokemons[index] -= element_for_work
  193.  
  194.     summed_elements += element_for_work
  195.     if len(sequence_of_pokemons) == 0:
  196.         break
  197.        
  198. print(summed_elements)
  199.  
  200.  
  201. # ========== 10. SoftUni Course Planning
  202.  
  203. initial_schedule = input().split(", ")
  204. course_start = False
  205.  
  206. while not course_start:
  207.     list_commands = input()
  208.     if list_commands == "course start":
  209.         course_start = True
  210.         break
  211.  
  212.     list_commands = list_commands.split(":")
  213.     current_command = list_commands[0]
  214.     if current_command == "Add":
  215.         lesson = list_commands[1]
  216.         if lesson not in initial_schedule:
  217.             initial_schedule.append(lesson)
  218.     elif current_command == "Insert":
  219.         lesson = list_commands[1]
  220.         index_for_insert = int(list_commands[2])
  221.         if lesson not in initial_schedule:
  222.             initial_schedule.insert(index_for_insert, lesson)
  223.     elif current_command == "Remove":
  224.         lesson = list_commands[1]
  225.         if lesson in initial_schedule:
  226.             initial_schedule.remove(lesson)
  227.         if lesson+"-Exercise" in initial_schedule:
  228.             initial_schedule.remove(lesson+"-Exercise")
  229.     elif current_command == "Swap":
  230.         lesson = list_commands[1]
  231.         second_lesson = list_commands[2]
  232.         if lesson in initial_schedule and second_lesson in initial_schedule:
  233.             first_index_for_swap = initial_schedule.index(lesson)
  234.             second_index_for_swap = initial_schedule.index(second_lesson)
  235.             initial_schedule[first_index_for_swap], initial_schedule[second_index_for_swap] = \
  236.                 initial_schedule[second_index_for_swap], initial_schedule[first_index_for_swap]
  237.             if lesson+"-Exercise" in initial_schedule:
  238.                 initial_schedule.remove(lesson+"-Exercise")
  239.                 initial_schedule.insert(second_index_for_swap+1, lesson+"-Exercise")
  240.             if second_lesson+"-Exercise" in initial_schedule:
  241.                 initial_schedule.remove(second_lesson+"-Exercise")
  242.                 initial_schedule.insert(first_index_for_swap+1, second_lesson+"-Exercise")
  243.     elif current_command == "Exercise":
  244.         lesson = list_commands[1]
  245.         if lesson not in initial_schedule:
  246.             initial_schedule.append(lesson)
  247.             initial_schedule.append(lesson+"-Exercise")
  248.         elif lesson+"-Exercise" not in initial_schedule:
  249.             index_for_exercise = initial_schedule.index(lesson)
  250.             initial_schedule.insert(index_for_exercise+1, lesson+"-Exercise")
  251.  
  252. number_of_lesson = 1
  253. for schedule in initial_schedule:
  254.     print(f"{number_of_lesson}.{schedule}")
  255.     number_of_lesson += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement