DOWNLOAD THE CODE:
Download the Code 24425.zip

As I suggested earlier, using a modular approach here is advisable. You could calculate the next automation state inside the fn_get_automation_states() function. However, to keep things simple, you can write a scalar UDF called fn_next_automation_state(), which accepts an automation state and a replication rule as arguments and returns the next automation state. You now have all the information you need to write the fn_get_automation_states() function. Listing 1 shows the version of the fn_get_automation_states() function that I came up with.

Now you have to write the usp_first_automation_state stored procedure, which generates the first automation state as a string of 80 random digits, and the fn_next_automation_state() function, which generates the next automation state. Let's start with the stored procedure. The code constructs the first automation state in a loop. In each iteration, the loop calculates a random digit in the range 0 to 3 and concatenates it to the output parameter. Remember that the puzzle requires that each automation state starts and ends with a 0. Run the code that Listing 2, page 23, shows to create the usp_first_automation_state stored procedure.

The last step is to write the fn_next_automation_state() scalar function, which implements the kernel of the puzzle's algorithm. The function accepts an automation state as a string of 80 digits in the range 0 to 3 and a replication rule as a string of 10 digits in the range 0 to 3. Remember that the first and last digits of a step are always 0. The function forms a loop to handle digits 2 though 79. The algorithm calculates each digit in the resulting automation state, as I outlined in Steps 1 through 3 earlier.

The following T-SQL code is a translation of the main algorithm that the fn_next_automation_state() function implements:

SUBSTRING(
  @rule,
  CAST(SUBSTRING(@state, @pos - 1, 1) 
AS int) +
  CAST(SUBSTRING(@state, @pos, 1) 
AS int) +
  CAST(SUBSTRING(@state, @pos + 1, 1) 
AS int) +
    1,
  1),

In this code, @state is the input automation state, @rule is the automation rule, and @pos is the current digit's position. You can run the script that Listing 3, page 23, shows to create the fn_next_automation_state() function.

You can now use the code that Listing 4, page 23, shows to invoke the usp_first_automation_state stored procedure to generate the first automation state, then invoke the fn_get_automation_states() function to generate the required output. (Run the code in Query Analyzer text mode.) To make the output look more like a drawing, I used the code that Listing 5, page 23, shows to replace the digits 0, 1, 2, and 3 with space, dot, lowercase o, and uppercase O characters, respectively.

Figure 2 and Figure 3 show a couple of drawings (abbreviated to fit in print) that my example code produced. Note that the first automation state is generated randomly, so you get a different drawing each time you run the code.

I hope that you've enjoyed drawing with T-SQL. In case you were wondering, I won't cover surreal drawings in my next column. However, I'll stay in the area of graphical and geometrical problems and discuss a scenario that will put your knowledge of geometry to the test.

End of Article

Prev. page     1 2 3 [4]     next page -->



You must log on before posting a comment.

If you don't have a username & password, please register now.

 
 

ADS BY GOOGLE