Relativity
Now let's implement the algorithm in T-SQL to test our T-SQL skills. Because T-SQL is a relational environment, you'd probably prefer to store the automation states in a table, then use a query to return the results in an ordered fashion, rather than just using variables and PRINT statements as you would in most programming environments. I envisioned a table with two columns. One column, a key column called num_step, determines the generation number that you use in the ORDER BY clause of a query that produces the desired output. The other column, step, holds the string of digits that you generated. Because each run of the algorithm generates a different output, I thought that writing a table-valued UDF that returns the table I envisioned would be a better idea than creating a real table. Using such a UDF would make the code that generates the output shorter and cleaner than creating, querying, then dropping a real table. By using a UDF, you could get the desired output by performing a SELECT query against the function as follows:
SELECT state
FROM fn_get_automation_states(<input_arguments>)
ORDER BY num_step
Initially, I thought that the replication rule and the number of desired automation steps would be the only parameters that the function fn_get_automation_states() would require. However, one of the puzzle's requirements is that each digit of the first state of the automation be randomly generated. This requirement means that you need to use the RAND() function to generate random values. The RAND() function, when invoked with no arguments, is by definition nondeterministicthat is, on different invocations, it returns different results. However, nondeterministic functions aren't allowed within UDFs, so you need to somehow produce the first automation state outside the UDF (by using a stored procedure, for example) and pass it to the UDF as an argument.
After you have all the input and output parameters for the fn_get_automation_states() function, you need to implement the function's algorithm, which is fairly simple. Beginning with this information
- Input: first_automation_state, rule, num_steps
- Output: steps table with columns num_step and state
you can write code based on the following pseudocode to implement the function's algorithm:
insert first_automation_state into the steps table
while not handled all steps
begin
calculate next_automation_state
insert next_automation_state into the steps table
end
Prev. page
1
2
[3]
4
next page