Informatics2-2018/Lab02
(Új oldal, tartalma: „previous up next = Exercises = == List first == Write a python function that retur…”) |
|||
18. sor: | 18. sor: | ||
== Color code == | == Color code == | ||
The colors on your screen are usually stored as a combination of three components (primary colors): red, green and blue. All components are between 0 and 1 (real numbers). | The colors on your screen are usually stored as a combination of three components (primary colors): red, green and blue. All components are between 0 and 1 (real numbers). | ||
− | This triplet of numbers is the RGB code | + | This triplet of numbers is the "RGB code". |
− | + | ||
− | + | Write a function that retrieves a given component from the RGB triplet. | |
− | + | The function should be named <code>color_code</code>, it should have two parameters: | |
− | + | * ''color'', a list of three numbers. | |
− | * '' | + | * ''component'', a string which is either "red", "green" or "blue" |
− | * '' | + | * The function should return the corresponding color intensity value. |
− | * | + | |
== Perfect numbers == | == Perfect numbers == | ||
32. sor: | 30. sor: | ||
Write a function which evaluates whether a number is [https://en.wikipedia.org/wiki/Perfect_number perfect]. | Write a function which evaluates whether a number is [https://en.wikipedia.org/wiki/Perfect_number perfect]. | ||
− | == | + | == Race numbers == |
− | + | We have a list of competitors in a competition and we wish to assign a unique race number to each of them. | |
+ | The numbers should start from 1 up to the number of competitors. | ||
− | + | Write a function that has one parameter: the list of competitors and returns a list of corresponding race numbers. | |
− | + | The function should be named <code>racenumbers</code>, with one parameter. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
== Separate == | == Separate == | ||
59. sor: | 45. sor: | ||
Then the result should be two lists. | Then the result should be two lists. | ||
− | == | + | == Argmax == |
− | + | Write a function that finds the largest value in a list and returns the index of that element. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | The function should be called <code>argmax</code>, should have one parameter: ''l'' the list of numbers. | |
− | + | ||
− | + | ||
− | + | ||
− | + | The function should return the index of the largest element. For example <code>[3, 2, 1]</code> should result 0. | |
− | + | == Multiply == | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | We have two parameters: a list of numbers ''l'' and an additional number ''k''. The output should be a list where every number is ''k'' times the corresponding number in ''l''. | ||
[[Informatics2-2018/Lab01|previous]] [[Informatics2-2018#Labs|up]] [[Informatics2-2018/Lab03|next]] | [[Informatics2-2018/Lab01|previous]] [[Informatics2-2018#Labs|up]] [[Informatics2-2018/Lab03|next]] |
A lap 2018. február 14., 19:08-kori változata
Tartalomjegyzék |
Exercises
List first
Write a python function that returns the first element of a list, if there is any, and None
is the list is empty.
The function should be named list_first
with one parameter:
l, the list.
the function should return the first element or a special None
value.
List end
Write a function that returns the last part of a list!
The function should be called list_end
, with two parameters: l the list and c the number of elements to keep (at the end).
If the list is shorter than c then return a None
value. Otherwise return the list of the last c elements.
Color code
The colors on your screen are usually stored as a combination of three components (primary colors): red, green and blue. All components are between 0 and 1 (real numbers). This triplet of numbers is the "RGB code".
Write a function that retrieves a given component from the RGB triplet.
The function should be named color_code
, it should have two parameters:
- color, a list of three numbers.
- component, a string which is either "red", "green" or "blue"
- The function should return the corresponding color intensity value.
Perfect numbers
Write a function which evaluates whether a number is perfect.
Race numbers
We have a list of competitors in a competition and we wish to assign a unique race number to each of them. The numbers should start from 1 up to the number of competitors.
Write a function that has one parameter: the list of competitors and returns a list of corresponding race numbers.
The function should be named racenumbers
, with one parameter.
Separate
Let L be a list of numbers, write a function that separates its elements into two lists: one for non-negative and one for positive elements. For example
L = [-1, 2, 5, -2, 3, -4, -5, 2, -2, 0, 5, 5, 6, 3, -3]
Then the result should be two lists.
Argmax
Write a function that finds the largest value in a list and returns the index of that element.
The function should be called argmax
, should have one parameter: l the list of numbers.
The function should return the index of the largest element. For example [3, 2, 1]
should result 0.
Multiply
We have two parameters: a list of numbers l and an additional number k. The output should be a list where every number is k times the corresponding number in l.