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
this term involves taking a list and producing a single value from the values in the original list
what does the term reduce/reduction mean in terms of lists
which python function does this describe
returns a number, the sum of all items in an iterable.
what does the following python function do
sum()
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
when would you apply the aggregate pattern
Iterable: Required. The sequence to sum
Start: Optional. A value that is added to the return value
the syntax for the python sum() function is as follows but what are each of the parameters for
sum(iterable, start)
when might you use this
len(object)
using python youd like to find the length of an object (list, string, etc) which built in function would you use
1 initialise the input with a non-empty list
2 set best_position to 0
3 for each position from 1 to length of list – 1:
3a if the item at position is better than the item at best_position:
3ai set best_position to position
- print best_position
what are the 6 steps of the find position of first best value pattern
when would you apply the find value pattern to an algorithm
This pattern can be applied when we wish to extract or search for a single value from a list (note the item returned will be the last item found that satisfied the condition)
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
what are the seven steps involved with the list transformation 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 are the 6 steps of the aggregate pattern
the syntax for the python sum() function is as follows but what are each of the parameters for
sum(iterable, start)
Iterable: Required. The sequence to sum
Start: Optional. A value that is added to the return value
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)
when would you apply the find the best value pattern to an algorithm
what are the 6 steps of the find position of first best value pattern
1 initialise the input with a non-empty list
2 set best_position to 0
3 for each position from 1 to length of list – 1:
3a if the item at position is better than the item at best_position:
3ai set best_position to position
- print best_position
this is expressed as None in python
(note the capital N)
how is the value NULL expressed in python
1 initialise the inputs
2 set value to the first value of the sequence
3 set sequence to the empty list
4 append value to the sequence
5 while the termination condition is not true:
5a set value to the next value of the sequence
5b append value to the sequence
- print the sequence
what are the 8 steps involved with the list generation 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
what are the 7 steps involved with the list filtering pattern
what are the 6 steps of the counting problem
1 initialise the input list
2 set a counter to zero
3 for each item in list:
3a if the item satisfies the condition:
3ai increment the counter, i.e. add 1 to it
4 print the counter
using python youd like to find the length of an object (list, string, etc) which built in function would you use
when might you use this
len(object)
This pattern can be used in cases where we wish to transform every element in a list into a new value
when would we apply the list transformation pattern
when might you apply the list generation pattern
This can be used to produce a sequence of numbers and store the sequence inside a list
This pattern can be applied when we wish to extract or search for a single value from a list (note the item returned will be the last item found that satisfied the condition)
when would you apply the find value pattern to an algorithm
when might you apply the list filtering pattern
This can be used to filter out particular values from a given list
when testing lists what are two considerations that must be taken into consideration when writing tests
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
1 initialise the input list
2 set a counter to zero
3 for each item in list:
3a if the item satisfies the condition:
3ai increment the counter, i.e. add 1 to it
4 print the counter
what are the 6 steps of the counting problem
This is similar to Pattern 2.8 find the best value however it has no redundant checks and instead returns the position/index of the best value
when would you apply the find position of first best value pattern to an algorithm
This can be used to filter out particular values from a given list
when might you apply the list filtering pattern
when might you use this
For element in list:
If element > 30:
Do something
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
what are the 6 steps of the find the best value pattern
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
when would you apply the find position of first best value pattern to an algorithm
This is similar to Pattern 2.8 find the best value however it has no redundant checks and instead returns the position/index of the best value
what does the term reduce/reduction mean in terms of lists
this term involves taking a list and producing a single value from the values in the original list
what are the 8 steps involved with the list generation pattern
1 initialise the inputs
2 set value to the first value of the sequence
3 set sequence to the empty list
4 append value to the sequence
5 while the termination condition is not true:
5a set value to the next value of the sequence
5b append value to the sequence
- print the sequence