Lesson #16. Sequence

Дата изменения: 30 ноября 2021

Theory

Lection in pdf format

Standard sequence generators

    Range(a,b: integer): sequence of integer

    print(Range(1,10)); // [1,2,3,4,5,6,7,8,9,10]

    Range(a,b,step: integer): sequence of integer

    print(Range(1,10,2)); // [1,3,5,7,9]

    Range(c1,c2: char): sequence of char

    print(Range('a','k')); // [a,b,c,d,e,f,g,h,i,j,k]

    Partition(a,b: real; n: integer): sequence of real

    print(Partition(0.0, 6.0, 4)); // делим поровну на 4 части [0, 1.5, 3, 4.5, 6]

    SeqRandomInteger(n: integer[; a,b: integer]): sequence of integer;

    var q:=SeqRandomInteger(5,10,20);
    print(q); // [12,18,16,14,16]

    SeqRandomReal(n: integer[; a,b: real]): sequence of real;

    var q:=SeqRandomReal(3, 1.0, 5.0);
    print(q); // [4.98996168374548,2.22339218166815,2.81110574389394]

    Seq(params a: array of T): sequence of T;

    foreach var x in Seq (1,3,8) do
        print(x*x); // 1 9 64

    SeqFill(count: integer; x: T): sequence of T;

    begin
    var q:=SeqFill(7,5);
    print(q); // [5,5,5,5,5,5,5] 
    end.

Sequence generators with lambda-expressions

The following functions are used to generate sequences using lambda expressions:

  • SeqGen(count: integer; f: integer -> T): sequence of T;
  • begin
    var sq:=SeqGen(5,x->x+1);
    sq.println; // 1 2 3 4 5
    end.
  • SeqGen(count: integer; first: T; next: T -> T): sequence of T;
  • begin
    var sq:=SeqGen(5, 4, x->x+1);
    sq.println; // 4 5 6 7 8
    end.
  • SeqGen(count: integer; first,second: T; next: (T,T) -> T): sequence of T;
  • begin
    var sq:=SeqGen(5, 1,3, (x,y)->x+y);
    sq.println; // 1 3 4 7 11
    end.
  • SeqWhile(first: T; next: T -> T; pred: T -> boolean): sequence of T;
  • Condition is added:

    begin
    var sq:=seqWhile(2,x->x*2,x->x<=1024);
    sq.println; // 2 4 8 16 32 64 128 256 512 1024
    end.
  • SeqWhile(first,second: T; next: (T,T) -> T; pred: T -> boolean): sequence of T;
  • SeqGen(count: integer; f: integer -> T): sequence of T;

Infinite sequence generators

  • Cycle()
  • Repeating a sequence block

    Seq(1,2,10,5).Cycle().Take(15).Println; // 1 2 10 5 1 2 10 5 1 2 10 5 1 2 10

    Take is used to restrict

  • Repeat
  • Infinite sequence of numbers

    var q:=55.Repeat.Take(10).Println; // 55 55 55 55 55 55 55 55 55 55
  • Step
  • Generating an infinite sequence with a step

    var q:=5.Step(2).Take(10).Println; // 5 7 9 11 13 15 17 19 21 23
  • Iterate
  • Generating an infinite sequence using a lambda-function

    var q:=10.Iterate(x->x-2).Take(10).Println; // 10 8 6 4 2 0 -2 -4 -6 -8

Labs and tasks

Lab 1.

To do: Ask user to input 10 integer elements of the sequence. Calculate a sum of the seq elements.

The result examples:

1
2
3
4
1
6
7
2
5
6
sum = 37 

✍ Algorithm:

    begin
    var q:=ReadSeqReal(10); 
    var s:=0.0;
    foreach var x in q do
      s+=x;
    print('sum = ', s)
    end.
{0.3 points} Task 1:

To do: Create the function to calculate the product (multiplication) of 5 entered sequence elements.

Note: the signature of the function:

function Product(q: sequence of real):real;

The resulting example:

Please, enter 5 real numbers:
1.2
3.1
6.7
2.8
3.4
product:  237.27648 

[Program name: 16task-01.pas]

{0.2 points} Task 2:

To do: Create sequence of integers from -20 to 30 with a step of 3 (Range function).

The resulting example:

[-20,-17,-14,-11,-8,-5,-2,1,4,7,10,13,16,19,22,25,28] 

[Program name: 16task-02.pas]

{0.2 points} Task 3:

To do: Create N random numbers of a sequence from -50 to 100 (SeqRandom function).

The resulting example:

enter n 
>>15
[-23,13,-27,2,46,-26,10,92,60,-9,75,28,85,7,18] 

[Program name: 16task-03.pas]

Lab 2: Methods

To do: Create a function that searches in a sequence for the quantity of maximum elements.

The resulting example:

1 5 2 10 1 10
number of max elements is 2 

✍ Algorithm:

    function findCountMax(a: sequence  of integer):integer;
    begin
    var k:=a.Max();
      foreach var i in a do
        if i=k then result+=1;
    end;
    begin
      var c:=Seq(1,5,2,10,1,10);
      c.Println();
      println('number of max elements is ',findCountMax(c));
    end.
{0.4 points} Task 4:

To do: Create a function that calculates a sum of odd elements in a sequence. You should use IsOdd method within the foreach loop.

The resulting example:

1 5 2 10 1 10
sum of odd elements  7 

[Program name: 16task-04.pas]

Lab 3.

To do: Define a sequence using Seq method. Calculate a quantity of x (entered number) within the sequence. You should use count (condition) method.

The result examples:

-1 2 3 -5 2 -7 8 2 11
enter x: 2
there is 2 within the seq 3 times 

✍ Algorithm:

    begin
      var c:=Seq(-1,2,3,-5,2,-7,8,2,11);
      c.Println();
      var x:=readinteger('enter x:');
      var n:=c.count(c->c=x);
      println($'there is {x} within the seq {n} times ');
    end.
{0.3 points} Task 5:

To do: Define a sequence using Seq method. Calculate a number of negative elements in a sequence. You should use count (condition) method.

The resulting example:

-1 2 3 -5 6 -7 8 9 11
number of negative elements is 3

[Program name: 16task-05.pas]

Sequence generators with lambda-expressions

{0.2 points} Task 6:

To do: Using lambda expression, create a sequence of N even numbers starting at 10 (SeqGen should be used)

The resulting example:

Enter n 
>> 7
10 12 14 16 18 20 22

[Program name: 16task-06.pas]

{0.2 points} Task 7:

To do: Create the following sequence: 1 3 9 27 81 243 729 2187 6561 19683 (SeqGen should be used)

The resulting example:

3 9 27 81 243 729 2187 6561 19683

[Program name: 16task-07.pas]

{0.3 points} Task 8:

To do: Create a sequence of 15 numbers: 2 1 0.5 0.25 0.125 0.0625 0.03125 0.015625 ... (specifying the first element and the function to get the next one) (SeqGen should be used)

The resulting example:

2  1  0.5  0.25  0.125  0.0625  0.03125  0.015625 0.0078125 0.00390625 
0.001953125 0.0009765625 0.00048828125 0.000244140625 0.0001220703125 

[Program name: 16task-08.pas]

{0.2 points} Task 9:

To do: Create the following sequence: 2017 2012 2007 2002 1997 1992 1987 1982 1977 1972 (SeqGen should be used)

The resulting example:

2017 2012 2007 2002 1997 1992 1987 1982 1977 1972

[Program name: 16task-09.pas]

Lab:

To do: Create a sequence of N Fibonacci numbers

The resulting example:

Enter n 
>>8
1 1 2 3 5 8 13 21

✍ Algorithm:

    begin
    var n:=readInteger('Enter n');
    var sq:=SeqGen(n,1,1,(x,y)->x+y);
    sq.println();
    end.
Lab:

To do: Create a sequence of N numbers generated by the iterative process: а1=2, аk=(аk-1+1)*аk-1, k = 2,3,… (SeqGen should be used)

The resulting example:

Enter n 7
2 6 42 1806 3263442 -1461943274 -757910022

✍ Algorithm:

    begin
    var n:=readInteger('Enter n');
    var sq:=SeqGen(n,2,x->(x+1)*x);
    sq.println();
    end.
{0.4 points} Task 10:

To do: Create a sequence of N numbers generated by the iterative process: a1 = 5, ak = ak-1 / 2-1, k = 2,3, … (SeqGen should be used)

The resulting example:

Enter n 
>> 9
5  1.5  -0.25  -1.125  -1.5625 -1.78125 -1.890625 -1.9453125 -1.97265625

[Program name: 16task-10.pas]

{0.4 points} Task 11:

To do: Create a sequence of N numbers generated by the iterative process: а1=1, а2=2, аkk-1+2аk-2, k=3,… (SeqGen should be used)

The resulting example:

Enter n 
>>7
1 2 4 8 16 32 64

[Program name: 16task-11.pas]

{0.4 points} Task 12:

To do: Create a sequence of N numbers generated by the iterative process: а1=1, а2=2, аk=(аk-1k-2)/2, k=3,… (SeqGen should be used)

The resulting example:

Enter n 
>> 7
1 2 1.5 1.75 1.625 1.6875 1.65625

[Program name: 16task-12.pas]

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

*
*


Вставить формулу как
Блок
Строка
Дополнительные настройки
Цвет формулы
Цвет текста
#333333
Используйте LaTeX для набора формулы
Предпросмотр
\({}\)
Формула не набрана
Вставить