Advertisement
SimeonTs

SUPyF Lists - 06. Odd Numbers at Odd Positions

Jun 15th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. """
  2. Lists
  3. Проверка: https://judge.softuni.bg/Contests/Practice/Index/924#5
  4.  
  5. 06. Odd Numbers at Odd Positions
  6.  
  7. Условие:
  8. Write a program to read a list of integers and find how many odd numbers at odd positions the list holds. If there are no numbers, which match this criteria, do not print anything
  9. Examples:
  10.  
  11. Input:
  12. 2 3 5 2 7 9 -1 -7
  13. Output:
  14. Index 1 -> 3
  15. Index 5 -> 9
  16. Index 7 -> -7
  17.  
  18. Input:
  19. 2 3 55 2 4 1
  20. Output:
  21. Index 1 -> 3
  22. Index 5 -> 1
  23.  
  24. Input:
  25. 5 0 1 2
  26. Output:
  27. (no output)
  28.  
  29. Hints
  30. -Positions are counted from 0 from left to right, so if for example the second item (index 1) is odd,
  31. then we should count it, and so on…
  32. -Do NOT count odd numbers, which are at even positions (0, 2, 4, etc…)
  33. """
  34.  
  35. """
  36. How I solved it:
  37.  
  38. First we will take all the added integers on one line divided by space and it will add them in a new list one by one:
  39. Then we will create a counter so it will know which is the index position of the current check.
  40. Then using For Loop for each item in the list we will check first if the Counter is odd and if so we will check if the
  41. current checked item is odd and if so: We will print the output.
  42. Even if the counter is odd or even outside of the IF check we need to add 1 to it.  
  43. """
  44. nums = [int(item) for item in input().split(" ")]
  45. counter = 0
  46. for item in nums:
  47.     if counter % 2 != 0:
  48.         if item % 2 != 0:
  49.             print(f"Index {counter} -> {item}")
  50.     counter += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement