Python Homework

Adnan Mohammad
PythonHomework45.docx

Python Homework 4

Program1: Very frequently there is a need to monitor a presence of a file in some directory, this maybe needed for:

· Automated deployment jobs - every time a new bundle is uploaded, deploy it

· Synch processes - there are multiple clustered servers, and they need to have same files on them

· Error file - if file is ever present, then there is an error - send an email

write a program that will take a command line argument of full path and prints True if the file/directory at the provided location is present Change program such that it runs indefinitely, monitoring a file/directory at a certain location  Program 2: Modify the quote generator program such that if there are multiple quotes for the given author, instead of writing them out into the same file, the program instead creates a new file for each quote, hence if you have 3 quotes for an author, you would have 3 files in that author's folder, named quote_1.txt, quote_2.txt, quote_3.txt. If the author has only 1 quote, then file can be named wither quote.txt or quote_1.txt

Program 3:

Use decorator function you created for the previous homework and estimate how much operation on a dictionary is faster(or slower) than operation on a shelve

Program 4: Prompt user to enter a start path and file name, search recursively for the given file name starting at the given path, when file is found, display the full path to the file. 

Python Homework 5

Program 1:  Use generator functions to create your own version of range function, call it my_range. Do not use the python's range function in the code.

Program 2: We have bunnies standing in a line, numbered 1, 2, ... The odd bunnies (1, 3, ..) have the normal 2 ears. The even bunnies (2, 4, ..) we'll say have 3 ears, because they each have a raised foot. Recursively return the number of "ears" in the bunny line 1, 2, ... n (without loops or multiplication).  bunnyEars2(0) → 0 bunnyEars2(1) → 2 bunnyEars2(2) → 5 ​ Use pytest to validate the correctness of the function Program 3: The program below gives elapsed execution time in seconds of the print statement  Create a decorator function that will take any function, with any number of arguments and print the time it takes to execute it​

Program 4: You are working on a "secure cloud data transfer" team. You are tasked with creating a mechanism for encrypting data prior to transfer and decrypting it once the transfer is complete. Write two decorator functions encrypt and decrypt . When applied as a decorator, the encrypt function will encrypt the returning string (assume it is always a string) and decrypt function will decrypt the encrypted string back to its original value.  The encryption logic does not matter (just don't leave the string "as is"); for example, a reverse of the string or shifting characters is sufficient (you do not need to have the encryption work perfectly well) if you like to do more research and use some real crypto algorithms, you may do so for up to 3.3 (double) additional points on the homework. You can use this resource for the research: www.tutorialspoint.com/cryptography_with_python/cryptography_with_python_quick_guide.htm If you need to use a common key for encryptor and decryptor, you can either duplicate the key in each function's declaration or use the global variable or any other method, ex: env vars (your approach will not be penalized, however it would be nice if you discuss con's and pro's of your implementation) Use pytest to ensure the decrypted string matches the original string.  

program 1: Write a function that converts temperature from Fahrenheit to Celsius using formula Tc=(5/9)*(Tf-32)  To test your answer, 68F = 20C program 2: Write a function count_frequency that takes a list of words as an argument, counts how many times each word appears in the list, and then returns this frequency listing as a Python dictionary Sample function call and output: mylist=["one", "two","eleven",  "one", "three", "two", "eleven", "three", "seven", "eleven"] print(count_frequency(mylist))   {'seven': 1, 'one': 2, 'eleven': 3, 'three': 2, 'two': 2} additional independent research, understand how  Counter  can be used. You do not need to submit anything, but there will be a question in the next week's quiz covering this topic program 3: leetcode two sum: https://leetcode.com/problems/two-sum/ ​

Picture

program 4 (up to 3.3 points extra credit for quality of research):  design URL shortener, similar to bit.ly 

You  need to build a URL shortener, similar to https://bitly.com/  On the bottom of the bitly URL above, you can paste a long URL and it will generate a short URL For this program you will need to write two functions: shorten_url function: takes a long URL and generates and returns a short URL - the short URL doesn't need to match the one on the bitly page, it should be just a shorter than the original URL get_original_url function: takes a short URL as input and returns original long URL, if the short URL not found, return "HTTP 404" string instead You will need to come up with the URL shortening mechanism, it is up to you what method you will use, but you must go through the following options and discuss in the comments of your program submission the pro's and con's of each of these  methods. The correctness of your reasoning will not be graded. As you discuss pro's and con's think through chances of possible clashes of the shortened URL's, how many URL's will you be able to shorten total using the method and how much memory you think you will need to store all the short and matching long URL's. You can make and state your assumptions in the comments as well, for example you may assume an average length of a long URL to be 500 characters  ​1. uuid:  https://docs.python.org/3/library/uuid.html 2. timestamp: https://realpython.com/python-time-module/#python-time-in-seconds-as-a-floating-point-number 3. MD5 hash: https://www.geeksforgeeks.org/md5-hash-python/ 4. Another short string generation mechanism either your own or some other than above 3 python functions