Part 2, Patterns, algorithms and programs 2 Flashcards
This can be used to produce a sequence of numbers and store the sequence inside a list
when might you apply the list generation pattern
in python how would you append an item to an end of a list
to perform this follow the following syntax
List = []
List = list + [1]
1 initialise the input with a non-empty list
2 set best to the first item in the list
3 for each item in the list:
3a if the item is better than best:
3ai set best to the item
- print best
what are the 6 steps of the find the best value pattern
what are the seven steps involved with the list transformation pattern
initialise the input_list with the given values
2 initialise other inputs of the problem
3 initialise the output_list to the empty list
4 for each input_value of the input_list:
4a transform the input_value into an output_value
4b append the output_value to the output_list
- print the output_list
1 initialise the input list
2 set found to the null value
3 for each item in the list:
3a if the item satisfies the condition:
3ai set found to the item
4 print found
what are the 6 steps of the find value pattern
what do these two considerations refer to
- the min and max size of the list itself
- the min/max and boundary values of the elements inside the list
when testing lists what are two considerations that must be taken into consideration when writing tests
what are these a sub category of
- counting problems
- aggregation problems
- retrieval (or search) problems
name 3 types of reduction problems
what are the 6 steps of the find value pattern
1 initialise the input list
2 set found to the null value
3 for each item in the list:
3a if the item satisfies the condition:
3ai set found to the item
4 print found
how is the value NULL expressed in python
this is expressed as None in python
(note the capital N)
when would you apply the find the best value pattern to an algorithm
This pattern can be used to find the best value in the list such as the hottest or coldest day
(note it has 1 redundant check being the first item in the list and returns the first best item found)
what are the 7 steps involved with the list filtering pattern
1 initialise the input_list with the given values
2 initialise other inputs of the problem
3 initialise the output_list to the empty list
4 for each input_value of the input_list:
4a if the input_value satisfies the condition:
4ai append the input_value to the output_list
- print the output_list
This pattern can be applied in problems that involve counting how many items are in a list or counting how many of the items in the list satisfy a given condition
explain when the counting problem can be applied to an algorithm
to perform this follow the following syntax
List = []
List = list + [1]
in python how would you append an item to an end of a list
when would we apply the list transformation pattern
This pattern can be used in cases where we wish to transform every element in a list into a new value
in python how can you iterate over all values in a list while at the same time using the currently held value from the list from the iteration
when might you use this
For element in list:
If element > 30:
Do something
what are the 6 steps of the aggregate pattern
1 initialise the input list
2 initialise the aggregator with a suitable value
3 for each number in the list:
3a if the number satisfies the condition:
3ai update the aggregator according to the value of the number
4 print the aggregator
what does the following python function do
sum()
which python function does this describe
returns a number, the sum of all items in an iterable.
when would you apply the aggregate pattern
This pattern may be applied when we need to aggregate (something formed by adding together several amounts or things) all numbers in a list or when we want to only aggregate the numbers in a list that satisfy a certain condition
name 3 types of reduction problems
what are these a sub category of
- counting problems
- aggregation problems
- retrieval (or search) problems
explain when the counting problem can be applied to an algorithm
This pattern can be applied in problems that involve counting how many items are in a list or counting how many of the items in the list satisfy a given condition