## Scramble Letter Game reference to Pseudocode Development and TCL Procedures V2, and TCL 8.6 demo example, numerical analysis

### Preface

gold Update 3/26/2024. Here are some calculations for the Letter Scramble Game with Pseudocode outline reference to TCL Procedures. Intended to supplement the TCL Wiki page Playing with Recursion by RS. This supplemental code following RS is intended for study of McCarthy theorems on computer arithmetic into one-line programs. Fun idea was The Gallows game from Richard Suchenwirth. Interesting letter game was Word Chain from Keith Vetter. Useful error test on foreach {input expected} from Playing Cobol from RS etc. Error testing procedures were useful in Simple Test from EKB & RLE & RS.

gold Update 3/7/2024. The author is retired engineer on Windows 10 and no longer has proofing access to Unix machines. Unix is respected after use of so many decades in engineering, but my wings are lost. I did find a useful online IDE, jdoodle. I can paste from a text file, edit, and run an output using this online IDE.

### Not a Replacement for TCL Core

This page on developing pseudocode examples and one line procedures is not a replacement for the current Tcl core and Tcllib, which is much improved since Tcl version 4, and other <faster> language constructs. math ops, Tcllib routines, and other compiled routines can reduce the cost of big-data tasks by about 1/3. The time savings of the core are not always obvious on small quantities of data, like 4 or 5 numbers. Performance of one-line programs may suffer degradation due to lengthy recursion calls, and may be limited by constraints on recursion. Dependence on math operator notation, helper procedures, math check examples, degradation due to lengthy recursion calls, and special library functions should be noted in the comment lines.

### Letter Scrambler Game in TCL

This modified subroutine, shuffle_with_fixed_first_and_last, takes a list as input and shuffles the inner list while keeping the first and last items in their original positions. The first and last items are extracted and stored in separate variables (first_item and last_item). The inner list is then obtained by removing the first and last items using lrange. The original shuffle subroutine is then called to shuffle the inner list. Finally, the shuffled inner list is concatenated with the first and last items, and the modified list is returned as the result.

### Proc with Split String Command

To transform a string into a list in TCL, you can use the split command. This is a one line procedure. Here’s an example of how you can create a procedure (proc) to split a string into a list:

Pretty Print

```# TCL
proc split_form {stringx} {
return [split \$stringx {}]
}
set assignment "educator"
puts "[split_form \$assignment]"```

In this example: The split_form procedure takes a single argument (stringx). It splits the input string (\$stringx) using the empty string {} as the delimiter. The resulting list is returned by the procedure. Finally, we apply this procedure to the string “educator” and print the resulting list. The output will be: {e d u c a t o r}

### Quick Start on Pseudocode

` 1.  `shuffle_list_items(list)`:`
```    -   This function takes a list as input.
-   Initialize an empty list called `shuffled_list`.
-   While the input list is not empty:
-   Generate a random index between 0 and the length of the list.
-   Add the list item at the random index to `shuffled_list`.
-   Remove the same item from the input list.
-   Return the `shuffled_list`.```

2. shuffle_list_with_fixed_first_and_last(list):

```    -   This function shuffles the inner items of a list while keeping the first and last items fixed.
-   If the input list has less than 2 items, return the original list (no shuffling needed).
-   Extract the first and last items from the list.
-   Create an `inner_list` by excluding the first and last items.
-   Call the `shuffle_list_items` function on the `inner_list` to get a `shuffled_inner_list`.
-   Concatenate the first item, `shuffled_inner_list`, and the last item to form the final `shuffled_list`.
-   Return the `shuffled_list`.```

3. Example Usage:

```    -   The provided print statements demonstrate how to use the `shuffle_list_with_fixed_first_and_last` function with different input lists:
-   Shuffling the list `[c, a, t, c, h]`.
-   Shuffling the list `[t, o, p]`.
-   Shuffling the list `[b, o, b]`.
-   Shuffling the list `[t, o]`.
-   Shuffling the list `[a]`.
-   Shuffling the list `[s, c, r, a, m, b, l, e, d]`.```

Draft Auxiliary Text on Letter Scramble Game.

code into simpler terms: Original Subroutine (shuffle_list_items): This subroutine shuffles the items in a list randomly. The subroutine starts with an empty list called shuffled_list. While there are still items in the input list (\$list): The subroutine picks a random index within the range of the list length. The subroutine Adds the item at that index to shuffled_list. The subroutine Removes the same item from the original list. Finally, The subroutine returns the shuffled list.

Modified Subroutine (shuffle_list_with_fixed_first_and_last): This version of the subroutine ensures that the first and last items in the list remain fixed (unchanged). Here’s how it’s different: If the input list has fewer than 2 items, The subroutine just returns the original list (no need to shuffle). Otherwise: The subroutine stores the first item (first_item) and the last item (last_item). The subroutine Creates an “inner list” by excluding the first and last items. The subroutine Shuffles the inner list using the original shuffle_list_items subroutine. The subroutine Combines the fixed first item, shuffled inner list, and fixed last item to create the final shuffled list. Testcase Routine (?): This routine tests whether the modified subroutine behaves correctly. The subroutine runs the modified subroutine with specific input lists and checks if the result matches the expected output. If there’s a mismatch, The subroutine prints a message indicating which test failed.

Test Cases: The test cases demonstrate how the modified subroutine works: For example, when given the list c a t c h, the expected output should be c c a t h. Similarly, for other input lists, it checks if the modified subroutine produces the correct results. Feel free to experiment with different lists and see how the shuffling works!

### References:

• GoDuck search engine < Functional Programming >
• GoDuck search engine < Imperative Programming >
• GoDuck search engine < Programming Examples >
• Google search engine < vaporware >
• Tcllib math::special Special mathematical functions
• Tcllib math::figurate Evaluate figurate numbers
• Tcllib simulation::random Pseudo-random number generators
• Tcllib simulation::montecarlo Monte Carlo simulations
• Wikipedia search engine < Lehmer random number generator >
• Professor Frisby's Mostly Adequate Guide to Functional Programming on internet archive
• Writing code using the Pseudocode Programming Process, article by David Zych
• Mathematical Methods in Large-scale Computing Units
• by Derrick H. Lehmer
• L’Ecuyer, Pierre (January 1999). "Tables of linear congruential generators of different sizes and good lattice structure"
• Mathematics of Computation. 68
• A Comprehensive Review of Quantum Random Number PDF
• Good Pedagogical Random Number Generators
• from J. Stanley Warford
• Coding the Lehmer Pseudo random Number Generator
• from W. H. PAYNE
• The most commonly used version of the Mersenne Twister algorithm
• is based on the Mersenne prime expr { 2**19937-1 }
• TWISTER = 431542479738816264805523551633791983905393504322.....
• GoDuck search engine < TCL version >
• One Liners Programs Pie in the Sky
• One Liners
• One Liners Programs Compendium [L1 ]
• WIKI BOOKS, Programming_Examples pdf
• WIKI BOOKS, Tcl_Programming_Introduction pdf
• Note. I did find a useful online IDE, jdoodle
• Note. I can paste, edit, and run an output using this online IDE.
• How Do I Write Pseudocode? video by Mr. Brown CS
• Refers to rand, RandMT, Mersenne Twister, & random
• Suchenworth RS on Horseracing in Tcl.
• Random Number Generators: Good Ones Are Hard to Find, Keith Willam Miller, pdf online
• Throwing Two Dice GWM and Dice by Keith Vetter.
• int from RLE has dice expression expr {1 + int(rand()*6)} RLE.
• Several Dr. Math emails may reduce some dice issues to simple paths, internet archive.
• Counting Elements in a List from RWT.

### Extra Credit on One Line Procedures

• Wikipedia search engine < random >
• Wikipedia search engine < dice >
• Wikipedia search engine < Programming Examples >
• Google search engine < vaporware >
• One Liners Programs Pie in the Sky
• One Liners
• One Liners Programs Compendium [L3 ]
• WIKI BOOKS, Programming_Examples pdf
• WIKI BOOKS, Tcl_Programming_Introduction pdf
• google search engine < HgA1c to Average Blood Glucose>
• Tcllib math::numtheory::numberPrimesGauss N
• math::numtheory::numberPrimesLegendre N
• math::numtheory::numberPrimesLegendreModified N
• math::numtheory::differenceNumberPrimesLegendreModified lower upper
• math::numtheory::listPrimePairs lower upper step
• math::numtheory::listPrimeProgressions lower upper step

Pretty print

```
# gold on tcl Wiki, jdoodle interpreter
# Letter Scramble Game V2, 3/26/2024
# Subroutine transforming string to list items
proc split_form {stringx} {
return [split \$stringx {}]
}
# Original subroutine for shuffling list items
proc shuffle_list_items list {
set shuffled_list {}
while {[llength \$list]} {
set random_index [expr {int([llength \$list] * rand())}]
lappend shuffled_list [lindex \$list \$random_index]
set list [lreplace \$list \$random_index \$random_index]
}
return \$shuffled_list
}
# Modified subroutine with fixed first and last items
proc shuffle_list_with_fixed_first_and_last list {
set list [split_form \$list]
if {[llength \$list] < 2} {
return \$list
}
set first_item [lindex \$list 0]
set last_item [lindex \$list end]
set inner_list [lrange \$list 1 end-1]
set shuffled_inner_list [shuffle_list_items \$inner_list]
set shuffled_list [concat \$first_item \$shuffled_inner_list \$last_item]
return \$shuffled_list
}
# Testcase routine
proc ? {cmd expected} {
catch {uplevel 1 \$cmd} res
if {1} {
puts "\$cmd -> \$res, example expected but random inside \$expected"
}
}
# Test cases
? {shuffle_list_with_fixed_first_and_last {catch}} {c c a t h}
? {shuffle_list_with_fixed_first_and_last {top}} {t o p}
? {shuffle_list_with_fixed_first_and_last {bob}} {b o b}
? {shuffle_list_with_fixed_first_and_last {to}} {t o}
? {shuffle_list_with_fixed_first_and_last {a}} {a}
? {shuffle_list_with_fixed_first_and_last {123}} {123}
? {shuffle_list_with_fixed_first_and_last {12}} {12}
? {shuffle_list_with_fixed_first_and_last {1}} {1}
? {shuffle_list_with_fixed_first_and_last {123456789}} {123456789}
? {shuffle_list_with_fixed_first_and_last {scambled}} {s m r e a l c b d}```

```# printout
shuffle_list_with_fixed_first_and_last {catch } -> c c h t a, example expected but random inside c c a t h
shuffle_list_with_fixed_first_and_last {top } -> t o p, example expected but random inside t o p
shuffle_list_with_fixed_first_and_last {bob} -> b o b, example expected but random inside b o b
shuffle_list_with_fixed_first_and_last {to} -> t o, example expected but random inside t o
shuffle_list_with_fixed_first_and_last {a} -> a, example expected but random inside a
shuffle_list_with_fixed_first_and_last {123} -> 1 2 3, example expected but random inside 123
shuffle_list_with_fixed_first_and_last {12} -> 1 2, example expected but random inside 12
shuffle_list_with_fixed_first_and_last {1} -> 1, example expected but random inside 1
shuffle_list_with_fixed_first_and_last {123456789} -> 1 2 7 4 6 8 3 5 9, example expected but random inside 123456789
shuffle_list_with_fixed_first_and_last {scambled} -> s b l e m c a d, example expected but random inside s m r e a l c b d```

## Hidden Comments Section

Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, gold 3/26/2024

 Category Numerical Analysis Category Toys Category Calculator Category Mathematics Category Example Toys and Games Category Games Category Application Category GUI

 Category Development Category Concept Category Algorithm Category Biology