10

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Mason
  • 133
  • 1
  • 2
  • 7

3 Answers3

26

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maddy
  • 2,025
  • 5
  • 26
  • 59
  • I'm sorry, I still don't quite understand- is it something like this? def helper(): if admissionstatus(sat_math) = 800: print "accepted" (I'm not trying to get anyone to do my hw for me, I really just don't get it. If anyone could give me an example or something, that would be great). – Mark Mason Mar 16 '17 at 23:06
  • @MarkMason, I have updated the answer. Let me know if that helps. – Maddy Mar 17 '17 at 14:51
  • That begs the question of the difference between a helper function and a function that is not a helper function. The Wikipedia quote seems to imply almost all functions are helper functions (as they are called by some other function), with only Main() (or the equivalent) and call back functions (or the equivalent) not being helper functions (which does not seem to be useful). Or are helper functions functions that are only called in one place by one function only and thus are private/"owned" by the calling function (not called by any other function)? Or something else? – Peter Mortensen Dec 29 '21 at 17:44
18

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.

E.D.
  • 639
  • 6
  • 14
  • I'm sorry, I still don't quite understand- is it something like this? def helper(): if admissionstatus(sat_math) = 800: print "accepted" (I'm not trying to get anyone to do my hw for me, I really just don't get it. If anyone could give me an example or something, that would be great). – Mark Mason Mar 16 '17 at 23:06
  • 3
    There isn't a set definition of what a helper function looks like. Any function could be a helper function, as long as it allows you to replace a complicated block of code with a single function call, making it much clearer whats happening. – E.D. Mar 17 '17 at 05:01
0

It means that you can create additional functions that might help you complete the project.

Oftentimes, an assignment or test will provide you with a specific function signature. That might imply that you're only allowed to code within that one function to solve the problem.

By specifying that you're allowed to use helper functions, it's saying that you're not limited to just the one function. You can create others to be called from within the specified function.

To be more specific for your case: your assignment requires you to complete the following functions:

  • admissionStatus(sat_math,sat_reading,sat_writing,class_rank)
  • isValid(sat_math,sat_reading,sat_writing,class_rank)
  • a main function

Then, the assignment says:

Implement some of the computations within admission status as separate functions that can be called from admission status.

What computations are available there? Computing the average test score, say. Python's standard library doesn't contain a pre-written function for computing an average. So, you write your own function to do that.

From Calculating arithmetic mean (average) in Python:

def mean(numbers):
    return float(sum(numbers)) / max(len(numbers), 1)

That would be an example of a "helper function".

Community
  • 1
  • 1
leanne
  • 7,940
  • 48
  • 77
  • I'm sorry, I still don't quite understand- is it something like this? def helper(): if admissionstatus(sat_math) = 800: print "accepted" (I'm not trying to get anyone to do my hw for me, I really just don't get it. If anyone could give me an example or something, that would be great). – Mark Mason Mar 16 '17 at 23:02
  • @MarkMason, I've updated my answer to give you a more specific example. – leanne Mar 17 '17 at 00:42