Skip to content

Commit 8dac298

Browse files
committed
This is just a collection of smaller python programs that I used for learning and practice purpouses
1 parent b8bad5d commit 8dac298

File tree

12 files changed

+522
-0
lines changed

12 files changed

+522
-0
lines changed

Bubble-sort/Bubble_sort.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import random
2+
3+
#Create the list
4+
bubble_list = list()
5+
i = 0
6+
while (i < 10):
7+
bubble_list.append(random.randint(1, 1000))
8+
i += 1
9+
print(bubble_list)
10+
length = len(bubble_list)
11+
12+
pos = 1
13+
swaps = 0
14+
times_swapped = 0
15+
sorted = False
16+
17+
#Sort the actual list
18+
while sorted == False:
19+
prev_swap = swaps
20+
for pos in range(1, length):
21+
if bubble_list[pos - 1] > bubble_list[pos]:
22+
23+
pl_hldr = bubble_list[pos]
24+
25+
bubble_list[pos] = bubble_list[pos - 1]
26+
27+
bubble_list[pos - 1] = pl_hldr
28+
29+
swaps += 1
30+
times_swapped += 1
31+
32+
if(swaps == prev_swap):
33+
sorted = True
34+
35+
print(bubble_list)
36+
print("Times swapped:",times_swapped)

Bubble-sort/readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Just a bubble sort.
2+
3+
the program generates a list of 10 random numbers
4+
in the span of 1 to 1000.
5+
6+
The number of swaps is also recorded

Caesar-Cipher/Caesar_cipher.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
def encrypt(message, key):
2+
message.upper()
3+
alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
4+
crypted = []
5+
msg = ""
6+
translator = message.upper()
7+
for i in range(len(translator)):
8+
if translator[i] in alphabet:
9+
value = alphabet.index(translator[i])
10+
if(value+key <= 25):
11+
crypted.append(alphabet[value+key])
12+
else:
13+
numb = ((value+key)-26)
14+
crypted.append(alphabet[(-26+numb)])
15+
else:
16+
crypted.append(translator[i])
17+
msg = ''.join(crypted)
18+
return msg.lower()
19+
20+
def decrypt(message, key):
21+
message.upper()
22+
alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
23+
crypted = []
24+
msg = ""
25+
translator = message.upper()
26+
for i in range(len(translator)):
27+
if translator[i] in alphabet:
28+
value = alphabet.index(translator[i])
29+
if(value-key >= 0):
30+
crypted.append(alphabet[value-key])
31+
else:
32+
numb = (value-key)
33+
crypted.append(alphabet[numb])
34+
else:
35+
crypted.append(translator[i])
36+
msg = ''.join(crypted)
37+
return msg.lower()
38+
39+
running = True
40+
41+
while(running):
42+
print("Would you like to encrypt(1) or decrypt(2) or exit(3)?")
43+
choice = input("Enter choice: ")
44+
45+
if(choice == "1" or choice == "2" or choice == "3"):
46+
if(choice == "1"):
47+
message = str(input("Type your encryption message: "))
48+
key = int(input("Type your key: "))
49+
print(encrypt(message,key),"\n")
50+
51+
if(choice == "2"):
52+
message = str(input("Type your decryption message: "))
53+
key = int(input("Type your key: "))
54+
print(decrypt(message,key),"\n")
55+
56+
if(choice == "3"):
57+
running = False
58+
else:
59+
print("\nInvalid input\n")

Caesar-Cipher/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Just a Caesar cipher
+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
import os, hashlib
2+
from os import path
3+
4+
#Collects the location of the folder to overlook and the location for the log file
5+
current_location = path.dirname(__file__)
6+
file_to_overlook = path.join(current_location, "./"+"YOUR-FILENAME")
7+
log_file = str(current_location) + "/" + "YOUR-FILENAME"
8+
9+
#Gets the location of all items in the file
10+
def get_all_items(file_to_overlook):
11+
all_items = []
12+
13+
for item in os.listdir(file_to_overlook):
14+
15+
file_items = file_to_overlook + "/" + item
16+
17+
if(path.isdir(file_items) == True):
18+
all_items += get_all_items(file_items)
19+
20+
else:
21+
all_items.append(file_items)
22+
23+
return all_items
24+
25+
#If the log file exists, looks through the file and
26+
try:
27+
changes = False
28+
removed = False
29+
added = False
30+
31+
added_files = []
32+
removed_files = []
33+
existing_files = []
34+
changed_files = []
35+
hash_current_files = []
36+
37+
log_items = []
38+
log_hashes = []
39+
40+
with open(log_file) as log_to_read:
41+
read_log = log_to_read.readlines()
42+
read_log = [back_n.strip() for back_n in read_log]
43+
44+
#separates the name of the file from its hash value
45+
for string in read_log:
46+
separator = string.index(" : ")
47+
48+
log_items.append(string[0:separator])
49+
50+
log_hashes.append(string[(separator + 3):])
51+
52+
#Detects if the hashed content of all files currently in the folder are the same as in the log file
53+
for file_to_hash in get_all_items(file_to_overlook):
54+
55+
with open(file_to_hash, "r") as content_to_hash:
56+
hashed_content = content_to_hash.readlines()
57+
58+
hashed_content = [back_n.strip() for back_n in hashed_content]
59+
60+
sha256 = hashlib.sha256()
61+
62+
sha256.update(str(hashed_content).encode("utf-8"))
63+
64+
file_hash = sha256.hexdigest()
65+
66+
hash_current_files.append(file_to_hash + " : " + file_hash)
67+
68+
#Detects if there are more or less items in the folder
69+
for item in get_all_items(file_to_overlook):
70+
71+
if (len(log_items) <= len(get_all_items(file_to_overlook))):
72+
existing_files.append(item)
73+
74+
if (len(log_items) >= len(get_all_items(file_to_overlook))):
75+
existing_files.append(item)
76+
77+
#Gets the removed files and puts them in the removed file list
78+
for item in range(len(log_items)):
79+
80+
if (log_items[item] not in existing_files):
81+
82+
removed = True
83+
84+
removed_files.append(log_items[item])
85+
86+
#Gets the added files and puts them in the added file list
87+
for item in range(len(existing_files)):
88+
89+
if(existing_files[item] not in log_items and existing_files[item] not in added_files):
90+
91+
added = True
92+
93+
added_files.append(existing_files[item])
94+
95+
#If the hash value of a certain file has been altered it will be detected here
96+
for item in read_log:
97+
if(item in hash_current_files):
98+
del hash_current_files[hash_current_files.index(item)]
99+
100+
for item in hash_current_files:
101+
if item not in read_log:
102+
changes = True
103+
changed_files.append(item)
104+
105+
for item in changed_files:
106+
separator = item.index(" : ")
107+
108+
if(item[0:separator] in added_files):
109+
del changed_files[changed_files.index(item)]
110+
111+
if(item[0:separator] in removed_files):
112+
del changed_files[changed_files.index(item)]
113+
114+
#if files have been changed
115+
if (changes == True):
116+
117+
print("The folowing file(s) have been changed\n")
118+
print(*changed_files, sep="\n")
119+
120+
item_to_log = {}
121+
122+
outfile = open(log_file, "w+")
123+
124+
#updates the log file
125+
for item in get_all_items(file_to_overlook):
126+
127+
with open(item, "r") as content_to_hash:
128+
hashed_content = content_to_hash.readlines()
129+
130+
hashed_content = [back_n.strip() for back_n in hashed_content]
131+
132+
sha256 = hashlib.sha256()
133+
134+
sha256.update(str(hashed_content).encode("utf-8"))
135+
136+
file_hash = sha256.hexdigest()
137+
138+
item_to_log[str(hashed_content)] = file_hash
139+
140+
outfile.write(item + " : " + item_to_log[str(hashed_content)])
141+
142+
outfile.write("\n")
143+
144+
outfile.close()
145+
146+
#if files have been added
147+
if(added == True):
148+
print("These files have been added:\n")
149+
print(*added_files, sep="\n")
150+
151+
item_to_log = {}
152+
153+
outfile = open(log_file, "w+")
154+
155+
#updates the log file
156+
for item in get_all_items(file_to_overlook):
157+
158+
with open(item, "r") as content_to_hash:
159+
hashed_content = content_to_hash.readlines()
160+
161+
hashed_content = [back_n.strip() for back_n in hashed_content]
162+
163+
sha256 = hashlib.sha256()
164+
165+
sha256.update(str(hashed_content).encode("utf-8"))
166+
167+
file_hash = sha256.hexdigest()
168+
169+
item_to_log[str(hashed_content)] = file_hash
170+
171+
outfile.write(item + " : " + item_to_log[str(hashed_content)])
172+
173+
outfile.write("\n")
174+
175+
outfile.close()
176+
177+
#if files have been removed
178+
if(removed == True):
179+
print("These files have been removed:\n")
180+
print(*removed_files, sep="\n")
181+
182+
item_to_log = {}
183+
184+
outfile = open(log_file, "w+")
185+
186+
#updates the log file
187+
for item in get_all_items(file_to_overlook):
188+
189+
with open(item, "r") as content_to_hash:
190+
hashed_content = content_to_hash.readlines()
191+
192+
hashed_content = [back_n.strip() for back_n in hashed_content]
193+
194+
sha256 = hashlib.sha256()
195+
196+
sha256.update(str(hashed_content).encode("utf-8"))
197+
198+
file_hash = sha256.hexdigest()
199+
200+
item_to_log[str(hashed_content)] = file_hash
201+
202+
outfile.write(item + " : " + item_to_log[str(hashed_content)])
203+
204+
outfile.write("\n")
205+
206+
outfile.close()
207+
208+
#if nothing has changed
209+
if(changes == False and added == False and removed == False):
210+
print("No changes")
211+
212+
#If the log file does not already exist ceate one
213+
except FileNotFoundError:
214+
215+
item_to_log = {}
216+
217+
outfile = open(log_file, "w+")
218+
219+
for item in get_all_items(file_to_overlook):
220+
221+
with open(item, "r") as content_to_hash:
222+
hashed_content = content_to_hash.readlines()
223+
224+
hashed_content = [back_n.strip() for back_n in hashed_content]
225+
226+
sha256 = hashlib.sha256()
227+
228+
sha256.update(str(hashed_content).encode("utf-8"))
229+
230+
file_hash = sha256.hexdigest()
231+
232+
item_to_log[str(hashed_content)] = file_hash
233+
234+
outfile.write(item + " : " + item_to_log[str(hashed_content)])
235+
236+
outfile.write("\n")
237+
238+
outfile.close()

Filechange-Detector/readme.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This program will overlook a file and all its sub directories
2+
3+
Using sha256 the program will make a log file of
4+
all files in the directory and notify the user
5+
if any changes have been made, such as adding
6+
removal or edits.
7+
8+
Simply change the "YOUR-FILENAME" in the
9+
file_to_overlook and the log_file

0 commit comments

Comments
 (0)