Advertisement
SimeonTs

SUPyF Lists - 08. Append Lists

Jun 15th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. """
  2. Lists
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/924#7
  4.  
  5. 08. Append Lists
  6.  
  7. Условие:
  8. Write a program to append several lists of numbers.
  9. - Lists are separated by ‘|’.
  10. - Values are separated by spaces (‘ ’, one or several)
  11. -Order the lists from the last to the first, and their values from left to right.
  12. Examples
  13. Input                       Output
  14. 1 2 3 |4 5 6 |  7  8        7 8 4 5 6 1 2 3
  15. 7 | 4  5|1 0| 2 5 |3        3 2 5 1 0 4 5 7
  16. 1| 4 5 6 7  |  8 9          8 9 4 5 6 7 1
  17. Hints
  18. - Create a new empty list for the results.
  19. - Split the input by ‘|’ into list of tokens.
  20. - Pass through each of the obtained tokens from tight to left.
  21. - For each token, split it by space and append all non-empty tokens to the results.
  22. - Print the results.
  23. """
  24.  
  25. """
  26. How I solved it:
  27.  
  28. First we will take all the added strings on one line divided by "|" and it will add them in a new list one by one:
  29. then we need to create a new empty list to store the converted items.
  30. Then using for loop in reverse for each item in the first list. On each cycle of the list we will add the item to
  31. the new(empty) list but we will split the item by " "(blank space).
  32. Then using another list checking each index of the new list. And if the index is NOT empty ("") we will print the index
  33. on one line. Separated by spaces.
  34. """
  35.  
  36. tokens = [str(item) for item in input().split("|")]
  37. nums = []
  38.  
  39. for item in reversed(tokens):
  40.     nums += item.split(" ")
  41. for num in nums:
  42.     if num != "":
  43.         print(num, end=" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement