Algorithms
Home | API | Notes |
Notes/Vocabulary
Truth Tables: Truth tables are a type of mathematical table used in logic and computer science to display the truth-values of logical expressions. A truth table has one column for each input variable (for example, a, b, and c) and one final column showing all of the possible results of the expression for each combination of inputs. In other words, a truth table shows the output of a logical expression for every possible combination of input values. The output column of a truth table is often referred to as the "truth value" column because it shows the truth-value of the logical expression for each combination of inputs.
Procedures/Functions: A procedure or function is a set of instructions that a computer can execute in order to accomplish a given task. Procedures and functions are a fundamental component of computer programming, and are used to break complex tasks into smaller, more manageable pieces. Generally, a procedure or function will take some input, perform some processing on the input, and return some output. Procedures and functions can be written in any programming language, though they are most commonly written in high-level languages such as C++, Java, and Python.
Sequences of Code: A sequence of code is a set of instructions written in a programming language that can be executed by a computer. Sequences of code can range from simple commands to complex algorithms. Sequences of code are written to accomplish a specific task, such as sorting a list of numbers or displaying a user interface. Sequences of code are generally written in a programming language such as C++, Java, or Python.
Example
def xor(x,y):
return (x or y) and not (x and y)
# The boolean function
def F(A, B, C, D):
P = xor(A, B)
Q = xor(C, D)
R = xor(P, Q)
return R
# Translates between 'T'/'F' and True/False:
def f(a,b,c,d):
t = 'T'
(A,B,C,D) = (a==t, b==t, c==t, d==t)
R = F(A, B, C, D)
return "FT"[R]
print("Truth Table")
print()
print("A B C D | f(A,B,C,D)")
print("========|===========")
r = "TF"
for a in r:
for b in r:
for c in r:
for d in r:
print(a, b, c, d, "| ", f(a, b, c, d))