Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News Sign UpSign In
| Download

SageMath assignment

Views: 234
Kernel: SageMath (stable)

SageMath Assignment : Due June 12 (end of day)

1. Count the number of subsets of the set {1,2,3,...,20}\{1,2,3,...,20\} whose elements add up to 20. The set can be created with the expression Set(range(1,21)). Here is code for a function the can be used for testing whether a set is to be counted.

def sums_to_20(set): return sum(set)==20
count=0 for set in Set(range(1,21)).subsets(): if sums_to_20(set): count+=1 count
64

2. Create SageMath code to do the following:

  • Import the csv package
  • Read in the States csv file
  • For each state, compute its population density (population/area). Use // to compute this number as an integer. Then print the state name followed by the density.

csvArray=[] import csv with open('States.csv') as csvDataFile: csvData = csv.reader(csvDataFile) for row in csvData: csvArray=csvArray+[row]

We want to avoid using the first item in the array since it is the headings. Use csvArray[1:] instead of just csvArray.

csvArray[0]
['name', 'abbreviation', 'capital', 'most populous city', 'population', 'square miles', 'time zone 1', 'time zone 2', 'dst']

Here are two ways to get the same result, first using a loop and second using map.

density_array=[] for row in csvArray[1:]: density_array=density_array+[[row[0],float(row[4])//float(row[5])]] density_array
[['ALABAMA', 89.0], ['ALASKA', 1.0], ['ARIZONA', 57.0], ['ARKANSAS', 54.0], ['CALIFORNIA', 225.0], ['COLORADO', 48.0], ['CONNECTICUT', 634.0], ['DELAWARE', 452.0], ['FLORIDA', 281.0], ['GEORGIA', 165.0], ['HAWAII', 118.0], ['IDAHO', 18.0], ['ILLINOIS', 222.0], ['INDIANA', 176.0], ['IOWA', 53.0], ['KANSAS', 34.0], ['KENTUCKY', 106.0], ['LOUISIANA', 86.0], ['MAINE', 37.0], ['MARYLAND', 459.0], ['MASSACHUSETTS', 624.0], ['MICHIGAN', 102.0], ['MINNESOTA', 60.0], ['MISSISSIPPI', 60.0], ['MISSOURI', 85.0], ['MONTANA', 6.0], ['NEBRASKA', 23.0], ['NEVADA', 23.0], ['NEW HAMPSHIRE', 141.0], ['NEW JERSEY', 998.0], ['NEW MEXICO', 16.0], ['NEW YORK', 358.0], ['NORTH CAROLINA', 174.0], ['NORTH DAKOTA', 9.0], ['OHIO', 257.0], ['OKLAHOMA', 52.0], ['OREGON', 38.0], ['PENNSYLVANIA', 273.0], ['RHODE ISLAND', 681.0], ['SOUTH CAROLINA', 142.0], ['SOUTH DAKOTA', 10.0], ['TENNESSEE', 149.0], ['TEXAS', 92.0], ['UTAH', 32.0], ['VERMONT', 64.0], ['VIRGINIA', 184.0], ['WASHINGTON', 93.0], ['WEST VIRGINIA', 75.0], ['WISCONSIN', 86.0], ['WYOMING', 5.0]]
map(lambda row:[row[0],float(row[4])//float(row[5])],csvArray[1:])
[['ALABAMA', 89.0], ['ALASKA', 1.0], ['ARIZONA', 57.0], ['ARKANSAS', 54.0], ['CALIFORNIA', 225.0], ['COLORADO', 48.0], ['CONNECTICUT', 634.0], ['DELAWARE', 452.0], ['FLORIDA', 281.0], ['GEORGIA', 165.0], ['HAWAII', 118.0], ['IDAHO', 18.0], ['ILLINOIS', 222.0], ['INDIANA', 176.0], ['IOWA', 53.0], ['KANSAS', 34.0], ['KENTUCKY', 106.0], ['LOUISIANA', 86.0], ['MAINE', 37.0], ['MARYLAND', 459.0], ['MASSACHUSETTS', 624.0], ['MICHIGAN', 102.0], ['MINNESOTA', 60.0], ['MISSISSIPPI', 60.0], ['MISSOURI', 85.0], ['MONTANA', 6.0], ['NEBRASKA', 23.0], ['NEVADA', 23.0], ['NEW HAMPSHIRE', 141.0], ['NEW JERSEY', 998.0], ['NEW MEXICO', 16.0], ['NEW YORK', 358.0], ['NORTH CAROLINA', 174.0], ['NORTH DAKOTA', 9.0], ['OHIO', 257.0], ['OKLAHOMA', 52.0], ['OREGON', 38.0], ['PENNSYLVANIA', 273.0], ['RHODE ISLAND', 681.0], ['SOUTH CAROLINA', 142.0], ['SOUTH DAKOTA', 10.0], ['TENNESSEE', 149.0], ['TEXAS', 92.0], ['UTAH', 32.0], ['VERMONT', 64.0], ['VIRGINIA', 184.0], ['WASHINGTON', 93.0], ['WEST VIRGINIA', 75.0], ['WISCONSIN', 86.0], ['WYOMING', 5.0]]

Here is how to sort the states by population density.

def sortkey(row): return row[1]
sorted(map(lambda row:[row[0],float(row[4])//float(row[5])],csvArray[1:]),key=sortkey)
[['ALASKA', 1.0], ['WYOMING', 5.0], ['MONTANA', 6.0], ['NORTH DAKOTA', 9.0], ['SOUTH DAKOTA', 10.0], ['NEW MEXICO', 16.0], ['IDAHO', 18.0], ['NEBRASKA', 23.0], ['NEVADA', 23.0], ['UTAH', 32.0], ['KANSAS', 34.0], ['MAINE', 37.0], ['OREGON', 38.0], ['COLORADO', 48.0], ['OKLAHOMA', 52.0], ['IOWA', 53.0], ['ARKANSAS', 54.0], ['ARIZONA', 57.0], ['MINNESOTA', 60.0], ['MISSISSIPPI', 60.0], ['VERMONT', 64.0], ['WEST VIRGINIA', 75.0], ['MISSOURI', 85.0], ['LOUISIANA', 86.0], ['WISCONSIN', 86.0], ['ALABAMA', 89.0], ['TEXAS', 92.0], ['WASHINGTON', 93.0], ['MICHIGAN', 102.0], ['KENTUCKY', 106.0], ['HAWAII', 118.0], ['NEW HAMPSHIRE', 141.0], ['SOUTH CAROLINA', 142.0], ['TENNESSEE', 149.0], ['GEORGIA', 165.0], ['NORTH CAROLINA', 174.0], ['INDIANA', 176.0], ['VIRGINIA', 184.0], ['ILLINOIS', 222.0], ['CALIFORNIA', 225.0], ['OHIO', 257.0], ['PENNSYLVANIA', 273.0], ['FLORIDA', 281.0], ['NEW YORK', 358.0], ['DELAWARE', 452.0], ['MARYLAND', 459.0], ['MASSACHUSETTS', 624.0], ['CONNECTICUT', 634.0], ['RHODE ISLAND', 681.0], ['NEW JERSEY', 998.0]]