Browse code

Added Binary Search algorithm; updated the README accordingly.

hdw authored on 21/07/2020 17:02:43
Showing 3 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,27 @@
1
+'''
2
+Recursive binary search algorithm
3
+Hayden Walker
4
+21 July 2020
5
+'''
6
+
7
+# The array and the target
8
+myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
9
+target = 11
10
+
11
+def binarySearch(top, bottom, target):
12
+	# Try and look for the target
13
+	try:
14
+		mid = (top + bottom) // 2 # Get midpoint of array
15
+
16
+		if myArray[mid] == target: # If the midpoint is the target, then return it
17
+			return mid
18
+		elif myArray[mid] > target: # If mid > target, set the midpoint as the top limit
19
+			return binarySearch(mid, bottom, target)
20
+		elif myArray[mid] < target: # If mid < target, set the midpoint as the bottom limit
21
+			return binarySearch(top, mid, target)
22
+
23
+	# If the program reaches max recursion depth, the target isn't there
24
+	except:
25
+		return "null"
26
+
27
+print(binarySearch(len(myArray), 0, target))
... ...
@@ -1,6 +1,6 @@
1
-Hayden Walker 2019
1
+Hayden Walker 2019-2020
2 2
 
3
-This is a portfolio of some of my favourite programs that I wrote during the summer of 2019.
3
+This is a portfolio of some of my favourite programs that I wrote during the summer of 2019 and onward.
4 4
     *The programs are arranged alphabetically (By folder name, not by title)
5 5
 
6 6
 NOTE: The following dependencies are required in order to successfully run some of these programs. These dependencies are *not* included in the Python Standard Library.
... ...
@@ -8,12 +8,12 @@ NOTE: The following dependencies are required in order to successfully run some
8 8
     *Pygame (for the aquarium program as well as Conway's Game of Life)
9 9
 
10 10
 ------------------------------------------------------------------------------------------------------------
11
-Title (Date)
11
+Title (Date) [Dependencies]
12 12
     *Skills
13 13
 
14 14
     Desc.
15 15
 ------------------------------------------------------------------------------------------------------------
16
-Aquarium (19 August) [Requires Pygame]
16
+Aquarium (19 August 2019) [Requires Pygame]
17 17
     *Pygame
18 18
         *Importing sprites
19 19
     *Random library
... ...
@@ -29,7 +29,15 @@ Aquarium (19 August) [Requires Pygame]
29 29
 
30 30
     A simple aquarium. A random number of fish are spawned, each being an instance of the Fish class. Each is randomly assigned one of four breeds, and each has a randomly assigned speed. The fish will change directions when they come in contact with the screen's edge. Furthermore, each 50 millisecond cycle, each fish has a 1% chance (for each axis) of changing direction on its own, regardless of its position.
31 31
 ------------------------------------------------------------------------------------------------------------
32
-Binary to Decimal Converter (18 August)
32
+Binary Search Algorithm (21 July 2020)
33
+    *Recursion
34
+    *Conditionals
35
+    *Exception handling
36
+
37
+    A binary search algorithm. A sorted array of numbers is given along with a number to search for. The function
38
+will return the position of the target number in the array, or 'null' if it isn't there. Uses recursion.
39
+------------------------------------------------------------------------------------------------------------
40
+Binary to Decimal Converter (18 August 2019)
33 41
     *String slicing
34 42
         *Reversing
35 43
         *Concatenation
... ...
@@ -40,7 +48,7 @@ Binary to Decimal Converter (18 August)
40 48
     
41 49
     A simple binary to decimal converter; will accept an arbitrtary amount of bytes of arbitrary lengths.
42 50
 ------------------------------------------------------------------------------------------------------------
43
-Caesar Cypher (25 July)
51
+Caesar Cypher (25 July 2019)
44 52
     *Iterables
45 53
     *Conditionals
46 54
     *For Loops    
... ...
@@ -49,7 +57,7 @@ Caesar Cypher (25 July)
49 57
     
50 58
     Takes an encryption key (n) from 0-25 and moves each character ahead n spaces in the alphabet. If the letter reaches z, it wraps back to a.
51 59
 ------------------------------------------------------------------------------------------------------------
52
-Cash register (3 August)
60
+Cash register (3 August 2019)
53 61
     *Rounding
54 62
     *While Loops
55 63
     *Conditionals
... ...
@@ -57,7 +65,7 @@ Cash register (3 August)
57 65
     
58 66
     Takes a sales total and amount tendered, then calculates change and lists the amount of each denomination of change to return.
59 67
 ------------------------------------------------------------------------------------------------------------
60
-Collatz Conjecture (7 August) [Requires Matplotlib]
68
+Collatz Conjecture (7 August 2019) [Requires Matplotlib]
61 69
     *A Classic Algorithm
62 70
     *While Loops
63 71
     *Conditionals
... ...
@@ -69,7 +77,7 @@ Collatz Conjecture (7 August) [Requires Matplotlib]
69 77
     
70 78
     Takes a positive integer (n), and depending on its parity, will either divide it by 2 or multiply it by 3 then add 1, and repeat the whole process until the integer is equal to 1. It will then display a graph where x is the number of steps and y is the value of n.
71 79
 ------------------------------------------------------------------------------------------------------------
72
-Conway's Game of Life (22 August) [Requires Pygame]
80
+Conway's Game of Life (22 August 2019) [Requires Pygame]
73 81
     *A Cellular Automaton
74 82
     *Random Library
75 83
     *PyGame
... ...
@@ -83,7 +91,7 @@ Conway's Game of Life (22 August) [Requires Pygame]
83 91
 
84 92
     Generates 100 squares that can each be either living or dead; each 1-second "generation," if a living square has less than two or more than three living neighbours, it will die from either underpopulation or crowding. If a dead square has exactly three living neighbours, it will become living, via reproduction.
85 93
 ------------------------------------------------------------------------------------------------------------
86
-Line/square drawing (27 July)
94
+Line/square drawing (27 July 2019)
87 95
     *Tkinter GUI Library
88 96
         *Canvas
89 97
         *Buttons
... ...
@@ -96,7 +104,7 @@ Line/square drawing (27 July)
96 104
     
97 105
     A GUI application that takes a number (n) and, depending on which button is pressed, will generate either n lines or n rectangles, all of random size, position, and colour. It will display the last action completed in a status bar at the bottom.
98 106
 ------------------------------------------------------------------------------------------------------------
99
-Hangman (5 August)
107
+Hangman (5 August 2019)
100 108
     *Random Library
101 109
     *Reading from a text file
102 110
     *For Loops
... ...
@@ -107,7 +115,7 @@ Hangman (5 August)
107 115
     
108 116
     A game of hangman that pulls words from a 100-word text file.
109 117
 ------------------------------------------------------------------------------------------------------------
110
-Minefield/Minesweeper (20 July)
118
+Minefield/Minesweeper (20 July 2019)
111 119
     *System commands
112 120
     *Random Library
113 121
     *Wrapping
114 122
Binary files a/README.pdf and b/README.pdf differ