Home > Blockchain >  How can I let my code return the average of the numbers?
How can I let my code return the average of the numbers?

Time:11-07

So I wrote a code that would return the distance from parse_dist(s) and the total time parse_time(s). However, I defined another function jogging_average(activities) that would return the distance and time for every string in activities: list.

Now I want to let jogging_average(activities: list) return (x x)/(y y) (basically the average). How can I modify my code to find the average like that??

CodePudding user response:

def jogging_average(activities: list[str]):
    cum_dist = cum_time = 0
    for activity in activities:
        if activity.startswith("jogging"):
            cum_dist  = float(parse_dist(activity))
            cum_time  = parse_time(activity)
    return cum_time/cum_dist

CodePudding user response:

Unrelated to your question, on your parsing functions, you might consider

import re
def parse_time(s: str) -> float:
    result = list(map(int,re.search("(?<=time:)(\d (,\d )?)").group(0).split(",")))
    return result[0]*60   (0 if len(result) < 2 else result[1])

def parse_dist(s: str) -> int:
    return float(re.search("(?<=distance:)(\d (.\d )?)").group(0))
  • Related