# SPL Programming Exercise - Chapter 4 Sequence

## 4.1 Sequence

1. Write the value of the cells

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

1 | =[7,9,6,23,56,1+20] | [7,9,6,23,56,1+20] |

2 | =A1(6) | =B1(6) |

3 | =A1.to(2) | =A1(A1(3)) |

4 | =A1.to(3,5) | =A1(4)+A1(5) |

5 | =A1.to(3,) | >A1(6)=1 |

6 | =A1(6) | =A1.m(-2) |

7 | =A1.m([-2,-4]) | =A1([2,4]) |

8 | =A1.m(-2:-4) |

2. Sequence operation exercise

(1) Generate a sequence of 10 in length, with sequence members taking random integers up to 100

(2) Extract the 1st, 3rd, and 5th members

(3) Extract the last three members

(4) Modify the values of the 5th, 6th, and 7th members to 1, 2, and 3

(5) Add one new member at the end, with a value of twice the value of the third member

(6) Delete the 4th and 6th members

(7) Invert sequence members, such as [1,2,3,4] to [4,3,2,1] after inversion

3. Print each member of the sequence [1,2,3,4,5,6,7,8,9,10]

4. Program to implement the function A.rvs()to invert the members of a sequence [1,2,3,4,5,6,7,8,9,10]

5. Swap the content in sequence [1,3,5,7,9] with the content in sequence [2,4,6,8,10].

6. Find the maximum value and position of the sequence

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. Enter the scores of 10 students in the sequence, such as [95,92,83,89,76,98,58,77,85,90], and output the total and average scores

9. Enter the scores of 10 students, store the scores of students above the average in a new sequence, and output them

10. Find the numbers between 1 and 100 where the product of each digit is greater than the sum of each digit, and write the result into the sequence

## 4.2 Loop of sequence

1. Enter an ordered sequence of length 9, such as [1,2,3,4,6,7,8,9,10], and then enter a number x to insert x into the sequence, from smallest to largest.

2. Find a specific number n in a sequence. If n exists in the sequence, output its position. If n does not exist, output null

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

4. Remove the zeros from the sequence [2,0,6,1,77,0,52,0,25,7] to form a sequence that does not contain zeros

5. Please write code to randomly generate six integers between 0 and 100 and store them in the sequence, calculate the sum of the members in the sequence, output the sequence and the sum value

6.

(1) Define a sequence that contains multiple numbers. Implement it in your own way, placing 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)

(2) Request to change in the original sequence to implement odd numbers on the left and even numbers on the right

7. In a sequence of length N, there is a value between 1 to N-1 that repeats twice, identify the duplicate value

For example, find duplicate values 5 from [4,3,5,8,5,2,1,9,6,7]

8. 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 is equal to *target*, and output their positions.

Assuming that each input will only correspond to one answer. However, the same member 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]

9. Give you an integer sequence *nums* to determine if there is a triplet [nums(i),nums(j),nums(k)] that satisfies i!=j, i!=k and j != k. At the same time, it also satisfies nums(i)+nums(j)+nums(k)==0. Please return all triples with a sum of 0 and no duplicates.

Note: The answer cannot contain duplicate triples.

Example 1:

Input: nums=[-1,0,1,2,-1,-4]

Output: [-1,-1,2] and [-1,0,1]

Explanation:

```
Nums(0)+nums(1)+nums(2)=(-1)+0+1=0.
Nums(1)+nums(2)+nums(4)=0+1+(-1)=0.
Nums(0)+nums(3)+nums(4)=(-1)+2+(-1)=0.
```

The different triples are [-1,0,1] and [-1,-1,2].

Note that the order of output and the order of triples are not important.

Example 2:

Input: nums=[0,1,1]

Output: Null

Explanation: The only possible triplet sum is not 0

Example 3:

Input: nums=[0,0,0]

Output: [0,0,0]

Explanation: The only possible triplet sum is 0

Tip:

```
3<=nums.length<=3000
-10^5<=nums[i]<=10^5
```

## 4.3 Multi-layer sequence

1. Write code results

A | |
---|---|

1 | =[[11,12],[21,22,23],[31,32,33,34]] |

2 | =A1(2) |

3 | =A1(3)(4) |

4 | >A1(1)(2)=0 |

5 | =A1.len() |

6 | =A1(3).len() |

7 | >A1(2)=0 |

2. Please use a two-layer sequence to store the following data and traverse to display.

```
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
```

3. Write the calculation results of the 9*9 multiplication table into a two-layer sequence by row, [[1],[2,4],[3,6,9],……,[9,18,27,36,45,54,63,72,81]]

```
1*1=1
1*2=2，2*2=4
1*3=3，2*3=6，3*3=9
……
1*9=9，2*9=18，3*9=27，4*9=36，5*9=45，6*9=54，7*9=63，8*9=72，9*9=81
```

4. Find the sum of annual sales of the company

The data of a certain company arranged by quarter and month is as follows: Unit (10k dollar)

Q1: 22,66,44

Q2: 77,33,88

Q3: 25,45,65

Q4: 11,66,99

5. A two-layer sequence with 3 rows and 4 columns, where the value of the sequence member is the sum of the square of its corresponding row number and the corresponding column number. Output the two-layer sequence and sum all members of the sequence.

6. A two-layer sequence with 4 rows and 4 columns, find the sum of the main diagonals of the sequence.

7. Use a random function to initialize a two-layer sequence of 4 rows and 5 columns, and find the minimum value in the two-layer sequence and the position where the minimum value first appears.

8. Enter six random numbers into a 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, store them in the two-layer sequence *b* of 3 rows and 2 columns, and output the members in the two-layer sequence* b*.

9. Calculate the sum of the lower triangular members of a square matrix, such as [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

10. A two-layer sequence with a size of 10*10, randomly place 10 landmines, and display how many landmines are around the location where there are no landmines.

Tip: Use 0~8 to represent the number of landmines, and 9 to represent landmines

It must be 10 landmines.

A.contain (xi,…) to determine if the data is a sequence member.

## 4.4 Understanding objects

1. Read the code and write the results

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

1 | =5 | =A1 |

2 | >B1=6 | |

3 | =[5] | =A3 |

4 | >B3=[7,8] | |

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

6 | >B5(2)=99 | |

7 | =[1]*6 | |

8 | >A7(3)=59 | |

9 | =[[1]*3]*5 | |

10 | >A9(1)(2)=33 |

2. Sequence Function Exercises

(1) Generate a sequence *A1* consisting of continuous natural numbers from 10 to 1

(2) Extract its 1st, 3rd, 5th, and 10th members to form a new sequence *A2*

(3) Extract odd numbered members from *A1* to form a new sequence *A3*

(4) Get the third to last member in *A1*

(5) Assign values to the 6th, 7th, and 8th members in sequence *A1*, with values of 99, 88, and 77 respectively

(6) Insert 20 and 30 before the third member in *A1*

(7) Add member 65 at the end of *A1*

(8) Delete the 2nd, 4th, and 5th members from *A1*

(9) Invert the members in *A1* to generate a new sequence *A9*

3. Find prime numbers within 1000 and store them in the sequence. (Written using the insert function)

4. Find the factors of each number between 2 and 50, and store each number and its factors in the two-layer sequence. Shaped as [[2,1,2],[3,1,3],[4,1,2,4],……], the first position in each subsequence is a number between 2 and 50, and the other values are its factors.

5. Program to find all perfect numbers within 1000 and place the results in a sequence. (Written using the insert function)

## Suggested answers:

**4.1 Sequence**

2.

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

1 | =[0]*10 | ||

2 | for 10 | =rand(100) | >A1(A2)=B2 |

3 | =A1([1,3,5]) | ||

4 | =A1.m(-1:-3) | ||

5 | =A1.modify(5,[1,2,3]) | ||

6 | =A1.insert(0,A1(3)*2) | ||

7 | =A1.delete([4,6]) | ||

8 | =A1.rvs() |

3.

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

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

2 | for A1.len() | >output(A1(A2)) |

4.

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

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

2 | for B1\2 | =A1(A2) |

3 | >A1(A2)=A1(B1+1-A2) | |

4 | >A1(B1+1-A2)=B2 | |

5 | >output(A1) |

5.

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

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

2 | for A1.len() | =A1(A2) |

3 | >A1(A2)=B1(A2) | |

4 | >B1(A2)=B2 |

6.

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

1 | =[2,1,3,15,7,9] | =A1(1) | 1 |

2 | for 2,A1.len() | if A1(A2)>B1 | >B1=A1(A2) |

3 | >C1=A2 |

B1 Max value

C1 Position of the max value

7.

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

1 | =[2,1,3,15,7,9] | =A1(1) | 1 |

2 | =A1(1) | 1 | |

3 | for 2,A1.len() | if A1(A3)>B1 | >B1=A1(A3) |

4 | >C1=A3 | ||

5 | if A1(A3)<B2 | >B2=A1(A3) | |

6 | >C2=A3 | ||

7 | >A1(C1)=B2, A1(C2)=B1 |

8.

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

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

2 | for 10 | >B1=B1+A1(A2) |

3 | >output(B1,B1/10) |

9.

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

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

2 | for 10 | >B1=B1+A1(A2) | |

3 | for 10 | if A1(A3)>B1/10 | =C1.insert(0,A1(A3)) |

4 | >output(C1) |

10.

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

1 | =[] | ||

2 | for 100 | =A2%10 | |

3 | =A2\10 | ||

4 | if B2*B3>B2+B3 | =A1.insert(0,A2) |

**4.2 Loop of sequence**

1.

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

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

2 | 5 | ||||

3 | if A1(9)<=A2 | >A1.insert(0,A2) | |||

4 | else | for A1 | if B4>A2 | >A1.insert(#B4,A2) | break |

2.

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

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

2 | 3 | |||

3 | for A1 | if A2==A3 | >output(#A3) | break |

4 | if !A3 | >output(null) |

3.

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

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

2 | for A1 | if A2>=10 | >B1.insert(0,A2) |

4.

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

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

2 | for A1 | if A2==0 | >A1.delete(#A2) |

5.

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

1 | =[0]*6 | 0 | ||

2 | for A1 | =rand(100) | >A1(#A2)=B2 | >B1+=B2 |

3 | >output(A1,B1) |

6.

（1）

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

1 | [1,2,3,4,5,6,7,8,9,10] | =[0]*A1.len() | 1 | =A1.len() |

2 | for A1 | if A2%2!=0 | >B1(C1)=A2 | >C1+=1 |

3 | else | >B1(D1)=A2 | >D1-=1 |

（2）

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

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

2 | for B1<C1 | if A1(B1)%2==0 | |||

3 | for B1<C1 | if A1(C1)%2!=0 | =A1(B1) | ||

4 | >A1(B1)=A1(C1) | ||||

5 | >A1(C1)=E3 | ||||

6 | break | ||||

7 | >C1-=1 | ||||

8 | >B1+=1 |

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

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

2 | for A1 | for #A2+1,A1.len() | if A2==A1(B2) | >output(A2) | break |

3 | if C2 | break |

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

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

2 | for A1 | for #A2+1,A1.len() | if A2+A1(B2)==B1 | >output([#A2,B2]) | break |

3 | if C2 | break |

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

1 | [-1,0,1,2,-1,-4] | ||||

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

3 | for A2 | if A3>0 | break | ||

4 | if #A3>1 && A3==A2(#A3-1) | next | |||

5 | =#A3+1 | ||||

6 | =A1.len() | for B5<B6 | =A3+A2(B5)+A2(B6) | ||

7 | for B5<B6 && A2(B5)==A2(B5+1) | >B5+=1 | |||

8 | for B5<B6 && A2(B6)==A2(B6-1) | >B6-=1 | |||

9 | if D6==0 | >output([A3,A2(B5),A2(B6)]) | |||

10 | >B6-=1 | ||||

11 | >B5+=1 | ||||

12 | elseif D6>0 | >B6-=1 | |||

13 | elseif D6<0 | >B5+=1 |

Approach:

1. A.sort() sorting

2. Define 3 values, current value, right adjacent value, maximum value, and add the 3 values together

3. Determine if the sum of additions is 0, shift the right adjacent value to the right, and shift the maximum value to the left (skip if they are the same)

sum>0, maximum value shift to the left

sum<0, right adjacent value shift to the right

If the position of the right adjacent value is greater than or equal to the position of the maximum value, stop shifting and proceed to the next current value.

**4.3 Multi-layer sequence**

2.

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

1 | =[0]*5 | |||

2 | for 5 | >A1(A2)=[A2]*A2 | for A1(A2) | >output@s(C2) |

3 | >output("") |

3.

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

1 | =[0]*9 | |||

2 | for 9 | >A1(A2)=[0]A2for A2>A1(A2)(C2)=C2A2 |

4.

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

1 | =[[22,66,44],[77,33,88],[25,45,65],[11,66,99]] | ||

2 | for A1 | for A2 | >B1=B1+B2 |

5.

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

1 | =[0]*3 | |||

2 | for 3 | >A1(A2)=[0]4for 4=A2A2+C2 |
||

3 | >A1(A2)(C2)=D2 | |||

4 | >B1+=D2 | |||

5 | >output(A1,B1) |

6.

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

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

2 | for 4 | >B1+=A1(A2)(A2) |

7.

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

1 | =[0]*4 | =999 | =0 | =0 | |

2 | for 4 | >A1(A2)=[0]*5 | for 5 | =rand() | |

3 | >A1(A2)(C2)=D2 | ||||

4 | if D2<B1 | >B1=D2 | |||

5 | >C1=A2 | ||||

6 | >D1=C2 |

8.

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

1 | =[0]*2 | =[0]*3 | |

2 | for 2 | >A1(A2)=[0]*3 | |

3 | for 3 | >A1(A2)(B3)=rand(100) | |

4 | for 3 | >B1(A4)=[0]*2 | |

5 | for 2 | =A1(B5)(A4) | |

6 | >B1(A4)(B5)=C5 | ||

7 | >output@s(C5) | ||

8 | >output("") |

9.

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

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

2 | for A1.len() | for A2 | >B1+=A1(A2)(B2) |

10.

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

1 | =[0]*10 | =[0]*10 | =1 | |

2 | for 10 | >A1(A2)=[0]*10 | ||

3 | for C1<=10 | =[rand(9)+1,rand(9)+1] | if !B1.contain(B3) | >B1(C1)=B3 |

4 | >C1+=1 | |||

5 | for 10 | >A1(B1(A5)(1))(B1(A5)(2))=9 | if B1(A5)(1)-1>0 && B1(A5)(2)-1>0 && A1(B1(A5)(1)-1)(B1(A5)(2)-1)!=9 | >A1(B1(A5)(1)-1)(B1(A5)(2)-1)+=1 |

6 | if B1(A5)(1)-1>0 && A1(B1(A5)(1)-1)(B1(A5)(2))!=9 | >A1(B1(A5)(1)-1)(B1(A5)(2))+=1 | ||

7 | if B1(A5)(1)-1>0 && B1(A5)(2)+1<=10 && A1(B1(A5)(1)-1)(B1(A5)(2)+1)!=9 | >A1(B1(A5)(1)-1)(B1(A5)(2)+1)+=1 | ||

8 | if B1(A5)(2)-1>0 && A1(B1(A5)(1))(B1(A5)(2)-1)!=9 | >A1(B1(A5)(1))(B1(A5)(2)-1)+=1 | ||

9 | if B1(A5)(2)+1<=10 && A1(B1(A5)(1))(B1(A5)(2)+1)!=9 | >A1(B1(A5)(1))(B1(A5)(2)+1)+=1 | ||

10 | if B1(A5)(1)+1<=10 && B1(A5)(2)-1>0 && A1(B1(A5)(1)+1)(B1(A5)(2)-1)!=9 | >A1(B1(A5)(1)+1)(B1(A5)(2)-1)+=1 | ||

11 | if B1(A5)(1)+1<=10 && A1(B1(A5)(1)+1)(B1(A5)(2))!=9 | >A1(B1(A5)(1)+1)(B1(A5)(2))+=1 | ||

12 | if B1(A5)(1)+1<=10 && B1(A5)(2)+1<=10 && A1(B1(A5)(1)+1)(B1(A5)(2)+1)!=9 | >A1(B1(A5)(1)+1)(B1(A5)(2)+1)+=1 |

Approach: Randomly generate 10 locations for placing landmines, and the locations cannot be duplicated

Add 1 to the surrounding numbers for each landmine placed

**4.4 Understanding objects**

2.

A | |
---|---|

1 | =to(10,1) |

2 | =A1([1,3,5,10]) |

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

4 | =A1.m(-3) |

5 | =A1.modify(6,[99,88,77]) |

6 | =A1.insert(3,[20,30]) |

7 | =A1.insert(0,65) |

8 | =A1.delete([2,4,5]) |

9 | =A1.rvs() |

3.

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

1 | =[] | |||

2 | for 1000 | if A2==2 | >A1.insert(0,A2) | |

3 | for 2, A2-1 | if A2%B3==0 | next A2 | |

4 | >A1.insert(0,A2) |

4.

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

1 | =[] | |||

2 | for 2,50 | >A1.insert(0,A2) | ||

3 | for A2 | if A2%B3==0 | >A1(A2-1).insert(0,B3) |

5.

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

1 | 0 | =[] | ||

2 | for 1000 | |||

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

4 | if A1==A2 | >B1.insert(0,A2) | ||

5 | >A1=0 |

*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