Introduction to the Code Examples
This page gives access to code examples related to subjects covered in COP 3223H at UCF, as taught by Gary T. Leavens.
There are sections on:
Examples
Programming with Python
See python.org for documentation and tutorial information about Python.
Python numeric functions
- multiply.py, which returns the product of two numbers and test_multiply.py, its unit tests. Compare these with print_product.py and the tests in test_print_product.py.
- square.py and test_square.py. Compare these with print_square.py and the tests in test_print_square.py.
- circlearea.py and test_circlearea.py
- roomarea.py and test_roomarea.py
- energy.py and test_energy.py
- tvdiagonal.py and test_tvdiagonal.py
Python output (print) and Numerical Calculations
- hello.py
- height.py, which calculates the height of a ball thrown
- print_product.py and test_print_product.py, which calls print_procuct several times. Compare these with multiply.py and the unit tests test_multiply.
- print_square.py and test_print_square.py which calls print_square several times.
Python printing and procedures
The examples in the section mostly print song lyrics for repeatitive songs. Note that these have also been made into programs that print the song lyrics when run.
- weshallovercome.py
- wheelsonthebus.py
- bottlesofbeer.py, which only prints one verse of the song.
- run_pytest.py, which helps automate testing using pytest from within IDLE.
Python interaction
The following examples show how to combine input and output in a Python program.
- sum3.py, which reads 3 ints and prints their sum, and test_sum3.py, which uses inputs in sum31.in, sum32.in, and sum33.in, and expected outputs in sum31.expected, sum32.expected, and sum33.expected. The tests use the module testing_io.py.
- print_truth_tables.py, which reads a truth table operator name, and prints the corresponding truth table, and test_print_truth_tables.py, with test inputs print_truth_tables1.in and print_truth_tables2.in, and expected outputs in print_truth_tables1.expected and print_truth_tables2.expected. The tests use the module testing_io.py.
- name_polygon.py, which reads an int from stdin and prints the name of the corresponding regular polygon, and test_name_polygon.py, with test inputs name_polygon3.in, name_polygon4.in, name_polygon5.in, name_polygon6.in, name_polygon7.in, and name_polygon8.in, with expected outputs in name_polygon3.expected, name_polygon4.expected, name_polygon5.expected, name_polygon6.expected, name_polygon7.expected, and name_polygon8.expected. The tests use the module testing_io.py.
Python Booleans, Boolean Expressions, and Predicate Functions
The following are examples of predicates (Boolean-valued functions) in Python.
- both.py and test_both.py
- mymax.py and test_mymax.py
- positive.py and test_positive.py
- strictly_ascending.py and test_strictly_ascending.py
- divisible.py and test_divisible.py
- implies.py and test_implies.py
- notOrDM.py and test_notOrDM.py
- is_tautology.py and test_is_tautology.py
- bottlesofbeerTo1.py prints the entire "99 bottles of beer" song using recursion.
Python String Formatting
The following demonstrate the format method of strings in Python.
Higher-order Functions and Procedures in Python
- testing_io.py has functions to help unit testing of interactive I/O programs
- print_truth_table.py is a higher-order function that can print the truth table of a 2-arugument Boolean predicate. See tests in test_print_truth_table.py, with expected outputs in print_truth_table1.expected, print_truth_table2.expected, and print_truth_table3.expected
Following the Grammar with Python (Recursion and Recursive Data)
Most of the examples in this section are described in detail in the technical report, "Following the Grammar with Python" (CS-TR-17-01, February 2017).Grammars and Classes
- Cell.py a simple class, and tests in test_Cell.py.
- Stmt.py classes that correspond to the grammar of statements, and tests in test_Stmt.py.
Only Alternatives, No Recursion
- Temperature.py classes that correspond to the grammar for Temperatures, and tests in test_Temperature.py.
- isFreezing.py and test_isFreezing.py.
- Color.py classes that correspond to the grammar of colors, and tests in test_Color.py.
Only Recursion, No Alternatives
- ISeq.py classes that correspond to the grammar of infinite sequences, and tests in test_ISeq.py.
- iSeqMap.py and test_iSeqMap.py, which uses test_ISeq.py for some test data.
- anyNegative.py and test_anyNegative.py, which uses test_ISeq.py and iSeqMap.py for some test data.
Multiple Nonterminals
- Rectangle.py classes that correspond to the grammar of Rectangles and Points, and tests in test_Rectangle.py.
- moveUp.py and test_moveUp.py.
- doubleRect.py and test_doubleRect.py.
Combination of Different Grammar Types
Lisp Lists
- LispList.py classes that correspond to the grammar of Lisp lists (with Nil and Cons), and tests in test_LispList.py.
- lcopy.py and test_lcopy.py.
- incAll.py and test_incAll.py.
- lmap.py and test_lmap.py.
- sumList.py and test_sumList.py.
- take.py and test_take.py.
- extractNames.py and test_extractNames.py.
- deleteListing.py and test_deleteListing.py.
- insertBeforeAll.py and test_insertBeforeAll.py.
- insertBeforeFirst.py and test_insertBeforeFirst.py.
- multList.py and test_multList.py.
- lllt.py and test_lllt.py.
- allLessThan.py and test_allLessThan.py.
Binary Trees
- BinTree.py classes that correspond to the grammar of binary trees (with EmptyTree and Branch), and tests in test_BinTree.py.
- doubleTree.py and test_doubleTree.py.
- sumTree.py and test_sumTree.py.
- reduceTree.py and test_reduceTree.py.
Sales Data
- SalesData.py classes that correspond to the grammar of SalesData (with Store and Group), and tests in test_SalesData.py.
- normalizeSalesData.py and test_normalizeSalesData.py, which uses test_normalizeSalesDataTesting.py.
- normalizeSalesData_lmap.py, which uses lmap, and test_normalizeSalesData_lmap.py, which uses test_normalizeSalesDataTesting.py.
Tail Recursion
This is an extra credit topic. The following are examples of tail-recursive functions.
- reverse.py and test_reverse.py. Note that this file has both a LispList tail recursion and one written over Python lists.
- tr_sumList.py and test_tr_sumList.py.
Loops (while and for loops)
Most examples in this section include systematic developments with loop invariants and assertions. In assertions we use implies (see above) and allIntsIn (see below).
While Loops and break
- count_up_to.py.
- int_division.py and test_int_division.py.
- multPyList.py and test_multPyList.py, which itself uses multTesting.py.
- exp.py (which uses squaring to avoid some multiplications) and test_exp.py
For Loops and continue
The functions allIntsIn and someIntsIn act as quantifiers for assertions over ranges.
- product.py and test_product.py, which itself uses multTesting.py.
- sumReciprocals.py and test_sumReciprocals.py.
- allIntsIn.py and test_allIntsIn.py.
- someIntsIn.py and test_someIntsIn.py.
- mystrlen.py and its tests in test_mystrlen.py. Compare with mystrlen.c.
- is_substring.py and test_is_substring.py. Compare this with is_substring.c.
Programming with C
There is a draft standard for C available online at http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf. The C Library Reference Guide is also useful as a reference.
Input and Output with Variables
- The function helloname.c, with header file helloname.h, main program helloname_main.c, and tests in test_helloname.c, that use testing_io.h (with implementation in testing_io.c) and tap.h (with implementation in tap.c). Test inputs are in hellonameg.in, and hellonamev.in, with expected outputs in hellonameg.expected and hellonamev.expected.
- The function sumnums.c, with header file sumnums.h, main program sumnums_main.c, and tests in test_sumnums.c, that use testing_io.h (with implementation in testing_io.c) and tap.h (with implementation in tap.c). Test inputs are in sumnums12.in, and sumnums98.in, with expected outputs in sumnums12.expected and sumnums98.expected.
- The program divisionops.c demonstrates the use of printf and also the division operators in C and how they work on ints.
Testing for C programs
You can download our testing files for C programs from c-testing.zip. This zip file includes the following two modules.
We will be using libtap, by Jacob Gelbman, for testing of C programs. It can be obtained from https://github.com/zorgnax/libtap (by clicking on the green "clone or download" button), or you can simply use the provided files: tap.h and tap.c.
For testing of functions that return floating point numbers, the function approx from approx.c and its header file approx.h give a notion of approximate equality.
For testing whole programs that read and write stdin and stdout, we will make use of a custom C module, called testing_io. The module's header file is testing_io.h, and its implementation is in testing_io.c. To use it we require that you write a whole program as a function that takes no arguments and returns an int, and that the testing file be compiled with both testing_io.c and tap.c, which must reside in the same directory as the testing file.
The module quantifiers.c with header in quantifiers.h contains some helping functions (allIntsIn and someIntsIn) for testing. These act as universal and existential quantifiers over int ranges. See test_quantifiers.c.
The makefile (for use with the Unix command make) for the C programs in this directory is found in Makefile.
Booleans and Conditionals in C
- bitwise.c and a main program in bitwise_main.c that prints out results of working with the bitwise operators.
- max.c and its tests in test_max.c
- parse_answer.c and its tests in test_parse_answer.c
- odd_even.c which demonstrates mutual recursion and header file odd_even.h and tests in test_odd_even.c
Arrays and Loops in C
- addToEach.c and its tests in test_addToEach.c
- sum.c and its tests in test_sum.c
- mystrlen.c and its tests in test_mystrlen.c. Compare with mystrlen.py.
- is_substring.c, is_substring.h, and tests in test_is_substring.c. Compare with is_substring.py.
- downcase.c and its tests in test_downcase.c
- print_histogram.c and its tests in test_print_histogram.c. The tests use the testing_io module (in testing_io.h and testing_io.c) and have expected outputs in h1.expected and h2.expected. (Note that h1.in and h2.in are empty files.)
- init_array.c and its header file init_array.h, which are useful for initializing arrays of doubles from rules (expressed as functions from ints to doubles), and its testing code in test_init_array.c.
Pointers in C
- memory.c, which shows the equivalence of array indexing (subscripting) and pointer arithmetic.
- fixed_dangling.c, which shows how to use malloc to avoid dangling pointers. Compare this with the error example dangling.c
- swap.c and header file swap.h and testing code test_swap.c.
- multi_assign.c and header file multi_assign.h and testing code test_multi_assign.c.
- bubble_sort.c and header file bubble_sort.h and testing code test_bubble_sort.c. Note that bubble_sort.c uses swap.
- set_all_array.c and header file set_all_array.h and tests in test_set_all_array.c.
- reverse.c and header file reverse.h and tests in test_reverse.c.
Modularity and Information Hiding in C
- Header file stack.h and its implementation in stack.c. Testing (client) code in test_stack.c.
- Header file rational.h and its implementation in rational.c. Also an implementation done in class: rational_in_class.c. (See below for an implentation done using structs.) Testing (client) code in test_rational.c.
Structs (and pointers to structs) in C
- array_by_value_in_struct.c, whichh shows how arrays that are inside structs are copied during assignment and parameter passing.
- struct_assign.c demonstrates assignments to structs
- struct_rational.c and header file struct_rational.h. Testing (client) code in test_struct_rational.c. (See above for an implementation done using arrays.)
- measure.c and header file measure.h. Testing (client) code in test_measure.c.
- vector.c and header file vector.h. Testing (client) code in test_vector.c.
- list.c and header file list.h. Testing (client) code in test_list.c.
- Client code for list.h, with a header file list_client_code.h that specifies 4 functions. Two implementations of this specification are: list_client_code_recursive.c and list_client_code_iterative.c. Testing is in test_list_client_code.c.
Last modified $Date: 2019/04/10 18:46:04 $.