5.44. Discussion: Creating Functions from Sample Input and Output

In this discussion you will be given some input and output for a function and will determine how the function transforms the input into the output. Look at the output. How does it relate to the input? What is the function doing to the input to get the output?

Once you think you know what the funciton is supposed to do, you can either generate the correct code using a generative AI tool like ChatGPT (https://umgpt.umich.edu/) or write the code yourself.

If you use ChatGPT, check the code that ChatGPT generates. Is it using things you have learned in this class so far? If not, tell it to use the things we have covered, like for loops, conditionals, and lists.

Even if you write your own code, describe what the function does in enough detail so that ChatGPT can generate the correct answer and test that it does.

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.44.1. Function 1

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

Input                             Output
function1([10, 80, 100])           72.5
function1([70, 90, 50])            75.0
function1([2, 4, 6, 8])            5.6

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. If you write the code yourself be sure to test your description with ChatGPT to make sure it generates the correct code.

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

Input                             Output
function1([10, 80, 100])           72.5
function1([70, 90, 50])            75.0
function1([2, 4, 6, 8])            5.6

5.44.2. Function 2

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

Input                             Output
function2([1, 4])                   1
function2([2, 4, 6])                0
function2([4, 1, 6, 13])            14

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, 4])                   1
function2([2, 4, 6])                0
function2([4, 1, 6, 13])            14

5.44.3. Function 3

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

Input                             Output
function3(25)                       1
function3(26)                       2
function3(50)                       2
function3(51)                       3
function3(75)                       3
function3(76)                       4

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(25)                       1
function3(26)                       2
function3(50)                       2
function3(51)                       3
function3(75)                       3
function3(76)                       4

5.44.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]
function4([-8, 2, 4, -1])           [2, 4]
function4([0, 3, 5])                [3]

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) takes a list of numbers, nums, and outputs a list of numbers as shown below:

Input                             Output
function4([1, 3, -2, 5])            [1, 3]
function4([-8, 2, 4, -1])           [2, 4]
function4([0, 3, 5])                [3]
You have attempted of activities on this page