Informatics2-2020/Lab02

A MathWikiből

previous up next

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 positive and one for non-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.

previous up next

Személyes eszközök