Python Programming Practice

3

Click here to load reader

description

This is a series of problems for practicing programming in Python.

Transcript of Python Programming Practice

Page 1: Python Programming Practice

Programming #1 SSP

Programming Assignment #1

When you are ready to submit your solutions, email all your source files (i.e. “.py” files) tothe TAs ([email protected]) with the subject ”programmingHW1”. Be sure to includeyour name, the date, and a description of your program in comments at the top of your code.Please submit a separate file for each exercise and name each file according to the exercise.Specifically, the file for exercise 1 should be called ex1 firstname lastname.py, the file forexercise 2 should be called ex2 firstname lastname.py, and so on.

Feel free to discuss these exercises with other students, but refrain from giving or receivingcode verbatim from anyone else. Also, the solutions to these exercises may be available online.Please be careful and keep the Code of Honor in mind as you complete this assignment.

The primary goal of these programming homeworks is to help you learn and practice theprogramming skills you’ll need to write your orbit-determination code.

Exercise 1.

A common Unix/Linux utility is a small program called wc (word count). This programanalyzes a file to determine the number of lines, words, and characters contained therein.Write your own version of wc. The program should ask the user for a file name and thenprint three numbers showing the count of lines, words, and characters in the file.

Exercise 2.

Write a program that computes the sum of the squares of numbers read from a file. Assumethe file has one number per line. Your program should prompt the user for a file name andprint out the sum of the squares of the values in the file. Hint: use the file object methodreadlines().

Exercise 3.

The Fibonacci sequence starts 1, 1, 2, 3, 5, 8,. . . Each number in the sequence (after the firsttwo) is the sum of the previous two numbers. Write a function that computes and returnsthe nth Fibonacci number, where n is a parameter of the function. Please use a loop in yoursolution (we’ll get to the recursive version later). The function should not print anything.

Exercise 4.

The Syracuse (also called Collatz or Hailstone) sequence is generated by starting with anatural number and repeatedly applying the following function until reach 1:

syr(x) =

{x/2 if x is even

3x + 1 if x is odd

For example, the Syracuse sequence starting with 5 is: 5, 16, 8, 4, 2, 1. It is an open questionin mathematics whether this sequence will always go to 1 for every possible starting value.

1

Page 2: Python Programming Practice

Programming #1 SSP

Write a function that takes a starting value and returns the Syracuse sequence for that valueas a list. The function should not print anything.

Exercise 5.

The greatest common divisor (GCD) of two values can be computed using Euclid’s algorithm.Starting with the values m and n, we repeatedly apply the formula:

n′ = mm = n mod mn = n′

until m is 0. At that point, n is the GCD of the original m and n. Write a function thatfinds the gcd of two numbers using this algorithm. The function should not print anything.

Exercise 6.

The Goldbach conjecture asserts that every even number is the sum of two prime numbers.Write a function that takes a number, checks to make sure the number is even, and thenfinds two prime numbers that add up to that number. The function should return a listcontaining the two numbers. The function should not print anything.

Exercise 7.

Write a program that computes the fuel efficiency of a multi-leg journey. The program willprompt the user for a starting odometer reading (current car mileage) and the name of a filecontaining the odometer reading and gas used (separated by a space) after each leg. The fileshould have each leg on a separate line. The program should print out the miles per gallonachieved on each leg and the total MPG for the trip.

Exercise 8.

Write a program that gets a series of numbers from the user and computes a suite of statisticalproperties about the set of number. The user should enter the numbers one at a time andenter a blank line to indicate they are finished entering numbers. The program should printout the mean, standard deviation, and median of the set of numbers. You should write aseparate function to calculate each of these quantities. These functions should not printanything. The formula for standard deviation is

σ =

√√√√√√n∑

i=1

(x− xi)2

n− 1

where x is the mean, xi represents the ith data value and n is the number of data values.

2

Page 3: Python Programming Practice

Programming #1 SSP

Exercise 9.

Write a function innerProd(x, y) that computes and returns the inner product of two(same length) lists. The inner product of list x and y is computed as:

n∑i=1

xiyi

The function should not print anything.

Exercise 10.

The Sieve of Eratosthenes is an elegant algorithm for finding all of the prime numbers up tosome limit n. The basic idea is to first create a list of numbers from 2 to n. The first numberis removed from the list, and announced as a prime number. All multiples of this numberup to n are then removed from the list. This process continues until the list is empty.

For example, if we wished to find all the primes up to 10, the list would originally contain 2,3, 4, 5, 6, 7, 8, 9, 10. The 2 is removed and announced to be prime. Then 4, 6, 8, and 10 areremoved, since they are multiples of 2. That leaves 3, 5, 7, and 9. Repeating the process,3 is announced as prime and removed, and 9 is removed because it is a multiple of 3. Thatleaves 5 and 7. The algorithm continues by announcing that 5 is prime and removing is fromthe list. Finally, 7 is announced and removed, and were done.

Write a function that takes a number n, and then uses the sieve algorithm to return a listof all the primes less than or equal to n. The function should not print anything.

3