25 XP

42. Count the rises

Beginner lists loops

A weather station writes down the temperature once an hour. Write a function count_rises(readings) that takes the list of readings and returns how many times a reading is higher than the one just before it.

  • Only a reading that is strictly higher counts: going from 6 to 6 isn't a rise.
  • Each reading is compared with the one just before it, not with the first reading or the highest so far.
  • A list with fewer than two readings has nothing to compare, so the answer is 0.

Example 1

Inputreadings = [3, 5, 4, 6, 6, 8]
Output3
Explanation3 to 5, 4 to 6, 6 to 8; 6 to 6 isn't one.

Example 2

Inputreadings = [10, 1, 10, 1, 10]
Output2
ExplanationEach 1 to 10 counts, though 10 isn't new.

Example 3

Inputreadings = [5, 4, 3]
Output0
ExplanationThe readings only ever go down.

Constraints

  • readings has 0 to 100 whole numbers, each from -100 to 100.

Submit also runs 7 more tests you can't see.

Practises Lists (lesson 9).

Code
def count_rises(readings):
    """Count the readings higher than the one before.

    readings: list of int; may be empty
    Returns: int, how many times the next reading is
        higher than the one before it
    """
    # Write your code here
    pass