Updated 2016-01-14 20:06:15 by pooryorick

Intro  edit

A list of exercises for those learning Tcl

Description  edit

In the process of Learning Tcl, it can be helpful to have a set of reference exercises that serves as an additional resource to other curricula. The following is a list of exercises that can be practiced in order to become a more proficient Tcl programmer.

After an exercise is complete, post your work somewhere and ask the community to try to break it. You can learn a lot this way!

Guidelines for Contributors  edit

The exercises should be written so as to maximize their educational value. Each exercise should try to introduce at most one concept not yet introduced by previous exercises, with the minimal amount of specification necessary to illustrate the concept. It should be possible to write a solution that doesn't require much auxiliary code, and the solutions should not take pages and pages of code, or be highly interactive.

Exercises  edit

interpreter-provided variables
Write a script that, when evaluated by tclsh, displays the variables provided by the interpreter, along with the value of each variable, as a list where the name of the variable is the first item in the list and the value is the second item. Display each list on a separate line. In the case of array variables, display each variable/key combination as name1(name2), along with its value as described above.
no procs
Write a procedure that returns all commands that are visible to the caller that are not procedures.
random number
Write a procedure that takes the arguments minimum, and maximum, and returns a random integral $number such that $minimum <= $number <= $maximum
random string
Write a procedure that takes the argument length, and returns a string of random lower and upper case letters of that length.
random guesses
Write a procedure that takes the argument integer, which is any integer, and randomly chooses numbers from 0 through that integer until it randomly chooses a number equal to integer, and returns the number of "guesses" it had to make.
Write a procedure that takes one argument, $word, which is a single English word, and re-orders the letters so that they are in alphabetical order ("A" before "a", and "a" before "B").
Write a procedure that takes any number of arguments, interprets each as a base-7 integer, and sums them. key principles: interpret strings in some arbitrary way.
decimal number
Write a procedure that takes one argument, interprets that argument as a decimal number, and returns that decimal number, or if the argument doesn't make sense as a decimal number, returns an error value of NaN. Key principles: Make sure Tcl doesn't treat a number as an octal number.
Sierpiński triangle
Write a procedure that prints a Sierpiński triangle to the terminal using some text character.
list search
Write a procedure that takes the arguments list, and term, and finds term in the list in the smallest number of steps, without using lsearch, of course. Now write a procedure that takes the arguments chan and term, and finds term in the list that is the content of chan, without reading the content too far beyond the first occurrence of term.
range search
Given a command that returns true for all integers in some range, and false for all other integers, write a procedure that takes x, direction, and cmdprefix, and returns the last integer in direction away from x, for which cmdprefix returns true. For example, myproc 13 + [list list string is integer] returns 4294967295. Solution at Binary Search.
Write a procedure that that takes the arguments directory and pattern, and returns a list of files, each whose name relative to directory matches the glob-style pattern, including any glob-style patterns in any directory component in pattern. For example [fullglob /name/of some/otherdir*/fname*] might return /name/of/some/otherdir1/fnameA /name/of/some/otherdir2/fnameA. Write both a version that performs this task using a recursive strategy, and a version that does the same using an iterative strategy.
Write a procedure like fullglob, except that it additionally searches the entire directory tree of each directory that matches the full directory components of pattern for files or directories whose basename matches the last component in pattern. Write both a version that performs this task using a recursive strategy, and a version that does the same using an iterative strategy.
number sign class
Write a procedure that takes one argument, a number, and returns -1, 0, or 1, indicating that the number is negative, zero, or positive, respectively. The procedure may contain exactly one command, expr, which may use the if-then-else (?...:) operator at most one time. Bonus Round: Add an additional constraint: The if-then-else operator may not be used.
Write a program that displays elapsed time to a terminal. Use an event-driven style. Enter the event loop at the end of the script with vwait forever. Use after to schedule elapsed time updates. Provide a mechanism to pause and resume. Estimated code length: 40 lines.
Write a procedure that takes one argument, $text and removes the common leading whitespace characters from all the lines of $text. When determining what is common whitespace, don't use the contents of lines containing only whitespace, and remove all whitespace from such lines
eight queens
Write a procedure that takes no arguments and generates a list of eight positions in algebraic notation, such that queens placed at those positions could attack any other position but could not attack each other.
fibonacci series
Write a procedure that generates a fibonacci number list of specified length. Extra credit: write a version that uses recursion, and one that doesn't.
classroom average and spreadsheet
Write a program which runs on the terminal. The program mimics the work on an Excel spreadsheet of students in a classroom. You will have 4 columns in this spreadsheet. the columns are the student name, the first exam mark, the second exam mark, and the third exam mark. Your program must have a menu system to allow the user of the program (the teacher) to update or delete any student record. It also must allow updating any mark in the spreadsheet. The teacher must be able to print the final mark of each student on a fifth column. The final mark is just the average of the three exams. For this exercise,and to save the spreadsheet, you may use either Sqlite3 database OR you can use a normal comma separated values ASCII text file using the Tcl command [open spreadsheet.txt w]. PYK 2015-02-24: This exercise is a little broad compared to the other exercises, and doesn't meet the "introduce at most one concept not yet introduced by previous exercises" criteria. Hopefully other contributions will be a little more directed.

Tk Exercises  edit

This section may eventually have its own page, but is included here for the time being.
file browser
Write a file browser that includes two visual components: a label indicating the current directory and a listbox below the label showing the files in that directory. Use the [pack] geometry manager.
file browser2
Add a window to the right of the directory indicator and file list that shows the contents of the file if it is text. Hint: the directory indicator and file list into a frame. Hint: pack the content window to the right side, and let the frame containing the directory indicator and file list continue to pack by default to the top.
restaurant cashier and point of sale
Write a small tk application having a number of buttons. Each button will have its text mentioning the name of the meal . Clicking the button ads the meal to a listbox and the total price will be updated. If you select a listbox item and press delete key, it must remove that item and the total price must be updated too. The total price must always be displayed on a label. Hint: use a global variables for the total price and for the lis of selected items. Also, use grid layout. When you delete from the listbox, you take a copy of it as a list variable then you delete at the select position then you delete the whole listbox then you set the listbox again with the updated list variable.
Write using the calendar widget and the sqlite3 database an agenda. The agenda is a text note versus its date. Selecting a date from the calendar shows today's notes in a text widget.

See Also  edit

could be useful learning exercises for GUI techniques.