└── Exam ├── Part_3 ├── Question_2.py └── Question_1.py ├── Part_1 ├── Code_1.py ├── Code_2.py └── Code_3.py └── Part_2 ├── Code_2.py └── Code_1.py /Exam/Part_3/Question_2.py: -------------------------------------------------------------------------------- 1 | import webbrowser 2 | import re 3 | 4 | -------------------------------------------------------------------------------- /Exam/Part_3/Question_1.py: -------------------------------------------------------------------------------- 1 | # Using random, datetime, and collections : 2 | 3 | 4 | from datetime import date 5 | import random 6 | import collections 7 | 8 | start_date = date.today().replace(day=1, month=1).toordinal() 9 | end_date = date.today().toordinal() 10 | 11 | # Random dates 12 | dates = [date.fromordinal(random.randint(start_date, end_date)) for i in range(100)] 13 | 14 | # Count the number of times each day of the week appears 15 | day_count = collections.Counter(date.weekday(d) for d in dates) 16 | 17 | # Print the results 18 | for day, count in day_count.items(): 19 | print(f"{day}: {count}") 20 | 21 | 22 | 23 | # This is a Python code generates 100 24 | # random dates in the current year and counts the number of 25 | # times each day of the week appears in those dates. 26 | 27 | 28 | # replace(day=1, month=1) sets it to January 1 of the current year. 29 | 30 | -------------------------------------------------------------------------------- /Exam/Part_1/Code_1.py: -------------------------------------------------------------------------------- 1 | # Part I: Read Code Written by another Coder 2 | 3 | 4 | import random 5 | import string 6 | def function_one(length): 7 | characters = string.ascii_letters + string.digits + string.punctuation 8 | return ''.join(random.choice(characters) for _ in range(length)) 9 | 10 | function_one(10) 11 | # ============================Modules Imported :======================== 12 | # The random : This module Generates pseudo-random numbers. 13 | 14 | # Imported string : This Module defines Constants like : 15 | 16 | # 1-ascii_letters 17 | # 2-ascii_lowercase 18 | # 3-ascii_uppercase 19 | # 4-digits 20 | # 5-punctuation 21 | # 6-hexdigits 22 | # 7-octdigits 23 | # 8-printable 24 | # 9-whitespace 25 | 26 | 27 | # This code generally generates random characters using ascci letters + digits + ponctuation 28 | # (using string module). 29 | 30 | 31 | # This code can be useful in cases where you want to generate a random code , that is hard to be 32 | # memorized or cracked , for all general uses. 33 | 34 | 35 | # we can run the code like this : 36 | 37 | function_one(10) 38 | 39 | # 10 is the length of the password generated 40 | 41 | -------------------------------------------------------------------------------- /Exam/Part_2/Code_2.py: -------------------------------------------------------------------------------- 1 | # Original Code That needs Debuging : 2 | 3 | 4 | # import webbrowser 5 | # import re 6 | # def search_google(query): 7 | # """Search for the given query on Google and open the first result in a web 8 | # browser.""" 9 | # url = f'https://www.google.com/search?q={query}' 10 | # response = webbrowser.open(url) 11 | # if not response: 12 | # return 'Unable to open web browser' 13 | # html = webbrowser.get().open(url).read().decode('utf-8') 14 | # pattern = re.compile(r' If the file is a regular file, searches for a match for the pattern using match = date_pattern.search(filename). 48 | # ------> If a match is found, extracts the matched date using match.group() . 49 | # (used two consucutive if statements in the code) 50 | 51 | # 5- Difining variables : 52 | # ------> file_date: date of the file 53 | # ------> Year: the year 54 | # ------> month: the month 55 | # ------> new_directory: Creates a new directory with the path using os.path.join(directory, year, month) 56 | # ------> old_path: assigns this variable to directory and old path 57 | # ------> new_path: assigns this variable to directory and new path 58 | 59 | 60 | 61 | 62 | # ====================This Code Used For:========================= 63 | 64 | 65 | # This code renames files in the selected directory : 66 | # ------> extracting the date from the filename 67 | # ------> creating a new directory with the date extracted. 68 | # ------> moves the file to the new directory while you can rename it. 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /Exam/Part_1/Code_3.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | from datetime import datetime, timedelta 3 | from collections import defaultdict 4 | async def is_w(date): 5 | return date.weekday() >= 5 6 | async def is_h(date): 7 | holidays = [datetime(2023, 1, 1), datetime(2023, 4, 15), datetime(2023, 5, 1), 8 | datetime(2023, 6, 7), datetime(2023, 9, 16)] 9 | return date in holidays 10 | async def generater(start_date, end_date): 11 | dates = defaultdict(list) 12 | current_date = start_date 13 | while current_date <= end_date: 14 | if not await is_w(current_date) and not await is_h(current_date): 15 | dates[current_date.year].append(current_date) 16 | current_date += timedelta(days=1) 17 | return dates 18 | async def main(): 19 | start_date = datetime(2023, 1, 1) 20 | end_date = datetime(2023, 12, 31) 21 | dates = await generater(start_date, end_date) 22 | year_counts = {year: len(dates[year]) for year in dates} 23 | print('Number of workdays in 2023:', sum(year_counts.values())) 24 | if __name__ == '__main__': 25 | asyncio.run(main()) 26 | 27 | 28 | 29 | 30 | # ============================Modules Imported :======================== 31 | # In This Code We Imported The asyncio : asyncio library which is used for writing 32 | # asynchronous code in Python. 33 | 34 | 35 | # Imported collections : 36 | 37 | # importing the defaultdict class from the collections module. 38 | 39 | 40 | 41 | # Imported datetime : 42 | 43 | 44 | 45 | # Explaining the Code: 46 | 47 | # 1-defines four functions is_w, is_h, generater. 48 | # async def is_w(date)/async def is_h(date): 49 | # --------> is_w: defines a coroutine function takes a date and returns whether the 50 | # day of the week for that date is a weekend day (Saturday or Sunday) 51 | # --------> is_h: defines a coroutine function called is_h that takes in a date and returns whether the date 52 | # falls on one of the holidays specified in the function. 53 | 54 | # 2- async def main(): 55 | # -------> defines a coroutine function called main that initializes the start_date and end_date parameters. 56 | # -------> generate the dates, and how many workdays for each year. 57 | 58 | 59 | # 3-if __name__ == '__main__': 60 | # asyncio.run(main(): 61 | # -----------> runs the main coroutine using asyncio. 62 | 63 | # Result : finally :prints the total number of work days in 2023. 64 | 65 | 66 | 67 | # This code is used for counting the working days of a given year in this cases : 68 | # 2023 = 258 69 | # by eliminating holidays and sundays and maybe saturdays 70 | 71 | # it is useful for working people to know how much time they gonna spend with their families each year 72 | 73 | 74 | 75 | 76 | 77 | 78 | --------------------------------------------------------------------------------