
Code snippets
Snippet 1
var maxC rune = -1
for _, r := range text {
if unicode.IsUpper(r) && r > maxC {
= r
maxC }
}
Snippet 2
:= 1
product
for i := 0; i < DIM; i++ {
*= numbers[i]
product }
Snippet 3
:= -1
lastRainyDay
for i, measure := range mmRain {
if measure > 0 {
= i
lastRainyDay }
}
Snippet 4
:= 0
sum
for i := 0; i < DIM; i++ {
if numbers[i]%3 == 0 {
+= numbers[i]
sum }
}
Snippet 5
for i := 1; i < DIM; i++ {
.Println(numbers[i] - numbers[i-1])
fmt}
Snippet 6
:= 0
sum := 0
i for sum < TARGET {
+= numbers[i]
sum ++
iif sum >= TARGET {
break
}
}
.Println(TARGET,"reached at",i) fmt
Snippet 7
var min byte = text[0]
for i := 1; i < DIM; i++ {
if text[i] < min {
= text[i]
min }
}
Snippet 8
:= true
alternating
for i := 1; i < DIM; i++ {
if (numbers[i] > 0) == (numbers[i-1] > 0) {
= false
alternating break
}
}
Snippet 9
var previous rune = text[0]
for _, current := range text {
if current < previous {
.Println()
fmt}
.Print(string(current), " ")
fmt= current
previous }
Analyze the code snippets above and then write the answers to the following questions
- What is the purpose of each snippet? Match each one to one of the following descriptions:
- Identify the last rainy day.
- Sum the numbers that are multple of 3.
- Find the “greatest” capital letter (the one with the highest alphabet position).
- Divide a sequence of characters in its increasing subsequences (sequences of characters in increasing alphabetical positions).
- Find the smallest character (the one with the least ASCII code).
- Compute the differences between consecutive numbers in a a sequence.
- Compute the product of a sequence of numbers.
- Check if the numbers in a sequence alternate between positive and negative.
- Establish when the sum of a sequenze of numbers exceeds a given bound.
Cluster the code snippets based on similarities. Which similarities can you find? (otherwise said: which criterium -or criteria- did you use to cluster them?)
What do the following snippets have in common?
- snippets 1, 3, and 9?
- snippets 2, 4, and 5?
- snippets 6 and 8?
- snippets 1 and 9?
- snippets 4 and 8?
- What do the following snippets have in common?
- snippets 5, 8 and 9?
- snippets 1 and 7?
- snippets 2, 4 and 6?
- The following pairs of snippets have similar structures and carry out somewhat similar tasks, but not identical. What structural, and thus semantic differences do they exhibit?
- snippets 2 and 4?
- snippets 1 and 3?