0
0
Terraformcloud~10 mins

Collection functions (length, flatten, merge) in Terraform - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the number of elements in the list.

Terraform
length = length([1])
Drag options to blanks, or click blank then click option'
A[1, 2, 3, 4]
B["apple", "banana", "cherry"]
C[]
D[true, false]
Attempts:
3 left
💡 Hint
Common Mistakes
Using an empty list which returns 0.
Using a non-list value which causes an error.
2fill in blank
medium

Complete the code to flatten a list of lists into a single list.

Terraform
flat_list = flatten([1])
Drag options to blanks, or click blank then click option'
A["a", "b", "c"]
B[1, 2, 3, 4]
C[["a"], ["b", "c"]]
D[[1, 2], [3, 4]]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a flat list which does nothing.
Passing a non-list or empty list.
3fill in blank
hard

Fix the error in the merge function to combine two maps correctly.

Terraform
combined = merge([1], {"key2" = "value2"})
Drag options to blanks, or click blank then click option'
A["key1" = "value1"]
B["key1", "value1"]
C{"key1" = "value1"}
D{"key1", "value1"}
Attempts:
3 left
💡 Hint
Common Mistakes
Using lists instead of maps.
Using incorrect syntax for maps.
4fill in blank
hard

Fill both blanks to create a map with keys as strings and values as lengths of words longer than 4 characters.

Terraform
result = { [1] : length([2]) for word in words if length(word) > 4 }
Drag options to blanks, or click blank then click option'
Aword
C"word"
Dwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals as keys instead of variables.
Using the whole list instead of the single word.
5fill in blank
hard

Fill all three blanks to create a merged map with uppercase keys and values greater than 3.

Terraform
filtered = { [1] : [2] for k, v in merge(map1, map2) if [3] }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Cv > 3
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys without uppercase.
Filtering with wrong conditions.
Swapping keys and values.