Python | Pokémon Training Game
Problem :
You are a Pokémon trainer. Each Pokémon has its own power, described by a positive integer value. As you travel, you watch Pokémon and you catch each of them. After each catch, you have to display maximum and minimum powers of Pokémon caught so far. You must have linear time complexity. So sorting won’t help here. Try having minimum extra space complexity.
Examples:
Suppose you catch Pokémon of powers 3 8 9 7. Then the output should be
3 3
3 8
3 9
3 9
Input :
The single line describing powers of N Pokémon caught.
Output :
N lines stating minimum power so far and maximum power
so far separated by single space
Code : Python code to implement Pokemon training game
powers = [ 3 , 8 , 9 , 7 ]
mini, maxi = 0 , 0
for power in powers:
if mini = = 0 and maxi = = 0 :
mini, maxi = powers[ 0 ], powers[ 0 ]
print (mini, maxi)
else :
mini = min (mini, power)
maxi = max (maxi, power)
print (mini, maxi)
|
Output :
3 3
3 8
3 9
3 9
Last Updated :
26 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...