# Project Set 1

Basic operations, lists, conditionals, loops.

## Project 1

Write a program that:

- Creates a list of temperatures [0, 10, 20, 30, 40, 50].
- Prints the number of items in the list.
- Prints the index of temperature 30.
- Adds another temperature 60 at the end.
- Loops through the list of temperatures and converts them from Celsius to Fahrenheit, printing each value in degrees C and F.

## Example solution

```
c_temps = [0, 10, 20, 30, 40, 50]
print("Number of items:",len(c_temps))
print("Index of '30':",c_temps.index(30))
c_temps.append(60)
for c in c_temps:
f = c*1.8 + 32
print (c,f)
```

## Project 2

Write a program that:

- Creates the temperature list 0 through 60 by 10 using a loop rather than by typing in all the values.
- As each degree C value is added, converts it to F and adds the F value to a list for the Fahrenheit equivalents.
- Makes another loop which prints out C and F similarly to Project 1, i.e. both on the same line, but does it by indexing into the two lists.

## Example solution

```
c_temps = []
f_temps = []
for t in range(0,70,10):
c_temps.append(t)
f = t * 1.8 + 32
f_temps.append(f)
for index in range(len(c_temps)):
print(c_temps[index],f_temps[index])
```

## Project 3

Write a program that:

- Generates a list of temperatures from -40 to 100 inclusive by increments of 5 degrees Celsius.
- Creates another list of the corresponding Fahrenheit temperatures.
- Creates a list of temperatures in degrees Fahrenheit which are greater than zero but for which the corresponding temperature in Celsius is less than zero. Print the elements of this last list.

## Example solution

```
c_temps = []
f_temps = []
for t in range(-40,105,5):
c_temps.append(t)
f = t * 1.8 + 32
f_temps.append(f)
filtered = []
for index in range(len(c_temps)):
print (c_temps[index], f_temps[index])
if c_temps[index]<0 and f_temps[index]>0:
filtered.append(f_temps[index])
print("Filtered results")
print(filtered)
```

## Project 4

The Collatz conjecture is a fun little exercise in number theory.

- Given a positive integer, if it is odd multiply it by 3 and add 1. If it is even divide by 2.
- Repeat this procedure until the result is 1.

The Collatz conjecture is that the sequence will always reach 1. No exceptions have been found…yet. The number of steps required to reach 1 is called the stopping time.

**A.** Write a program that will find and print the stopping time for the first N positive integers. Count the starting number itself as one of the steps. Print a table of N and stopping time.
Test your program for N=30 and N=50.

**B.** Modify your program to print the starting number, its stopping time, and the maximum value of the sequence of numbers. **Hint:** If you use a list you will be able to use the len() and max() intrinsic (built-in) functions. Confirm that you get the same stopping numbers as before. Note: the example solution uses some capabilities of
printing in Python 3 that we have not yet encountered, in order to make the output neater and easier to read. Do not worry about aligning your results at this point.

## Example solution

```
def collatz(N):
"""Collatz conjecture algorithm."""
steps = [N]
while N>1:
if (N % 2 == 0):
N = N // 2
else:
N = N * 3 + 1
steps.append(N)
return len(steps),max(steps)
numbers = [30,50]
for N in numbers:
print (f"Computing Collatz for N={N}")
header = f"{'N':>5}|{'stopping time':>15}|{'max value':>10}"
print (header)
print ("".join(['-'] * len(header)))
for n in range(1,N+1):
stop,max_value = collatz(n)
print (f"{n:5}|{stop:15d}|{max_value:10d}")
```

## Project 5

The algorithm for converting an integer N in base 10 to another base is as follows:

- Find the remainder of N divided by the target base.
- Divide N by the target base using integer division. If the result is greater than zero, replace the previous value of N by the result of the integer division. Store the remainder previously obtained as the new leftmost digit for the number in the target base. Repeat until the result of the integer division is 0.

**A.** Write a program to convert the first 51 integers, starting at 0 and ending at 50, to octal (base 8). Print a table of the decimal number and its octal equivalent.
**Hint:** construct a list of digits as you work through the integer divisions. The elements of the list should be strings so you’ll need to convert from integer to string. To change from a list of individual strings to a single string for printing, use the join function as follows:
“”. join(digits)
That is two (regular, not “smart”) double quotes with nothing between them, followed by a period, followed by join and in parentheses, the name of the list you have created.

**B.** Modify your program to handle bases up to 16 (hexadecimal). Use the letters of the alphabet to represent digits 10, 11, 12, … as A, B, C, … Hint: the char(

## Example solution

```
# -*- coding: utf-8 -*-
"""
Created on Mon May 20 10:30:26 2013
This program converts a sequence of decimal numbers into another base.
Author: K. Holcomb
Changelog: 2013-05-20 Initial version
"""
N=32
base=16
header1="Base 10"
header2="Base "+str(base)
print(f"{header1:>8} {header2:>8}")
print(f"------------------")
for i in range(N+1):
if i==0:
conversion=str(i)
else:
digits=[]
result=i
while result>0:
digit=result%base
result=result//base
if digit<=9:
base_digit=str(digit)
else:
alpha_order=digit%10
base_digit=chr(alpha_order+65)
digits.insert(0,base_digit)
conversion="".join(digits)
print("{:>6} | {:>5}".format(i,conversion))
```