Sei sulla pagina 1di 3

Programming #1

SSP

Programming Assignment #1
When you are ready to submit your solutions, email all your source les (i.e. .py les) to the TAs (socorroTAS@gmail.com) with the subject programmingHW1. Be sure to include your name, the date, and a description of your program in comments at the top of your code. Please submit a separate le for each exercise and name each le according to the exercise. Specically, the le for exercise 1 should be called ex1 firstname lastname.py, the le for exercise 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 receiving code 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 the programming skills youll need to write your orbit-determination code. Exercise 1. A common Unix/Linux utility is a small program called wc (word count). This program analyzes a le 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 le name and then print three numbers showing the count of lines, words, and characters in the le. Exercise 2. Write a program that computes the sum of the squares of numbers read from a le. Assume the le has one number per line. Your program should prompt the user for a le name and print out the sum of the squares of the values in the le. Hint: use the le object method readlines(). Exercise 3. The Fibonacci sequence starts 1, 1, 2, 3, 5, 8,. . . Each number in the sequence (after the rst two) is the sum of the previous two numbers. Write a function that computes and returns the nth Fibonacci number, where n is a parameter of the function. Please use a loop in your solution (well 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 a natural 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 question in mathematics whether this sequence will always go to 1 for every possible starting value. 1

Programming #1

SSP

Write a function that takes a starting value and returns the Syracuse sequence for that value as a list. The function should not print anything. Exercise 5. The greatest common divisor (GCD) of two values can be computed using Euclids algorithm. Starting with the values m and n, we repeatedly apply the formula: n = m m = n mod m n = n until m is 0. At that point, n is the GCD of the original m and n. Write a function that nds 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 then nds two prime numbers that add up to that number. The function should return a list containing the two numbers. The function should not print anything. Exercise 7. Write a program that computes the fuel eciency of a multi-leg journey. The program will prompt the user for a starting odometer reading (current car mileage) and the name of a le containing the odometer reading and gas used (separated by a space) after each leg. The le should have each leg on a separate line. The program should print out the miles per gallon achieved 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 statistical properties about the set of number. The user should enter the numbers one at a time and enter a blank line to indicate they are nished entering numbers. The program should print out the mean, standard deviation, and median of the set of numbers. You should write a separate function to calculate each of these quantities. These functions should not print anything. The formula for standard deviation is
n

(x xi )2 =
i=1

n1

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

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

xi yi
i=1

The function should not print anything. Exercise 10. The Sieve of Eratosthenes is an elegant algorithm for nding all of the prime numbers up to some limit n. The basic idea is to rst create a list of numbers from 2 to n. The rst number is removed from the list, and announced as a prime number. All multiples of this number up to n are then removed from the list. This process continues until the list is empty. For example, if we wished to nd 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 are removed, 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. That leaves 5 and 7. The algorithm continues by announcing that 5 is prime and removing is from the 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 list of all the primes less than or equal to n. The function should not print anything.

Potrebbero piacerti anche