Lists and Comprehensions
The Nada DSL supports the introduction and use of Python lists that contain secret integer values. This includes using a subset of the list comprehension syntax supported by Python.
Basic Example
The program below uses Python list comprehensions to build an ascending sequence of three secret integers in which the first entry is a secret integer input. This sequence of secret integers is then returned by the program.
Voting Example using List Comprehensions
The program below assembles the secret votes from four voting parties (i.e., voters
) and returns the total for each of the two candidates. Because each voting party submits an input of either 1
or 2
for each candidate, the value Integer(4)
is subtracted from the total for each candidate.
A list comprehension is used to construct the list of parties corresponding to the voters. An expression containing a Python list comprehension nested in another list comprehension is used to assemble a list of lists votes_per_candidate
that contains two lists (i.e., a list of the votes submitted for each of the two candidates). Finally, a list comprehension is used to build the list of outputs.