Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Role of "helper functions"?

I am not looking for help on this assignment. I have already completed the first three parts; I am only posting the entire question here to help clarify it. I am only confused about part 4 (written below); I am not sure what it's asking for and am wondering if someone could explain what is meant by "helper functions," and what I am supposed to compose.

The criteria for admission are based on the scores on the three parts of the SAT, and rank in the high school graduating class; the exact criteria are given below. These rules are applied in the priority order shown here (1, 2, 3, etc.).

  • If the input data is invalid (SAT less than 200 or greater than 800, or a class rank that is not a positive integer) then they are rejected

  • If any test score is 800, then they are accepted

  • If any test score is below 300, then they are rejected

  • If the average test score is greater than 650 and the class rank less than or equal to 25, they are accepted.

  • If two or more of the test scores are less than 400 or the class rank is greater than or equal to 75, they are rejected.

  • In any other instance, the applicant is placed on a waiting-list Da program to replace the spreadsheet (with several functions) to determine if a student will be accepted, rejected or wait-listed. Your solution MUST contain the following functions:

Part 1:

A function called admissionStatus(sat_math,sat_reading,sat_writing,class_rank) that accepts four parameters (as shown) and returns a string "Accept", "Reject", or "Waitlist"

Part 2:

A function called isvalid(sat_math,sat_reading,sat_writing,class_rank) that returns True or False, depending on whether the input data are valid. If all of the data is valid, it returns True, otherwise it returns False.

Part 3:

A main function that prompts the user for pertinent data (applicant name, math SAT score, reading SAT score, writing SAT score, and class rank. It also calls admissionStatus() passing the appropriate parameters.

Part 4:

Any other helper functions that you think appropriate (Implement some of the computations within admission status as separate functions that can be called from admission status. For instance, you might want to write a function for rule 2 above).

Again - I am only asking about part 4. Am I supposed to write a function for each of the criteria? I'm really not sure.

like image 815
Mark Mason Avatar asked Mar 16 '17 14:03

Mark Mason


2 Answers

A helper function is a function that performs part of the computation of another function

-- from Google

def add(a, b): # <-- This is a helper function
    return a + b

def main():
    subtract = 10 - 9
    multiply = 2 * 2
    add(5, 4) # <-- Helper function is called here

The add(5, 4) is a helper function. You have already defined add(a, b) and added the required functionality in the function. Now you can use that add function as many time as you like, wherever you like and it will add two integers for you.

So the add function is "helping" you to add two integers as many times as you like, wherever and whenever you like.

like image 170
Maddy Avatar answered Nov 13 '22 01:11

Maddy


A "helper function" is a function you write because you need that particular functionality in multiple places, and because it makes the code more readable.
A good example is an average function. You'd write a function named avg or similar, that takes in a list of numbers, and returns the average value from that list.
You can then use that function in your main function, or in other, more complex helper functions, wherever you need it. Basically any block of code that you use multiple times would be a good candidate to be made into a helper function.
Another reason for helper functions is to make the code easier to read. For instance, I might be able to write a really clever line of code to take the average of a list of numbers, and it only takes a single line, but it's complicated and hard to read. I could make a helper function and replace my complicated line with one that's much easier to read.

like image 28
E.D. Avatar answered Nov 13 '22 01:11

E.D.