You are on page 1of 47

This booklet is designed to help advisors and students prepare for the American Computer

Science League contests. Do not become alarmed if the material looks difficult! Although the
topics cover basic areas in computer science, few secondary schools normally teach them (at
least not until they’ve been involved in ACSL!). However, these categories are all manageable
at the junior and senior high school levels, as experience from past years has proven. Each
contest contains at most one or two new categories, and most advisors find that about one hour is
sufficient to introduce students to each new category. Some topics late in the year are based on
topics given earlier in the year. For example, Digital Electronics is based on Boolean Algebra,
which in turn, is derived from the well-known area of Set Theory. The information contained in
this booklet supercedes all previous years’.

The purpose of this booklet is not to serve as a textbook; rather, it is intended to be used as a
reference for the types of problems which are likely to be asked, to give references for further
reading, and to present some actual problems which were used in the past.
______________________________________________________________________________

TABLE OF CONTENTS

League Origanization

General Contest Procedures

How ACSL Works

Category Descriptions
Assembly Language Programming
Bit-String Flicking
Boolean Algebra
Computer Number Systems
Data Structures
Digital Electronics
Graph Theory
LISP Programming
Prefix/Infix/Postfix Notation
Recursive Functions
Regular Expressions and FSA’s
What Does This Program Do?

Sample Programming Problems


Junior Division
Intermediate Division
Senior Division

______________________________________________________________________________

We’d like to acknowledge and thank Steve Conrad and Dan Flager for allowing us to adapt our Rules and
Procedures from their copyrighted format
League Organization material at their respective levels. If a
school is enrolled in more than one
Membership: Any secondary school division, its students may compete in
that agrees to abide by these rules shall any division – but not in more than one –
be admitted to the American Computer for each contest. Scores are not
Science League (ACSL) upon transferable between divisions.
application and receipt of the League An unlimited number of students may
dues. There are four divisions: Senior, compete at each school in each contest.
Junior, Intermediate, and Classroom. For each contest, a school’s score in
Participation and Team Structure: each division is the sum of the scores of
The Senior Division is geared to senior its three or five highest scoring students
high schools; the Junior Division to in that division.
junior high schools; the Intermediate Contest Dates: Except in unusual
Division for advanced junior high circumstances, all contests must be held
schools and for senior high schools that during the scheduled week. In the event
are just starting computer courses; the of unscheduled school closings, special
Classroom Division is for all students administrative functions, testing days,
but does not have a programming severely inclement weather, etc.,
problem. permission is granted to conduct the
The Senior Division is geared to those contest late. Do not call the League
senior high schools that have well requesting an extension; administer the
established programs in computers. We contest as soon as possible, and include a
strongly suggest that schools do not written explanation with the results. If it
register for the Senior Division during is known ahead of time that a contest
their first year of ACSL participation. In cannot be held during the contest week
addition, any student taking an (e.g. the official date falls during a
Advanced Placement course in school vacation), administer it before the
Computer Science should be registered scheduled week.
on a team in the Senior Division, except Awards: At the completion of the school
as noted above. year, awards will be presented to
The Intermediate Division is geared to outstanding schools and individuals at
advanced junior high schools and for regional levels in all divisions. In
senior high schools that are just starting addition, each team will receive an
computer programs. This division is for award to be given to a top student on the
students taking a first course in computer team, per the advisor’s discretion.
science and for non-AP schools that are Contest Problems: Each contest will be
competing in the League for the first administered in two parts: Short
time. Problems and a Programming Problem.
The Junior Division is geared to junior If for any reason a problem must be
highs and middle schools that are just dropped (as decided by the Appeals
starting computer courses. Students Judge), no replacement will be made and
must be in the 9th grade or below to no credit will be given for any answer to
participate in this division. a problem thus discarded
Schools are encouraged to enroll in Contest Materials Package: The
more than one division, so that both Contest #1 materials should be received
advanced and novice students will find by each school by the end of November.
Each contest contains the following
pages: Short Problems, Short Problems
Solutions, Programming Problem, and
Test Data and Solutions to the
Programming Problem. Faculty advisors
are expected to make as many copies of
the contest sheets as required, so that
each participant has his/her own copy.
The advisor is responsible for the clarity
of reproduced copies.
Study Aids: In addition to this booklet,
previous years’ contest materials are
available for a nominal fee (see flyer).
Communications: In addition to the
Contest Materials Package, all schools
will receive a Newsletter after each
contest containing the contest results,
general announcements, and items of
interest. Questions, comments,
interesting solutions to any of the
problems, etc. are welcomed by the
League at all times, and often will be
included in the Newsletters. Student and
advisor solutions to the Programming
Problem must include the programmer’s
name and school in a comment at the
start of the program. All programs
submitted become property of ACSL.
Role of the Advisor: Since many of the
topics are new to most students, the
advisor will usually take an active role in
presenting the material. Advisors should
not look at, or reproduce, the actual
contest sheets until just before the
contest. This will eliminate the
possibility of inadvertently “teaching the
test.” Advisors must not discuss any
aspect of the programming problem with
students.
General Contest Procedures nor anyone else may interpret any problem
to any student during the contest. Each
After each contest the advisor will complete student must work independently.
a form for each team. The TEAM SCORE Materials Allowed: The contestant may use
is the sum of the top 3 or 5 students on the only plain paper and pencil or pen. No
team. The SCORE column is the total score graph paper, compasses, straightedges,
for a student on that contest, an integer from rulers, books, calculators, computers or
0 to 10, based upon the number of points other devices are allowed.
from the short answer questions and the Implementation: Each student will be given
programming problem. The CUM column a copy of the problem sheet. Warnings that
is the total of the scores from all previous “ten minutes remain,” that “five minutes
contests for that student. Advisors must remain,” and that “one minute remains,”
maintain the CUM data from contest to should be made to all contestants at the
contest. The GRADE column is the school appropriate times. At the conclusion of the
year of the student, 9 - 12. Short Problems section, the proctor should
Appeals Procedure: Appeals will be collect all contest sheets.
awarded because of a disallowed exactly Marking the Answers: At the conclusion of
equivalent answer, an incorrect official the Short Problems section, the proctor
answer, or a correct answer to a correct should, after collecting the contest papers,
alternative interpretation of a problem. refer to the Solutions to the Short Problems
Written appeals must be sent to the League and award one point for each problem
along with the report form. A complete correctly solved. Answers submitted for
listing and execution of the appellant’s each problem must appear in the appropriate
program must accompany any appeals space in the answer column. Answers
involving the Programming Problem. exactly equivalent to the official answer(s)
Notification of the decision of the Appeals should be given credit. Incomplete answers
Judge will be made in the following should receive no credit. There is no partial
Newsletter. Decisions of the Appeals Judge credit.
will be final. Advisors should retain all Procedures: Programming Problems
student answer sheets until the Newsletter is Format: Each contestant will be given a
received. If any appeals have been granted copy of the Programming Problem, to be
(as reported in a newsletter), advisors should solved at his/her convenience, during the
submit the names of any of their students next 72 hours. (Students may be given the
who should receive credit for the problem problem the Friday before the contest week,
appealed on the score report form for the in which case they must complete the
new contest. program by Monday of the contest week.)
Procedures: Short Problems Each contestant must work independently.
Format: This section may be held at any Materials Allowed: All reference manuals,
time during the contest week convenient to textbooks, and inanimate sources may be
the school. It consists of five problems from consulted, provided that no computer
designated topics covering basic concepts in program or partial program listings are
computer science. Students are given 30 referenced by the contestant. Computational
minutes in which to solve the problems. devices, including calculators and
The Classroom Division is given 50 home/personal computers may be used by a
minutes. contestant. Programs may be written in any
Proctoring: This section must be actively computer language.
proctored at all times. Neither the proctor
Implementation: Prior to the completion of
the 72 hours, the student will provide the
advisor with access to the completed
program. The program may be done at
home, and submitted on a diskette or may be
done using the school’s computer.
Marking the Programs: When all
contestants have completed their programs,
the proctor should refer to the Test Data and
Solutions to Programming Problem and
execute each student’s program on the test
data exactly ONCE. The only reason to
restart a program is to correct bad data entry.
Each contestant will be awarded one point
for each answer to the test data which
matches the correct output. For some
problems, a tolerance will be allowed to
account for different machine round off
errors. This will be noted on the problem
sheet, if applicable. Answers must appear in
order unless clearly labeled.
Students are advised to print out each
answer as it is calculated, rather than
printing all solutions at the conclusion of the
run. This strategy ensures that a student will
not score a zero if his/her program aborts
near the end but before any answers are
printed. Students must know their system
and be aware of possible round-off errors,
the maximum value which can be stored in
an integer or floating point number,
maximum number of strings which can
exist, etc.
HOW ACSL WORKS

ONCE YOU REGISTER

1. ACSL will send the team advisor an ACSL Category Description Booklet, sent on a
CD. The booklet contains the rules for each category and some sample problems and
solutions. Team advisors will use the booklet and other sample problems to prepare
students for the short answer test.

2. Prior to each contest, ACSL will send the team advisor an e-mail file that contains the
short answer questions, the programming problem and solutions for both.

3. The team advisor will make copies of the contest questions. Note that for one
registration fee every student at a school may take the tests. However, if a school
registers for more than one division, a student may only take one of the tests.

4. The team advisor will administer the short answer test and score the results. One point
is awarded for each answer that matches the ACSL solution. The time limit is 30 minutes
for the Senior, Intermediate and Junior tests. The time limit is 50 minutes for the
Classroom Division test. The only materials allowed for the short answer tests are plain
paper and a writing implement. Calculators are not allowed.

5. The advisor will give each student a copy of the programming problem. Students have
up to 72 hours to submit a programming solution to the advisor. Students must work
alone in solving the programming problem. The advisor will run the ACSL test data on
each student's programming solution. The program must accept all data in one run of the
program. If the program stops for any reason, the program may not be restarted to enter
more data. The advisor will award one point for each program output that matches
ACSL's test output.

SCORING THE RESULTS

1. A student’s score is the sum of his/her scores on the short answer test and the
programming problem. A maximum score for any one student is 10 points (5 + 5).

2. The team score is the sum of the best 3 or 5 student scores for that contest.

3. The advisor will complete the ACSL Scoresheet and submit it by e-mail to ACSL. If a
student score of 10 is reported, the advisor must also attach the programming file for that
student.
AFTER EACH CONTEST

1. ACSL will send each advisor a newsletter containing the team standings and a listing
of students with high scores.

2. ACSL will post results on its web site.

3. ACSL will select sample student programs from those submitted and post them on its
web site. Programs using several different languages will be posted.

ALL-STAR CONTEST AND PRIZES

1. Based upon cumulative results after the four contests, ACSL will invite the top scoring
teams in each division to complete at its All-Star Contest. At the All-Star Contest, prizes
are awarded to top scoring teams and to top scoring students.

2. Based upon cumulative results after the four contests, ACSL will award plaques to top
scoring students and teams based upon previously announced geographic regions. In the
case of ties, duplicate awards will be presented.
Assembly Language Programming

Programs written in high-level languages such as BASIC, Ada and Pascal are usually
converted by compilers into assembly language (which in turn is translated into machine
language programs - sequences of 1’s and 0’s – by an assembler). Even today, with
very good quality compilers available, there is the need for programmers to understand
assembly language. First, it provides programmers with a better understanding of the
compiler and what are its constraints. Second, on occasion, programmers find
themselves needing to program directly in assembly language in order to meet constraints
in execution speed or space (for example, writing games for micros and arcade
machines). A good programmer can typically tweek and tune an assembly language
program to run significantly better than the code generated by even the best compilers.

The ASCL Assembly Language, AAL, runs on an ACSL computer that has an unlimited
amount of memory. Each “word” of memory contains a decimal integer in the
range –999,999 through 999,999. Additions, subtractions, and multiplications are
performed modulo 1,000,000. For example, 999,998 plus 7 equals 5. Division is
performed in the conventional sense, but the fractional part of the answer is dropped – not
rounded off. For example, 14 divided by 5 is 2.

Execution starts at the first line of the program and continues sequentially, except for
“branch” instructions, until the “end” instruction is encountered. The result of each
operation is stored in a special word of memory, called the “accumulator” (ACC). Each
line of an assembly language program has the following fields (lower-case italics
indicates optional components):

label OPCODE LOC comments

The label is a character string beginning in the first column. Valid OPCODE’s are listed
in the chart below. The LOC field is either a reference to a label or “immediate data”.
For example, “LOAD A” would put the contents referenced by the label “A” into the
ACC; “LOAD =123” would store the value 123 in the ACC. Only those instructions that
do not modify the LOC field can use the “immediate data” format. In the following
chart, they are indicated by an asterisk in the first column.

OPCODE ACTION
* LOAD Contents of LOC are placed in the ACC. LOC is unchanged.
STORE Contents of ACC are placed in the LOC. ACC is unchanged.
* ADD Contents of LOC are added to the contents of the ACC. The sum is
stored in the ACC. LOC is unchanged. Addition is modulo 1,000,000.
* SUB Contents of LOC are subtracted from the contents of the ACC. The
difference is stored in the ACC. LOC is unchanged. Subtraction is
modulo 1,000,000.
* MULT The contents of LOC are multiplied by the contents of the ACC. The
product is stored in the ACC. LOC is unchanged. Multiplication is
modulo 1,000,000.
* DIV Contents of LOC are divided into the contents of the ACC. The
signed integer part of the quotient is stored in the ACC. LOC is
unchanged.
BE Branch to instruction labeled with LOC if ACC=0.
BG Branch to instruction labeled with LOC if ACC>0.
BL Branch to instruction labeled with LOC if ACC<0.
BU Branch unconditionally to instruction labeled with LOC.
END Program terminates. LOC field is ignored.
READ Read a signed integer (modulo 1,000,000) into LOC.
* PRINT Print the contents of LOC.
DC The value of the memory word defined by the LABEL field is defined
to contain the specified constant. The LABEL field is mandatory for
this opcode. The ACC is not modified.

References

We chose to define our own assembly language rather than use a “real” one in order to
eliminate the many sticky details associated with real languages. The basic concepts of
AAL are common to all assembly languages. A reference manual for any real assembly
language should prove helpful to prepare for this category. Also, the following article
presents many of the concepts of assembly language programming in a very readable and
fun way:

Dewdney, A.K. “Computer Recreations,” in Scientific American, May 1984, pp. 14-22.

Sample Problems

After the following program is executed,


what value is in location TEMP? The ACC takes on values -2, -6, 2, -1 and -9
in that order. The last value, -9, is stored in
TEMP DC 0 TEMP.
A DC 8
B DC -2
C DC 3
LOAD B
MULT C
ADD A
DIV B
SUB A
STORE TEMP
After the following program is executed, what is the
This program finds the Nth odd integer. The
final value of A ? The data for the program is 3. following table gives the values of N and A
through execution:
READ N
A DC 1 N A
START LOAD N
3 1
SUB =1 2 3
BE RSLT 1 5
STORE N 0 5
LOAD A
ADD =2 Thus, the final value of A is 5.
STORE A
BU START
RSLT END

If the following program has an input value of N,


what is the final value of X which is computed? This program loops between labels TOP and
Express X as a mathematical expression in DONE for A times. A has an initial value of X,
terms of N. and subsequent values of A-1, A-2, …, 1. Each
time through the loop, X is multiplied by the
READ X current value of A. Thus, X=A*A-1*…*1 or
X=A!. Since the initial value of A is the
LOAD X number input (i.e., N), X=N!.
TOP SUB =1
BE DONE
STORE A
MULT X
STORE X
LOAD A
BU TOP
DONE END
Bit-String Flicking

Bit strings (binary numbers) are frequently manipulated using logical operators, shifts, and
circulates. Mastering this topic is essential for systems programming, programming in assembly
language and optimizing code.
A typical use of bit string is to maintain a set of flags. Suppose that associated with a data
structure in a program are 10 “options”, each of which can be either “on” or “off”. One could
maintain this information using an array of size 10, or one could use a single variable (if it is
internally stored using at least 10 bits, which is usually the case) and use 10 bits to record each
option. In addition to saving space - a significant consideration if enough data structures and
options are involved - the program is often cleaner if a single variable is involved rather than an
array. Incidentally, bit strings are usually used to maintain Pascal “sets”.
The logical operators which will be used are: AND(&), OR(|), XOR(⊕) and NOT(~). These
operators examine the operand(s) on a bit by bit basis. For example, (10110 AND 01111) has a
value of 00110. The AND, OR and XOR are binary operators; the NOT is a unary operator.
The category description of Boolean Algebra/Digital Electronics has a complete description of
each logical function. The following chart summarizes this information:

p q p AND q p OR q p XOR q NOT p


0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0

Logical shifts (LSHIFT-x and RSHIFT-x) “ripple” the bit string x positions in the indicated
direction. Bits shifted out are lost; zeros are shifted in at the other end. Circulates (RCIRC-x
and LCIRC-x) “ripple” the bit string x positions in the specified direction. As each bit is shifted
out one end, it is shifted in at the other end. Thus, for this category, the size of a bit string is
fixed; it cannot be lengthened or shortened by any of the logical operators, shifts or circulates.
If any bit strings are initially of different lengths, all shorter ones are padded with zeros in the
left bits until all strings are of the same length. The following table gives some examples of
these operations:

p LSHIFT-2 p RSHIFT-2 p LCIRC-3 p RCIRC-3 p


01101 10100 00011 01011 10101
10 00 00 01 01
1110 1000 0011 0111 1101
1011011 1101100 0010110 1011101 0111011

The order of precedence (from highest to lowest) is: NOT; SHIFT and CIRC; AND; XOR; and
finally, OR. Operators with equal precedence are evaluated left to right; all operators bind from
left to right.

References

This topic is an art that is developed through experience. Most of the references for Boolean
Algebra and Assembly Language cover this topic in passing.
Sample Problems

Evaluate the following expression: The expression evaluates as follows:

(RSHIFT-1 (LCIRC-4 (RCIRC-2 01101))) (RSHIFT-1 (LCIRC-4 (RCIRC-2 01101)))


= (RSHIFT-1 (LCIRC-4 01011))
= (RSHIFT-1 10101)
= 01010

List all possible values of x (5 bits long) that solve Since x is a string 5 bits long, represent if by
the following equation. abcde. (RCIRC-3 x) is cdeab which, when
ANDed with 11011 gives cd0ab. This is XORed
(LSHIFT-1 (10110 XOR to 10110 to yield Cd1Ab (the capital letter is the
(RCIRC-3 x) AND 11011)) = 01100 NOT of its lower case).

Now, if (LSHIFT-1 Cd1Ab) has a value of 01100,


we must have: d=0, A=1 (hence a=0), b=0. Thus,
the solution must be in the form 00*0*, where * is
an “I-don’t-care”. The four possible values of x
are: 00000, 00001, 00100 and 00101.

Evaluate the following expression: (LC-3 (10110 XOR 11010) AND (RS-1 10111))
= (LCIRC-3 01100 AND 01011)
((LCIRC-3 (10110 XOR 11010)) = (00011 AND 01011)
AND (RSHIFT-1 10111)) = 00011

Evaluate the following expression: The rules of hierarchy must be followed carefully:
A = (RCIRC-14 (LCIRC-23 01101))
((RCIRC-14 (LCIRC-23 01101)) | = (LCIRC-9 01101)
(LSHIFT-1 10011) & (RSHIFT-2 10111)) = (LCIRC-4 01101)
= 10110
B = (LSHIFT-1 10011) = 00110
C = (RSHIFT-2 10111) = 00101
D = B & C = 00100
final answer = A | D = 10110

Find all possible values of x for which the Let x=ABCDE and (NOT x)=abcde. Substitute
following expression has a value of 10100. into the expression:

(LSHIFT-2 (RCIRC-3 (NOT x))) (RCIRC-3 abcde) = cdeab


and
(LSHIFT-2 cdeab) = eab00.

Since eab00 = 10100 the value of (NOT x) is


01**1 (where the * indicates an “I-don’t-care”),
and the value of x is 10**0: 10000, 10010, 10100
and 10110.
Boolean Algebra

Opening up a computer, a terminal, or practically any other “computerized” item reveals


boards containing little black rectangles. These little black rectangles are the integrated
circuit (IC) chips that perform the logic of the computer. Each IC can be represented by a
Boolean Algebra equation, and vice versa. The representation that is used depends on the
context. Boolean Algebra provides a convenient representation and notation for
simplifying and solving equations. Digital Electronics provides a layout that can then be
implemented with IC chips.

The operators used in these categories are listed in the description of the Digital
Electronics category. The logic gates are usually used in Digital Electronics questions;
the algebraic equations, symbols and truth tables, in Boolean Algebra. Of course, it is
crucial to be able to translate between a digital electronics circuit and its Boolean Algebra
notation. The order of operator precedence is NOT; AND and NAND; XOR and
EQUIV; OR and NOR. Binary operators with the same level of precedence are evaluated
from left to right.

References

Handbooks for building circuits using IC are available in most electronics and home
computer stores. These contain many example circuits. A formal description of the
material can be found in textbooks covering discrete mathematics, finite mathematics and
compute hardware and architecture. Example texts include:

Lipschutz, Seymour. Essential Computer Mathematics – Shaum’s Outline Series,


McGraw Hill (1982), Chapters 6, 7 and 8.

Mano, M. Morris. Digital Logic and Computer Design, Prentice-Hall (1979).

Preparata, Franco P. Introduction to Computer Engineering, Harper & Row (1985).

Boolean Algebra Identities

1. A+ B = B + A A* B = B * A (Communicative Property)
2. A + ( B + C ) = ( A + B ) + C A * ( B * C ) = ( A * B ) * C (Associative Property)
3. A * (B + C) = A * B + A * C (Distributive Property)
4. A+ B = A*B (DeMorgan’s Law)
5. A* B = A + B (DeMorgan’s Law)
6. A+0 = A A*0 = 0
7. A +1 = 1 A *1 = A
8. A+ A =1 A* A = 0
9. A+ A= A A* A = A
10. A=A

11. A+ A*B = A+ B
12. ( A + B) * ( A + C ) = A + B * C
13. ( A + B) * (C + D) = A * C + A * D + B * C + B * D
14. A * ( A + B) = A
15. A ⊕ B = A* B + A * B
16. A⊕ B = A ⊕ B = A⊕ B
Sample Programs

Simplify the following expression as much as Using various elementary identities, the expression
possible: simplifies as follows:

A( A + B ) + BA A( A + B ) + BA = A( A + B ) * B * A
= A( A + B ) * ( B + A)
= ( A + AB )( B + A)
= A(1 + B )( B + A)
= A(1)( B + A)
= A( B + A)
= AB + AA
= AB + A
= A( B + 1)
= A(1) = A

Find all ordered pairs (A, B) that make the


following expression TRUE.
A + B + A * B = ( A + B )( AB )
= ( A + B )( A + B )
A+ B + A*B
= AA + AB + BA + BB
= A + A( B + B ) + 0
= A + A(1) = A + A = A

This yields the solutions (1, 0) and (1, 1). This


problem, like most Boolean Algebra problems,
could also be solved by drawing a truth table with
the following seven column headings: A, B, A+B,
A + B, AB, A + B + AB, A + B + AB .

Simplify the following expression to one that uses The evaluation is as follows:
only two operators.
( A + B ∗ C ) + ( A * (B + C ) )
= ( A * B * C ) + ( A * B * C)
( A + B ∗ C ) + ( A * (B + C ) )
= A * (B * C + B * C)
= A * (B ⊕ C)
To realize this equation as a circuit, two gates are
used: an XOR (input is B and C) and an AND
(inputs are A and the output of the XOR gate).
The output of the AND is the output of the circuit.
Computer Number Systems

All computers – from large mainframes to hand-held micros – ultimately can do one thing: detect whether an
electrical signal is “on” or “off”. Computer programs in BASIC and Pascal are converted by various pieces of
systems software into sequences of bits (Binary digITs) which correspond to sequences of on/off (equivalently
TRUE/FALSE or 1/0) signals. Proficiency in the binary number system is essential to understanding how a
computer works.

Since binary numbers representing moderate values quickly become rather lengthy, bases eight (octal) and
sixteen (hexadecimal) are frequently used as short-hand. Octal numbers group binary numbers in bunches of 3
digits and convert the triplet to a single digit between 0 and 7, inclusive. For example, 10010101102 = 001 001
010 1102 = 11268. Hexadecimal numbers group binary numbers by fours, and convert the quadruplet to a single
digit in the range 0, 1, 2 …, 9, A, B, C, D, E, F. For example, 101101101001012 = 0010 1101 1010 01012 =
2DA516.

References

Many pre-Algebra textbooks cover bases other than 10. From the computer science point of view, most books
covering Assembly Language also cover binary, octal and hex number systems. The texts cited for the Boolean
Algebra category cover computer number systems.

Sample Problems

Solve for X. One method of solution is to convert 36768 into base


10, and then convert that number into base 16 to
X 16 = 36768 yield the value of X.

An easier solution, less prone to arithmetic mistakes,


is to convert from octal (base 8) to hexadecimal (base
16) through the binary (base 2) representation of the
number:

36768 = 011 110 111 1108


= 0111 1011 11108
= 7BE16

Solve for X. The rightmost digit becomes F, because 1D-E=F.


Next, D-C=1 (the E becomes a D because we had to
X 16 = FEED16 – 6ACE16 borrow from it to do the units’ subtraction), E-A=4,
and then F-6=9. Combining these results of each
column, we get a final answer of 941F16.
In the ACSL computer, each “word” of memory E 1 B 7 D = 1110 0001 1011 0111 1101
contains 20 bits representing 3 pieces of information. = 1110 00 01 1011 0111 1 101
The most significant 6 bits represent Field A; the next
11 bits, Field B; and the last 3 bits represent Field C. Field B = 01 1011 0111 1
For example, the 20 bits comprising the “word” = 011 0110 1111
1814916 has fields with values of 616 , 2916 and 116 . = 3 6 F16
What is Field B in E1B7D16 ? (Express your answer as
a base 16 number.)
Data Structures

At the heart of virtually every computer program are its algorithms and its data structures. It is hard to separate
these two items, for data structures are meaningless without algorithms to create and manipulate them, and
algorithms are usually trivial unless there are data structures on which to operate.

This category concentrates on four of the most basic structures: stacks, queues, binary search trees, and priority
queries. Questions will cover these data structures and implicit algorithms, not on implementation language
details. A stack is usually used to save information that will need to be processed later. Items are processed in
a “last-in, first-out” (LIFO) order. A queue is usually used to process items in the order in which requests are
generated; a new item is not processed until all items currently on the queue are processed. This is also known
as “first-in, first-out” (FIFO) order. A binary search tree is used when one is storing a set of items and needs to
be able to efficiently process the operations of insertion, deletion and query (i.e. find out if a particular item is
part of the set and if not, which item in the set is close to the item in question). A priority queue is used like a
binary search tree, except one cannot delete an arbitrary item, nor can one make an arbitrary query. One can
only delete the smallest element of the set, and can only find out what is the smallest element of the set.

A stack supports two operations: PUSH and POP. A command of the form “PUSH(A)” puts the key A at the
top of the stack; the command “POP (X)” removes the top item from the stack and stores its value into variable
X. If the stack was empty (because nothing had ever been pushed on it, or if all elements has been popped off
of it), then X is given the special value of NIL. An analogy to this is a stack of books on a desk: a new book is
placed on the top of the stack (pushed) and a book is removed from the top also (popped). Some textbooks call
this data structure a “push-down stack” or a “LIFO stack”.

Queues operate just like stacks, except items are removed from the bottom instead of the top. A good physical
analogy of this is the way a train conductor or newspaper boy uses a coin machine to give change: new coins
are added to the tops of the piles, and change is given from the bottom of each. Some textbooks refer to this
data structure as a “FIFO stack”.

Consider the following sequence of 14 operations:


PUSH(A), PUSH(M), PUSH(E), POP(X), PUSH(R),
POP(X), PUSH(I), POP(X), POP(X), POP(X),
POP(X), PUSH(C), PUSH(A), PUSH(N)

If these operations are applied to a stack, then the values of the pops are: E, R, I, M, A and NIL. After all of the
operations, there are three items still on the stack: the N is at the top (it will be the next to be popped, if nothing
else is pushed before the pop command), and C is at the bottom. If, instead of using a stack we used a queue,
then the values popped would be: A, M, E, R, I and NIL. There would be three items still on the queue: N at the
top and C on the bottom. Since items are removed from the bottom of a queue, C would be the next item to be
popped regardless of any additional pushes.

A binary search tree is composed of nodes having three parts: information (or a key), a pointer to a left child,
and a pointer to a right child. It has the property that the key at every node is always greater than or equal to the
key of its left child, and less than the key of its right child. The following tree is built from the keys A, M, E, R,
I, C, A, N in that order:
A

M
A

E R

C I N

The root of the resulting tree is the node containing the key A; note that duplicate keys are inserted into the tree
as if they were less than their equal key. The tree has a depth (sometimes called height) of 3 because the
deepest node is 3 nodes below the root. Nodes with no children are called leaf nodes; there are four of them in
the tree: A, C, I and N. An external node is the name given to a place where a new node could be attached to
the tree. In the final tree above, there are 9 external nodes; these are not drawn. The tree has an internal path
length of 15: the sum of the depths of all nodes. It has an external path length of 31: the sum of the depths of
all external nodes. To insert the N (the last key inserted), 3 comparisons were needed: against the root A, the M
and the R.

To perform an “inorder” traversal of the tree, recursively traverse the tree by first visiting the left child, then the
root, then the right child. In the tree above, the nodes are visited in the following order: A, A, C, E, I, M, N and
R. A “preorder” travel (root, left, right) visits in the following order: A, A, M, E, C, I, R and N. A “postorder”
traversal (left, right, root) is: A, C, I, E, N, R, M, A. Inorder traversals are typically used to list the contents of
the tree in order.

Binary search trees can support the operations: insert, delete and search. Moreover, it handles the operations
efficiently: in a tree with, say, 1 million items, one can search for a particular value in about log21000000≈ 20
steps. Items can be inserted or deleted in about as many steps, too. However, binary search trees can become
unbalanced, if the keys being inserted are not pretty random. For example, consider the binary search tree
resulting from inserting the keys A, E, I, O, U, Y. Sophisticated techniques are available to maintain balanced
trees. Binary search trees are “dynamic” data structures: they can support an unlimited number of operations,
and in any order.

To search for a node in a binary tree, the following algorithm (in pseudo-code) is used:
p = root
found = FALSE
loop while (p ≠ NIL) and (not found)
if (x<p’s key) then p = p’s left child
else if (x>p’s key) then p = p’s right child
else found = TRUE
repeat
Deleting from a binary search tree is a bit more complicated. The algorithm we’ll use is as follows:

p = node to delete
f = father of p
if (p has no children) then
delete p
else if (p has one child) then
make p’s child become f’s child
delete p
else (p has two children)
l = p’s left child (it might also have children)
r = p’s right child (it might also have children)
make l become f’s child instead of p
stick r onto the l tree
delete p
end

The following three diagrams illustrate the algorithm using the tree above. At the left, we delete I (no children);
in the middle, we delete the R (one child); and at the right, we delete the M (two children).

A A A

A E
M M
A A

E R E N C I

R
C N C I

A priority queue is quite similar to a binary search tree, but one can only delete the smallest item and “search”
for the smallest. These operations can be done in a guaranteed time proportional to the log of the number of
items. One popular way to implement a priority queue is using a “heap” data structure. A heap uses a binary
tree (that is , a tree with two children) and maintains the following two properties: every node is larger than its
two children (nothing is said about the relative magnitude of the two children), and the resulting tree contains
no “holes”. That is, all levels of the tree are completely filled, except the bottom level, which is filled in from
the left to the right.

You may want to stop for a moment to think about how you might make an efficient implementation of a
priority queue.

The algorithm for insertion is not too difficult: put the new node at the bottom of the tree and then go up the
tree, making exchanges with its parent, until the tree is valid. Consider inserting C into the following heap that
has been built by inserting A, M, E, R, I, C, A, N:
A
A

C A
I A

N M E C I M E C

R
R N

The smallest value is always the root. To delete it (and one can only delete the smallest value), one replaces it
with the bottom-most and right-most element, and then walks down the tree making exchanges with the child in
order to insure that the tree is valid. The following pseudo-code formalizes this notion:

b = bottom-most and right-most element


p = root of tree
p’s key = b’s key
delete b
loop while (p is larger than either child)
exchange p with smaller child
p = smaller child
repeat

BUILDING A HEAP FROM A, M, E, R, I, C, A, N:

A A
A A

M E
M E
M

A A
A I A

I E
I A
I C
N M E C
R M
R M E C
R M E
R

References
Amberg, Wayne. Data Structures from Arrays to Priority Queues, Wadsworth (1985).

Bentley, Jon. “Thanks, Heaps” in Programming Pearls, Communications of the ACM, Vol . 28, No. 3, March
1985.

Sedgewick, Robert. Algorithms, Addison-Wesley (1983), Chapters 11 and 14.

Wirth, Niklaus. “Data Structures and Algorithms” in Scientific American, September 1984, pp. 60-79.
Sample Problems

The statements push(p, q) and pop(p, q) handle two


parallel stacks. The push puts p on the top of one The first pop puts a=12 and b=20; the second pop set
stack and the q on the top of the second stack. The a=35 and b=3. The push(10, 23) would cause the next
pop command does a pop of the first stack and puts the pop to set a=10 and b=23. The wording of the
value into p, and a pop of the second stack and puts problem indicates that a is the “first” stack and b the
the value into q. For example, “second”.
push(4, 6)
push (2, 5)
pop(a, b)
pop(c, d)
would result in a=2, b=5, c=4, d=6.
Consider the following operations on an initially
empty stack:
push(40, 10)
push(35, 3)
push(12, 20)
pop(a, b)
pop(a, b)
push(10, 23)

What would be the next item removed from the “first”


stack?

Which of the following binary trees are valid binary Be careful! A binary tree is a tree where each node
search trees? (Empty nodes are not drawn.) has at most 2 children. A binary search tree is a binary
tree with the additional property that the letter of each
A B node is greater than the value of its left child, and less
A
J than the value of its right child. The valid trees are:
(a), (b), and (d).
C S
B

S R T

C D E
B A S

R W Z
A

O F M Q

W T

N M
If one traversed the following tree in preorder (visit Answer: A B D E F H I C G
the root, and then each of the subtrees from left to A common mistake is not to recursively visit all nodes
right), in what order would nodes be visited? Be sure in each subtree.
your answer is neat and clear!

C
B

D E F G

H I
Digital Electronics

See Boolean Algebra for a description of the category as well as references.

NAME GRAPHICAL ALGEBRAIC EQN TRUTH TABLE


SYMBOL
A X
A X
BUFFER X=A 0 0
1 1

A X A X
NOT X= A 0 1
1 0

A A B X
AND X X = AB or A*B 0 0 0
0 1 0
B
1 0 0
1 1 1
A B X
NAND A X = AB or A * B 0 0 1
X 0 1 1
B
1 0 1
1 1 0
A B X
OR A X = A+B 0 0 0
X 0 1 1
B
1 0 1
1 1 1
A A B X
NOR X X = A+ B 0 0 1
0 1 0
B
1 0 0
1 1 0
A B X
EXCLUSIVE-OR A X = A⊕B 0 0 0
X
(XOR) B 0 1 1
1 0 1
1 1 0
A A B X
EQUIVALENCE X X = A⊕ B 0 0 1
(XNOR) B 0 1 0
1 0 0
1 1 1
Sample Problems

Find all ordered 4-tuples (A, B, C, D), which make the following circuit FALSE:

The circuit translates to the following Boolean expression:

(C + D + B ) ⊕ ( A B ) ⊕ (C + D )

The following table has the following headings: H1 is (C + D ) , H2 is H1+ B , H3 is A B, H4 is H2⊕H3 and H5
is H4⊕H1, the final expression.

A B C D H1 H2 H3 H4 H5
0 0 0 0 1 1 0 1 0
0 0 0 1 0 1 0 1 1
0 0 1 0 0 1 0 1 1
0 0 1 1 0 1 0 1 1
0 1 0 0 1 1 1 0 1
0 1 0 1 0 0 1 1 1
0 1 1 0 0 0 1 1 1
0 1 1 1 0 0 1 1 1
1 0 0 0 1 1 0 1 0
1 0 0 1 0 1 0 1 1
1 0 1 0 0 1 0 1 1
1 0 1 1 0 1 0 1 1
1 1 0 0 1 1 0 1 0
1 1 0 1 0 0 0 0 0
1 1 1 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0

Thus, the 4-tuples (0,0,0,0), (1,0,0,0), (1,1,0,0), (1,1,0,1), (1,1,1,0), and (1,1,1,1) all make the circuit FALSE.
Find all ordered triplets (A, B, C) which make the
following circuit FALSE: The circuit translates to the following Boolean
expression: AB + C . To find when this is
FALSE we can equivalently find when the
AB + C is TRUE. We can simplify this by
A applying DeMorgan’s Law and cancelling the
B double not over AB to yield ABC . This is
TRUE when all three terms are TRUE, which
happens for (1, 1, 0).
C
Expression Translation into BASIC

Since most computers cannot yet understand human voice input, one must communicate through a keyboard,
even if this form of communication carries restrictions. For example, one cannot enter English sentences, but
must use a dialect such as BASIC, Pascal, LISP, etc….

This category builds the skills of expressing conventional mathematical equations in a notation that a computer
can readily understand. The purpose of this category is NOT to use one’s knowledge of mathematics to
simplify the expression or to translate it into an equivalent expression. The purpose is to express the given
expression into an exact BASIC translation. Students must avoid simplifying the given expression.

Our answers will be given in as simplified form as possible (ex. minimum number of parentheses necessary).
The word “LET” is optional; the case of the variables (upper or lower) is optional; and extra parenthesis are
allowed, unless the minimum number of parentheses is required.

Sample Problems

Translate the following equation into a statement in Expressions which are identical except that they
BASIC. Do not simplify or express the equation in contain additional parentheses and/or “LET” are
any equivalent form. acceptable. Changing to an equivalent expression is
not acceptable. For example,
N ( N + 1)
S= valid: S = N*(N+1)/2
2
valid: LET S = (N*(N+1))/2

invalid: S = N*N+1/2
invalid: S = N*N/2+N/2
invalid: S = (N(1+N))/2

The third is illegal because it is not equivalent to the


given equation; the fourth is illegal because it expands
the given equation instead of translating it. The last is
illegal because it is missing the multiplication operator
(“*”) and because it commuted the operands of the
addition term of numerator.

Translate the following equation into a statement in The solution (with the fewest number of parentheses)
BASIC. Do not simplify or express the equation in is
any equivalent form. Y = (A+C/D)/(B-A↑2)

C Examples of invalid expressions are:


A+
D Y = (A+C/D)/(B-A*A)
Y =
B − A2 Y = A+C/D/(B-A↑2)
Y = (A+C/D)/B-A↑2
The value is the square of B minus the product of 4, A
If A=1, B=4 and C=-3, evaluate: and C. Using the given values for the variables,
the expression becomes
B↑2 – 4*A*C
4↑2 – 4*1*(-3) = 16+12 = 28.

By the way, this expression computes the discriminant


of the quadratic formula.
Graph Theory

This description is adapted from Robert Sedgewick’s Algorithms, (Addison-Wesley, 1983). This book, a
reference for virtually all genres of computer algorithms, contains many example programs in Pascal.
Moreover, it is lucid and well written. We recommend this book highly.

Many problems are naturally formulated in terms of points and connections between them. For example, an
electric circuit has gates connected by wires, an airline map has cities connected by routes, and a program
flowchart has boxes connected by arrows. A graph is a mathematical object which models such situations.

A graph is a collection of vertices and edges. An edge is a connection between two vertices (or nodes). One
can draw a graph by marking points for the vertices and drawing lines connecting them for the edges, but it
must be borne in mind that the graph is defined independently of the representation. For example, the following
two drawings represent the same graph: (page 374)

I G A

J A H I
B
C

K B C
M L
J K
H G
D
D
E
E F L M
F

The precise way to represent this graph is to say that it consists of the set of vertices {A, B, C, D, E, F, G, H, I,
J, K, L, M}, and the set of edges between these vertices {AG, AB, AC, LM, JM, JL, JK, ED, FD, HI, FE, AF,
GE}.

A path from vertex x to y in a graph is a list of vertices, in which successive vertices are connected by edges in
the graph. For example, BAFEG is path from B to G in the graph above. A simple path is a path with no vertex
repeated. For example, BAFEGAC is not a simple path.

A graph is connected if there is a path from every vertex to every other vertex in the graph. Intuitively, if the
vertices were physical objects and the edges were strings connecting them, a connected graph would stay in one
piece if picked up by any vertex. A graph which is not connected is made up of connected components. For
example, the graph above has three connected components: {I, H}, {J, K, L, M} and {A, B, C, D, E, F, G}.

A cycle is a path, which is simple except that the first and last vertex are the same (a path from a point back to
itself). For example, the path AFEGA is a cycle in our example. Vertices must be listed in the order that they
are traveled to make the path; any of the vertices may be listed first. Thus, FEGAF and GAFEG are different
ways to identify the same cycle. For clarity, we list the start / end vertex twice: once at the start of the cycle and
once at the end. A graph with no cycles is called a tree. There is only one path between any two nodes in a
tree. A tree on N vertices contains exactly N-1 edges. A spanning tree of a graph is a subgraph that contains all
the vertices and forms a tree. A group of disconnected trees is called a forest.

Directed graphs are graphs which have a direction associated with each edge. An edge xy in a directed graph
can be used in a path that goes from x to y but not necessarily from y to x. For example, a directed graph similar
to our example graph is drawn below. (page 422)
H I
A

B G J K

E
D

L M
F

There is only one directed path from D to F. Note that there are two edges between H and I, one each direction,
which essentially makes an undirected edge. An undirected graph can be thought of as a directed graph with all
edges occurring in pairs in this way. A dag (directed acyclic graph) is a directed graph with no cycles.

We’ll denote the number of vertices in a given graph by V, the number of edges by E. Note that E can range
anywhere from V to V2 (or 1/2 V(V-1) in an undirected graph). Graphs will all edges present are called complete
graphs; graphs with relatively few edges present (say less than V log(V)) are called sparse; graphs with
relatively few edges missing are called dense.

It is frequently convenient to represent a graph by a matrix, as shown in the second sample problem below. If
we consider vertex A as 1, B as 2, etc., then a “one” in M at row i and column j indicates that there is a path
from vertex i to j. If we raise M to the pth power, the resulting matrix indicates which paths of length p exist in
the graph. In fact, the quantity Mp(i,j) is the number of paths.

References

Ore, Oystein. Graphs and Their Uses, MAA New Mathematic Library #10 (1963).
Sedgewick, Robert. Algorithms. Addison-Wesley (1983).

Sample Problems

Find the number of different cycles contained in the The graph is as follows:
directed graph with vertices
A B C
{A,B,C,D,E}
and edges
{AB,BA,BC,CD,DC,DB,DE}.
E D

By inspection, the cycles are: {A,B}, {B,C,D} and


{C,D}. Thus, there are 3 cycles in the graph.

In the following directed graph, find the total number By inspection, the only path of length 2 is A→A→C.
of different paths from vertex A to vertex C of length The paths of length 4 are: A→A→A→A→C,
2 or 4. A→A→C→A→C and A→C→A→A→C.

Alternatively, let matrix M represent the graph. Recall


A B
that the number of paths from vertex i to vertex j of
length p equals Mp(i,j). The values of M, M2 and M4
are:

101 201 503


C
011 , 111 , 413
100 101 302

There is 1 path of length 2 (M2(1,3)) and 3 paths of


length 4 (M4(1,3)).
LISP Programming

LISP is one of the simplest computer languages in terms of syntax and semantics, and also one of
the most powerful. It was developed in the mid-1950’s by John McCarthy at M.I.T. as a “LISt
Processing language”. Today, it is used for virtually all Artificial Intelligence programs and is
the environment of choice for applications which require a powerful interactive working
environment. LISP presents a very different way to think about programming from the
“algorithmic” languages, such as BASIC, Fortran and Pascal.

As its name implies, the basis of LISP is a list. One constructs a list by enumerating elements
inside a pair of parentheses. For example, here is a list with four elements (the second element is
also a list):

(23 (this is easy) hello 821)

The elements in the list, which are not lists, are called “atoms.” For example, the atoms in the
list above are: 23, this, hello, 821, easy, and is. Everything in LISP is either an atom or a list (but
not both). The only exception is “NIL,” which is both an atom and a list. It can also be written
as “()” – a pair of parentheses with nothing inside.

All statements in LISP are function calls with the following syntax: (function arg1 arg2 arg3 …
argn). To evaluate a LISP statement, each of the arguments (possibly functions themselves) are
evaluated, and then the function is invoked with the arguments. For example, (MULT (ADD 2
3) (ADD 1 4 2)) has a value of 35, since (ADD 2 3) has a value of 5, (ADD 1 4 2) has a value of
7, and (MULT 5 7) has a value of 35. Some functions have an arbitrary number of arguments;
others require a fixed number. All statements return a value, which is either an atom or a list.

We may assign values to variables using the function SET. For example, the statement (SET
’test ’6) would have a value of a 6, and (more importantly, however) would also cause the atom
“test” to be bound to the atom “6”. The quote in front of the arguments indicates that the
arguments should not be evaluated before the function is invoked. The quote in front of numbers
is optional. Observe the following examples:

Statement Value Comment


(SET ’a ( MULT 2 3)) 6 a is an atom with a vaue of 6
(SET ’a ’(MULT 2 3)) (MULT 2 3) a is a list with 3 elements
(SET ’b ’a) a b is an atom with a value of the
character a
(SET ’c a) (MULT 2 3) c is a list with 3 elements
(SET ’TEST (ADD 3 13 test has a value of 13
(MULT 2 5)))
(SETQ VOWELS ’(A E I O (A E I O U) VOWELS is a list of 5 elements
U))
(SETQ x (SETQ y ’same)) same both x and y are bound to value of
“same”
(SETQ y ’diff) diff x is still “same”
(SETQ x y) diff x is now “diff”

The function SETQ is the same as SET, but it causes LISP to act as if the first argument was
quoted. The function EVAL returns the value of its argument, after it has been evaluated. For
example, (SETQ z ’(ADD 2 3)) has a value of the list (ADD 2 3); the function (EVAL ’z) has a
value of (ADD 2 3); the function of (EVAL z) has a value of 5 (but the binding of the atom z has
not changed). In this last example, you can think of z being “resolved” twice: once because it is
an argument to a function and LISP evaluates all arguments to functions before the function is
invoked, and once when the function EVAL is invoked to resolve arguments. The function
ATOM can be used to tell whether an item is an atom or a list.

Statement Value Comment


(SETQ X (ADD 45 8)) 53 X is now 53
(SETQ Y ’(ADD X 11)) (ADD X 11) Y is now a list
(EVAL ’Y) (ADD X 11)
(EVAL Y) 64
(ATOM 5) true
(ATOM X) true
(ATOM ’Y) true
(ATOM Y) NIL
(ATOM ’(8 7 6)) NIL

The two most famous LISP functions are CAR and CDR (pronounced: could-er), named after
registers of a now long-forgotten IBM machine on which LISP was first developed. The
function (CAR x) returns the first item of the list x (and x must be a list or an error will occur);
(CDR x) returns the list without its first element (again, x must be a list). The function CONS
takes two arguments, of which the second must be a list. It returns a list which is composed by
placing the first argument as the first element in the second argument’s list. The function
REVERSE returns a list which is its arguments in reverse order. The following examples
illustrate the use of CAR, CDR, and CONS:

Statement Value
(REVERSE ’(8 7 6)) (6 7 8)
(REVERSE ’(1 (2 3 4) 5 6)) (6 5 (2 3 4) 1)

(CAR ’(This is a list)) This


(CAR (CDR ’(This is a list))) is
(SETQ x (CDR (CDR ’(a b c d)))) (c d)
(CDR ’(This is a list )) (is a list)
(CAR ’(hi)) hi
(CDR ’(one)) NIL
(CAR ’((1) (2 3) (4 5 6))) (1)
(CONS 32 ’(22 12)) (32 22 12)
(CONS ’first ’(last)) (first last)
(CONS ’(one) ’(two three)) ((one) two three)
(CDR (CAR ’((red white) blue))) (white)
(CONS (CAR ’(red green blue)) (CDR ’(brown tan (red tan gold)
gold)))
(CAR ’hi) error
(CAR ()) error
(CAR ’(ADD 2 2)) ADD
(CONS 78 NIL) (78)
(CDR ’((red green) (blue white)) ((blue white))

(SETQ a ’(orange black)) (orange black)


(ATOM (SETQ b (CONS ’red a))) NIL
(ATOM b) NIL
(ATOM (CAR b)) true
(ATOM NIL) true

(SETQ fruit ’(apple orange)) (apple orange)


(SETQ more (CONS ’grape fruit)) (grape apple orange)
(CDR (CDR (REVERSE more))) (grape)
(SETQ colors ’(red green yellow)) (red green yellow)
(SETQ area (MULT 2 3)) 6
(SETQ A (CONS area ’(CDR colors))) (6 CDR colors)
(SETQ B (CONS ’area (CDR colors))) (area green yellow)
(SETQ C (REVERSE colors)) (yellow green red)
(CAR (CDR C)) green
(CONS ’pink (CONS ’orange (CONS C NIL))) (pink orange (yellow green
red))

As you have probably deduced, the function ADD simply summed its arguments. We’ll also be
using the following arithmetic functions:

FUNCTION RESULT
(ADD x1 x2 …) sum of all arguments
(MULT x1 x2 …) product of all arguments
(SUB a b) a-b
(DIV a b) a/b
(SQUARE a) a*a
(EXP a n) an
(EQ a b) true if a and b are equal, NIL otherwise
(POS a) true if a is positive, NIL otherwise
(NEG a) true if a is negative, NIL otherwise
Some examples of these functions are as follows:

STATEMENT VALUE
(ADD (EXP 2 3) (SUB 4 1) (DIV 54 4)) 24.5
(SUB (MULT 3 2) (SUB 12 (ADD 2 2))) -2
(ADD (SQUARE 3) (SQUARE 4)) 25

LISP also allows us to create our own functions using the DEF function. For example,
(DEF SECOND(parms) (CAR (CDR parms)))
defines a new function called SECOND which operates on a single parameter named “parms”.
SECOND will take the CDR of the parameter and then the CAR of that result. So, for example:
(SECOND ’(a b c d e))
would first CDR the list (yielding (b c d e)) and then CAR the result. So the value would be the
single character “b”. Consider the following program fragment:
(SETQ X ’(a c s l))
(DEF WHAT(parms) (CONS parms (REVERSE (CDR parms))))
(DEF SECOND(parms) (CONS (CAR (CDR parms)) NIL))
The following chart illustrates the use of the user-defined functions WHAT and SECOND:

STATEMENT VALUE
(WHAT X) ((a c s l) l s c)
(SECOND X) (c)
(SECOND (WHAT X)) (l)
(WHAT (SECOND X)) ((c))

Questions in this round will typically present a line of LISP code or a short sequence of
statements and ask what is the value of the (final) statement.

References

Hofstadter, Douglas R. “Metamagical Themas,” in Scientific American, February, 1983. Also,


see the March and April, 1983 columns for a more detailed look. These articles can also be
found in Hofstadter’s delightful book, Metamagical Themas published by Basic Books (1985).
Charniak, Eugene and Drew McDermott. An Introduction to Artificial Intelligence, Addison-
Welsley (1985).
Sample Problems

Evaluate: (CDR ’((2 (3))(4 (5 6) 7))) The CDR function takes the first element of its
parameter (which is assumed to be a list) and returns
the modified list. The first element of the list:
((2 (3))(4 (5 6) 7))
is
(2 (3)),
and the list without this element is
((4 (5 6 ) 7)).

Consider the following program fragment: The first statement binds variable X to the list
(SETQ X ’(RI VA FL CA TX)) (RI VA FL CA TX).
(CAR (CDR (REVERSE X))) The REVERSE of this list is the list
What is the value of the CAR expression? (TX CA FL VA RI).
whose CDR is
(CA FL VA RI)
The CAR of this list is just the atom “CA” (without
the quotes).

Given the function definitions for HY and FY as follows: To evaluate (FY ’(DO RE (MI FA) SO)), we must
(DEF HY(PARMS) (REVERSE (CDR PARMS))) first evaluate
(DEF FY(PARMS) (CAR (HY (CDR PARMS)))) (CAR (HY (CDR ’(DO RE (MI FA) SO))))
What is the value of the following? Thus, HY is invoked with PARMS= ’(RE (MI FA)
(FY ’(DO RE (MI FA) SO)) SO), and we evaluate
(REVERSE (CDR ’(RE (MI FA) SO) ))
This has a value of (SO (MI FA) ) which is returned to
FY. FY now takes the CAR of this.

Evaluate the following expression. (EXP (MULT 2 (SUB 5 (DIV (ADD 5 3 4) 2)) 3) 3)
(EXP (MULT 2 (SUB 5 (DIV (ADD 5 3 4) 2)) 3) 3) (EXP (MULT 2 (SUB 5 (DIV 12 2)) 3) 3)
(EXP (MULT 2 (SUB 5 6) 3) 3)
(EXP (MULT 2-1 3) 3)
(EXP –6 3)
-216
Prefix/Infix/Postfix Notation

B
One commonly writes mathematical expressions, such as A − in infix notation:
C+D
A-B/(C+D). In this example, one must first evaluate C+D (call the result X), then B/X
(call the result Y), and finally A-Y. The order of evaluation is not simply “go from left to
right, evaluating on the fly.” Rather, evaluation is dependent on the precedence of the
operators and the location of parentheses. Two alternative formats, prefix and postfix,
have been developed to make evaluation more mechanical (and hence, solvable by a
simple computer program).

Prefix notation places each operator before its operands, and postfix places each operator
after its operands. The example above becomes –A/B+CD in prefix notation, and
ABCD+/- in postfix notation. In all notations, the relative order of the operands is the
same.

A simple algorithm for converting from infix to prefix (postfix) is as follows: (i) Fully
parenthesize the infix expression. It should now consist solely of “terms”: a binary
operator sandwiched between two operands. (ii) Write down the operands in the same
order that they appear in the infix expression. (iii) Look at each term in the infix
expression in the order that one would evaluate them, i.e., inner most parenthesis to outer
most and left to right among terms of the same depth. (iv) For each term, write down the
operand before (after) the operators. The following sequence of steps illustrates
converting X=(A*B-C/D)↑E from infix to postfix:

(X=(((A*B)-(C/D))↑E))
XABCDE
XAB*CDE
XAB*CD/E
XAB*CD/-E
XAB*CD/-E↑
XAB*CD/-E↑=

This equation has a prefix value of =X↑-*AB/CDE. A quick check for determining
whether a conversion is correct is to convert the result back into the original format.

The area of computer science that uses prefix and postfix notation (also known as
“Polish” and “Reverse Polish” notation for the Polish logician Jan Lukasiewicz) most
frequently is compiler design. Typically, expressions (as well as statements) are
translated into an intermediate representation, known as a “syntax tree,” which is
equivalent to either prefix or postfix notation. The intermediate representation can then
be easily executed.
References

Tenenbaum, Aaron M. and Moshe J. Augenstein. Data Structures Using Pascal,


Prentice-Hall (1981), pp.76-95.

Tremblay, Jean-Paul and Richard Bunt. Introduction to Computer Science, McGraw-Hill


(1979), pp. 456-478.

Sample Problems

Translate the following infix expression The expression converts as follows:


into postfix. (A-BC/)+D)1/2AB+/
(ABC/-D+)1/2AB+/
1
B ABC/-D+12/↑AB+/
( A − + D) 2 It is wrong to simplify the 1/2 to .5 or to
C
A+ B “sqrt”.

Given A=4, B=14 and C=2, evaluate the Convert to infix:


following prefix expression: →* / - + A B C * A C B
→* / -(A+B) C(A*C) B
*/-+ABC*ACB →* /((A+B) – C)(A*C) B
→*(((A+B) – C) / (A*C)) B
→((((A+B) – C) / A*C)) * B
A+ B −C
→ ( )*B
A*C
Substitute and simplify:

4 + 14 − 2
( ) * 14 = 28
4*2
Evaluate the following prefix expression, Take the original prefix expression,
when A=10, B=2, C=12 and D=2.
+ / ↑ - A B 2 ↑ / - C D / A B 3 / + AC B
+ / ↑ - A B 2 ↑ / - C D / A B 3 / + AC B
and make passes, each time putting as
many binary operators as possible into
place:

+ / ↑(A-B)2 ↑ /(C-D)(A/B) 3 /(A+C) B


+ /(A-B)2 ↑((C-D)/(A/B)) 3((A+C)/B)
+((A-B)2/((C-D)/(A/B))3)((A+C)/B)

( A − B)2 A+C
+
⎛C − D⎞
3
B
⎜ ⎟
⎝ A/ B ⎠

This evaluates to 19.


Recursive Functions

A definition that defines an object in terms of itself is said to be recursive. This


theoretical mathematical topic serves as an introduction to recursive programming (which
is supported by Pascal, but not by most BASICs). It also provides a framework for
analyzing algorithms (determining the running time and/or space required) which contain
loops or recursive sections.

Many expressions may be defined recursively in a very simple and elegant manner. The
art of recursive thinking is an important skill. Applications of recursion appear in many
areas of mathematics (factorials, compound interest, difference equations, etc.)

In this round recursive relationships will be shown not in the context of a programming
language but usually in functional notation from mathematics or in an algorithmic
description.

References

Roberts, Eric S. Thinking Recursively, Wiley (1986).


Rohl, J.S. Recursion via Pascal, Cambridge University Press (1984).
Wirth, Niklaus. Algorithms + Data Structures = Programs, Prentice-Hall (1976),
Chapter 3.

Sample Problems

Consider the following recursive algorithm for In the first pass, we get four squares of
painting a square: side 8. One is painted; three are
unpainted. Next, we have 3*4 squares of
1. Given a square. side 4: three are painted (area=3*42), nine
2. If the length of a side is less than 2 feet, then stop. are not. Next, we have 9*4 squares of
3. Divide the square into 4 equal size squares (i.e., side 2: nine are painted (area = 9*22), 27
draw a “plus” sign inside the square). are not. Finally, we have 27*4 squares of
4. Paint one of these 4 small squares. side 1: twenty-seven are painted.
5. Repeat this procedure (start at step 1) Therefore, the total painted is
for each of the 3 unpainted squares.
1*82 + 3*42 + 9*22 + 27*12 = 175.
If this algorithm is applied to a square with a side of
16 feet (having a total area of 256 sq. feet), how many
square feet will be painted?
Evaluate f(12, 6), given: Evaluate the function as follows:
f(12, 6) = f(6, 5)+2

{ f(x-y,y-1)
= (f (1, 4)+2)+2 = f(1, 4)+4
+2 when x>y
f(x,y) = = (1+4)+4
x+y otherwise
=9

Find f(6), given:


Working backwards, we get
f(0)= 1

{
f(1) = 2
f (f (x-2))+1 when x>1 f(2) = f(f(0))+1 = f(1)+1 = 2+1 = 3
f(x) = 2 when x=1 f(3) = f(f(1))+1 = f(2)+1 = 3+1 = 4
1 when x=0 f(4) = f(f(2))+1 = f(3)+1 = 4+1 = 5
f(5) = f(f(3))+1 = f(4)+1 = 5+1 = 6
f(6) = f(f(4))+1 = f(5)+1 = 6+1 = 7

One of the best known recursive functions, Ackerman’s Function is infamous for its
Ackerman’s Function, is defined below. Evaluate potential growth. In fact, we don’t have
A(2, 3). room here to give a full explanation of the

{
problem. For details, refer to the 1981-82
N+1 if M=0 ACSL All-Star Contest.
A(M,N) = A(M-1, 1) if M≠0, N=0
A(M-1, A(M, N-1)) if M≠0, N≠0 By evaluating A(1,0), A(1,1), A(1,2) and
A(1,3), we see that in general, A(1,
x)=2+x.

If we evaluate A(2,0), A(2,1), …, we see


Challenge for the bored: that in general, A(2,x)=2x+3. To solve our
Evaluate A(n, m) in terms of n and m. problem, we substitute x=3 and we get an
answer of 9.
Regular Expressions and FSAs

A Finite State Automation (FSA) has four components: an input alphabet (those letters or
strings which are legal inputs); a set of transition rules to advance from state to state
(given a current state and an element of the input alphabet, what is the next state); a
unique start state; and one or more final states. We can draw the FSA, as shown below,
by representing each state as a circular node; the final state as a double circle; the start
state as the only node with an incoming arrow; and the transition rules by the strings on
the edges connecting the nodes. When labels are assigned to states, they appear inside
the circle representing the state.
Y

Y
B

C
X

A X

If there is a path from the start state to a final state by which an input string can be
parsed, then the input string is said to be “accepted” by the FSA. The FSA above will
accept strings composed of one or more x’s followed by one or more y’s (e.g., xy, xxy,
xxxyy, xyyy). A more complicated example is given below. The input alphabet is the
digits 0, 1, 2 and 3. This FSA accepts those strings whose base 4 value is a multiple of 3.
It does this by summing the value of the digits: when in the left node, the running sum of
the digits has a value, modulo 3, of “1”; in the middle node, “2”, and in the right node,
“0”. A base four number, like a base ten number, is a multiple of 3 only when its digits
sum to a multiple of 3.
0,3 0,3 1 0,3
1

2 2

Just like Boolean Algebra is a convenient algebraic representation of Digital Electronic


Circuits, a regular expression is an algebraic representation of an FSA. For example, the
regular expression corresponding to the first FSA given above is xx*yy*. The regular
expression for the second FSA is extremely complex! The following simple FSAs and
REs illustrate the correspondence:
c
b a

a c
b
c

ab*c (aUb)c or acUbc


The rules for forming a regular expression (RE) are as follows:
[1] The null string (λ) is a RE.
[2] If the string a is in the input alphabet, then it is a RE.
[3] if the strings a and b are both REs, then so are the strings built up using the
following rules:
[3a] CONCATENATION. “ab” (a followed by b).
[3b] UNION. “aUb” (a or b).
[3c] CLOSURE. “a*” (a repeated zero or more times).

If we have a regular expression, then we can mechanically build an FSA to accept the
strings which are generated by the regular expression. Conversely, if we have an FSA,
we can mechanically develop a regular expression which will describe the strings which
can be parsed by the FSA. For a given FSA or regular expression, there are many others
which are equivalent to it. A “most simplified” regular expression or FSA is not always
well defined.

Identities for regular expressions appear below. The order of precedence for regular
expression operators is: Kleene Star, concatenation; and then union. The Kleene Star
binds from right to left; concatenation and union both bind from left to right.

Typical problems in the category will include: translate an FSA to/from a regular
expression; simplify an FSA or regular expression (possibly to minimize the number of
states or transitions); create an FSA to accept certain types of strings.

References
FSAs are usually covered in books on compilers. Sedgewick’s Algorithms covers this
topic rather nicely in the context of Pattern Matching and Parsing (Chapters 20 and 21).

Basic Identities
1. (a*)* = a* 5. a(ba)* = (ab)*a
2. aa* = a*a 6. (aUb)* = (a*Ub*)*
3. aa*Uλ = a* 7. (aUb)* = (a*b*)*
4. a(bUc) = abUac 8. (aUb)* = a*(ba*)*
Sample Problems

Find a simplified regular expression for the The expression 01*01 is read directly from
following FSA: the FSA. It is in its most simplified form.

0 0 1

List all the following FSAs which represent Only choice (a) is correct: the other FSAs
1*01*0. correspond to the following regular
expressions:
(a) 1 1 (c)
0,1 0,1
(b) 1*0
(c) (0U1)*1(0U1)*0
0 0
1 0 (d) 01*10*U10*10*
(b)
1 (d)
1
0 Note that choices (c) and (d) can be
0 1
rewritten using various identities. For
0
1
1
example, and equivalent regular expression
0 corresponding to FSA (d) is
(01*U10*)10*.

Which, if any, of the following Regular On first inspection, B can be discarded


Expressions are equivalent? because it is the only RE whose strings
A. (aUb)(ab*)(b*Ua) must end in an a. D can also be discarded
B. (aab*Ubab*)a since it is the only RE that can accept a null
C. aab*Ubab*UaabaUbab*a string. C and D are not equal by
D. aab*Ubab*Uaab*aUbab*a inspection. After expanding A, we must
E. a*Ub* compare it to C and D. It is equal to D, but
not to C.
What Does This Program Do?

Frequently one must use or modify sections of another programmer’s code. Since the original author
is often unavailable to explain his/her code, it is essential to be able to read and understand an
arbitrary program. This category builds these vital skills. In addition, the code fragments used in this
category are examples of “good” BASIC programming, and can be studied by students for that
purpose.

The dialect of BASIC that we will use is ACSL BASIC. The questions will emphasize elementary
structured flow of control rather than the particular syntax of the dialect. Students familiar with
BASIC or any other high-level language (e.g., Pascal, PL/C, C, FORTRAN), should be able to master
this category with just a little additional preparation.

Topics covered in the Senior and Intermediate Divisions include (but are not limited to): the
evaluation of arithmetic expressions, loops (single and nested), branching, subscripted variables,
multi-dimensional arrays, and string manipulations. In the Junior Division, the specific topic to be
tested is given in the title of the category.

The following system functions may be used: INT, ABS, LEFT$, RIGHT$, MID$, LEN, VAL,
STR$, SQR and SGN. The functions NOT, AND and OR will also be used; they have the order of
precedence listed (that is, NOT is highest and OR is the lowest).

The following statements may be used: INPUT, DATA, READ, RESTORE, PRINT, TAB,
FOR/NEXT, GOTO, GOSUB, RETURN, IF-THEN, IF-THEN-ELSE, LET, REM, DIM, DEF (for
user defined functions), END and STOP.

Files and matrix commands will NOT be used. Assume that our programs are immune to syntax
errors, variables have enough storage (for example, one doesn’t need an explicit “DIM X(100)” in a
program that uses an array with 100 elements).

While most commands are functions that are the same on each system, the following frequently vary
from system to system. Below we have listed these functions and the interpretation that will be used
for the contests.

INT(A) returns the largest integer not greater than A.

LEFT$(A$,N) returns a string consisting of the first N characters of the string A$.

RIGHT$(A$,N) returns a string consisting of the last N characters of the string A$.

MID$(A$,A,B) returns a string consisting of the B characters beginning with the Ath character
of A$. For example, if A$=”AMERICAN COMPUTER” then MID$(A$,7,5) would equal
“AN CO”.

LEN(A$) returns an integer representing the number of characters in the string A$. If A$ is
null (A$= “”) then LEN(A$)=0.
STR$(A) converts the integer A into a string. Note that an extra space will exist on the left of
the string. For example, if A=123 then STR$(A)= “ 123”.

VAL(A$) is the inverse to STR$. This converts the string to its numeric value. The string’s
contents must form a valid number.

References

Golden, Neal. Computer Programming in the BASIC Language, Harcourt Brace Jovanovich (1981).

Kemeny, John G. and Thomas E. Kurtz. True BASIC, Addison Wesley (1985).

Kemeny, John G. and Thomas E. Kurtz. BASIC Programming, John Wiley & Sons (1980).

Sample Problems

After the following program is executed, what is the This program computes an employee’s weekly
final value of B? salary, given the hourly rate (R) and the number of
hours worked in the week (H). The employee is
10 read H, R paid his/her hourly rate for the number of hours
15 data 50, 10 worked, up to 40; time and a half for the overtime
20 B = 0 hours, up to 48 hours; double for all hours after 48.
25 if H<=48 then 40 The following table monitors variables B and H
30 B = B + (H-48)*2*R through the program execution:
35 H=48
40 rem LINE B H
45 if H<=40 then 60 20 0 50
50 B = B +(H-40)*(3/2)*R 30 40 50
55 H=40 35 40 48
60 rem 50 160 48
65 B = B + H*R 55 160 40
70 end 65 560 40

Therefore, the final value of B is 560.

After the following program is executed, what is the K takes on values of 7, 13, 19, …, 49. The loop
final value of X? adds these numbers together in line 30.

10 X = 0
20 for K = 7 to 52 step 6
30 X = X + K
40 next K
50 end

After the following program is executed, what is the The program first stores the reverse of A$ into T$,
final value of X? (Assume that the initial value of T$ is and then counts the number of letters that are in
the null string.) the same position in both strings.

10 read A$ A$ B A N A N A S
20 X = 0 A$ S A N A N A B
25 for J = len(A$) to 1 step –1 * * * * *
30 T$ = T$ + mid$(A$,J,1)
35 next J Those positions marked with an asterisk contribute
40 for J = 1 to len(A$) one to the value of X. There are 5 such positions.
45 if mid$(A$,J,1) = mid$(T$,J,1) then X = X+1
50 next J
55 data BANANAS
60 end

The following table traces the variables through


After the following program is executed, what is the the execution of the program.
final value of C(4)?
J K L A(J) B(K) C(L)
10 A(1)=12: A(2)=41: A(3)=52 1 1 1 12 17 12
15 A(4)=57: A(5)=77: A(6)=-100 2 1 2 41 17 17
20 B(1)=17: B(2)=34: B(3)=81 2 2 3 41 34 34
25 J=1: K=1: L=1 2 3 4 41 81 41
30 rem
35 if B(K) > A(J) then 60 Thus the value of C(4) is 41. Note that this
40 C(L) = B(K) program merges two arrays in increasing order
45 L = L+1 into one.
50 K = K+1
55 goto 35
60 C(L) = A(J)
65 L = L+1
70 J = J+1
75 if A(J)>0 then 30
80 end

You might also like