Advertisement
GeorgiLukanov87

list_and_list_more_ex

Jun 14th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.99 KB | None | 0 0
  1. LIST EXERC -
  2. ///////////////////////////////////////////////////////////////////////////
  3.  
  4. 1
  5.  
  6. my_list = input().split()
  7. new_list = []
  8. for element in my_list:
  9.     new_list.append(int(element)*-1)
  10. print(new_list)
  11.  
  12. ///////////////////////////////////////////////////////////////////////////
  13.  
  14. 2
  15.  
  16. factor = int(input())
  17. count = int(input())
  18.  
  19. data = []
  20.  
  21. for i in range(1, count + 1):
  22.     data.append(i * factor)
  23.  
  24. print(data)
  25.  
  26. ///////////////////////////////////////////////////////////////////////////
  27.  
  28. 3
  29.  
  30. red_cards = input().split(' ')
  31. team_A = []
  32. team_B = []
  33. TOTAL_PLAYERS_IN_TEAM = 11
  34. is_game_terminated = False
  35.  
  36. for player in range(1, TOTAL_PLAYERS_IN_TEAM + 1): #making team_A
  37.     team_A.append("A-" + str(player))
  38.    
  39. for player in range(1, TOTAL_PLAYERS_IN_TEAM + 1): #making team_B
  40.     team_B.append("B-" + str(player))
  41.  
  42. for card in red_cards:
  43.     if card in team_A:
  44.         team_A.remove(card)
  45.     elif card in team_B:
  46.         team_B.remove(card)
  47.     if len(team_A) < 7 or len(team_B) < 7:
  48.         is_game_terminated = True
  49.         break
  50.  
  51. print(f"Team A - {len(team_A)}; Team B - {len(team_B)}")
  52. if is_game_terminated:
  53.     print(f"Game was terminated")
  54.  
  55. ///////////////////////////////////////////////////////////////////////////
  56.  
  57. 4
  58.  
  59. coins = list(map(int, input().split(",")))
  60. beggars = int(input())
  61. list_of_beggars = [0] * beggars
  62. counter_beggars = 0
  63. for coin in coins:
  64.     list_of_beggars[counter_beggars] += coin
  65.     counter_beggars += 1
  66.     if counter_beggars >= beggars:
  67.         counter_beggars = 0
  68. print(list_of_beggars)
  69.  
  70.  
  71. ///////////////////////////////////////////////////////////////////////////
  72.  
  73. 5
  74.  
  75. card = input().split()
  76. number_shuffles = int(input())
  77. half = len(card) // 2
  78.  
  79. for shuffles in range(number_shuffles):
  80.  
  81.     current_shuffle = zip(card[:half], card[half:])
  82.     card.clear()
  83.    
  84.     for pair in current_shuffle:
  85.         card.extend(pair)
  86.  
  87. print(card)
  88.  
  89.  
  90. ///////////////////////////////////////////////////////////////////////////
  91.  
  92. 6
  93.  
  94. numbers = input().split(' ')
  95. n = int(input())
  96. list_as_int = []
  97. for element in numbers:
  98.     list_as_int.append(int(element))
  99. for nums in range(1, n + 1):
  100.     list_as_int.remove(min(list_as_int))
  101. final_result = []
  102. for element in list_as_int:
  103.     final_result.append(str(element))
  104. final_print = ', '.join(final_result)
  105. print(final_print)
  106.  
  107.  
  108. ///////////////////////////////////////////////////////////////////////////
  109.  
  110. 7
  111.  
  112. gifts_list = input().split(" ")
  113. command = input()
  114.  
  115. while not command == 'No Money':
  116.     if 'OutOfStock' in command:
  117.         command, gift = command.split(" ")
  118.         for current_gift in range(len(gifts_list)):
  119.             if gifts_list[current_gift] == gift:
  120.                 gifts_list[current_gift] = 'None'
  121.     elif 'Required' in command:
  122.         command, gift, index = command.split(" ")
  123.         index = int(index)
  124.         if 0 < index < len(gifts_list):
  125.             gifts_list[index] = gift
  126.     elif 'JustInCase' in command:
  127.         command, gift = command.split(" ")
  128.         gifts_list[-1] = gift
  129.     command = input()
  130.  
  131. for gifts in range(len(gifts_list)):
  132.     if 'None' in gifts_list:
  133.         gifts_list.remove('None')
  134. print(' '.join(gifts_list))
  135.  
  136. ///////////////////////////////////////////////////////////////////////////
  137.  
  138. 8
  139.  
  140. HIGH_RANGE = range(81, 126)
  141. MID_RANGE = range(51, 81)
  142. LOW_RANGE = range(1, 51)
  143. fire_data = input().split('#')
  144. total_water = int(input())
  145. fire_cells = []
  146. for every_fire in fire_data:
  147.     fire_range, fire_value = every_fire.split(' = ')
  148.     fire_value = int(fire_value)
  149.     if fire_value > total_water:
  150.         continue
  151.     if fire_range == 'High' and fire_value not in HIGH_RANGE:
  152.         continue
  153.     elif fire_range == 'Medium' and fire_value not in MID_RANGE:
  154.         continue
  155.     elif fire_range == 'Low' and fire_value not in LOW_RANGE:
  156.         continue
  157.     total_water -= fire_value
  158.     fire_cells.append(fire_value)
  159. print("Cells:")
  160. for cell in fire_cells:
  161.     print(f" - {cell}")
  162. effort = sum(fire_cells) * 0.25
  163. print(f'Effort: {effort:.2f}')
  164. total_fire = sum(fire_cells)
  165. print(f"Total Fire: {total_fire}")
  166.  
  167. ///////////////////////////////////////////////////////////////////////////
  168.  
  169. 9
  170.  
  171. items = input().split("|")
  172. budget = int(input())
  173. start_budget = budget
  174. income = []
  175. profit = 0
  176. for item in items:
  177.     product, price = item.split('->')
  178.     price = float(price)
  179.     condition = False
  180.     if price > budget:
  181.         continue
  182.     if product == 'Clothes':
  183.         if not 0 <= price <= 50:
  184.             condition = True
  185.     elif product == 'Shoes':
  186.         if not 0 <= price <= 35:
  187.             condition = True
  188.     else:
  189.         if not 0 <= price <= 20.50:
  190.             condition = True
  191.     if not condition:
  192.         budget -= price
  193.         income.append(price + price * 0.40)
  194.         profit += price * 0.40
  195. for el in income:
  196.     print(f"{el:.2f}", end=' ')
  197. print()
  198. print(f"Profit: {profit:.2f}")
  199. if start_budget + profit >= 150:
  200.     print('Hello, France!')
  201. else:
  202.     print('Not enough money.')
  203.  
  204.  
  205. ///////////////////////////////////////////////////////////////////////////
  206.  
  207. 10
  208.  
  209. events = input().split('|')
  210. energy = 100
  211. coins = 100
  212. closed = False
  213. for event in events:
  214.     action, numbers = event.split('-')
  215.     numbers = int(numbers)
  216.     if action == 'rest':
  217.         temp = energy + numbers
  218.         if temp >= 100:
  219.             print(f'You gained {100 - energy} energy.')
  220.         else:
  221.             print(f'You gained {numbers} energy.')
  222.         energy += numbers
  223.            
  224.         if energy > 100:
  225.             energy = 100
  226.  
  227.         print(f'Current energy: {energy}.')
  228.  
  229.     elif action == 'order':
  230.         if energy < 30:
  231.             energy += 50
  232.             print(f"You had to rest!")
  233.             continue
  234.         coins += numbers
  235.         energy -= 30
  236.         print(f"You earned {numbers} coins.")
  237.  
  238.     else:
  239.         if coins >= numbers:
  240.             coins -= numbers
  241.             print(f"You bought {action}.")
  242.         else:
  243.             print(f"Closed! Cannot afford {action}.")
  244.             closed = True
  245.             break
  246.  
  247. if not closed:
  248.     print(f"Day completed!")
  249.     print(f"Coins: {coins}")
  250.     print(f"Energy: {energy}")
  251.  
  252. ///////////////////////////////////////////////////////////////////////////
  253.  
  254. Lists Basics - More Exercises !
  255.  
  256. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  257.  
  258. 1
  259.  
  260. numbers = input().split(", ")
  261. numbers = list(map(int, numbers))
  262. for zero in numbers:
  263.     if zero == 0:
  264.         numbers.remove(zero)
  265.         numbers.append(0)
  266. print(numbers)
  267.  
  268. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  269.  
  270. 2
  271.  
  272. numbers = input().split(" ")
  273. message = input()
  274. sum_list = []
  275.  
  276. for num in numbers:
  277.     numbers_sum = 0
  278.     for i in num:
  279.         numbers_sum += int(i)
  280.     sum_list.append(numbers_sum)
  281.  
  282. for index in range(len(sum_list)):
  283.     letter_to_remove = ""
  284.     result = sum_list[index] % len(message)
  285.     letter_to_remove += message[result]
  286.     print(letter_to_remove, end='')
  287.     new_message = message.replace(str(letter_to_remove), "", 1)
  288.     message = new_message
  289.  
  290. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  291.  
  292. 3
  293.  
  294.  
  295. time = list(map(int, input().split(" ")))
  296. divisor = len(time)//2
  297. left_time = time[:divisor]
  298. right_time = time[:divisor:-1]
  299. left_racer_time = 0
  300. for index in range(len(left_time)):
  301.     if left_time[index] == 0:
  302.         left_racer_time *= 0.80
  303.     left_racer_time += left_time[index]
  304. right_racer_time = 0
  305. for index in range(len(right_time)):
  306.     if right_time[index] == 0:
  307.         right_racer_time *= 0.80
  308.     right_racer_time += right_time[index]
  309. if left_racer_time < right_racer_time:
  310.     print(f"The winner is left with total time: {left_racer_time:.1f}")
  311. else:
  312.     print(f"The winner is right with total time: {right_racer_time:.1f}")
  313.  
  314.  
  315. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  316. 4
  317.  
  318.  
  319. soldiers = list(map(int, input().split()))
  320. k = int(input())
  321.  
  322. executed = []
  323. position = k - 1
  324. index = 0
  325. len_list = (len(soldiers))
  326.  
  327. while len_list > 0:
  328.     index = (position + index) % len_list
  329.     current_killed = (soldiers.pop(index))
  330.     len_list -= 1
  331.     executed.append(current_killed)
  332.  
  333. print(str(executed).replace(" ", ""))
  334.  
  335.  
  336. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  337.  
  338. 5
  339.  
  340.  
  341. l1 = list(map(int, input().split()))
  342. l2 = list(map(int, input().split()))
  343. l3 = list(map(int, input().split()))
  344. if (l1[0] == 1 and l1[1] == 1 and l1[2] == 1) or (l2[0] == 1 and l2[1] == 1 and l2[2] == 1) or (
  345.         l3[0] == 1 and l3[1] == 1 and l3[2] == 1) or \
  346.         (l1[0] == 1 and l2[1] == 1 and l3[2] == 1) or (l1[2] == 1 and l2[1] == 1 and l3[0] == 1) or (
  347.         l1[0] == 1 and l2[0] == 1 and l3[0] == 1) or \
  348.         (l1[1] == 1 and l2[1] == 1 and l3[1] == 1) or (l1[2] == 1 and l2[2] == 1 and l3[2] == 1):
  349.     print('First player won')
  350. elif (l1[0] == 2 and l1[1] == 2 and l1[2] == 2) or (l2[0] == 2 and l2[1] == 2 and l2[2] == 2) or (
  351.         l3[0] == 2 and l3[1] == 2 and l3[2] == 2) or \
  352.         (l1[0] == 2 and l2[1] == 2 and l3[2] == 2) or (l1[2] == 2 and l2[1] == 2 and l3[0] == 2) or (
  353.         l1[0] == 2 and l2[0] == 2 and l3[0] == 2) or \
  354.         (l1[1] == 2 and l2[1] == 2 and l3[1] == 2) or (l1[2] == 2 and l2[2] == 2 and l3[2] == 2):
  355.     print('Second player won')
  356. else:
  357.     print("Draw!")
  358.  
  359. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  360. 6
  361.  
  362.  
  363. import sys
  364.  
  365.  
  366. def is_valid_index(collection: list, index: int):
  367.     if 0 <= index < len(collection):
  368.         return True
  369.  
  370.     return False
  371.  
  372.  
  373. def exchange_list_at_index(collection: list, index: int):
  374.     exchanged_list = []
  375.  
  376.     if not is_valid_index(collection, index):
  377.         print('Invalid index')
  378.         exchanged_list = collection
  379.     else:
  380.         left_sub_list = collection[:index + 1]
  381.         right_sub_list = collection[index + 1:]
  382.  
  383.         for el in right_sub_list:
  384.             exchanged_list.append(el)
  385.  
  386.         for el in left_sub_list:
  387.             exchanged_list.append(el)
  388.  
  389.     return exchanged_list
  390.  
  391.  
  392. def max_num_by_criteria(collection: list, custom_filter):
  393.     # Returns the index of max even/odd element by given filter function.
  394.     # It returns -1 if there is no match
  395.     max_element = float('-inf')
  396.     max_element_index = -1
  397.  
  398.     for i in range(len(collection)):
  399.         num = collection[i]
  400.  
  401.         if custom_filter(num) and num >= max_element:
  402.             max_element = num
  403.             max_element_index = i
  404.  
  405.     if max_element_index == -1:
  406.         # No matches found
  407.         print('No matches')
  408.  
  409.     return max_element_index
  410.  
  411.  
  412. def min_num_by_criteria(collection: list, custom_filter):
  413.     min_element = sys.maxsize
  414.     min_element_index = -1
  415.  
  416.     for i in range(len(collection)):
  417.         num = collection[i]
  418.  
  419.         if custom_filter(num) and num <= min_element:
  420.             min_element = num
  421.             min_element_index = i
  422.  
  423.     if min_element_index == -1:
  424.         print('No matches')
  425.  
  426.     return min_element_index
  427.  
  428.  
  429. def first_count_elements_by_criteria(collection: list, count: int, custom_filter):
  430.     if count > len(collection):
  431.         print('Invalid count')
  432.     else:
  433.         matching_elements = []
  434.  
  435.         for num in collection:
  436.             if custom_filter(num) and len(matching_elements) < count:
  437.                 matching_elements.append(num)
  438.  
  439.         print(matching_elements)
  440.  
  441.  
  442. def last_count_elements_by_criteria(collection: list, count: int, custom_filter):
  443.     if count > len(collection):
  444.         print('Invalid count')
  445.     else:
  446.         matching_elements = []
  447.  
  448.         for i in range(len(collection) - 1, -1, -1):
  449.             num = collection[i]
  450.  
  451.             if custom_filter(num) and len(matching_elements) < count:
  452.                 matching_elements.append(num)
  453.  
  454.         print(matching_elements[::-1])
  455.  
  456.  
  457. def parse_collection_to_int(collection: list):
  458.     parsed_collection = []
  459.  
  460.     for el in collection:
  461.         parsed_collection.append(int(el))
  462.  
  463.     return parsed_collection
  464.  
  465.  
  466. def stringify_collection(collection: list, delimiter: str):
  467.     parsed_collection = []
  468.  
  469.     for num in collection:
  470.         parsed_collection.append(str(num))
  471.  
  472.     return delimiter.join(parsed_collection)
  473.  
  474.  
  475. elements = input().split()
  476. numbers = parse_collection_to_int(elements)
  477.  
  478. command = input()
  479. while command != "end":
  480.     cmd_args = command.split()
  481.     cmd_type = cmd_args[0]
  482.  
  483.     if cmd_type == "exchange":
  484.         index = int(cmd_args[1])
  485.  
  486.         numbers = exchange_list_at_index(numbers, index)
  487.     elif cmd_type == "max":
  488.         cmd_filter = cmd_args[1]
  489.  
  490.         max_index = -1
  491.  
  492.         if cmd_filter == "even":
  493.             max_index = max_num_by_criteria(numbers, lambda n: n % 2 == 0)
  494.         elif cmd_filter == "odd":
  495.             max_index = max_num_by_criteria(numbers, lambda n: n % 2 != 0)
  496.  
  497.         if max_index != -1:
  498.             print(max_index)
  499.     elif cmd_type == "min":
  500.         cmd_filter = cmd_args[1]
  501.  
  502.         min_index = -1
  503.  
  504.         if cmd_filter == "even":
  505.             min_index = min_num_by_criteria(numbers, lambda n: n % 2 == 0)
  506.         elif cmd_filter == "odd":
  507.             min_index = min_num_by_criteria(numbers, lambda n: n % 2 != 0)
  508.  
  509.         if min_index != -1:
  510.             print(min_index)
  511.     elif cmd_type == "first":
  512.         count = int(cmd_args[1])
  513.         cmd_filter = cmd_args[2]
  514.  
  515.         if cmd_filter == "even":
  516.             first_count_elements_by_criteria(numbers, count, lambda n: n % 2 == 0)
  517.         elif cmd_filter == "odd":
  518.             first_count_elements_by_criteria(numbers, count, lambda n: n % 2 != 0)
  519.     elif cmd_type == "last":
  520.         count = int(cmd_args[1])
  521.         cmd_filter = cmd_args[2]
  522.  
  523.         if cmd_filter == "even":
  524.             last_count_elements_by_criteria(numbers, count, lambda n: n % 2 == 0)
  525.         elif cmd_filter == "odd":
  526.             last_count_elements_by_criteria(numbers, count, lambda n: n % 2 != 0)
  527.  
  528.     command = input()
  529.  
  530. print(f'[{stringify_collection(numbers, ", ")}]')
  531.  
  532. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  533.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement