-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkdown
More file actions
3185 lines (1916 loc) · 175 KB
/
markdown
File metadata and controls
3185 lines (1916 loc) · 175 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# CS Notes
# Beginning of Paper 1 content
# Programming
**Memory** The location where instructions and data are stored on the computer
**Algorithm** A sequence of steps that can be followed to complete a task. The sequence must always terminate
**Syntax** The rules of how words are used within a given language
**Memory address** A specific location in memory where instructions or data are stored
**Pointer** A data item that identifies a particular element in a data structure
**Array** A set of related items stored under a single identifier
**Element** A single item within a set or list
**Record** A single line of a text file
**Subroutine/Procedure/Subprogram/Routine** A name block of code designed to carry out a specific task
**Function** A subroutine that returns a value
**Exception handling** The process of dealing with events that cause the current subroutine to stop
**Procedural programming languages** Languages where the programmer specifies the steps that must be carried out in order to achieve a result
**Imperative programming languages** Languages based on giving the computer commands or procedures to follow
**Hierarchy chart** A diagram that shows the design of a system from the top down
**Structure chart** Similar to a hierarchy chart with the addition of showing how data is passed around the system
**Top-down approach** A design process in which you start at the top of the process and work down into smaller and smaller sub-processes
**Modular design** A method of system design that breaks a whole system down into smaller units, or modules
**Class** A class defines methods and fields that capture the common characteristics of objects
## Encapsulation
Encapsulation is the concept of keeping the data and code within the same object.
**Method** The code or routines contained within a class
**Properties** The defining features of an object or a class in terms of its data
A class defines properties, methods and how they will behave.
An object is an instance of a class, containing its methods and the data defined in the class.
## Inheritance
**Inheritance** The concept that properties and methods in one class can be shared with a subclass
### Class diagrams for inheritance
**Class diagrams** A way of representing the relationship between classes
Class diagrams are hierarchical in structure, with the base class at the top and sub-classes beneath.
- The subclass inherits the properties and method of the base class
- Arrows are used to show the direction of inheritance
- Each class is represented with a box made up of three sections to include the class name, properties, and methods
## Instantiation
**Instantiation** The process of creating an object from a class.
## Polymorphism and overriding
**Polymorphism** The ability of different types of data to be manipulated with the same method
Polymorphism allows a method to be redefined, overridden, to work with the changes made to the sub-class.
## Abstract, virtual, and static methods
Static methods can be used without an instance of the class being instantiated
Virtual methods are defined in the base class but can be overridden by the method in the sub-class where it is used.
Abstract methods are not supplied in the base class, but must be provided in the subclass
## Aggregation
Aggregation is a method of creating new objects that contain existing object, based on the way in which objects are related.
All of the objects exist in their own right, but their is a relationship between them.
## Composition aggregation
**Composition aggregation** Creating an object that contains objects, and will cease to exist if the containing object is destroyed
## Association aggregation
**Association aggregation** Creating an object that contains other objects, which can continue to exist even if the containing object is destroyed
## Design principles
- **Encapsulate what varies**: Properties and methods are subdivided into as many classes as needed to reflect the real-life scenario
- **Favour composition over inheritance**: This refers to the way in which classes and objects are instantiated. Composition is less error prone and enables simpler maintenance
- **Program to interfaces, not implementation**:Interface methods are widely used. When a class is created that adheres to the methods in the interface, it can be seen as an implementation of the interface. Programs can be written based on interfaces rather than each individual implementation of a class. If a class needs to be amended, this can be done with reference to the interface, meaning that there will be little or no impact on the other classes in the program
# Data structures
## Data structures and abstract data types
**Data structure** A common format for storing large volumes of related data, which is an implementation of an abstract data type
**Abstract data type** A conceptual model of how data can be stored and the operations can be carried out on the data
**File** A collection of related data
### Binary files
Binary files contain binary codes and usually some header information
### Static and dynamic data structures
**Static data structure** A method of storing data where the amount of data stored is fixed
**Dynamic data structure** A method of storing data where the amount of data stored will vary as the program is being run
**Heap** A pool of unused memory that can be allocated to a dynamic data structure
**Stack** A data structure where the last item added is the first item removed
**Queue** A data structure where the last item added is the first item removed
| Static | Dynamic |
| --- | --- |
| Inefficient as memory is allocated that may not be needed | Efficient as the amount of memory varies as needed |
| Fast access to each item of data as the memory location is fixed when the program is written | Slower access as the memory location is allocated at run-time |
| Memory addresses allocated will be contiguous so quicker to access | Memory addresses allocated may be fragmented, resulting in slower access |
| Structures are a fixed size, making them more predictable to work with | Structures vary in size so there needs to be a mechanism for knowing the size of the current structure |
The relationship between different elements of data does not change | The relationship between different elements of data will change as the program is run |
## Stacks and queues
### Stacks
**Stack** A last in first out structure
The stack pointer stores where the stop of the stack is.
#### Pushing to a stack
``` python
if stack_pointer < stack.size:
stack_pointer += 1
stack_array[stack_pointer] = data_item
else:
error("Stack full")
```
#### Popping from the stack
``` python
if stack_pointer > 0:
data_item = stack_array[stack_pointer]
stack_pointer -= 1
else:
error("The stack is empty")
```
#### Stack frames
**Stack frame** A collection of data about a subroutine call
The stack frame contains the parameters to the subroutine call, its local variables, and its return address.
**Call stack** A type of stack used to store information about active subroutines and functions within a program
**Interrupt** A signal sent by a device or program to the processor requesting its attention
**Recursion** The process of a subroutine calling itself
### Queues
**Queue** A first in first out structure
**Linear queue** A first in first out structure organised as a line of data
**Circular queue** A first in first out structure implemented as a ring where the front and rear pointers can wrap around from the end to the start of the array
**Priority queue** A variant of a first in first out structure where some data types may leave out of sequence if they have a higher priority than other data items
## Graphs and trees
**Graph** A mathematical structure that models the relationship between pairs of objects
**Graph theory** The underlying mathematical principles behind the use of graphs
**Arc** A join or relationship between two nodes, also known as an edge
**Vertex/Vertices** An object in a graph, also known as a node
**Weighted graph** A graph that has a data value labelled on each edge
**Undirected graph** A graph when the relationship between the vertices is two way
**Directed graph** A graph when the relationship between the vertices is one way
**Latency** The time delay that occurs when transmitting data between devices
### Adjacency lists and matrices
**Adjacency list** A data structure that stores a list of nodes with their adjacent values
A weighted graph may be stored in an adjacency list by writing each adjacency of a pair of the adjacent node and the weight of the edge
**Adjacency matrix** A data structure set up as a two dimensional array that shows whether there is an edge between each node
| Adjacency list | Adjacency matrix |
| --- | --- |
| Only stores data where there is an edge, requiring less memory | Stores a value for every combination, requiring more memory |
| The list must be parsed to identify whether particular adjacencies exist | Adjacencies can be identified more quickly as every combination is stored |
| Where there are not many edges, a sparse graph, this method would be more suitable | Where there are many edges, a dense graph, this method would be more suitable
### Trees
**Tree** A data structure similar to a graph, containing no loops
**Rooted tree** A tree in which one vertex has been designated as the root
**Node** An object in a graph
**Edge** A join of relationship between nodes
**Root** The starting node in a rooted data structure from which all other nodes branch off
**Parent** A type of node in a tree, where there are further nodes below it
**Leaf** A node that does not have any other nodes beneath it
#### Binary search tree
**Binary search tree** A tree where each node can only have up to two child nodes attached to it
##### Binary tree implementation
This implementation uses three arrays:
- The first stores the data
- The second stores the left pointers
- The right stores the right pointers
#### Tree traversal
Pre-order for copying a tree
In-order for a binary search tree or outputting the contents of a binary search tree in ascending order
Post-order for Infix to RPN conversions, producing an infix expression from a tree, or emptying a tree
## Hash tables and dictionaries
**Hash table** A data structure that stores key value pairs based on an index calculated from an algorithm
### Hash tables
**Hashing algorithm** An algorithm that creates a unique index from given items of key data
**Cache** A high speed temporary area of memory
Features and problems for a hashing algorithm:
- Numeric value needs to be generated from the data to perform the calculation
- Unique indices must be generated. Non-unique indices are collisions which must be coped with
- The indices must be uniformly distributed to avoid clustering
- The algorithm must be capable of producing the necessary number of unique indices
- The algorithm must balance efficiency and complexity
**Collision** When a hashing algorithm produces the same index for two or more different keys
**Clustering** When a hashing algorithm produces indices that are not randomly distributed
**Load factor** The ratio of the number of indices available to how many there are in total
**Index** The location where values will be stored, calculated from the key
**Chaining** A technique for generating a unique index when there is a collision by adding the key value to a list stored at the same index
**Rehashing** The process of running the hashing algorithm again when a collision occurs
### Dictionaries
**Dictionary** A data structure that maps keys to data
**Associative array** A two dimensional structure containing key value pairs of data
## Vectors
Vectors can be represented as values stored in a list.
Vectors can also be represented as a function.
A function maps an input to an output.
F:S -> R
F is the function that creates the vector
S is the complete set of values that the function can be applied to
R is the potential outputs of the function
**Magnitude** The size of the vector
**Direction** The direction of the vector
**Components** The values within a vector
### Vector addition
Vectors are added by adding the respective components of the vectors
### Scalar vector multiplication
**Scalar** A real value used to multiply a vector to scale the vector
### Dot product
**Dot product** The process of combining two vectors to calculate a single number.
A.B = A<sub>x</sub>B<sub>x</sub>+ A<sub>y</sub>B<sub>y</sub>
### Convex combinations
**Convex hull** A spatial representation of the vector space between two vectors
**Convex combination** A method of multiplying vectors that produces a resulting vector within the convex hull
**Vector space** A collection of elements that can be formed by adding or multiplying vectors together
Performing a convex combination is either multiplying one vector by either a scalar or another vector
D = αAB + βAC
Where AB and AC are the two vectors.
α and β represent the real numbers that each vector will be multiplied by.
α and β must both be greater than or equal to 0, and αβ must equal 1. D will then fall within the vector space.
*Surely if either is 0, then αβ != 1?*
### Uses of the dot product
Given two vectors u and v, it is possible to generate parity using bitwise AND and XOR operations.
Where u = [1,1,1,1] and v = [1,0,1,1], the dot product would be u.v = 1.
This is calculated by performing arithmetic over GF(2) where GF has two elements 0 and 1.
This calculation finds the parity bit for even t parity.
The first vector will always be [1,1,1,1].
uv. = [1,1,1,1].[1,0,1,1]
= 1 AND 1 XOR 1 AND 0 XOR 1 AND 1 XOR 1 AND 1
= 1 XOR 0 XOR 1 XOR 1
= 1
Arithmetic over GF(2) can be summarised in two small tables.
Multiplication can be achieved by the bitwise AND operation, and addition can be achieved by the bitwise XOR operation.
# Algorithms
## Graph and tree traversal
**Implementation** Creating code to produce a programmed solution
**Depth first** A method for traversing a graph that starts at a chosen node and explores as far as possible along each branch away from the starting node before backtracking
**Breadth first** A method for traversing a graph that explores nodes closest to the starting node first before progressively exploring nodes that are further away
Method for depth first traversal
| Explanation | Current node | Visited nodes |
| --- | --- | --- |
| Selected the node to start [A] | A | |
| Mark node a as visited. Choose a node that is connected to A [B] and recursively call the search routine to explore this node | A | A |
| Mark Node B as visited. Choose a node that is connected to B and has not been visited [C] (recurse) | B | A, B |
| As above with C. Choosing D | C | A, B, C |
| As above with D. Choosing E | D | A, B, C, D |
| Mark node E as visited. All connecting nodes have been visited, so unwind recursion. There are no nodes left to visit so terminate | E | A, B, C, D, E |
In pseudocode
``` python
def traverse(node n):
visited.add(n)
# Do something with the node
for node in n.neighbours:
if node not in visited:
traverse(n)
```
Method for breadth first traversal
| Explanation | Contents of queue |
| --- | --- |
| Add the initial node to the queue | A |
| Add all adjacent nodes which are not already fully explored to the queue | A, B |
| Remove A from the queue as it is fully explored | B |
| Add all nodes that are adjacent to B and not already fully explored to the queue | B, C, E |
| Remove B from the queue as fully explored | C, E |
| Add all the nodes that are adjacent to C and not already fully explored to the queue | C, E, D |
| Remove C from the queue as it is already fully explored | E, D |
| Add all nodes that are adjacent to E and not already fully explored to the queue | E, D |
| Remove E from the queue as fully explored | D |
| Remove D from the queue as fully explored. Terminate as queue empty | |
*Book says remove E and then shows the queue as containing only E*
**Binary tree** A structure where each node can only have up to two child nodes attached to it
**Pre-order** A method of traversing a tree by visiting the root, traversing the left subtree, and traversing the right subtree
**In-order** A method of traversing a tree by traversing the left subtree, visiting the root, and traversing the right subtree
**Post-order** A method of traversing a tree by traversing the left subtree, traversing the right subtree, and visiting the root
**Traversal** The process of reading data from a tree or graph by visiting all of the nodes
**Binary search** A technique for searching sorted data that splits the dataset in half repeatedly until the search data is found
## Dijkstra's shortest path algorithm
**Single source** The shortest path is calculated from a single starting point
## Reverse Polish notation
**Infix** Expressions written with operators within operands
**Polish/Prefix notation** Expressions with operators before operands
**Reverse Polish/Postfix notation** Expressions with operators after operands
### Evaluating postfix expressions
- Create a stack
- For each element:
- If the element is an operand, push it onto the stack
- If the element is an operator, pop twice to get A and B respectively. Calculate B O A and push it to the stack
Example 4 3 * 6 2 * /
| Step | Stack |
| --- | --- |
| Push 4 | 4 |
| Push 3 | 4, 3 |
| Evaluate 4 * 3 | 12 |
| Push 6 | 12, 6 |
| Push 2 | 12, 6, 2 |
| Evaluate 6 * 2 | 12, 12 |
| Evaluate 12 / 12 | 1 |
**In order traversal** A method of extracting data from a binary tree that will result in an infix expression
**Post-order traversal** A method of extracting data from a binary tree that will result in postfix expressions
**Pre-order traversal** A method of extracting data from a binary tree that will result in prefix expressions
### Conversion of infix to postfix
- Create an empty stack for keeping op
### Applications of postfix
Some languages are specifically designed to be stack-oriented and would therefore be ideally suited to this application.
In these cases, the interpreter or compiler checks all of the syntax with reference to the postfix notation of each expression.
## Sorting algorithms
### Bubble sort
**Bubble sort** A technique for putting data in order by repeatedly stepping through an array, comparing adjacent elements and swapping them if necessary until the array is in order
1. Iterate through each element
1. Iterate through each element
2. If the outer element is greater, swap them
2. When no swap is made in inner loop break
In each iteration, the largest element will be moved to the end.
Enhanced bubble sort decreases the size of the inner loop by 1 each time, as the last element is now correct.
### Merge sort
**Merge sort** A technique for putting data in order by splitting lists into single elements and then merging them back together
#### Splitting procedure
Split the array into halves, and repeat on each sub-array until the arrays can no longer be divided into integer parts.
#### Merging procedure
Assume that the lists to be merged are already sorted
Compare the first element of each list, adding the new number to the merged list.
Repeat the process, comparing the first element in each list and placing the lowest item in the merged list
# Computational thinking
## Abstraction and automation
**Problem solving** The process of finding a solution to real-life problems
**Automation** Creating a computer model of a real-life situation and putting it into action
**Logical reasoning** The process of using a given set of facts to determine whether new facts are true or false
*a fact is "a thing that is known or proved to be true." so the definition above is crap*
Automation requires putting models (abstractions of real world objects/phenomena) into action to solve problems:
### Abstraction
The concept of abstraction is to reduce problems to their essential features.
This allows a problem to be viewed from a high level, concentrating on the key aspects of designing a solution whilst ignoring the detail.
Once a solution has been identified for the current problem, a feature of abstraction is that the abstraction from one problem can be applied to another similar problem which shares the same common features.
#### Representational abstraction
**Representational abstraction** The process of removing unnecessary details so that only information that is required to solve the problem remains
This is the process of removing unnecessary details until it is possible to represent the problem in a way that can be solved.
#### Abstraction by generalisation or categorisation
**Abstraction by generalisation or categorisation** The concept of reducing problems by putting similar aspects of a problem into hierarchical categories
This process involves identifying common characteristics of representations so that more general representations can be developed.
**Procedural abstraction** The concept that all solutions can be broken down into a series of procedures or subroutines
At the design stage it is sufficient for the programmer to work out what each procedure will do without defining how it will do it.
The procedure may in turn call other procedures although it does not need to know how these work in order to call them.
This is the basis for top down design.
Other considerations include what event will trigger the procedure, how procedures will link together, including any possible side effects, and how errors will be handled.
**Functional abstraction** Breaking down a complex problem into a series of reusable functions
Similar to procedural abstraction, functional abstraction focuses on common functions that can be used to solve problems.
Functions are a feature of procedural languages. Functions can be created for any common procedure and functions can be built on top of other functions producing higher levels of abstraction.
All the program needs is for the parameters to be input into the function in order to generate a result.
Using functions reduces complexity as the function only needs to be written once.
**Problem abstraction** The process of reducing a problem down to its simplest components until the underlying processing requirements that solve the problem are identified
For example, satnavs use vectors and a variation of Dijkstra's algorithm, neither of which were developed specifically for the satnav, but both of which have been adopted to create the required solution.
**Data abstraction** Hiding how data is represented so that it is easier to build a new kind of data object (e.g. building a stack from an array)
#### Information hiding
**Information hiding** The process of hiding all details of an object that do not contribute to its essential characteristics
An example is where a GUI is used.
The complexity of the calculations used to generate the displayed information is hidden. If the method of route calculation was changed, it would not necessarily affect the interface. Information hiding separates the user interface from the actual implementation of the program.
#### Composition and decomposition
**Composition** Building up a whole system from smaller units
**Decomposition** Breaking down a large task into a series of subtasks
## Finite state machines
**Finite state machine** Any device that stores its current status and whose status can change as the result of an input
### State transition diagrams
**State transition diagram** A visual representation of a finite state machine using circles and algorithms
**Accepting state** The state that identifies whether an input has been accepted
**State transition table** A tabular representation of a finite state machine, showing inputs, current state, and next state
### Finite state machines with outputs
**Mealy machine** A finite state machine which outputs data
**Shift cipher** A simple substitution cipher where the letters are coded by moving a certain amount forwards or backwards in the alphabet
## Turing machines
**Turing machine** A theoretical model of computation
A Turing machine has an infinitely long tape divided into cells, as well as a read write head
- Each cell contains a symbol, which can be blank
- The read write head can either read what is in the cell or write into the cell. It can also erase the current contents of the cell
- The tape can move left or right one cell at a time so that every cell is accessible by the read write head
- The machine can halt at any point if it enters a halting state or the entire input has been processed
Turing machines require:
- A start state
- A halting state
- An alphabet that lists acceptable symbols
- A transition function indicating what should be written at each cell and whether to move left or right based on the input read
### Universal Turing machine
**Universal Turing machine** A machine that can simulate a Turing machine by reading a description of the machin along with the input of its own tape
Rather than defining each individual process within a single machine, the universal machine takes two inputs:
- A description of all the individual Turing machines required to perform the calculations
- All the inputs required for the calculations
## Regular and context free languages
**Regular expression** Notation that contains a string of characters that can be matched to the contents of a set
**Regular language** Any language that a finite state machine will accept
| Regular expression | Meaning | Strings produced |
| --- | --- | --- |
| `a|b|c` | a or b or c | a, b, c |
| `abc` | a and b and c | abc |
| `a*bc` | zero or more a followed by b and c | bc, abc, aabc, aaabc |
| `(a|b)c` | a or b and c | ac, bc |
| `a+bc` | one or more a and b and c | abc, aabc, aaabc |
| `ab?c` | a and either zero or one b and c | ac, abc |
### Searching strings
| Expression | Definition | Example |
| --- | --- | --- |
| `.` | Any character | .ole would match mole, hole, ect |
| `[]` | Matches to a single character within the brackets | `[mh]ole` would match to mole and hole, but not vole |
| `[^]` | Matches to any character except those in the brackets | `[^m]ole` would match to hole and vole, but not mole |
| `*` | Matches the preceding characters zero or more times | `m*ole` would match ole, mole, mmole etc |
### Context free languages
**Context free language** An unambiguous way of describing the syntax of a language useful where the language is complex
Regular expressions map directly to state transition diagrams.
However, there are situations where the grammar used within a language is too complex to be defined by regular expressions.
the key problem with regular expressions is that they only work when matching or counting other symbols in a string where there is a finite limit.
Consider a binary palindrome. It is not possible to create a regular expression that describes the syntax as there is no regular expression that can describe how each letter is used.
When the counting and matching is infinite, a context free language is needed.
Context free languages can also support notation for recursion and are sometimes a clearer way of defining syntax even where regular expressions can be used.
### Backus-Naur form (BNF)
**Backus-Naur Form** A form of notation for describing the syntax used by a programming language
BNF produces a set of acceptable strings, which effectively describe the rules of the language.
It uses a set of rules that define the language in the format:
```
<S> ::= <alternative1> | <alternative2> | <alternative3>
<alternative1> ::= <alternative2> | <alternative4>
<alternative4> ::= terminal
```
BNF works by replacing the symbol on the left with the symbols on the right until the string is defined.
The idea is to keep going until you reach a terminal, which is a rule that cannot be broken down any further.
In the example above:
- Each symbol or element is enclosed within chevrons
- The `::=` symbol means 'is replace with' and defines the rule for the symbol
- Each symbol needs to be split down further until you reach a terminal
To define integers a BNF expression may look like this:
```
<integer> ::= <digit> | <digit> <integer>
<digit> :: = 0|1|2|3|4|5|6|7|8|9
```
This defines an integer as either a digit or a digit followed by another integer.
A digit is defined as the number 0 to 9 and this is a terminal as there is no further rule needed to define digits.
This expression would be recursive as the integer is defined in terms of itself.
Consider customer details held in a database:
```
<customerdetails> ::= <name> <address>
<name> ::= <title> <firstname> <lastname>
<address> ::= <housenumber> <streetname> <town> <country> <postcode>
...
<housenumber> ::= <integer>
// integer as above
```
This shows how BNF can produce simple rules which can be written as:
- Customer dtails must be made up of name and address
- Name must be made up of title, first name, and last name
- Address must be made up of house number, street name, town, country, and postcode
- House number must be made up of an integer
This is only a partial BNF definition and could be continued to further split down the name into a series of acceptable characters, or the postcode into a series of letters and numbers.
### Syntax diagrams
BNF expressions, or any kind of context free language, can be represented as a syntax diagram.
- An oval shape represents a terminal element
- A rectangular shape represents a non-terminal element and will therefore have another syntax diagram that breaks it down into more detail
- A rectangular shape with an arrowed path around it represents a non-terminal element that may be used more than once
**Syntax diagram** A method of visualising rules written in a context-free language
Syntax diagrams are modular so there are likely to be many syntax diagrams required to represent a whole language.
Each has an entry point and exit point to identify the start and end of each particular part.
## Maths for regular expressions
### Sets
A set is a collection of unordered values where each value appears only once in the set.
The values in the set are sometimes referred to as elements, objects, or members.
$\mathbb{N} = \{0, 1, 2, 3, 4, 5, ...\} \text{ The natural numbers}$
$\mathbb{Z} = \{..., -3, -2, -1, 0, 1, 2, 3, ..,\} \text{ The integers}$
**Natural number** A positive integer including zero
**Set building** The process of creating sets by describing them using notation rather than listing the elements
$A = \{x | x \in \mathbb{N} \land x \geq 1\}$
- $A$ is the name of the sets
- The braces $\{\}$ represent the contents of the set
- $x$ represents the actual values of the set, defined after the pipe $|$
- The pipe means such, meaning that the equation after defines the values of $x$
- $\in$ means "is a member of"
- $\mathbb{N}$ is the natural numbers
- $\land$ means "and"
- $x \geq 1$ means that $x$ must satisfy this inequality to be part of the set
**Member** A value or element that belongs to a set
#### The binary set
The binary set is usually denoted $\Sigma = \{0, 1\}$
This shows that the elements of the set of binary values are $0$ or $1$.
to build a set of all binary strings containing two bits, you would use $\Sigma^2$ where the ${}^2$ indicates two bits.
$\Sigma^2 = \{00, 01, 01, 10, 11\}$
In some cases the number of elements may be infinite.
$A = \{0^n 1^n | n \geq 1 \}$ would produce the set $\{01, 0011, 000111, ... \}$ representing all binary strings with an equal number of 0s and 1s, with the 0s preceding the 1s.
#### Representing a set in a programming language
Many programming languages have built in set-building routines.
- In Python `a = set([0, 1, 2, 3])` produces a set, as does `a = set(x**2 for x in [1, 2, 3])`
- In Haskell `[0..100]` produces a list containing the values 0 to 100, and `[x*2 | x <- [0..100]]` produces a list of the doubles of these numbers
- In C# `IEnumerable<int> numbers = Enumerable.Range(0, 9)` produces the integers 0 to 9. `var events = from num in numbers where num%2 == 0 select num;` would then extract the even numbers
#### The empty set
The empty set is represented as either $\{\}$ or $\emptyset$
**Empty set** The set that contains no values
#### Finite and infinite sets
When a set is finite, it has cardinality which means that it can be counted using natural numbers.
It can also be referred to as a countable set.
The cardinality of the set is the number of elements in the set.
**Countably infinite set** Sets where the elements can be put into a one-to-one correspondence with the set of natural numbers
**Cartesian product $A\times B$** Combining the elements of two or more sets to create a set of ordered pairs
**Union $A\cup B$** When two sets are joined and all of the elements of both sets are included in the joined set
**Intersection $A\cap B$** Describes which elements are common to both sets when the two sets are joined
**Difference $A\ominus B$ or $A \Delta B$** Describes which elements differ when two sets are joined together
#### Subsets
Where all the elements of one set are also contained within another set, it is said to be a subset.
**Subset** set where the elements of one are entirely contained within the other. This can include two sets that are exactly the same
**Proper subset** Where one st is wholly contained within another and the other set has additional elements
## Big O notation and classification of algorithms
### Functions
**Function** Relates each element of a set with the element of another set
**Domain** All the values that may be input to a mathematical function
**Codomain** All the values that may be output from a mathematical function
### Big O notation
This notation is used to classify the time and space complexity of an algorithm.
It considers the worst-case scenario.
**Constant time** Where the time taken to run an algorithm does not vary with the input size
**Linear time** When the time taken to run an algorithm increases in direct proportion with the input size
**Exponential time** When the time taken to run an algorithm increases as an exponential function of the number of inputs
**Logarithmic time** Where the time taken to run an algorithm increases or decreases in line with a logarithm
### Tractable and intractable problems
**Tractable problem** A problem that can be solved in an acceptable amount of time
**Intractable problem** A problem that cannot be solved in an acceptable time frame
**Heuristic** A method for producing a 'rule of thumb' to produce an acceptable solution to intractable problems
### Unsolvable problems
**Unsolvable problem** a problem that it has been proved cannot be solved on a computer
**Halting problem** An example of an unsolvable problem where it is impossible to write a program that can work out whether another problem will halt given a particular input
The Halting problem demonstrates that there are some problems that cannot be solved by a computer
# Beginning of Paper 2 content
# Data representation
## Number systems
**Natural numbers** A positive integer including zero
**Integer** Any whole positive or negative number including zero
**Rational numbers** Any number that can be expressed as a ratio of integers
**Irrational number** A number that cannot be represented as a fraction or ratio as the decimal form will contain an infinite number of values
**Real numbers** Any positive or negative number with or without a fractional part
**Ordinal number** A number used to identify position relative to other numbers
**Cardinal number** A number that identifies the size of something
**Well ordered set** A group of related numbers with a defined order
Ordinal numbers are used to identify the position of data within an ordered set.
## Number bases
**Number base** The number of digits available within a particular number system
**Bit** A single binary digit from a binary number
**Byte** A group of bits, usually 8
**Unit** The grouping together of bits or bytes to form larger blocks of measurement
## The binary number system
### Adding unsigned binary integers
**Unsigned binary** Binary that represents positive numbers only
To add two numbers together in binary, first line up the numbers.
Add the columns starting from the right hand side, carrying 11 to the next column,
### Multiplying unsigned binary integers
To multiply in binary, you multiple the first number be each of the digits of the second number in turn, starting from the right hand side.
This means that you are multiplying each each digit by 0 or 1.
You then do the same for the next digit, shifting your answers to the left as you would in decimal multiplication.
You can then carry out a binary addition to find the final answer.
```
11011 *
11
------------
11011 (1 * 11011)
110110 (1 * 110110)
------------
1010001
------------
1111
```
### Two's complement
**Two's complement** A method of working with signed binary values
Two's complement is a method used to represent signed integers in binary form. This means that it can be used to represent positive and negative integers.
The first digit in a two's complement integer has a negative value, and all of the other digits have positive values.
#### Adding and subtracting using two's complement
Adding numbers together using two's complement is the same as adding numbers together in decimal. You add up the total and carry values across to the next column.
In order to carry out subtractions, the method used is to convert the number to be subtracted to a negative value, and then to add it.
### Fixed point numbers
In order to represent real decimal numbers, fixed point representation can be used. In the same way that decimal has a decimal point, binary has a binary point. The numbers after the binary point represent fractions.
**Fixed point** Where the decimal/binary point is fixed within a number
The binary point is not actually stored in the 8 bit code, its position is fixed by the programmer.
### Floating point numbers
**Floating point** Where the decimal/binary point can move within a number
In floating point, the binary point can be moved depending on the number that you are trying to represent. It 'floats' left to right rather than being in a fixed position.
Floating point extends the fixed point technique.
A floating point number is made up of two parts, the mantissa and the exponent.
In binary, the exponent and mantissa are used to allow the binary point to float. They are each two's complement values.
To calculate the value of a floating point value, we first calculate the value of the mantissa and then multiply it by 2 to the power of the exponent.
This is equivalent to finding the value of the exponent, 'floating' the binary point to n places to the right if the exponent is n, and n places to the left if the exponent is -n, before evaluating the binary as a fixed point value.
### Underflow and overflow
**Overflow** When a number is too large to be represented with the number of bits allocated
**Underflow** When a number is too small to be represented with the number of bits allocated
### Normalisation and precision
**Normalisation** A process for adjusting numbers onto a common scale
**Precision** How close a value is to the true value
In order to be normalised, the first bit of the mantissa, after the binary point, should always be 1 for positive integers, or 0 for negative numbers and the bit before the binary point should be the opposite.
A normalised positive floating point number must start 0.1 and a normalised negative floating point number must start 1.0
#### Example
Consider the number 108. The 8 bit binary equivalent of which is 01101100.
- The normalised mantissa would be 0.1101100
- The binary point will have to be moved seven places to the right to convert it back to the original number
- The exponent is therefore 7
- The two's complement value for 7 is 0111
- Therefore the normalised representation of 108 is 0.11011000111
### Rounding errors
When working with decimal numbers, we are used to the idea of rounding numbers up or down. As a consequence, we will get rounding errors.
A similar phenomenon occurs with binary representation. If you attempt to convert 0.1 into binary you will find that you get a recurring number, so it is not possible to exactly represent it.
If you try to represent 1.95 with 8 bits and a fixed point 1.1111010 gives 1.953125 while 1.1111001 gives 1.943125. With 8 bits the nearest value is 0.003125 out.
Extending the number of bits would allow for an answer closer to 1.95
### Absolute and relative errors
There are two main methods for calculating the degree of error in number that we use within a program.
The absolute error is the actual numerical difference between the answer and the approximation.
The relative error is the absolute error divided by the true value.
## Coding systems
**Character code** A binary representation of a particular letter, number, or special character
### ASCII and Unicode
The ASCII (American Standard Code for Information Interchange) was agreed upon. 7 bits of each byte are used to represent 128 characters, while the 8th bit can be used for checking.
ASCII has limitations:
- 256 characters are not sufficient to represent all of the possible characters, numbers, and symbols
- It was initially developed in English and therefore did not represent all of the other languages and scripts in the world
- Widespread use of the web made it more important to have a universal international coding system
- The range of platforms and programs has increased dramatically with more developers from around the world using a much wider range of characters
**Unicode** A standard binary coding system that has superseded ASCII
Unicode is being constantly developed and updated to include more languages and characters from around the world.
### Error checking and correction