Professional Documents
Culture Documents
CS Paper 1
CS Paper 1
CS Paper 1
Paper 1 03/12/2018
Started at: 9:08 End at 10:37
1.1
B conclusion 2 follows
1.2
C neither
3.a
def sqrt(x):
return x**0.5
print(sqrt(-8))
print(type(sqrt(-8)))
3.1
3.2
Vertex
1 2, 3
2 1,4
3 5
4
5
3.3
Where you need to identify quickly which nodes are connected so you can identify the shortest
route to an certain node like with route finding algorithms for moving packets across networks.
Harvey James
3.4
NoCats A B C 1 2 3 4 5
5 2 1 1 1
2 1 2 1
2 2 2 1 2
3 1 1 1 2
3 1 2 1 2
3 2 2 1 2
3 1 3 1 2
3 2 3 1 2
3 3 3 1 2 3
4 1 1 1 2 3
4 2 1 1 2 3
4 3 1 1 2 3
4 4 1 1 2 3 1
Using python:
M = [[0,1,1,0,0],[1,0,1,1,0],[1,1,0,0,1],[0,1,0,0,0],[0,0,1,0,0]]
def cats():
Cat = [1,0,0,0,0]
NoOfCats = 5
for A in range(2, NoOfCats):
B = 1
C = 1
print("1, A:{}, B={}, C={}".format(A,B,C))
while B < A:
if M[A-1][B-1] == 1:
if Cat[B-1] == C:
B = 1
C+=1
else:
B+=1
else:
B+=1
print("2, A:{}, B={}, C={}".format(A,B,C))
Cat[A-1] = C
print("Cat={}".format(Cat))
print(Cat)
cats()
Harvey James
3.5
3.6
3.7
4.1
False
4.2
4.3
4.4
O(n2)
4.5
4.6
O(1)
4.7
O(n)
4.8
Because you are going through the list one by one and if the item you are searching for is at the
very end of the list you will go through the entire list.
5.1
“Katherine Hepburn”
5.2
5.3
The values are stored as hashes of a value using a key. You can only turn the hashes back into
the true values by using the inverse of the key.
5.4
Harvey James
Hash values are added or subtracted to until they fit a free space
5.5
{“client_name”:catalogue_number}
5.1
3*4=12
5.2
12+8*4
5.3
6.1
Harvey James
Section B
6 Decimal to binary
6.1
Task 2:
6.2
Task 2/3:
Harvey James
7.1
4.1
4.2
Harvard architecture is used in specific and special computers used for processing large amounts
of data because it is faster for the computer to find the instructions and data when they are
separate rather than in the same place
7.1
Not organised
7.2
Because multiple functions can be called at a time which allows you to process more at once.
5.4 rehashing /
5.3 reverse polish notation advantages and disadvantages with infix notation Find
Sec B
Paper 2
Results:
Harvey James
38 out of 70?
3.a
3.3 When there aren’t that many edges, or the adjacency matrix is sparse.
3.7 Trial and error, Brute Force, Gut instinct, Best fit
4.2 Failed=True
4.3 L=M
5.3 A hashing algorithm is applied to the key field of each record to transform it into an address.
These are stored in an array. Where there are collisions, or synonyms, the address will be moved
up one space to an empty place in the array if there is any.
5b.2 (12+8) * 4
5b.3 Reverse polish notation is easier for a computer to evaluate or code the algorithm
7.1 Item class is the subclass of Trap, Monster and Character. Should be the other way around
4.2 Harvard architecture would be used in navigation systems because it is faster than von
Neumann architecture. This is important as it is used in real time
7.1 The velocity at which it is generated and received and the variety of data
7.2 Can process and run data without changing it meaning it has no side effects
Map or reduce operations can be easily paralysed. One function does not affect another function
7b.3
Harvey James
Revision
Types of data:
An integer is any whole number
A irrational number such as pi is one that cannot be expressed as a fraction and which has an
endless series of non-repeating digits.
Trees
A tree is an abstract data type that simulates a hierarchical tree structure. It has a root node at the
top which has subtrees of children each with a parent node, to represent a series of linked nodes.
The uses for trees include manipulating hierarchical data, such as system directories or folder
structures, Making information easy to search or manipulating sorted lists of data.
Graph without cycles as not possible to find a path in the tree which returns to the start node
without traversing an edge twice.
Edge connects two nodes; every node except the root is connected by exactly one edge from
another node in the level above it.
Root is the node that has no incoming edges and starts at the top of the tree
Child is the set of nodes that have incoming edges from the same node
A subtree is the set of nodes and edges comprised of a parent and all descendants of the parent.
A rooted tree is a special case of a connected graph, a node can only be connected to one parent
node and to its children. It is described as having no cycles because there can be no connection
between children or between branches.
A tree is a connected, undirected graph with no cycles. “Connected” implies that it is always
possible to find a path from a node to any other node, by backtracking if necessary. “No cycles”
means that it is not possible to find a path in the tree which returns to the start node without
traversing an edge twice. A tree does not have to have a root.
A binary tree is a rooted tree in which each node has a maximum of two children. A binary search
tree holds items in such a way that the tree can be searched quickly and easily for a particular
item, new items can be easily added and the whole tree can be printed out in sequence. A binary
search tree is a typical use of a rooted tree.
There are three ways of traversing a tree: Pre-order traversal, In-order traversal and Post-order
traversal.
A pre-order traversal can be used to produce prefix notation, used in functional programming
languages
Harvey James
Post order traversal is used in program compilation to produce reverse polish notation algorithms
for implementing a binary tree.
A binary search tree can be implemented using an array of records, with each node consisting of a
left pointer, data item, right pointer.
What is a tree?
A tree is a connected, undirected graph with no cycles, in that it is not possible to find a path in the
tree which returns to the start node without traversing an edge twice.
Graphs
A graph is a set of vertices or nodes connected by edges or arcs. The edges can be one-way to
two-way. In an undirected graph, all edges are bidirectional, data can move both ways, If the
edges are all one-way, the graph is said to be a directed graph or digraph.
Edges may be weighted to show there is a cost to go from one vertex to another. The weights can
represent the distances between towns or objects for example.
Two possible implementation of a graph are the adjacency matrix and the adjacency list
The adjacency matrix is a square matrix used to represent a finite graph. It can be seen as a two-
dimensional array which can store information about a directed or undirected graph. Each of the
rows and columns represents a node and a value stored in the cell at the intersection of a row and
column indicates that there is an edge connecting the two nodes. Example:
A B C D E F
A 5 4
B 6 3
C 8
D 2
E
F
With this matrix we can see that A and B have an edge connecting them with a value of 5.
The main advantage of an adjacency matrix is that it is very convenient to work with, as adding an
edge or testing for the presence of an edge is very simple and quick. However a sparse/scattered
graph with many nodes and not many edges will leave most of the cells empty-wasting memory.
The larger the graph the more memory space will be wasted. And with a static two-dimensional
array it is harder to add or delete nodes.
The adjacency list is a collection of unordered lists used to represent a finite graph. It is a more
space-efficient way to implement a sparsely connected graph. A list of all the nodes are created
and each node points to a list of all the adjacent nodes to which it is directly linked. The adjacency
list can be implemented as a list of dictionaries, with the key in each dictionary being the node and
the value, the edge weight.
The main advantage of an adjacency list is that is uses much less memory to represent a sparsely
connected graph.
Applications:
Harvey James
Graphs may be used to represent computer networks-with edges between computers representing
bandwidth, Roads between towns-edges representing distance, Tasks in a project, States in a
finite state machine, Webpages and links
PageRank is an algorithm used to rank websites in search engine results by counting the number
and quality of links to the website, which determines how important the website is.
Intractable problems
An intractable problem is one that is difficult to manage or solve. This problem is regarded as
inherently difficult if its solution requires significant resources.
Tractable problems can be solved by computer algorithms that run in polynomial time, O(n)
Intractable problems require times that are exponential functions of the problem size n. Polynomial
time algorithms are considered efficient, and exponential algorithms are considered inefficient.
A problem is NP if its solution can be guessed and verified in polynomial time. Finding an efficient
algorithm for any NP problem implies that an efficient algorithm can be found for all such
problems.
Binary Tree
A binary tree is a rooted tree in which each node has a maximum of two children. A binary search
tree holds items in such a way that the tree can be searched quickly and easily for a particular
item, makes it easy for new items to be added, and allows the whole tree to be printed out in
sequence. It is a typical use of a rooted tree.
For a list you place the first item at the root. Then for each item in the list, visit the root, which
becomes the current node, and branch left is the item is less than the value at the current node,
and right if the item is greater than the value at the current node. Continue down the branch,
applying the rule at each node visited until a leaf node is reached. The item is them placed to the
left or to the right of this node, depending on whether it is less than or greater than the value at the
node. Can be traversed in pre-order, in-order and post-order traversal.
Binary Search
The binary search is a much more efficient method of searching a list for an item than a linear
search, but crucially, the items in the list must be sorted. If they are not sorted, a linear search is
the only option.
Items to be searched are held in a ordered array. Tis array is divided up into three parts, a middle
item, the first part starting at List[0] up to the middle item and the second part after the middle item
Harvey James
and ending with the final item in the list. The middle item is examined to see if it is equal to the
sought item.
If it is not then it must be greater, in which the second half of the array is not important, or less
than in which the first half of the array is not important. The number of items being searched is
halved and process repeats until the last item is examined.
The binary search halves the search are with each execution of the loop-an example of a divide
and conquer strategy. The binary search is O(log n)
The basic concept of the binary search is recursive. The procedure calls itself, eventually
“unwinding” when the procedure ends. Must be a condition that if true causes the program to
terminate the recursive procedure or it will loop forever.
The recursive algorithm for searching a binary tree is similar to the binary search algorithm, except
that instead of looking at the midpoint of a list, or a subset of the list, on each pass, half of the tree
or subtree is eliminated each time its root is examined. O(log n)
Time complexities
O(1)-constant time
O(n)-linear time
O(n2)-polynomial time
O(2n)-Exponential time
O(n!)-factorial time
A hashing algorithm can be applied to the value in the key field of each record to transform it into
an address. Normally there are many more possible keys than actual records that need to be
stored. One common hashing algorithm is to divide the key by the number of available addresses
Harvey James
and take the remainder as the address. For example the algorithm “address = key mod 1000”,
where there are 1000 free spaces allocated to store the records.
When the addresses 631552 and 134552 are put through the algorithm they have the same
address 52. This is called a synonym and they are bound to occur with any hashing algorithm.
Two record keys hashing to the same address is referred to as a collision. A simple way to deal
with collisions is to store the item in the next available free space.
A hash table is a collection of items stored in such a way that they can be quickly located. The
hash table could be implemented as an array or list of given size with a number of empty spaces.
When searching for an item you first apply the hashing algorithm, examine the resulting cell in the
list, if item is there return item, if cell is empty, item is not in table, if another item in that spot keep
moving forward until either the item is found or a blank cell is encountered, when it is apparent the
item is not in the table.
To be as efficient as possible the hashing algorithm needs to be chosen so that it generates the
least number of collisions. This can depend to some extent on the distribution of the items to be
hashed.
There are many other algorithms, the folding method divides the item into equal parts, and adds
up the parts to give the hash value.
A hash function can be created for alphanumeric strings by using the ASCII code for each
character. This is hashing a string
The fuller the hash table becomes the more likely it is that there will be collisions. Needs to be
taken into account when designing the hashing algorithm and deciding on the table size.
Rehashing is the process of finding an empty slot when a collision has occurred. The rehashing
algorithm used above simply looks for the next available spot.
Hash tables are primarily used for efficient lookup, so that for example an index would typically be
organised as a hash table. A hash table could be used to look up a person’s telephone number
given their name. They can be used to store codes and encrypted passwords.
A dictionary is an abstract data type consisting of associated pairs of items, where each pair
consists of a key and a value. When they key is given a value is returned. Items can be easily
amended, added or removed as required.
Infix notation is the way we normally write arithmetic expressions, however it is not easy for a
computer to evaluate such an expression directly: (a+b)*c
The computer wants the operands and the operators in the sequence: a b + c *
In reverse polish notation the operator follows the operands-which is the logical sequence for a
computer.
In order to translate from infix to reverse polish notation we need to define the order of precedence
of operators. Shown in increasing order to precedence:
+=)
*/
^ (exponentiation)
~ (unary minus, -3 + 2)
A computer will use a fairly complex algorithm using a stack to translate from infix to reverse polish
notation. However it is quite simple to do it manually with knowledge of the rules of precedence
and a few simple rules.
Starting from the left hand side of the expression, allocated numbers to operands and operators as
follows: If the next symbol is an operand, allocate the next number to it. If it is an operator move to
the next operand. Ignore parentheses except in so far as they affect the order of calculation. Using
the rules of precedence, decide which is the next operation that should be performed, and as soon
as its operands have been allocated numbers, back up and allocate it the next number. E.g:
A+B*C
1 5 2 4 3
A + B * C
Working from the left, allocate 1 to a, 2 to b. Multiplication is done before addition, so keep going
and allocate 3 to c. Then back up and allocate 4 to * and finally 5 to +
Write down the tokens in the order of the numbers you have allocated: a b c * +
Once a compiler has translated an arithmetic expression into reverse polish notation, each symbol
in the expression may be held in a string or array. The expression may then be evaluated using a
stack, scanning the elements of the string from left to right as follows: If the next token is an
operand place it on the stack, if the next token is an operator, remove the required number of
operands from the stack, perform the operation, and put the result back on the stack.
Harvey James
program then consists of a series of calls to procedures or functions, each of which may in turn
call other procedures or functions.
In this method of programming, the world is viewed as a collection of objects, An object might be a
person, animal, place, concept or event, for example. It could be something more abstract like a
bank account or a data structure as a stack or queue that the programmer wished to implement.
An object-oriented program is composed of a number of interacting objects, each of which is
responsible for its own data the operations on that data. Program code in an object-oriented
program creates the objects and allows the objects to communicate with each other by sending
messages and receiving answers. All the processing that is carried out in the program is done by
objects.
Each object will have its own attributes. The attributes of a car might include its make, engine size,
colour, ext. The attributes of a person could include first name, last name and date of birth. An
object has a state. A radio for example may be on or off, turned to a particular station, set to a
certain volume. A bank account may have a particular balance, like £54.20. An object has
behaviours. These are the actions that can be performed by an object; for example, a cat can
walk, pounce, catch mice, purr, and meow and so on.
A class is a blueprint or template for a object, and it defines the attributes and behaviours of
objects in that class. An attribute is data that is associated with the class, and a method is a
functionality of the class – something that it can do or that can be done with it.
For example, a stock control system might be used by a bookshop for recording the items that it
receives into stock from suppliers and sells to customers. The only information that the stock class
will hold in this simplified system is the stock ID number, stock category, description, and quantity
in stock. Part of a sample definition of a class named StockItem is defined below. Program coding
will vary
As a general ruel, instance variables or attributes are declared private and most methods public,
so that other classes may use methods belonging to another class but may not see or change
their attributes. This principle of information hiding where other classes cannot directly access the
attributes of another class when they are declared private, is an important feature of object-orient
programming. A constructor is used to create objects in a class. In the above example the
constructor is called the StockItem; in many programming languages the constructor must have
the same name as the class.
Once the class and its constructor have been defined, and each of the methods coded we can
start creating and naming actual objects. The creation of a new object is know as instantiation
Multiple instance of a class can be created which each share identical methods and attributes, but
the values of those attributes will be unique to each instance. This means that multiple enemy
objects can be created in a computer game by programming just one zombie class, with health,
position and speed attributes; but each individual zombie could operate independently with
different attribute values. Suppose we want to create a new stock item called book1 The type of
variable to assign to book1 has to be stated. This will be the class name, StockItem. The word
new is typically used like in java, to instantiate a new object in the class.
Harvey James
Book1 is called a reference type variable or simply a reference variable. Reference variable are
named memory locations in which you can store information. However a reference variable does
not hold the object-it hold a pointer or reference to where the object itself is stored.
A variable reference diagram shows in graphic form the new stockitem object referenced by the
variable book1. In the diagram reference variable are shown as circles and primitive data types
are shown as rectangles.
Messages can be categorised as either “getter” or “setter” messages. In some languages “getter”
messages are written as a function which return an answer and setter messages as procedures
which change the state of an object.
An object encapsulates both its state and its behaviours or methods. All the data and methods of
each object are wrapped up into a single entity so that the attributes and behaviours of one object
cannot affect the way in which another object functions.
Information hiding is where the details of an objects instance variables are hidden so that other
objects must use messages to interact with that objects state.
Classes can inherit data and behaviour from a parent class in much the same way that children
can inherit characteristics from their parents. A “child” class in object oriented programing is
referred to as a subclass, and a parent as a superclass.
Inheritance diagram:
Harvey James
--The BETWEEN operator selects values within a given range. The values
can be numbers, text, or dates. It is inclusive, begin and end values
are included
SELECT column_names FROM table_name WHERE column_name BETWEEN value1 AND
value2;
--Can use NOT aswel:
SELECT column_names FROM table_name WHERE column_name NOT BETWEEN value1
AND value2;
--can be done between numbers,strings,letters,dates
--RIGHT (OUTER) JOIN returns all records from right table and matched
records from the left
--FULL (OUTER) JOIN returns all records when there is a match in either
left or right table
--INNER JOIN keyword selects records that have matching values in both
table
SELECT column_names FROM table1 INNER JOIN table2 ON table1.column_name
= table2.column_name;
--LEFT JOIN keyword returns all records from the left table, and matched
records from the right table. The result is NULL from the right side if
there is no match.
SELECT column_names FROM table1 LEFT JOIN table2 ON table1.column_name =
table2.column_name;
--RIGHT JOIN keyword returns all records from the right table and the
matched records from the left table. The result is NULL from the left
side, when there is no match.
SELECT column_names FROM table1 RIGHT JOIN table2 ON table1.column_name
= table2.column_name;
--FULL OUTER JOIN keyword returns all records when there is a match in
either left or right table records.
SELECT column_names FROM table1 FULL OUTER JOIN table2 ON
table1.column_name = table2.column_name;
--A self JOIN is a regular join, but the table is joined with itself.
SELECT column_names FROM table1 T1, table1 T2 WHERE condition;
UNION
SELECT column_names FROM table2;
--The UNION operator selects only distinct values by default. To allow
duplicate values, use UNION ALL
SELECT column_names FROM table1
UNION ALL
SELECT column_names FROM table2;
--The HAVING clause was added to SQL because the WHERE keyword could not
be used with aggregate functions.
SELECT column_names FROM table_name WHERE condition GROUP BY
column_names HAVING condition ORDER BY column_name;
--The EXISTS operator is used to test for the existence of any record in
a subquery, It returns true if one or more records are returned.
SELECT column_names FROM table_name WHERE EXISTS (SELECT column_name
FROM table_name WHERE condition);
--The ANY and ALL operators are used with a WHERE or HAVING clause
--The ANY operator returns true if any of the subquery values meet the
condition
SELECT column_names FROM table_name WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE condition);
--The ALL operator returns true if all of the subquery values meet the
condition
SELECT column_names FROM table_name WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);
--The SELECT INTO statement copies data from one table into a new table.
--Copy all columns into a new table:
SELECT * INTO newtable [IN externaldb] FROM oldtable WHERE condition;
--Copy some columns into a new table:
SELECT column1, column2, ... INTO newtable [IN externaldb] FROM oldtable
WHERE condition;
--The INSERT INTO SELECT statement copies data from one table and
inserts it into another table. It requires that data types in source and
target tables match. The existing records in the target table are
unaffected.
--Copy all columns from one table to another table:
INSERT INTO table2 SELECT * FROM table1 WHERE condition;
Harvey James
--Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, ...) SELECT column1, column2, ...
FROM table1 WHERE condition;
--The CASE statement goes through conditions and returns a value when
the first condition is met. So, once a condition is true is will stop
reading and return the result. If no conditions are true, it returns the
value in the ELSE clause. If there is no ELSE part, and no conditions
are true, it returns NULL.
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN result
ELSE result
END;
--A stored procedure is a prepared SQL code that you can save, so the
code can be reused over and over again. So if you have an SQL query that
you write over and over again, save it as a stored procedure and then
call it to execute it. You can pass parameters to a stored procedure, so
that the stored procedure can act based on the parameter values that is
passed.
--Creates a stored procedure: (parameter is optional)
-CREATE PROCEDURE procedure_name @parameter_name data_type
AS sql_statement
GO;
--Executes a stored procedure:
EXEC procedure_name;
--Example:
CREATE PROCEDURE SelectALLCustomers @City nvarchar(30)
AS SELECT * FROM Customers WHERE City = @City
GO;
SQL databases
--Data types:
/*
CHAR(size) fixed length string, 255 characters max
VARCHAR(size) variable length string, 255 characters max
Harvey James
/*
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table.
This ensures the accuracy and reliability of the data in the table. If
there is any violation between the constraint and the data action, the
action is aborted. Constraints can be column level or table level.
Column level constraints apply to a column, and table level constraints
apply to the whole table.
Following constraints are commonly used:
Can have many UNIQUE constraints per table but only one PRIMARY KEY
PRIMARY KEY may consists of single or multiple fields, composite key
Table containing the foreign key is called the child table, and the
table containing the candidate key is called the referenced or parent
table. FOREIGN KEY is used to link two table together. The FOREIGN KEY
constraint is used to prevent actions that would destroy links between
tables. The FOREIGN KEY constraint also prevents invalid data from being
inserted into the foreign key column, because it has to be one of the
value contained in the table it points to. CHECK constraint used to
limit value range that can be in a column.
*/
--MySQL:
CREATE TABLE Persons (
....
PersonID PRIMARY KEY (ID,LastName) --composite primary key
Age int CHECK (Age>=18) DEFAULT 18
);
--For alter:
ALTER TABLE Persons
ADD PRIMARY KEY (ID);
--Foreign key
CREATE TABLE Orders (
....
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
--To create an index
CREATE INDEX index_name ON table_name (col1, col2, ...);
--To create an unique index
CREATE UNIQUE INDEX index_name ON table_name (col1, col2, ...);
Harvey James
/*
As long as your data contains only the date portion, your queries will
work as expected. However, if a time portion is involved, it gets more
complicated.
MySQL has the following data types for dates and times:
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MI:SS
TIMESTAMP YYYY-MM-DD HH:MI:SS
YEAR YYYY or YY
/*
SQL injection is a code injection technique that has the potential to
destroy your database. It is one of the most common web hacking
techniques. SQL injection is the placement of malicious code in SQL
statements via web page input.
Harvey James
SQL injection usually occurs when you ask a user for input, like their
uername/userid, and instead of a name/id, the user gives you an SQL
statement that you will unknowingly run on your database.
The following example creates a SELECT statement by adding a variable to
a select string. The variable is fetched from user input:
txtUserId = getRequestString(“UserId”);
txtSQL = “SELECT * FROM Users WHERE UserId = “ + txtUserId;
To protect a web site from SQL injection, you can use SQL parameters.
SQL parameters are values that are added to an SQL query at execution
time, in a controlled manner. Parameters are represented in the SQL
statement by a @ marker. The SQL engine checks each parameter to ensure
that it is correct for its column and are treated literally, and not as
part of the SQL to be executed.
Harvey James
If you want your web site to be able to store and retrieve data from a
database, your web server should have access to a database-system that
uses the SQL language. You may need to look for SQL hosting plans with
your ISP. The most common SQL hosting databases are MS SQL Server,
Oracle, MySQL, and MS Access.
MS SQL Server is popular for websites with high traffic, very powerful,
robust and full featured SQL database system.
Oracle is a popular database software for websites with high traffic,
powerful, robust, full featured SQL database system, but is run by
assholes who only want your money.
MySQL is a popular choice for web sites, powerful, robust and full
featured SQL database system. Inexpensive alternative to expensive
Mirosoft and Oracle solutions
Access is great for simple databases, not suited for high-traffic, and
not as powerful as the rest.
Bitwise operators:
& AND
| OR
^ exclusive OR
Compound operators
+= Add equals
-= Subtract equals
*= Multiple equals
/= Divide equals
%= Modulo equals
&= AND equals
^-= exclusive equals
|*= OR equals