Workflow - melkumkc/5590-Python-DeepLearning GitHub Wiki

Question 1.

Solution steps: • Initialize the password to an empty string, the valid Boolean to false, the list of number, and special characters. • Use a while loop to continuously repeat until the user input a valid password. • The required length, number, special character, lowercase character, and uppercase character Booleans are set to false every time it loops. • After the user inputs a password, each of the needed password requirements are checked. • To check the correct length in range of 6 – 16 characters, an if statement along with the len() function is used. If the password doesn’t fit the requirements the user is told and if the password passes the requirement, the contain_length Boolean is set to true. • To check if the password as at least one number, a for loop is used to check every character. An if statement is used to see if any character is in the number list. If the password has at least one number, the contain_number Boolean is set to true. If the loop ends without finding a number, the user is told. • To check if the password has at least one special character, a for loop is used to check every character. An if statement is used to check if any character is in the special character list. If a special character is in the password, the contain_special Boolean is set to true. If the loop ends without finding a special character the user is told. • To check if the password contains at least one lowercase character, a for loop is used to check every character. Inside the for loop, an if statement and the islower() function is used to check if a lowercase letter is in the password. If there is a lowercase character, the contain_lower Boolean is set to true. If the loop ends without finding a lowercase character, the user is told. • To check if the password has at least one uppercase character, a for loop is used to check every character. Inside the for loop an if statement and the isupper() function is used to check if there is an uppercase letter in the password. If the statement is true, the contain_upper Boolean is set to true. If the loop ends without finding an uppercase character, the user is told. • After each of the requirements are checked, if all the Booleans are set to true, valid is set to true, the loop ends, and the password is output to the user as a valid password. Otherwise the loop starts over.

Question 2.

Solution steps: • Input string value from the user by using python input function and pass the result to a function (“func”) • To simplify the process the string is first converted into a list. Each word is separated by space and the string is separated and stored in a list by using the “split (“ ”) function. • Finding the middle word depends on the length of the string. If the length of the string is even, there will be two middle words. The index of the first middle word will be the length of the string divided by two. And the second middle word will be length of the string divided by two and then by adding one. If the length of the string is odd, there will be one middle word. The index of the middle word is calculated by using “//” operator. • To find the longest word in the string (hence in our case the list since we have converted the string into a list) a for loop was used. The length of each element of the list was calculated and the max was found. • To find the reverse of each element of the list, first an empty list to store the reversed strings was created. Using for loop iterate through each element of the list and find the reverse of each element using the slicer function (“[i[len(i)::-1])

Question 3 Solution steps: • To avoid duplicate solutions, sets were used as a data structure to store the required result • Three nested for loops were used to find the triplets. Using this approach makes the algorithm more complex that is O(n3), but since the given size of the list is small, the complexity of the algorithm won’t be that much of an issue • Inside the nested loop to find the triplets the following conditional statement was used: If (i+j+k == 0) and i != j and j !=k and i!= k • If the above condition is fulfilled I,jand k will be added to the set

Question 4 • Sets were used as an ideal data structure for this question. We can use sets to find intersections, unions and difference without worrying about duplicates • Web_class & pyt_class …… will give the intersection of the two sets, that is list of students taking both classes • Web_class – Pyt_class…… will give the difference of the two sets, that is the list of students taking only web_class • Pyt_class – Web_class…… will give list of students taking only pyt_class • Pyt_clss | Web_class ……. Will give intersection of the two sets, that is list of student taking both classes

Question 5 • Library management system using 5 classes was created for this question. The following classes were used “Person”, “Student”, “GeneralStaff”, “Librarian” and a “Book”. • Class Person o It is the base (super) class for student, GeneralStaff and Librarian o It has a member variables of “name”, “family”,”date_of_birth” , “id” and “count” o The “count” variable is to keep track of the person objects that are created (involved) in the management system and the other variables are to identify to whom a book is issued to. o All the variables are initialized when a person object is created o It has a method “age” it calculates the age of a person. This method will be helpful in the case we want to do an analysis of which age group is reading which books. • Class Student o To keep track of students we need additional information like major and year (i.e senior, junior…) and therefore this class has additional member variables “major” and “year” o It has a method “is_student()” to check whether a person is student or not • Class GeneralStaff o The library will have users besides students, this class is necessary to keep track of those users of the library o This class has additional member variable “department”, this helps to keep track of users department • Class Librarian o This class is necessary because the system needs to keep track of who is issued the book and also who stored the book. o This class has additional member variable “shift”, this helps to keep track who is working when • Class Books o It has a private Boolean member variable “__in_library”, whenever a book is issued the status of the book will change to false and when the book is returned the status will be true o To keep track of all books, the class has the following member variables “issued_to”, “recived_by”, “title”, “date_purchase”,”serial_no”. o It also has a member variable “book_count” which keeps track of the number of books in the system The system was tested on the following scenario: To demonstrate the relationship between the classes the following object were created o Two student Person “Melkam” and “abe” o One GeneralStaff Person “get” o One Librarian Person “bob” o Two books “pyt” and “deep_learn”

The books were issued out and one of the book was returned. The status of the books were printed out to see if the system works.

Question 6 Solution steps: • Using numpy a vector of 15 random elements ranging from 0 to 20 was created as follows: ar= np.random.randint (0,20,15) • The array was then converted into a list to find the max frequency of occurrence. The function list.count(i) was used to find the frequency of each element • Using for loop the max count was identified. The complexity of the algorithm is O(n)