Fork the repository, add/edit files in the directories in order to complete the test. The completed test must be submitted as a GitHub pull request to prog-1/test-2-2 repository.
The test consists of four tasks (see below).
Explain erlaeuterung
program and complete the tasks. You may execute and modify the program.
The program contains special comments:
-
QUESTION N:
requires an answer provided in the following line prefixed withANSWER N:
. E.g.// QUESTION 1: What is []int? // ANSWER 1: Creates a variable named `x` of type slice of ints. var x []int
-
TASK N:
requires an action described. In the following example we expect you to renamef1
into something likeprintSlice
- a function which has a better name:// TASK 1: Provide a better name for the function. func f1(s [][]int) { for _, row := range s { for _, v := range row { fmt.Printf("%3d", v) } fmt.Println() } }
Write a program that finds the n
th term (n
is a non-negative integer) of
the progression
- Create the program in the directory
progression
. - Implement the logic in a function declared as
func nthTerm(n uint) int
. - Implement
main()
function that- reads
n
from the keyboard - calls
nthTerm
function - prints a value returned from the function.
- reads
- Create a file with tests for the program. The tests must cover all representative inputs.
Enter n: 1
The nth term is 2.
Explanation:
nthTerm(1)
must return 2
, because .
Enter n: 4
The nth term is 38.
nthTerm(4)
must return 38
, because
Write a program that removes all integers divisible by 5
from a slice of
integers.
- Write the program in the directory
filter
. - Implement the logic in a function declared as
func filter(s []int) []int
. - Implement
main()
function that- reads a slice from the keyboard (a user provides the number of elements in a slice and the element values)
- calls
filter
function - prints the filtered slice.
- Create a file with tests for the program. The tests must cover all representative inputs.
Enter the number of elements in a slice: 2
Enter the elements: 10 -15
The filtered slice: []
filter([]int{-10, -15})
returns []int{}
, because all integer in the slice
are divisible by 5
, and are removed from the slice.
Enter the number of elements in a slice: 5
Enter the elements: -1 0 -5 15 4
The filtered slice: [-1 4]
filter([]int{-1, 0, -5, 15, 4})
returns []int{-1, 4}
. 0
, -5
and 15
are
integers divisible by 5
and are removed from the slice.
Write a program that generates a matrix of values such as:
- the last row is filled with 0 except for the last element, which is 1;
- any other element of the table is the sum of all elements below it (in the same column) and to the right (in the same row).
- Write the program in the directory
matrix
. - Implement the logic in a function declared as
func gen(width, height int) [][]int
. - Implement
main()
function that readswidth
andheight
from the keyboard and prints the generated matrix. - Create a file with tests for the program. The tests must cover all representative inputs.
Enter width and height: 5 4
Result:
130 53 21 8 4
36 16 7 3 2
8 4 2 1 1
0 0 0 0 1
Explanation for row=0, col=2: 21 = (vertical) 7+2+0 + (horizontal) 8+4