Question

Want to break the following code so that my functions are no larger than 10 statements...

Want to break the following code so that my functions are no larger than 10 statements

def monthly_averages(records):
    """monthly averages"""
    list_of_average_steps_per_month = []
    for month in range(1, 13):
        steps_for_month = []
        for date, steps in records:
            rec_month = int(date[5:7])
            if rec_month == month:
                steps_for_month.append(steps)
        if len(steps_for_month) > 0:
            average_steps_per_month = sum(steps_for_month) / len(steps_for_month)
            list_of_average_steps_per_month.append(average_steps_per_month)
        else:
            list_of_average_steps_per_month.append(-1)
    return list_of_average_steps_per_month

Homework Answers

Answer #1
def getAverageStepsPerMonth(steps_for_month):
    if len(steps_for_month) > 0:
        return sum(steps_for_month) / len(steps_for_month)
    else:
        return -1

def getStepsPerMonth(records, month):
    steps_for_month = []
    for date, steps in records:
        rec_month = int(date[5:7])
        if rec_month == month:
            steps_for_month.append(steps)
    return steps_for_month

def monthly_averages(records):
    """monthly averages"""
    list_of_average_steps_per_month = []
    for month in range(1, 13):
        steps_for_month = getStepsPerMonth(records, month)
        list_of_average_steps_per_month.append(getAverageStepsPerMonth(steps_for_month))
    return list_of_average_steps_per_month
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Want to break the following code so that my functions are no larger than 10 statements...
Want to break the following code so that my functions are no larger than 10 statements def start_end_indices(lines): """finds the beginning and ending lines""" start_index = lines.index('<begin step data>\n') end_index = lines.index('<end step data>\n', start_index + 1) return start_index, end_index def daily_totals_from_lines(lines, start_index, end_index): """iterates through all lines in list""" records = [] for line_num in range(start_index + 1, end_index): line = lines[line_num] date, step_string = line.strip().split(':') readings = step_string.split(',') total = 0 for reading in readings: steps = int(reading)...
Could you fix the errors in the code typed below. I want to find the smallest...
Could you fix the errors in the code typed below. I want to find the smallest composite integer greater than 5993 for which 2 is not a Fermat Witness. from math import sqrt def primeFactors(n): ps = []    while (n%2) == 0: p = 2 ps.append(p) n = n/p    sqrt_n = int (sqrt(n))+1 for p in range(3, sqrt_n,2): while (n%p) == 0: ps.append(p) n = n/p    if n>sqrt_n-1: p=n ps.append(p) return ps fL(5993):    for n in...
i need this code to print a number of stars depending on how much the user...
i need this code to print a number of stars depending on how much the user wants and after * it prints no stars. if the user enters a negative number it should print "error invalid number" this is my code so far: def stars(n,i): stars(n, 1) if n <= 0: return "No stars" if i <= n: print("* ", end="") stars(n, i + 1) else: print("no stars") stars(n - 1, 1) n = int(input("enter an integer")) def main(): stars()...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void search_node(struct linked_list* list, int find_node_ value) (The function to make) This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”....
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You must...
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
How do I get this portion of my List Iterator code to work? This is a...
How do I get this portion of my List Iterator code to work? This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this Exception in thread "main" java.lang.NullPointerException    at AddressBookApp.main(AddressBookApp.java:36) iter.reset(); Person findPerson = iter.findLastname("Duck"); if (findPerson == null) System.out.println("Person not found."); else findPerson.displayEntry(); findPerson = iter.findLastname("Duck"); if (findPerson == null) { System.out.println("Person not found.");...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...