Count the days a closing price went up

Write codeEasy10 min

Your task

A stock's daily closing prices are listed in date order. An analyst wants to know how often the price rose from one day to the next.

Write count_price_rises(prices) that returns how many days closed higher than the day before.

Input and output:

  • prices: a list of up to 100,000 closing prices in date order, each an integer between 0 and 1,000,000,000. It may be empty.
  • Returns an integer.

Rules:

  • Compare each day only with the day immediately before it.
  • A day counts only when its price is strictly greater than the day before. A tie does not count.
  • The first day has no day before it, so it never counts. An empty list or a single day returns 0.

Examples

count_price_rises(prices: list[int]) -> int
Example 1
Inputcount_price_rises([3, 4, 2, 5, 5])
Output2
Example 2
Inputcount_price_rises([10, 10, 10])
Output0
Example 3
Inputcount_price_rises([1, 3, 2, 4])
Output2