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 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)
            total = total + steps
        line_data = (date, total)
        records.append(line_data)
    return records

Homework Answers

Answer #1
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 get_total(readings):
    total = 0
    for reading in readings:
        steps = int(reading)
        total = total + steps
    return total

def get_readings(line):
    date, step_string = line.strip().split(':')
    readings = step_string.split(',')
    return (date, readings)
    
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):
        date, readings = get_readings(lines[line_num])
        total = get_total(readings)
        line_data = (date, total)
        records.append(line_data)
    return records
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 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
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]+" "+...
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.”....
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);...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT