Python Problems MTH 151 Texas A&M University November 8, 2017 Introduction Hello! Welcome to the first problem set for MTH 151 Python. By this point, you should be acquainted with the idea of variables, conditional statements (if-elif), looping, and basic I/O (input/output) in Python. Before continuing on, it is important to understand how Python obtains new features and tools for you to use while programming. Python is a modular language, meaning a lot of its features are contained in tidy little packages called modules. These modules are very similar to MATLAB.m files, except they are even more powerful. Instead of containing only one function (like in MATLAB), each Python module contains many different functions, classes, and cosntants. If we wish to use a particular module that is already installed with Python, we simply use the keyword import. As an example, suppose we wanted to print a random integer to the console, then we could use the following code: 1 import random 2 p r i n t ( random. r a n d i n t ( 0, 100) ) In this case, the module I needed to import was the random module. This module has all kinds of goodies in it for randomly generating numbers. Notice how in order to generate an integer, I had to make a call to random.randint instead of simply randint. This is because whenever we import an entire module, we must make sure to call functions from that module using the module name. The format will always be module.function. This seems annoying though, and thankfully, Python grants us the syntax necessary to avoid this issue. We now can do 1 from random import r andint 2 p r i n t ( r a n d i n t ( 0, 100) ) This enables us to import only the functions and utilities we absolutely need from a particular Python module. Oftentimes, this makes our code run faster and look prettier. I highly recommend you use this for finished code, when it makes sense. Sometimes, however, you may simply need to use all the functions in a particular module, so using import module and grabbing the entire thing may be useful. 1
Now, you may be asking yourself, Ryan, this is all well and good, but what if I need functionality outside of the default Python modules? Well, I m glad you asked, stranger reading this. Thankfully, a utility often installed alongside Python can help us in this problem. Suppose you re a game designer, and you need a quick and dirty library for handling input and rendering, called pygame. Python does not come pre-installed with such a library, so we will have to install using this magic utility, pip. This is a commandline tool that is used to update and install modules for Python, but it can only be used if, when installing Python, you chose to add Python to your PATH (Windows OS only, if you re Mac... I recommend googling it). To install this pygame module, you simply need to open up a Windows command line (type cmd into your search bar), and enter the command pip install pygame. On some occasions, it may be necessary to run the command prompt as an administrator, but usually, that doesn t seem to be the case. If everything worked out properly pip will say installed successfully, and you can now go about using import pygame for any Python program you write. Well, that about wraps it up for extending Python functionality, so with that, I wish you the best of luck on these problems! 1 Instructions Each problem should be placed into its own file and named after the particular problem i.e. Problem1a.py. To do this in IDLE, simply open up IDLE, find the File menu at the top left of the shell, select New File, and begin typing in the new window that opens. When you re ready to run your program and see if it works, first save it, then select the Run menu at the top of the text editor window you opened and click Run Module. This will execute your program in the Python shell. Feel free to use any modules that come pre-installed with Python or any you find online, so long as they don t render the problem trivial i.e. Oh, I found a module called Random Password Generator. I think I will use it. Exercise good judgment on these matters, after all, these are problems designed solely for your benefit, not a grade. Problem 1 A.) Computing the hypotenuse of a right triangle This program should ask the user to input two side lengths, then it should output the length of the hypotenuse of the right triangle composed of those two side lengths. (Research will be required to figure out Python s mathematical operators) Below is an example run of the program: 1 P l e a s e e n t e r a s i d e l e n g t h : 3 2 P l e a s e e n t e r another s i d e l e n g t h : 3 The hypotenuse i s o f l e n g t h... 5 B.) Making a good thing better... Now extend your program, so that the user may continue to enter side lengths until they type No. Below is an example: 2
1 P l e a s e e n t e r a s i d e l e n g t h : 3 2 P l e a s e e n t e r another s i d e l e n g t h : 3 The hypotenuse i s o f l e n g t h... 5 5 Would you l i k e to f i n d another hypotenuse? Yes 6 P l e a s e e n t e r a s i d e l e n g t h : 7 7 P l e a s e e n t e r another s i d e l e n g t h : 2 8 The hypotenuse i s o f l e n g t h... 25 9 10 Would you l i k e to f i n d another hypotenuse? No 11 E x i t i n g... C.) Double jeopardy: all or nothing... Now extend your program even further, so that any string starting with n or N will cause the program to exit, and any string starting with y or Y will cause the program to continue. Problem 2 A.) Password generator This program will be a random password generator. First, you must ask the user to input the kind of password they would like with choices weak, medium, or strong. If the user inputs help, then the program should output valid options for them to give the program, what those options mean, and of course, a friendly word of encouragement. A weak password is defined as being between 6 and 8 characters, uses no special symbols i.e.!@#$%^&*+-, and has at most 1 number. A medium password should be 8-10 characters, at least one of which must be a special symbol, at least 2 numbers. A strong password must be 12-16 characters, contain upper and lower characters, three special symbols, and at least 2 numbers. Finally, if the users input does not match any valid command, then it should output an error message and ask the user to try again. An example is given below, though please do condense my help message in some way: 1 What kind o f password would you l i k e? Help 2 Options are weak, medium, or s t r o n g. A weak password i s d e f i n e d as being between 6 and 8 c h a r a c t e r s, u ses no s p e c i a l symbols i. e.!@#$%ˆ& +, and has at most 1 number. A medium password should be 8 10 c h a r a c t e r s, at l e a s t one o f which must be a s p e c i a l symbol, at l e a s t 2 numbers. A s t r o n g password must be 12 16 c h a r a c t e r s, contain upper and lower c h a r a c t e r s, t h r e e s p e c i a l symbols, and at l e a s t 2 numbers. 3 What kind o f password would you l i k e? Weak 5 Password : primzq 6 E x i t i n g... Hint: You may find that researching string concatenation is helpful. benefit you to research the "".join() method. Hint: It may also 3
B.) A better generator... Now make sure that the locations of all numbers, uppercase letters, lowercase letters, and special symbols are mostly (use your judgment) different each time the user asks for a strong or medium password i.e. 123abc$# and 56def%& would not be considered valid, since all the numbers, letters, and special symbols occur in the same location no matter how many different passwords are generated. An example of two valid password generations would be 123Abc$# and Ef%56&g. Problem 2 adapted from http://www.practicepython.org/exercise/201/05/28/16-passwordgenerator.html. Problem 3 A.) Prime number generator For this program, you will be generating all prime numbers from 1 to n where n is input by the user, putting them into a list, then outputting the list. (You may find the list.append() method helpful) For this program, you are not allowed to use import at all for additional modules, and you may not simply output each prime as you find it i.e. your output should be print(primes) where primes is a list. Sample output is given below. 1 P l e a s e e n t e r the upper bound f o r the primes : 15 2 Primes : [ 1, 2, 3, 5, 7, 11, 1 3 ] 3 E x i t i n g... B.) Prime factorization In a radical departure from previous problems, this doesn t in any way extend the functionality of part a, so get excited! For this problem, you should take a positive integer from the user as input, then output the prime factorization of that number. If the number is prime, then you should output PRIME! :D. If the number is not prime, then you should print out all of its prime factors like this 60: 1 x 2 x 2 x 3 x 5. C.) Guess we ll go back to the extension pattern... In a radical departure from our radical departure, we will extend the functionality of part b. Once again, the user will input a positive integer, but this time, the output should be the prime factorization and all possible factors of that positive integer. In order for this to be a non-trivial problem, you must use the prime factorization you found in part b, to generate all of these factors. Here is a sample output. 1 P l e a s e e n t e r a number to be f a c t o r e d : 60 2 6 0 : 1 x 2 x 2 x 3 x 5 3 Factors : 1 2 3 5 6 10 12 15 20 30 60 5 E x i t i n g...
While this output is sorted, yours may not necessarily be. We will add that functionality in the next part. D.) And so it ends... Now, repeat part c, but your output should be pairs of numbers that when multiplied together equal the number input by the user. These should be sorted by the first number in the pair and there should be no duplicates in the list. (You may wish to look at tuples or lists of lists and the list.sort() method for this program) Finally, please continue to use the code from parts b and c. This problem becomes trivial if considering it from another angle. As always, sample output is below. 1 P l e a s e e n t e r a number to be f a c t o r e d : 60 2 6 0 : 1 x 2 x 2 x 3 x 5 3 Factors : ( 1, 60) 5 ( 2, 30) 6 ( 3, 20) 7 (, 15) 8 ( 5, 12) 9 ( 6, 10) 10 11 E x i t i n g... 5