5.43. Homework: Creating Functions from Sample Input and Output

In this homework you will be given some input and output and will determine how the function transforms the input into the output. Then you can either generate code using generative AI like Chat GPT or write the code yourself.

Note

If you work in a group of two to three people, only one member of the group should fill in the answers on this page. You will be able to share your answers with the other group members at the bottom of the page.

5.43.1. Function 1

Function1 takes a list of numbers and outputs a number as shown below:

Input                             Output
function1([10, 80, 100])           90.0
function1([70, 50, 90])            80.0
function1([2, 4, 6, 8])            6.0

To check that you understand what the function is doing please enter the expected output for the following input:

Q-3: Paste the prompt that you used to generate the code or write a description of what this code does.

Write the function function1(nums) that takes a list of numbers and outputs a number as shown below:

Input                             Output
function1([10, 80, 100])           90.0
function1([70, 50, 90])            80.0
function1([2, 4, 6, 8])            6.0

5.43.2. Function 2

Function 2 takes a list of numbers and outputs a number as shown below:

Input                             Output
function2([1, 3, 5, 4])             1
function2([2, 4, 6])                3
function2([4, 1, 6, 13])            2

To check that you understand what the function is doing please enter the expected output for the following input:

Q-7: Paste the prompt that you used to generate the code or write a description of what this code does.

Write the function function2(nums) that takes a list of numbers and outputs a number as shown below:

Input                             Output
function2([1, 3, 5, 4])             1
function2([2, 4, 6])                3
function2([4, 1, 6, 13])            2

5.43.3. Function 3

Function 3 takes a number and returns a string as shown below:

Input                             Output
function3(7)                      "child"
function3(8)                      "tween"
function3(12)                     "tween"
function3(13)                     "teen"
function3(17)                     "teen"
function3(20)                     "adult"

To check that you understand what the function is doing please enter the expected output for the following input:

Q-11: Paste the prompt that you used to generate the code or write a description of what this code does.

Write the function function3(num) that takes a number and returns a string as shown below:

Input                             Output
function3(7)                      "child"
function3(8)                      "tween"
function3(12)                     "tween"
function3(13)                     "teen"
function3(17)                     "teen"
function3(20)                     "adult"

5.43.4. Function 4

Function 4 takes a list of numbers and outputs a list of numbers as shown below:

Input                                  Output
function4([1, 3, -2, 5])            [1, 3, 0, 5]
function4([-8, 2, 4, -1])           [0, 2, 4, 0]
function4([0, 3, 5])                [0, 3, 5]

To check that you understand what the function is doing please enter the expected output for the following input:

Q-15: Paste the prompt that you used to generate the code or write a description of what this code does.

Write the function function4(nums) and takes a list of numbers and outputs a list of numbers as shown below:

Input                             Output
function4([1, 3, -2, 5])            [1, 3, 0, 5]
function4([-8, 2, 4, -1])           [0, 2, 4, 0]
function4([0, 3, 5])                [0, 3, 5]

5.43.5. Extra Credit

Function 5 takes a string and returns a string as shown below:

Input                             Output
function5("Walk This Way")        "Wathwa"
function5("run away")             "Ruaw"
function5("I love puppies")       "Ilopu"

To check that you understand what the function is doing please enter the expected output for the following input:

Q-19: Paste the prompt that you used to generate the code or write a description of what this code does.

Write the function function5(str) that takes a string and returns a string as shown below:

Input                             Output
function5("Walk This Way")        "Wathwa"
function5("run away")             "Ruaw"
function5("I love puppies")       "Ilopu"

If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.

The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.

You have attempted of activities on this page