# SPL Programming Exercise - Chapter 5 Sequence as whole

## 5.1 Set operations

1. Read code and write result

A | B | |
---|---|---|

1 | =to(10,1) | |

2 | =A1.to(2,6) | =A1.to(9,3) |

3 | =A2|B2 | =B2|A2 |

4 | =A2&B2 | =B2&A2 |

5 | =A2^B2 | =B2^A2 |

6 | =A2\B2 | =B2\A2 |

7 | =[A2,B2,A3] | =[A3,A4,B2] |

8 | =A7|A5 | =A7|[A5] |

9 | =A8(2|to(3,5)) | =A7(1)([1,3,5]) |

10 | =A7&B7 | =A10([2,3])(1)([6,3,2]) |

11 | =A7^B7 | |

12 | =A7\B7 |

2. Select members greater than or equal to 10 from the sequence [2,0,6,1,77,0,52,25,7] and place them in the new sequence.

3. Program to find all perfect numbers within 1000 and place the results in a sequence. (Using set operations)

4. Define a sequence that contains multiple numbers. Implement set operations to place odd numbers on the left side of the sequence and even numbers on the right side of the sequence. (You can create other sequences without changing them in the original sequence).

5. Any input of 8 integers is stored in a sequence, and then store odd and even numbers in two different sequences, and all data in these two sequences is output in alternating order of odd and even numbers (alternate output first, if there are many odd numbers, then output the remaining odd numbers, and if there are many even numbers, then output the remaining even numbers).

6. Sequence deduplication.

Requirement:

1). Enter a sequence of integers with a length of 10 and store 10 integers, some of which are duplicate data

2). Remove duplicates from the sequence, store a new sequence, and do not change the order of the original numbers in the sequence

7. Generate 10 random numbers between 1 and 20 and place them in the sequence, ensuring that the random numbers cannot be repeated.

## 5.2 Loop functions

1. (1) Use a loop function to generate a sequence A1 with a length of 10 and sequence values of random numbers

(2) Subtract 5 from each member value of A1 to obtain sequence A2

(3) Calculate the sum of squares of A2 member values

2. Calculate the sum of 2+4+6+…+100

3. Calculate the factorial of n, the first 20 items

4. Calculate the sum of the first 100 items of the series: S=1+1/3+1/5+1/7+…

5. Program to calculate expression , n takes a value of 100

6. 1/(1x(1+1))+1/(2x(2+1))+1/(3x(3+1))+1/(4x(4+1))+1/(5x(5+1))……………1/(n(n+1))，calculate the sum of the first 100 items.

7. Dayan sequence 0, 2, 4, 8, 12, 18, 24, 32, 40, 50– – –

General term formula:

an=(n*n-1)/2 (n is odd)

an=n*n/2 (n is even)

Find the first 100 items.

8. 1/2-2/3+3/4-…+(-1)^{n-1}n/n+1, calculate the sum of the first 20 terms.

9. ,take the first 10 items to obtain an approximate value of ln2.

10. Use the power series expansion formular to obtain an approximate value of , taking the first 5 terms.

## 5.3 Loop functions: advanced

1. Sequence [2,5,-3,8,9,7], calculate absolute values for odd numbered terms and square even numbered terms

2. Add 5 to odd numbers in sequence [1,2,1,5,3,4,8], multiply even numbers by 2, and then calculate the mean.

3. Subtract two sequences of the same length [2,5,-3,8,9,7], [1,2,3,4,5,6] by pairing them.

4. Use loop function to reverse the sequence, such as [2,5,-3,8,9,7], to [7,9,8,-3,5,2]

5. Use loop function to generate the sequence [[1],[2,2],[3,3,3],[4,4,4,4],[5,5,5,5,5]]

6. A two-layer sequence with 3 rows and 4 columns, where the value of the sequence element is the sum of the square of its corresponding row number and the corresponding column number. Find the sum of all its elements.

7. 20 day price data of a certain stock [15.8,15.92,16.05,16.4,16.28,16.39,15.75,15.38,15.21,14.68,14.85,14.98,15.2,15,15.18,14.59,14.7,14.45,14.31,13.68]

(1) Find the maximum daily growth

(2) Find the 5-day moving average MA 5, where MA 5=(price of four days ago + price of three days ago + price of two days ago + price of yesterday + price of the current day)/5

8. Calculate the daily cumulative rise and fall of a certain stock based on its 20 day rise and fall data [-0.7538,-0.81,-2.1341,0.7371,-0.6711,4.0635,2.4057,1.1177,3.6104,-1.1448,-0.8678,-1.4474,1.3333,-1.1858,4.0439,-0.7483,1.7301,0.9783,4.6053,-5.5901].

9. A two-layer sequence with 4 rows and 4 columns, calculate the sum of the main diagonals of the sequence using loop function.

10. Enter six random numbers into the two-layer sequence *a* of 2 rows and 3 columns, transpose the sequence members in the two-layer sequence *a*, that is, swap rows and columns, and store them in the two-layer sequence *b* of 3 rows and 2 columns. (Implemented using loop function)

## 5.4 Iterative function^{*}

Read code and write result

A | |
---|---|

1 | =[2,222,22,122,2222] |

2 | =A1.iterate(~*2) |

3 | =A1.iterate@a(~*2) |

4 | =A1.iterate(~~*2,3) |

5 | =A1.iterate(~~*2,5,~>500) |

## 5.5 Positioning and selection

1. Sequence [2,0,6,1,77,0,52,1,25,7]

(1) Select the second to last member

(2) Select the 2nd, 3rd, 6th, and -3rd members

(3) Starting from the first member, get one member every three members

(4) Select members greater than or equal to 10

(5) Select non zero members

(6) Select odd numbered members

(7) Find all the positions where 0 appears

(8) Find all odd member positions

(9) Find the sine of the members in the sequence, the maximum value of the sine, the position of the maximum value, and the corresponding original sequence members

2. Calculate the sum of all odd and even numbers in 1+2+3+…+100. Using the step() function

3. The relationship between the sales quantity and price of a certain product is shown in the figure. If there are order quantities [221,87,33,73,162,227,403,288,78,213,374,152], find the corresponding price.

4. Use the selection function to select the number between 1 and 100 where the product of each digit is greater than the sum of each digit.

5. [95,92,83,89,76,98,58,77,85,90] Identify the student numbers with scores greater than the average of ten students.

6. Locate the corresponding positions of members [227,33,288] from the sequence [221,87,33,73,162,227,403,288,78,213,374,152]

7. Swap the positions of the maximum and minimum values, for example [2,1,3,15,7,9], after swapping [2,15,3,1,7,9]

8. Define a sequence that contains multiple numbers, implementing odd numbers placed on the left side of the sequence and even numbers placed on the right side of the sequence.

9. Given an integer sequence *nums* and an integer target value *target*, please find the two integers in the sequence where the sum of the two are the target value, and output their positions.

Assuming that each input will only correspond to one answer. However, the same element in the sequence cannot appear repeatedly in the answer.

You can return the answers in any order.

Example 1:

Input: nums=[2,7,11,15], target=9

Output: [1,2]

Explanation: Because 2+7=9, output the positions of 2 and 7 [1,2].

Example 2:

Input: nums=[3,2,4], target=6

Output: [2,3]

## 5.6 Sorting related

1. The height and weight data of a group of students are as follows

Height: [1.21,1.35,1.32,1.18,1.46,1.25,1.33,1.37,1.29,1.40]

Weight: [24,31,35,28,33,36,37,42,29,32]

(1) Sort students in height from high to low

(2) Return student sequence numbers in descending order of height

(3) Get the top 3 heights

(4) Get the top 3 BMI values

(5) Get the sequence numbers of the top 3 BMI students

(6) Get the height data of the top 3 students with BMI values

2. Given a sequence, return the maximum difference between adjacent elements after sorting. If the number of sequence elements is less than 2, return 0.

Example 1:

Input: nums=[3,6,9,1]

Output: 3

Explanation: The sorted sequence is [1,3,6,9], where there is a maximum difference of 3 between adjacent elements (3,6) and (6,9).

Example 2:

Input: num=[10]

Output: 0

Explanation: If the number of sequence elements is less than 2, it returns 0.

3. Given a sequence of integers *nums* and integer *k*, please return the k-th largest element in the sequence. Please note that you need to find the k-th largest element after sorting the sequence, not the k-th different element.

Example 1:

Input: [3,2,1,5,6,4], k=2

Output: 5

Example 2:

Input: [3,2,3,1,2,4,5,5,6], k=4

Output: 4

4. Given a sequence *nums* containing n numbers from [0,n], find the number that does not appear in the sequence within the range of [0, n].

Example 1:

Input: nums=[3,0,1]

Output: 2

Explanation: n=3, because there are 3 numbers, all numbers are within the range of [0,3]. 2 is the missing number because it does not appear in nums.

Example 2:

Input: num=[0,1]

Output: 2

Explanation: n=2, because there are 2 numbers, all numbers are within the range of [0,2]. 2 is the missing number because it does not appear in nums.

Example 3:

Input: nums=[9,6,4,2,3,5,7,0,1]

Output: 8

Explanation: n=9, because there are 9 numbers, all numbers are within the range of [0,9]. 8 is the missing number because it does not appear in nums.

Example 4:

Input: nums=[0]

Output: 1

Explanation: n=1, because there is one number, all numbers are within the range of [0,1]. 1 is the missing number because it does not appear in nums.

## Suggested answers:

**5.1 Set operations**

2.

A | B | C | |
---|---|---|---|

1 | =[2,0,6,1,77,0,52,1,25,7] | ||

2 | for A1 | if A2>=10 | >B1=B1|A2 |

3.

A | B | C | D | |
---|---|---|---|---|

1 | 0 | =[] | ||

2 | for 1000 | |||

3 | for A2-1 | if A2%B3==0 | >A1+=B3 | |

4 | if A1==A2 | >B1=B1|A2 | ||

5 | >A1=0 |

4.

A | B | C | |
---|---|---|---|

1 | [1,2,3,4,5,6,7,8,9,10] | ||

2 | for A1 | if A2%2!=0 | >B1=A2|B1 |

3 | else | >B1=B1|A2 |

5.

A | B | C | D | |
---|---|---|---|---|

1 | =[46,68,99,28,50,41,76,39] | |||

2 | ||||

3 | for A1 | if A3%2==0 | >C1|=A3 | >C2+=1 |

4 | else | >B1|=A3 | >B2+=1 | |

5 | if B2<C2 | for B2 | >output(B1(B5),C1(B5)) | |

6 | for B2+1,C2 | >output(C1(B6)) | ||

7 | elseif B2==C2 | for B2 | >output(B1(B7),C1(B7)) | |

8 | else | for C2 | >output(B1(B8),C1(B8)) | |

9 | for C2+1,B2 | >output(B1(B9)) |

6.

A | B | C | |
---|---|---|---|

1 | =[46,68,99,28,50,41,76,39,99,50] | =[] | |

2 | for A1 | if !B1.contain(A2) | >B1=B1|A2 |

7.

A | B | C | D | |
---|---|---|---|---|

1 | =[] | |||

2 | for 10 | =rand(20)+1 | if !A1.contain(B2) | >A1|=B2 |

3 | else | goto B2 |

**5.2 Loop functions**

1.

A | |
---|---|

1 | =10.(rand(100)) |

2 | =A1.(~-5) |

3 | =A2.sum(_{*}) |

2.

A | |
---|---|

1 | =50.sum(2*~) |

3.

A | |
---|---|

1 | =(nf=1,20.((nf*=~,nf))) |

4.

A | |
---|---|

1 | =100.sum(1/(2*~-1)) |

5.

A | |
---|---|

1 | =(n=100,n.sum(~*~)/n) |

6.

A | |
---|---|

1 | =100.sum(1/(~*(~+1))) |

7.

A | |
---|---|

1 | =100.(if(~%2==0,~*~\2,(~*~-1)\2)) |

8.

A | |
---|---|

1 | =20.sum(if(~%2==0,-~/(~+1),~/(~+1))) |

9.

A | |
---|---|

1 | =10.sum(2/(2*~-1)/power(3,2*~-1)) |

10.

A | B | |
---|---|---|

1 | =pi(9/180) | >nf=1 |

2 | =A1+to(2,5).sum(power(-1,~-1)*power(A1,2*~-1)/(nf*=(2*~-1)*(2*~-2),nf)) |

**5.3 Loop functions: advanced**

1.

A | |
---|---|

1 | =[2,5,-3,8,9,7] |

2 | =A1.(if(#%2==1,abs(~),~*~)) |

2.

A | |
---|---|

1 | =[1,2,1,5,3,4,8] |

2 | =A1.avg(if(~%2==1,~+5,2*~)) |

3.

A | B | |
---|---|---|

1 | =[2,5,-3,8,9,7] | =[1,2,3,4,5,6] |

2 | =A1.(~-B1(#)) |

4.

A | B | |
---|---|---|

1 | =[2,5,-3,8,9,7] | =A1.len() |

2 | =A1.(A1(B1+1-#)) |

5.

A | |
---|---|

1 | =to(5) |

2 | =A1.(~.(A1.~)) |

6.

A | |
---|---|

1 | =to(3) |

2 | =A1.sum(4.sum(A1.#*A1.#+#)) |

7.

A | |
---|---|

1 | =[15.8,15.92,16.05,16.4,16.28,16.39,15.75,15.38,15.21,14.68,14.85,14.98,15.2,15,15.18,14.59,14.7,14.45,14.31,13.68] |

2 | =A1.(if(#>1,~-~[-1],0)) |

3 | =A1.(avg(~[-4:0])) |

8.

A | |
---|---|

1 | =[-0.7538,-0.81,-2.1341,0.7371,-0.6711,4.0635,2.4057,1.1177,3.6104,-1.1448,-0.8678,-1.4474,1.3333,-1.1858,4.0439,-0.7483,1.7301,0.9783,4.6053,-5.5901] |

2 | =A1.run(~=~+~[-1]) |

9.

A | |
---|---|

1 | =[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] |

2 | =A1.sum(~(#)) |

10.

A | B | |
---|---|---|

1 | =2.(3.(rand(100))) | =3.(2.(0)) |

2 | =B1.(~.(A1(#)(B1.#))) |

**5.5 Positioning and selection**

1.

A | |
---|---|

1 | =[2,0,6,1,77,0,52,1,25,7] |

2 | =A1.m(-2) |

3 | =A1.m([2,3,6,-3]) |

4 | =A1.step(3,1) |

5 | =A1.select(~>=10) |

6 | =A1.select(~!=0) |

7 | =A1.select(#%2!=0) |

8 | =A1.pos@a(0) |

9 | =A1.pselect@a(~%2!=0) |

10 | =A1.max(sin(~)) |

11 | =A1.pmax(sin(~)) |

12 | =A1.maxp(sin(~)) |

2.

A | |
---|---|

1 | =to(100) |

2 | =A1.step(2,1).sum() |

3 | =A1.step(2,2).sum() |

3.

A | |
---|---|

1 | =[221,87,33,73,162,227,403,288,78,213,374,152] |

2 | =[15,13.75,13,12.5] |

3 | =A1.([30,50,100,300,500].pseg(~)) |

4 | =A2.m(A3) |

4.

A | |
---|---|

1 | =to(100) |

2 | =A1.select((a=~\10,b=~%10,a*b>a+b)) |

5.

A | |
---|---|

1 | =[95,92,83,89,76,98,58,77,85,90] |

2 | =A1.pselect@a((avg=A1.avg(),~>avg)) |

6.

A | |
---|---|

1 | =[221,87,33,73,162,227,403,288,78,213,374,152] |

2 | =[227,33,288] |

3 | =A2.(A1.pos(~)) |

7.

A | |
---|---|

1 | =[2,1,3,15,7,9] |

2 | =A1.pmax() |

3 | =A1.pmin() |

4 | >(a=A1(A2),A1(A2)=A1(A3),A1(A3)=a) |

8.

A | |
---|---|

1 | =[1,2,3,4,5,6,7,8,9,10] |

2 | =A1.select(~%2!=0)|A1.select(~%2==0) |

9.

A | B | |
---|---|---|

1 | =[2,7,11,15] | 9 |

2 | =A1.(B1-~).select(~+~!=B1) | |

3 | =A2.(A1.pos(~)).select(~>0) |

**5.6 Sorting related**

1.

A | B | |
---|---|---|

1 | =[1.21,1.35,1.32,1.18,1.46,1.25,1.33,1.37,1.29,1.40] | =[24,31,35,28,33,36,37,42,29,32] |

2 | =A1.sort@z() | |

3 | =A1.psort@z() | |

4 | =A1.top(-3) | |

5 | =A1.top(-3,B1(#)/~/~) | |

6 | =A1.ptop(-3,B1(#)/~/~) | |

7 | =A1.top(3;-B1(#)/~/~) |

2.

A | |
---|---|

1 | =[3,6,9,1] |

2 | =A1.sort() |

3 | =A2.max(if(#>1,~-~[-1],0)) |

3.

A | B | |
---|---|---|

1 | =[3,2,1,5,6,4] | >k=2 |

2 | =A1.sort@z()(k) |

4.

A | B | |
---|---|---|

1 | =[9,6,4,2,3,5,7,0,1] | |

2 | =A1.sort() | |

3 | if A2(1)!=0 | >B1=0 |

4 | else if A2.m(-1)!=A1.len() | >B1=A1.len() |

5 | else | >B1=A2.select(~-~[-1]>1)(1)-1 |

*SPL Official Website**👉* *https://www.scudata.com*

*SPL Feedback and Help**👉* *https://www.reddit.com/r/esProc_SPL*

*SPL Learning Material**👉* *https://c.scudata.com*

*SPL Source Code and Package**👉* *https://github.com/SPLWare/esProc*

*Discord**👉* *https://discord.gg/cFTcUNs7*

*Youtube**👉* *https://www.youtube.com/@esProc_SPL*

Chinese version