Addition of Number Elements from a List in Terraform - terraform

Is there a way to add every number with the next one from a list and then the result to be added to the next number from the same list?
For example:
[0, 2, 5, 9]
Is there a way to get:
[0, 2, 7, 16]
I've come to:
resource "null_resource" "dmns_calc_l" {
count = "${length(local.num_cntd_and_zero_l) - 1}"
triggers {
num_calc_l = "${local.num_cntd_and_zero_l[count.index] + local.num_cntd_and_zero_l[count.index + 1]}"
}
}
locals {
num_calc_and_zero_l = [
"${0}",
"${null_resource.dmns_calc_l.*.triggers.num_calc_l}",
]
}
However, as it is clearly to see it - it is good enough only for a list of 3 elements /and if the first element is "0"/ because it does NOT add up with the previous addition result.

Related

Count duplicate string occurances in Terraform list

I am struggling with seemingly simple task in Terraform. I would easily do it with Ruby/Python with local variable inside the for loop, but Terraform doesn't have those.
Here is an issue.
I have a list with multiple duplicate string occurances:
list = ["a","b","c","a","b","c","a"]
I want to count how many times the same string occured from the beginning, but keep the count in the same location, so the resulting list would become this:
index_list = [1, 1, 1, 2, 2, 2, 3]
Is it possible with Terraform?
We can do the following:
locals {
lst = ["a", "b", "c", "a", "b", "c", "a"]
index_list = [for index, item in local.lst : length([for i in slice(local.lst, 0, index + 1) : i if i == item])]
}
output "index_list" {
value = local.index_list
}
Output for index_list:
index_list = [
1,
1,
1,
2,
2,
2,
3,
]
The first for loop iterates through the list. The second for loop is a slice combined with a filter. The slice function creates a sublist from the first element to the index if the current element. With the filter (if i == item) we filter out from this sublist all the elements which are equal to the current element. Last but not least, we get the length of this filtered list.

how can I change values from a nested list accordingly?

I am making a game which requires me to randomly select 2 buildings from a pool of different buildings, and after choosing 1 of the 2 buildings the count from the building pool will be deducted. I am having troubles changing the values of the selected items in the nested loop. Is there any way I can change it? This is my current code:
building_list = [['house', 8], ['hospital', 8], ['shopping-center', 8], ['airport', 8], ['park', 8]]
building_list1 = random.choice(building_list)[0]
building_list2 = random.choice(building_list)[0]
If you have concern about the performance of removing the "chosen" building, then I will propose different approach to this problem. The reason is that to constant search and remove list item is not efficient.
Please let me know if you have question.
n = 2 # select 2 buildings to pop
while n: # n is not zero: means it's True
random.shuffle(building_list)
building_pop = building_list.pop() # remove last item from list is O(1)
print(build_pop)
# do whatever you want to this two building here <----
n -= 1
this looks complex but basically we are finding what the random item was then decrementing its value by 1 then getting the name of the building.
import random
building_list = [['house', 8], ['hospital', 8], ['shopping-center', 8],
['airport', 8], ['park', 8]]
building1 = random.choice(building_list)
building_list[building_list.index(building1)][1] -= 1
building_list1 = building1[0]
print(building1)
print(building_list1)
building2 = random.choice(building_list)
building_list[building_list.index(building2)][1] -= 1
building_list2 = building2[0]
print(building2)
print(building_list2)
I got this output:
['shopping-center', 7]
shopping-center
['hospital', 7]
hospital

How to find how many items are in the same position in two lists?python

I need to be able to check if any items in one list are also in another list but in the same position. I have seen others but they return true or false. I need to know how many are in the same position.
So compare them directly!
This of course is assuming both lists are the same length:
a = [1, 1, 2, 3, 4, 5, 7, 8, 9]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
matches = 0
for index in range(len(a)):
if a[index] == b[index]:
matches += 1
print mat
Try it here!
overlap = set(enumerate(listA)).intersection(set(enumerate(listB))
print(len(overlap))
enumerate pairs up elements with their index, so you can check how many common element/ index pairs exist between the two lists.
One advantage of this approach (as opposed to iterating through either list yourself) is that it automatically takes care of the case where the lists have different lengths.
demo

What is the difference between any and find in groovy?

I am trying to get the set values from closure in groovy:
myList(1, 2, 3).any { it > 2 }
myList(1, 2, 3).find { it > 2 }
So not able to figure out, which one to use and better.
any returns boolean - true if any of the elements on the list matches the closure condition, while find returns first element that meets the criteria in closure being passed.
If you need to know if there're elements matching certain criteria, use any, if you need only a single element (the first one) use, find, if you need all the elements that matches the closure passed use findAll.
Example:
assert [1, 2, 3].any { it > 1 }
assert [1, 2, 3].find { it > 1 } == 2
assert [1, 2, 3].findAll { it > 1 } == [2, 3]

Collect only if result is not null

I have a collection and I'm wanting to find certain elements and transform them. I can do this in two closures but I was wondering if it is possible with only one?
def c = [1, 2, 3, 4]
def result = c.findAll {
it % 2 == 0
}
result = result.collect {
it /= 2
}
My true use case is with Gradle, I want to find a specific bunch of files and transform them to their fully-qualified package name.
You can use findResults:
def c = [1, 2, 3, 4]
c.findResults { i ->
i % 2 == 0 ? // if this is true
i / 2 : // return this
null // otherwise skip this one
}
Also, you will get [] in case none of the elements satisfies the criteria (closure)

Resources