Skip to content

Commit d6b0f19

Browse files
committed
first commit
0 parents  commit d6b0f19

File tree

5,778 files changed

+2094667
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

5,778 files changed

+2094667
-0
lines changed

Diff for: .gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.vscode
2+
/helsinki_env
3+
/programming-24-Course-Data
4+
/rage programming-23 main data.zip
5+
/rage programming-24 main data.zip
6+
/test-pending.py
7+
/test.md
8+
/test_pygame.py
9+
/__pycache__

Diff for: 0 - Master Notes.md

+11,470
Large diffs are not rendered by default.

Diff for: 1 - Pygame.md

+1,218
Large diffs are not rendered by default.

Diff for: Algorithms.py

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Sorting Algorithms
2+
# A sorting algorithm is a method or a process used to rearrange elements in a list or an array in a
3+
# certain order, whether it be ascending, decending, or even based on some complex rules.
4+
5+
# Bubble Sort
6+
# Bubble sort is a simple sorting algorithm that repeatedly steps through the array, element by element
7+
# comparing the current element with the one after it, swapping their values if the former is larger than the latter.
8+
arr = [12, 20, 15, 29, 10, 14]
9+
10+
def bubbleSort(arr):
11+
n = len(arr)
12+
for i in range(n-1):
13+
swapped = False
14+
for j in range(n-i-1):
15+
if arr[j] > arr[j+1]:
16+
arr[j], arr[j+1] = arr[j+1], arr[j]
17+
swapped = True
18+
if not swapped:
19+
break
20+
21+
bubbleSort(arr)
22+
print(arr)
23+
24+
# Insertion Sort
25+
# Insertion sort is a simple sorting algorithm that builds the final sorted array one element at a time
26+
arr = [13, 20, 7, 28, 3, 8]
27+
28+
def insertionSort(arr):
29+
for i in range(1, len(arr)):
30+
key = arr[i]
31+
j = i - 1
32+
while j >= 0 and arr[j] > key:
33+
arr[j + 1] = arr[j]
34+
j -= 1
35+
arr[j + 1] = key
36+
37+
insertionSort(arr)
38+
print(arr)
39+
40+
# Merge Sort
41+
# Merge sort is an efficient, stable, and comparison-based Divide and Conquer sorting algorithm, and its recursive.
42+
# It divides the input array into two halves, calls itself for the halves, sorting them and then merges the two sorted halves.
43+
arr = [39, 45, 45, 29, 50, 48, 41, 18, 14, 22, 27]
44+
45+
def merge_sort(arr):
46+
if len(arr) > 1:
47+
mid = len(arr) // 2
48+
L = arr[:mid]
49+
R = arr[mid:]
50+
51+
merge_sort(L)
52+
merge_sort(R)
53+
54+
merge(arr, L, R)
55+
56+
def merge(arr, L, R):
57+
i = j = k = 0
58+
59+
# Merging the temporary arrays
60+
# back into arr[]
61+
while i < len(L) and j < len(R):
62+
if L[i] < R[j]:
63+
arr[k] = L[i]
64+
i += 1
65+
else:
66+
arr[k] = R[j]
67+
j += 1
68+
k += 1
69+
70+
# Copy the remaining elements
71+
# of L[], if there are any
72+
while i < len(L):
73+
arr[k] = L[i]
74+
i += 1
75+
k += 1
76+
77+
# Copy the remaining elements
78+
# of R[], if there are any
79+
while j < len(R):
80+
arr[k] = R[j]
81+
j += 1
82+
k += 1
83+
84+
merge_sort(arr)
85+
print(arr)
86+
87+
# Other sorting alcorithms:
88+
# Selection, Shell, Heap, Quick and Quick3
89+
90+
91+
# Searching Algorithms
92+
# A searching algorithm is a method or process used to find or retrieve an element from a data structure.
93+
# The goal is to find whether an item exists in the data set, and oftentimes to determine its location.
94+
95+
# Linear Search
96+
# Each element is checked in sequence until you find what you're looking for or the list ends.
97+
# If the current element equals what we're looking for (x), return it.
98+
99+
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
100+
101+
def linear_search(arr, x):
102+
for i in range(len(arr)):
103+
if arr[i] == x:
104+
return i
105+
return -1
106+
107+
print(linear_search(arr, 8))
108+
arr[linear_search(arr, 8)]
109+
110+
# Binary Search
111+
# Binary Search works by repeatedly dividing in half the portion of the list that could contain the item,
112+
# until you've narrowed down the possible location to just one.
113+
114+
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
115+
116+
def binary_search(arr, x):
117+
l = 0
118+
r = len(arr) - 1
119+
while l <= r:
120+
mid = l + (r - l) // 2
121+
if arr[mid] == x:
122+
return mid
123+
elif arr[mid] < x:
124+
l = mid + 1
125+
else:
126+
r = mid - 1
127+
return -1
128+
129+
print(linear_search(arr, 8))
130+
arr[linear_search(arr, 8)]
131+
132+
# Binary Search - 2
133+
134+
def binary_search(target: list, item: int, left : int, right : int):
135+
""" The function returns True if the item is contained in the target list, False otherwise """
136+
# If the search area is empty, item was not found
137+
if left > right:
138+
return False
139+
140+
# Calculate the centre of the search area, integer result
141+
centre = (left+right)//2
142+
143+
# If the item is found at the centre, return
144+
if target[centre] == item:
145+
return True
146+
147+
# If the item is greater, search the greater half
148+
if target[centre] < item:
149+
return binary_search(target, item, centre+1, right)
150+
# Else the item is smaller, search the smaller half
151+
else:
152+
return binary_search(target, item, left, centre-1)
153+
154+
target = [1, 2, 4, 5, 7, 8, 11, 13, 14, 18]
155+
print(binary_search(target, 2, 0, len(target)-1))
156+
print(binary_search(target, 13, 0, len(target)-1))
157+
print(binary_search(target, 6, 0, len(target)-1))
158+
print(binary_search(target, 15, 0, len(target)-1))

Diff for: Angles in Pygame.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
Angles in Pygame
3+
A demonstration by Max Goldstein, Tufts University '14
4+
5+
In pygame, +y is down. Python's arctangent functions expect +y to be up.
6+
This wreaks havoc with the unit circle if you want to find the angle between
7+
two points (for, say, collision detection or aiming a gun). You can avoid the
8+
problem entirely by calling atan2(-y, x) and adding 2*pi to the result if it's
9+
negative.
10+
11+
Note that math.atan2(numerator, denominator) does the division for you.
12+
13+
Controls: move the mouse near the axes.
14+
15+
"""
16+
17+
import pygame, sys, os
18+
from pygame.locals import *
19+
from math import atan2, degrees, pi
20+
21+
def quit():
22+
pygame.quit()
23+
sys.exit()
24+
25+
pygame.init()
26+
screenDimensions = (400, 540)
27+
window = pygame.display.set_mode(screenDimensions)
28+
pygame.display.set_caption('Angles in Pygame')
29+
screen = pygame.display.get_surface()
30+
31+
clock = pygame.time.Clock()
32+
FPS = 50
33+
time_passed = 0
34+
35+
pos = (200, 200)
36+
origin = (200, 200)
37+
38+
white = (255, 255, 255)
39+
black = (0, 0, 0)
40+
red = (255, 0, 0)
41+
blue = (0, 0, 255)
42+
purple= (200, 0, 200)
43+
green = (0, 200, 0)
44+
45+
font = pygame.font.Font(None, 30)
46+
47+
arcRect = pygame.Rect(180, 180, 40, 40)
48+
49+
#Game loop
50+
while True:
51+
time_passed = clock.tick(FPS)
52+
53+
#Handle events
54+
for event in pygame.event.get():
55+
if event.type == pygame.QUIT:
56+
quit()
57+
elif event.type == KEYDOWN:
58+
if event.key == K_ESCAPE:
59+
quit()
60+
elif event.type == MOUSEMOTION:
61+
if 0 < event.pos[0] < 400 and 0 < event.pos[1] < 400:
62+
pos = event.pos
63+
64+
#Angle logic
65+
dx = pos[0] - origin[0]
66+
dy = pos[1] - origin[1]
67+
rads = atan2(-dy,dx)
68+
rads %= 2*pi
69+
degs = degrees(rads)
70+
71+
screen.fill(white)
72+
73+
#Draw coordinate axes and labels
74+
pygame.draw.circle(screen, black, origin, 5)
75+
pygame.draw.line(screen, black, (0, 440), (440, 440), 3)
76+
pygame.draw.line(screen, black, (200, 15), (200, 380))
77+
screen.blit(font.render( "pi/2", True, black), (178, 10))
78+
screen.blit(font.render("3pi/2", True, black), (175, 380))
79+
pygame.draw.line(screen, black, (28, 200), (355, 200))
80+
screen.blit(font.render("pi", True, black), (8, 190))
81+
screen.blit(font.render("0", True, black), (360, 190))
82+
83+
#Draw lines to cursor
84+
pygame.draw.line(screen, blue, origin, (pos[0], origin[1]), 2)
85+
pygame.draw.line(screen, red, (pos[0], origin[1]), (pos[0], pos[1]), 2)
86+
pygame.draw.line(screen, purple, origin, pos, 2)
87+
pygame.draw.arc(screen, green, arcRect, 0, rads, 4)
88+
#Note that the function expects angles in radians
89+
90+
#Draw numeric readings
91+
screen.blit(font.render(str(dx)+" x", True, blue), (10, 450))
92+
screen.blit(font.render(str(dy)+" y", True, red), (100, 450))
93+
screen.blit(font.render(str((dx**2 + dy**2)**.5)+" d", True, purple), (10, 480))
94+
screen.blit(font.render(str(degs)+" degrees", True, green), (10, 510))
95+
96+
pygame.display.flip()

0 commit comments

Comments
 (0)