Lab 06: HOFs and Functions as Data
Lab 6: HOFs and Functions as Data
Instructions
This worksheet serves as a guide and set of instructions to complete Lab 6. All material was sourced from the CS10 version of The Beauty and Joy of Computing (BJC) course.
- You must use the starter file, found here to get credit for the lab.
- Additionally, here is the workbook that you can read through for further context and additional (non-required) material.
- Please note: The file name does not equal the lab number!
Submitting
You will need to fill in the blocks under “Lab 9 Starter” and submit this to Gradescope (again, the file name does not equal the lab number!).
- To receive full credit, you must complete the required blocks, and those blocks must pass all tests from the Gradescope autograder.
- For instructions on how to submit labs to Gradescope, please see this page.
Please note: you must use the starter file above, and you must not edit the name of any required blocks. Failing to do either will result in autograder failure.
Objectives
Throughout the course, you’ve been exposed to HOF blocks like map
, keep
, and combine
. You may have used them to carry out relatively simple tasks, like summing a list with combine
, or filtering based on word length with keep
. In this lab, you’re going to attack some more complicated problems with the help of HOFs.
By the end of the lab, you will be comfortable implementing and understanding:
- Build non-HOF blocks to use as helper functions in larger HOF problems
- Complete complex, multi-step HOFs problems
- Use HOFs to solve real-life problems, like minimizing or composing functions
Required Blocks
- Block 1: is (num) a factorion?
- Block 2: list all factorions between (num1) and (num2)
- Block 3: is (num) pandigital?
- Block 4: list all pandigital numbers between (num1) and (num2)
Important Topics (Workbook)
For better understanding of the lab we highly recommend going through these workbook pages!
Topics that are important but not required for this lab will be indicated with an asterisk (**). These topics are best reviewed in order and as you complete the lab.
- HOFs Practice and Treating Functions as Data
- Factorions
- Functions as Data
- Pandigital
- Generalizing “List all” **
- Extra for Experience: Composing Functions **
Block 1: is (num) a factorion?
Objective
- Create a predicate block that, when input with a number, reports if that number is a factorion (True/False).
- A number is called a factorion if the number is equal to the sum of the factorials of its digits.
- Example: 145 is a factorion since 145 = 1! + 4! + 5!.
- Please use HOFs in your solution.
Hint: Filling out and using the factorial (num)
block may be extremely helpful.
Inputs
num
: any number
Output
- Reports a boolean (
true
if factorion, otherwisefalse
)
Examples
Block 2: list all factorions between (num1) and (num2)
Objective
- Create a block that takes two numbers and returns all numbers (including num1 and num2) that are factorions in a list.
Inputs
num1
,num2
: any numbers
Output
- Reports a list containing all factorions between (and including) num1 and num2
Examples
Block 3: is (num) pandigital?
Objective
- Create a predicate block that reports if the input number is pandigital.
- A number is called pandigital if it uses all of the digits from 1 to n exactly once, where n is the number of digits in the number.
- Example: 15432 is pandigital, but 11132 is not.
Inputs
num
: any number
Output
- Reports a boolean
Examples
Block 4: list all pandigital numbers between (num1) and (num2)
Objective
- Create a block that takes two numbers and lists all numbers (including num1 and num2) that are pandigital in a list.
Inputs
num1
,num2
: any numbers
Output
- Reports a list containing all pandigital numbers between (and including) num1 and num2
Examples
Remember to submit on Gradescope and complete the conceptual portion!