Python Problems MTH 151. Texas A&M University. November 8, 2017

Similar documents
Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

If Statements, For Loops, Functions

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

cs1114 REVIEW of details test closed laptop period

from scratch A primer for scientists working with Next-Generation- Sequencing data Chapter 1 Text output and manipulation

Python Games. Session 1 By Declan Fox

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

CSCE 110 Programming I

Intro. Scheme Basics. scm> 5 5. scm>

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Text Input and Conditionals

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Unit 7: Algorithms and Python CS 101, Fall 2018

CS 112: Intro to Comp Prog

Exercise 2: Automata Theory

A453 Task 1: Analysis: Problem: Solution:

PYTHON YEAR 10 RESOURCE. Practical 01: Printing to the Shell KS3. Integrated Development Environment

Python for Non-programmers

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Recall that strings and tuples are immutable datatypes, while lists are mutable datatypes. What does this mean?

Computer and Programming: Lab 1

ASCII Art. Introduction: Python

Python Unit

You Need an Interpreter! Comp Spring /28/08 L10 - An Interpreter

S206E Lecture 19, 5/24/2016, Python an overview

Table of Contents EVALUATION COPY

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

SCRATCH MODULE 3: NUMBER CONVERSIONS

Jython. secondary. memory

Getting started 7. Saving data 23

Programming Fundamentals and Python

Access Intermediate

Introduction to Programming

Section 0.3 The Order of Operations

Python Programming Exercises 3

Statistics 13, Lab 1. Getting Started. The Mac. Launching RStudio and loading data

: Intro Programming for Scientists and Engineers Final Exam

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

CME 193: Introduction to Scientific Python Lecture 1: Introduction

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

Scripting Languages. Diana Trandabăț

Decisions, Decisions. Testing, testing C H A P T E R 7

COPYRIGHTED MATERIAL. Dipping Your Toe into Python. Part I. Chapter 1: Programming Basics and Strings. Chapter 2: Numbers and Operators

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Math Day 2 Programming: How to make computers do math for you

Chapter 4. Unix Tutorial. Unix Shell

Variables, expressions and statements

APPM 2460 Matlab Basics

Topics. Chapter 5. Equality Operators

Introduction to Python (All the Basic Stuff)

T H E I N T E R A C T I V E S H E L L

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CMSC 201 Computer Science I for Majors

Condition Controlled Loops. Introduction to Programming - Python

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Semester 2, 2018: Lab 1

Algorithms and Programming I. Lecture#12 Spring 2015

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION

file:///users/cerdmann/downloads/ harvard-latest.html

CPSC 217 Assignment 3

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

def instructions(response): # Add to instruction_string so that it contains information on # how to play rock-paper-scissors instruction_string = ""

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Formatting: Cleaning Up Data

Other Loop Options EXAMPLE

nostarch.com/pfk For bulk orders, please contact us at

A Little Python Part 2

Express Yourself. The Great Divide

Programming Project #6: Password File Cracker

Encryption à la Mod Name

My First Command-Line Program

Intro to Python & Programming. C-START Python PD Workshop

Chapter 1. Data types. Data types. In this chapter you will: learn about data types. learn about tuples, lists and dictionaries

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Python 2.7 will also work for this workshop due to certain language rules, however I prefer working with the most current stable version.

Statements 2. a operator= b a = a operator b

Hello! ios Development

Play with Python: An intro to Data Science

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Congruence Arithmetic

Programming for Non-Programmers

SchoolMessenger App. User Guide - Web. 100 Enterprise Way, Suite A-300. Scotts Valley, CA

The current topic: Python. Announcements. Python. Python

MEIN 50010: Python Introduction

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

AMS 200: Working on Linux/Unix Machines

ENGR 101 Engineering Design Workshop

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

LOOPS. Repetition using the while statement

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

3x - 5 = 22 4x - 12 = 2x - 9

An Interesting Way to Combine Numbers

6.149 Checkoff 2. What to complete. Recall: Creating a file and running a program in IDLE.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

What you get When you install Python for your computer, you get a number of features:

4th Pui Ching Invitational Mathematics Competition. Heat Event (Secondary 2)

Transcription:

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