diff --git a/.vscode/settings.json b/.vscode/settings.json index 2369810..ca20f95 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,7 +17,6 @@ "files.autoSave": "afterDelay", "screencastMode.onlyKeyboardShortcuts": true, "terminal.integrated.fontSize": 18, - "workbench.activityBar.visible": true, "workbench.colorTheme": "Visual Studio Dark", "workbench.fontAliasing": "antialiased", "workbench.statusBar.visible": true diff --git a/src/01 Find Prime Factors/my_factor.py b/src/01 Find Prime Factors/my_factor.py new file mode 100644 index 0000000..d3e5f20 --- /dev/null +++ b/src/01 Find Prime Factors/my_factor.py @@ -0,0 +1,29 @@ +def get_primes(num): + primes = [] + isPrime = False + for numToTest in range(2, num + 1): + if (num % numToTest == 0): + if (not primes): + primes.append(numToTest) + for prime in primes: + if (numToTest not in primes and prime != 1 and numToTest % prime == 0): + isPrime = False + if (isPrime == True): + primes.append(numToTest) + else: + isPrime = True + return primes; + +def get_prime_factors(num): + primes = [] + numTmp = num + for i in get_primes(num): + numTmp = num + while(numTmp % i == 0): + primes.append(i) + numTmp = numTmp / i + return primes + + +print(get_prime_factors(630)) # [2, 3, 3, 5, 7] +print(get_prime_factors(13)) # [13] \ No newline at end of file diff --git a/src/02 Identify a Palindrome/my_palindrome.py b/src/02 Identify a Palindrome/my_palindrome.py new file mode 100644 index 0000000..0052336 --- /dev/null +++ b/src/02 Identify a Palindrome/my_palindrome.py @@ -0,0 +1,11 @@ +def is_palindrome(str): + str = str.upper().replace(" ", "").replace("'", "").replace(",", "") + for i in range((len(str) // 2) + 1): + print(i) + if (str[i] != str[len(str) - 1 -i]): + print(str[len(str) - 1 -i]) + print(str[i]) + return False + return True + +print(is_palindrome("Go hang a salami, I'm a lasagna hog")) # true \ No newline at end of file diff --git a/src/03 Sort a String/my_sort_words.py b/src/03 Sort a String/my_sort_words.py new file mode 100644 index 0000000..3fa62f6 --- /dev/null +++ b/src/03 Sort a String/my_sort_words.py @@ -0,0 +1,4 @@ +def sort_string(str): + return' '.join(sorted(str.split(" "), key=lambda s: s.lower())) + +print(sort_string('banana ORANGE apple')) \ No newline at end of file diff --git a/src/04 Find All List Items/my_index_all.py b/src/04 Find All List Items/my_index_all.py new file mode 100644 index 0000000..b84d4c8 --- /dev/null +++ b/src/04 Find All List Items/my_index_all.py @@ -0,0 +1,27 @@ +def index_all(array, search): + print("array " , array) + tab = [] + for i, value in enumerate(array): + if (type(value) == list and value != search): + tabList = [] + a = index_all(value, search) + if (a): + for n in a: + if (type(n) == list): + n.append(i) + tabList.append(n) + else: + tabList.append([i,n]) + for tabListItem in tabList: + tab.append(tabListItem) + print("ddd tab " , tab, "array " , array, "value" , value, " a = " , a) + else: + if (value == search): + tab.append(i) + print("array " , array, "tab " , tab, "value" , value) + return tab + +# example = [[[1, 2, 3], 2, [1, 3]], [1, 2, 3]] +example = [[[1, 2, 3], 2, [1, 3]], [1, 2, 3]] +print(index_all(example, 2)) # [[0, 0, 1], [0, 1], [1, 1]] +print(index_all(example, [1, 2, 3])) # [[0, 0], [1]] \ No newline at end of file diff --git a/src/05 Play the Waiting Game/my_waiting_game.py b/src/05 Play the Waiting Game/my_waiting_game.py new file mode 100644 index 0000000..fbcedac --- /dev/null +++ b/src/05 Play the Waiting Game/my_waiting_game.py @@ -0,0 +1,25 @@ +import time + +def waiting_game(): + print("Your target time is 4 seconds") + x = input("---Press Enter to Begin--") + end = 0 + print("x = " , x) + if (x != None): + start = time.time() + y = input("---Press Enter to Begin--") + print("start ", start) + print("x = " , x) + if (y != None): + end = time.time() + print("end ", end) + print("Elapsed time: " , abs(round((end - start) *1000) /1000)," seconds") + if ((end - start) >4): + s ="seconds too slow" + else: + s = "seconds too fast" + print("(", + abs(4 - round((end - start) *1000) /1000), + s," )" ) + +waiting_game() \ No newline at end of file diff --git a/src/06 Save a Dictionary/my_dictionary.py b/src/06 Save a Dictionary/my_dictionary.py new file mode 100644 index 0000000..1d10cb1 --- /dev/null +++ b/src/06 Save a Dictionary/my_dictionary.py @@ -0,0 +1,13 @@ +import json +def save_dict( dict,filePath): + with open(filePath, "w") as outfile: + json.dump(dict, outfile) + outfile.close() +def load_dict(filePath): + with open(filePath, "r") as outfile: + jsonString = outfile.read() + return json.loads(jsonString) +test_dict = {1: 'a', 2: 'b', 3: 'c'} +save_dict(test_dict, 'test_dict.pickle') +recovered = load_dict('test_dict.pickle') +print(recovered) \ No newline at end of file diff --git a/src/07 Schedule a Function/my_schedule_function.py b/src/07 Schedule a Function/my_schedule_function.py new file mode 100644 index 0000000..988a047 --- /dev/null +++ b/src/07 Schedule a Function/my_schedule_function.py @@ -0,0 +1,16 @@ +import sched +import time +from datetime import datetime + + +def schedule_function(event_time, function, *args): + scheduler = sched.scheduler(time.time, time.sleep) + scheduler.enterabs( event_time.timestamp(), 1, function, args) + scheduler.run() + + + +def hello(): + print("ggggggggggggggg") + +schedule_function(datetime(2024, 9, 8, 0, 28), hello, None) \ No newline at end of file diff --git a/src/08 Send an Email/my_send_email.py b/src/08 Send an Email/my_send_email.py new file mode 100644 index 0000000..678f10e --- /dev/null +++ b/src/08 Send an Email/my_send_email.py @@ -0,0 +1,20 @@ +import smtplib + +# Import the email modules we'll need +from email.message import EmailMessage + +# Open the plain text file whose name is in textfile for reading. +def send_mail(receiverAdress, subject, content): + msg = EmailMessage() + msg.set_content(content) + sender = "armand.fouquiau@orange.fr" + msg['Subject'] = subject + msg['From'] = sender + msg['To'] = receiverAdress + with smtplib.SMTP_SSL('smtp.orange.fr', 465) as smtp_server: + smtp_server.login(sender, "") + smtp_server.sendmail(sender,receiverAdress, msg.as_string()) + + + +send_mail('armand.fouquiau@orange.fr', 'Notification', 'Everything is awesome!') \ No newline at end of file diff --git a/src/09 Simulate Dice/my_roll_dice.py b/src/09 Simulate Dice/my_roll_dice.py new file mode 100644 index 0000000..08e516f --- /dev/null +++ b/src/09 Simulate Dice/my_roll_dice.py @@ -0,0 +1,35 @@ +import random + +def roll_dice(*args): + nbTirage = 1000000 + resultDice = [] + sumResultDice = [] + + + for i in range(len(args)): + a = [] + for l in range(nbTirage): + a.append(random.randrange(1, args[i] + 1)) + resultDice.append(a) + for i in range(nbTirage): + sum = 0 + for r in range(len(args)): + sum = sum + resultDice[r][i] + sumResultDice.append(sum) + sumNumberSide = 0 + for r in range(len(args)): + sumNumberSide = sumNumberSide + args[r] + dict ={} + for outcome in range(len(args), sumNumberSide + 1): + for i in sumResultDice: + if (outcome is i): + if (outcome not in dict.keys()): + dict[outcome] = 1 + else: + dict[outcome] = dict[outcome] + 1 + print(dict) + for k,j in dict.items(): + print(k, " ", round( (j/ nbTirage) * 10000) / 100) + +roll_dice(4, 6, 6) +roll_dice(4, 6, 6, 20) \ No newline at end of file diff --git a/src/10 Count Unique Words/my_count_words.py b/src/10 Count Unique Words/my_count_words.py new file mode 100644 index 0000000..76d76cc --- /dev/null +++ b/src/10 Count Unique Words/my_count_words.py @@ -0,0 +1,29 @@ +import re +def count_words(filePath): + with open(filePath, "r") as infile: + s = infile.read() + tokens =[token.upper() for token in re.findall(r'[0-9a-zA-Z-]+', s)] + setWord = list(set(tokens)) + dict = {} + + for i in tokens: + if i in dict.keys(): + dict[i] = dict[i] + 1 + else: + dict[i] = 1 + def myFunc(e): + if e in dict.keys(): + return dict[e] + else: + return 0 + + print("Total words: ", len(tokens)) + print("Top 20 words:") + # print(setWord.sort(reverse=True, key=myFunc)) + + # + setWord.sort(reverse=True, key=myFunc) + print(setWord[0:20]) + for i in setWord[0:20]: + print(i, " ", dict[i]) +count_words('shakespeare.txt') \ No newline at end of file diff --git a/src/11 Generate a Password/my_diceware.py b/src/11 Generate a Password/my_diceware.py new file mode 100644 index 0000000..68398b1 --- /dev/null +++ b/src/11 Generate a Password/my_diceware.py @@ -0,0 +1,29 @@ +import secrets + + +def splitLines(line): + lineSplited = line.split() + if len(lineSplited) > 1: + return (lineSplited[0], lineSplited[1]) + else: + return ("line","") + + +def generate_passphrase(num_words, wordlist_path='diceware.wordlist.asc'): + with open(wordlist_path, 'r', encoding='utf-8') as file: + lines = file.readlines() + dict = {splitLines(line)[0]: splitLines(line)[1] for line in lines[2:]} + resultArray = [] + for i in range(0, num_words): + resultArrayOfALine = [] + for j in range(0, 5): + resultArrayOfALine.append(str(secrets.choice(range(1, 7)))) + resultArray.append(resultArrayOfALine) + wordList = [dict["".join(tutu)] for tutu in resultArray] + return " ".join(wordList) + + + + +print(generate_passphrase(7)) +print(generate_passphrase(7)) \ No newline at end of file diff --git a/src/12 Merge CSV Files/all_students.csv b/src/12 Merge CSV Files/all_students.csv new file mode 100644 index 0000000..e120422 --- /dev/null +++ b/src/12 Merge CSV Files/all_students.csv @@ -0,0 +1,7 @@ +Lab,Midterm,Name,Final +,61,Tony,51 +,98,Nick,91 +,85,Keith,73 +79,52,Alice,87 +77,84,Steve,53 +91,82,Ralph,83 diff --git a/src/12 Merge CSV Files/my_merge_csv.py b/src/12 Merge CSV Files/my_merge_csv.py new file mode 100644 index 0000000..38b3790 --- /dev/null +++ b/src/12 Merge CSV Files/my_merge_csv.py @@ -0,0 +1,30 @@ +import csv + + +def merge_csv(arrayOfCsvFileNameInput, csvFileNameOutput): + inputEntries = {} + for fileName in arrayOfCsvFileNameInput: + with open(fileName, "r") as f: + inputEntries[fileName] = list(csv.DictReader(f)) + fieldNameSet = set({}) + for j in inputEntries.values(): + fieldNameSet = fieldNameSet.union(set(j[1].keys())) + + resLine = {} + resultsOutput = [] + for inputFileValue in inputEntries.values(): + for lineOfResults in inputFileValue: + for fieldOfTheSet in fieldNameSet: + if fieldOfTheSet in inputFileValue[1].keys(): + resLine[fieldOfTheSet] = lineOfResults[fieldOfTheSet] + else: + resLine[fieldOfTheSet] = None + resultsOutput.append(resLine) + resLine = {} + with open(csvFileNameOutput, "w") as f: + writer = csv.writer(f) + writer.writerow(list(fieldNameSet)) + for row in resultsOutput: + writer.writerow(row.values()) + +merge_csv(['class1.csv', 'class2.csv'], 'all_students.csv') \ No newline at end of file diff --git a/src/13 Solve a Sudoku/my_sudoku.py b/src/13 Solve a Sudoku/my_sudoku.py new file mode 100644 index 0000000..8fae0fc --- /dev/null +++ b/src/13 Solve a Sudoku/my_sudoku.py @@ -0,0 +1,134 @@ +def valid_line(sudoku_arr, line): + cpt = 0 + b = set() + for a in sudoku_arr[line]: + if (a > 0): + cpt = cpt + 1 + b.add(a) + if (len(b) < cpt): + return False + else: + return True + +def valid_colonne(sudoku_arr, colonne): + b =set() + cpt = 0 + for line in sudoku_arr: + if (line[colonne] > 0): + cpt = cpt + 1 + b.add(line[colonne]) + if (len(b) < cpt): + return False + else: + return True + +def valid_carre(sudoku_arr,startLine ,startColonne): + b =set() + cpt = 0 + for ligne in range(startLine, startLine + 3): + for colonne in range(startColonne, startColonne + 3): + if (sudoku_arr[ligne][colonne] > 0): + cpt = cpt + 1 + b.add(sudoku_arr[ligne][colonne]) + if (len(b) < cpt): + return False + else: + return True + +def makesudoku_data_struct(sudoku_arr): + sudo = [] + for i in range(0, 9): + a = [] + for j in range(0, 9): + if (sudoku_arr[i][j] > 0): + a.append((sudoku_arr[i][j], True)) + else: + a.append((sudoku_arr[i][j], False)) + sudo.append(a) + return sudo + + +def incremente(sudoku_arr): + sudo = [] + incNext = True + for i in range(8, -1, -1): + a = [] + for j in range(8, -1, -1): + if (sudoku_arr[i][j][1] == False and sudoku_arr[i][j][0] < 9 and incNext): + a.append((sudoku_arr[i][j][0] + 1, False)) + incNext = False + elif(sudoku_arr[i][j][1] == False and sudoku_arr[i][j][0] == 9 and incNext): + a.append((0, False)) + else: + a.append(sudoku_arr[i][j]) + sudo.append(a) + return sudo + +def extend(sudoku_arr): + extendNext = True + sudo = [] + for i in range(0, 9): + a =[] + for j in range(0, 9): + if (sudoku_arr[i][j][0] == 0 and extendNext == True): + extendNext = False + a.append((1, False)) + else: + a.append(sudoku_arr[i][j]) + sudo.append(a) + return sudo + + +def valid_sudok(sudoku_arr): + for i in range(0,9): + bool = valid_line(sudoku_arr, i) + if (bool is False): + return False + for i in range(0,9): + bool = valid_colonne(sudoku_arr, i) + if (bool is False): + return False + for startLine in range(0, 9, 3): + for startColonne in range(0, 9, 3): + bool = valid_carre(sudoku_arr, startLine ,startColonne) + if (bool is False): + return False + return True + +def winner(sudoku_arr): + for i in sudoku_arr: + for j in i: + a = j[0] + if (a == 0): + return False + return valid_sudok(sudoku_arr) + +def solve_sudok(sudoku_arr): + sudok = sudoku_arr + for startLine in range(0, 9): + for startColonne in range(0, 9): + if sudok[startLine][startColonne] == 0: + for no in range(1, 10): + if valid_sudok(sudoku_arr): + sudok[startLine][startColonne] = no + if trial := solve_sudok(sudok): + return trial + sudok[startLine][startColonne] = 0 + return False + return sudok + +tutu = [[5, 1, 7, 6, 9, 8, 2, 3, 4], [2, 8, 9, 1, 3, 4, 7, 5, 6], [3, 4, 6, 2, 7, 5, 8, 9, 1], [6, 7, 2, 8, 4, 9, 3, 1, 5], [1, 3, 8, 5, 2, 6, 9, 4, 7], [9, 5, 4, 7, 1, 3, 6, 8, 2], [4, 9, 5, 3, 6, 2, 1, 7, 8], [7, 2, 3, 4, 8, 1, 5, 6, 9], [8, 6, 1, 9, 5, 7, 4, 2, 3]] + +test_puzzle = [[5, 3, 0, 0, 7, 0, 0, 0, 0], + [6, 0, 0, 1, 9, 5, 0, 0, 0], + [0, 9, 8, 0, 0, 0, 0, 6, 0], + [8, 0, 0, 0, 6, 0, 0, 0, 3], + [4, 0, 0, 8, 0, 3, 0, 0, 1], + [7, 0, 0, 0, 2, 0, 0, 0, 6], + [0, 6, 0, 0, 0, 0, 2, 8, 0], + [0, 0, 0, 4, 1, 9, 0, 0, 5], + [0, 0, 0, 0, 8, 0, 0, 7, 9]] +test_puzzle1 =[[(5, True), (5, True), (0, False), (0, False), (7, True), (0, False), (0, False), (0, False), (0, False)], [(6, True), (0, False), (0, False), (1, True), (9, True), (5, True), (0, False), (0, False), (0, False)], [(0, False), (9, True), (8, True), (0, False), (0, False), (0, False), (0, False), (6, True), (0, False)], [(8, True), (0, False), (0, False), (0, False), (6, True), (0, False), (0, False), (0, False), (3, True)], [(4, True), (0, False), (0, False), (8, True), (0, False), (3, True), (0, False), (0, False), (1, True)], [(7, True), (0, False), (0, False), (0, False), (2, True), (0, False), (0, False), (0, False), (6, True)], [(0, False), (6, True), (0, False), (0, False), (0, False), (0, False), (2, True), (8, True), (0, False)], [(0, False), (0, False), (0, False), (4, True), (1, True), (9, True), (0, False), (0, False), (5, True)], [(0, False), (0, False), (0, False), (0, False), (8, True), (0, False), (0, False), (7, True), (9, True)]] + +tutu = solve_sudok(test_puzzle) +print(tutu) diff --git a/src/14 Build a Zip Archive/my_stuff.zip b/src/14 Build a Zip Archive/my_stuff.zip new file mode 100644 index 0000000..a915e11 Binary files /dev/null and b/src/14 Build a Zip Archive/my_stuff.zip differ diff --git a/src/14 Build a Zip Archive/my_stuff/tutu.txt b/src/14 Build a Zip Archive/my_stuff/tutu.txt new file mode 100644 index 0000000..2fa29ce --- /dev/null +++ b/src/14 Build a Zip Archive/my_stuff/tutu.txt @@ -0,0 +1,10 @@ +bird.png +chipmunks.jpg +flowers.jpg +formations.png +lizard.jpg +pelican.jpg +squirrel.png +starfish.png +turtle.png +waterfall.jpg diff --git a/src/14 Build a Zip Archive/my_zip_all.py b/src/14 Build a Zip Archive/my_zip_all.py new file mode 100644 index 0000000..1333c54 --- /dev/null +++ b/src/14 Build a Zip Archive/my_zip_all.py @@ -0,0 +1,20 @@ +import os +from zipfile import ZipFile; + +def listFile(dir, extensions, arrayFiles): + for (dirpath, dirnames, filenames) in os.walk(dir): + for i in filenames: + h = os.path.join(dirpath, i) + if len([ext for ext in extensions if h.endswith(ext)])> 0: + arrayFiles.append(h) + return arrayFiles + +def zip_all(dir, extensions, archiveFileName): + filenames = listFile(dir, extensions, []) + with ZipFile(archiveFileName, 'w') as zip_object: + for i in filenames: + zip_object.write(i) + +zip_all('my_stuff', ['.jpg','.txt'], 'my_stuff.zip') + +# print(listFile("my_stuff", ['.jpg','.txt'], [])) \ No newline at end of file diff --git a/src/15 Download Sequential Files/my_download_files.py b/src/15 Download Sequential Files/my_download_files.py new file mode 100644 index 0000000..de18455 --- /dev/null +++ b/src/15 Download Sequential Files/my_download_files.py @@ -0,0 +1,40 @@ + +import os +import urllib.request + +def make_url_with_num(url, num): + filename, ext = os.path.splitext(url) + numStr = str(num) + return filename[0: len(filename) - 3] + (((3 - len(numStr)) * "0") + numStr) + ext + +def get_filename_from_url(url): + for i in range(len(url) - 1, -1,-1): + if (url[i] == "/"): + return url[i + 1:len(url)] + return "" + +def download_files(url, kiki): + filename, ext = os.path.splitext(url) + cpt = int(filename[len(filename) - 3: len(filename)]) + lastRequestDidNotFailed = True + os.mkdir(kiki) + while(lastRequestDidNotFailed): + try: + urle = make_url_with_num(url, cpt) + req = urllib.request.Request(url=make_url_with_num(urle, cpt)) + with urllib.request.urlopen(req) as f: + with open(os.path.join(kiki, get_filename_from_url(urle)),'wb+') as output: + output.write(f.read()) + cpt = cpt + 1 + except Exception as inst: + print (inst) + lastRequestDidNotFailed = False + + + +# print(download_files('http://699340.youcanlearnit.net/image001.jpg', './images')) + + + + +print(download_files('http://699340.youcanlearnit.net/image001.jpg', './images')) \ No newline at end of file diff --git a/test_dict.pickle b/test_dict.pickle new file mode 100644 index 0000000..724f9ff --- /dev/null +++ b/test_dict.pickle @@ -0,0 +1 @@ +{"1": "a", "2": "b", "3": "c"} \ No newline at end of file