+1 (315) 557-6473 

Create a Program to Implement Files Tuples in Python Assignment Solution.


Instructions

Objective
Write a python homework to implement files tuples in python language.

Requirements and Specifications

# SI 506: Problem Set 05
## This week's Problem Set
This week's problem set focuses on files, functions, nested loops, and tuples.
## Background
On September 26, 2021, Germany held a national election. Germany's most recent previous national election occured in 2017. The 2021 election was important for many reasons. Angela Merkel, who has been the chancellor of Germany since 2005, elected not to run, leaving big shoes to fill. This election also served as a referendum on extreme and populist parties in Germany, which did very well in Germany's 2017 election. Germany is the largest country in the European Union and has huge sway on the continent. This election will impact politics worldwide. How did things shake up this year? Let's load some data and find out!
### Problem 01 (15 points)
Write a function called `read_csv` that defines one parameter:
* `filepath` (`str`): represents the path and name of a csv file
### Requirements
This function should load a csv file and return its contents in a list of lists, where each list is one row from the csv file.
Use `read_csv` to load `election_data_2021.csv` and `election_data_2017.csv` into variables called `raw_election_data_2021` and `raw_election_data_2017`, respectively. Do not conduct any data cleaning in the `read_csv` function.
### Problem 02 (15 points)
Define a function called `clean` with one parameter:
* `data` (list): A list of lists, each representing a party's election results
### Requirements
This function will be used to create a **deep copy** of the data that was loaded in Problem 1 and return a clean version of the raw data. This function should accomplish three things:
  1. convert numbers to integers,
  2. remove trailing whitespace from **all** strings, and
  3. make the values under party affiliation lowercase.
:bulb: Data cleaning is a crucial step when working with any real-world data. It makes working with data easier and helps us avoid mistakes!
Use `clean` to clean `raw_election_data_2021` and `raw_election_data_2017`. Store the cleaned version of these datasets in variables called `clean_election_data_2021` and `clean_election_data_2017`, respectively. The clean data should look something like this:
```python
['Party Name', 76, 'center left']
```
:bulb: Be sure to use the cleaned data and not the raw data for future problems!
### Problem 03 (15 points)
Write a function called `get_seat_differences` that defines two parameters:
* `current_election` (`list`): List of lists that represents election data
* `previous_election` (`list`): List of lists that represents election data
### Requirements
Using **nested loops**, calculate the difference between the number of seats that a party won in the current election and the number of seats that a party won in the previous election. Store that difference in a list of tuples, where each tuple has the party name at index 0 and the difference in seats between the two elections at index 1 (example: `("Party Name", -15)`)
Use `get_seat_differences` to get the seat differences between the 2021 and 2017 elections for every party. Store the resulting list of tuples in a variable called `party_seat_differences`.
### Problem 04 (20 points)
Write a function called `get_leaders` that defines two parameters:
* `election_data` (`list`): List of lists representing election data
* `party_leaders` (`list`): List of tuples, where each tuple contains (`party_name`, `party_leader(s)`)
### Requirements
Create a **deep copy** of `election_data`. Create a new column in the copy of `election_data` called `Party Leader(s)` and fill each party row with the appropriate party leader(s). Remember to return the edited deep copy of `election_data`. When looping over `party_leaders`, you must name your loop variable `party_tuple`. For this problem, you are required to employ **nested loops** and to **unpack the tuple** in order to access the values therein.
Use this function to add the data in `party_leaders_2021` to `clean_election_data_2021`. Store the resulting list of lists in a variable called `election_data_2021_with_leaders`.
:bulb: In Germany, chancellors are not elected directly. Rather, a chancellor has to both form a majority coalition with other parties *and* be the leader of the party with the most votes. If the leader of the party with the most votes cannot form a coalition, the leader of the party with the next highest vote total gets to try to form a majority coaltion.
### Problem 05 (20 points)
Write a function called `get_seats_percent` that defines one parameter:
* `election_data` (`list`): List of lists representing election data
### Requirements
This function takes a list of lists and then calculates the percentage of seats that different affiliations won. The percentages should be as follows:
* percent of seats that parties on the left won (contain "left" in affiliation)
* percent of seats that parties on the right won (contain "right" in affiliation)
* percent of seats that parties in the center won (does not contain "far" in affiliation)
* percent of seats that extreme parties won (contain "far" in affiliation)
Calculate the percentage of seats won using this formula:
* (seats / total_seats) * 100
Then, using the `round` function, round the resulting four percentage values to two decimal points.
:bulb: Hint: You may consider accumulating the number of seats for each affiliation category in one loop and performing the percent calculations outside of the loop.
Return the resulting percentage values in a tuple with the percentage values stored in this order: left, right, extreme, center.
Using `get_seats_percent`, calculate the percentage of seats that center and extreme parties and right and left parties won in 2021. Store the returned value in a variable called `affiliation_percents`.
:bulb: For this problem, you may used either `clean_election_data_2021` or `election_data_2021_with_leaders` to calculate the percentage values. Just be sure to index values according to the dataset you plan to use!
### Problem 06 (15 points)
Write a function called `write_csv` that defines two parameters:
* `filepath` (`string`): represents the path and name of a csv file
* `data` (`list`): List of lists representing election data
Write `data` to a csv file in `filepath`, where each list is a row in the csv file.
Finally, call `write_csv` to write `election_data_2021_with_leaders` to a new file called `revised_election_data_2021.csv`.
Source Code
# SI 506 Problem Set 05
import csv
import copy
print("Problem 01\n\n")
# Problem 01: Implement read_csv and load the election data.
# TODO Implement
def read_csv(filepath: str):
# Create list that will be returned
ret = list()
# Open file with a try-catch block
try:
file = open(filepath, 'r')
# Read all lines
lines = file.readlines()
# Loop through lines
for line in lines:
# Split line by comma
row = line.split(",")
# Add row to list
ret.append(row)
except FileNotFoundError:
print(f"Could not open file at: {filepath}")
return ret
# Read data
raw_election_data_2017 = read_csv("election_data_2017.csv")
raw_election_data_2021 = read_csv("election_data_2021.csv")
print("\n\nProblem 02\n\n")
# Problem 02: Implement clean and clean the election data.
# TODO Implement
def clean(data: list):
# First, create a copy of the data
data_copy = copy.deepcopy(data)
# Iterate through rows
for i, row in enumerate(data_copy):
row_clean = list()
# Iterate through the elements in the row and convert numbers to integers
for j, x in enumerate(row):
x = x.strip()
if x.isnumeric():
x = int(x)
else: # it is not a number, then remove trailing spaces and do all other cleaning stuff
if j == 0 and i > 0: # it is under party affiliation (first column). We check for i > 0 so we do not change the column names
x = x.lower()
row_clean.append(x)
data_copy[i] = row_clean
return data_copy
# Clean datasets
clean_election_data_2017 = clean(raw_election_data_2017)
clean_election_data_2021 = clean(raw_election_data_2021)
print("\n\nProblem 03\n\n")
# Problem 03: Implement get_party_seat_differences and get the party seat differences for the 2021 election.
# TODO Implement
def get_seat_differences(current_election: list, previous_election: list):
# Create the list that will store the tuples
ret = list()
# Loop through the data for the current election
for i, current_row in enumerate(current_election):
# loop through the data for the previous election
curr_party_name = current_row[0]
curr_party_seats = current_row[1]
for j, previous_row in enumerate(previous_election):
prev_party_name = previous_row[0]
prev_party_seats = previous_row[1]
if i > 0 and j > 0 and prev_party_name == curr_party_name:
# Calculate difference
diff = curr_party_seats - prev_party_seats
# Create tuple
tup = (curr_party_name, diff)
# Append
ret.append(tup)
return ret
party_seat_differences = get_seat_differences(clean_election_data_2021, clean_election_data_2017)
print("\n\nProblem 04\n\n")
# Problem 04: Implement get_leaders and get the leaders for the 2021 election data.
party_leaders_2021 = [
('AfD', 'Joerg Meuthen and Tino Chrupalla'),
('FDP', 'Christian Lindner'),
('CDU/CSU', 'Armin Laschet'),
('SPD', 'Olaf Scholz'),
('Greens', 'Annalena Baerbock and Robert Habeck'),
('Left', 'Janine Wissler and Susanne Hennig-Wellsow')
]
# TODO Implement
def get_leaders(election_data: list, party_leaders: list):
# First, create deep copy of election data
election_data_copy = copy.deepcopy(election_data)
# Add new column name
election_data_copy[0].append("Party Leader(s)")
# Now, iterate through rows
for i, row in enumerate(election_data_copy):
if i > 0: # not in row that contains column names
party_name = row[0]
# Iterate through party leaders
for party_tuple in party_leaders:
# Unpack tuple
(leader_party, leader_name) = party_tuple
if party_name == leader_party.lower():
row.append(leader_name)
# Return
return election_data_copy
election_data_2021_with_leaders = get_leaders(clean_election_data_2021, party_leaders_2021)
print("\n\nProblem 05\n\n")
# Problem 05: Implement get_affiliation_percents and get affiliation percents for the 2021 election data.
# TODO Implement
def get_seats_percent(election_data: list):
# First of all, calculate the total number of seats in the election
total_seats = 0
# Also, in this loop, accumulate the total number of seats for each affiliation. For this, we can use a dictionary
seats_per_affiliation = dict()
for i, row in enumerate(election_data):
if i > 0:
total_seats += row[1]
affiliation = row[2]
if "left" in affiliation:
& seats_per_affiliation["left"] = seats_per_affiliation.get("left", 0) + row[1]
if "right" in affiliation:
seats_per_affiliation["right"] = seats_per_affiliation.get("right", 0) + row[1]
if "center" in affiliation:
seats_per_affiliation["center"] = seats_per_affiliation.get("center", 0) + row[1]
if "far" in affiliation:
seats_per_affiliation["extreme"] = seats_per_affiliation.get("extreme", 0) + row[1]
# Now, calculate percentages and save them into a tuple. Then, store each tuple in a list
ret = list()
for affiliation in seats_per_affiliation:
percent = round(seats_per_affiliation[affiliation]/total_seats *100.0, 2)
tup = (affiliation, percent)
ret.append(tup)
# Finally, return list
return ret
affiliation_percents = get_seats_percent(election_data_2021_with_leaders)
print("\n\nProblem 06\n\n")
# Problem 06: Implement write_csv and write election_data_2021_with_leaders to a file called revised_election_data_2021.csv.
# TODO Implement
def write_csv(filepath: str, data: list):
with open(filepath, 'w+') as file:
# Write each row separated by commas
for row in data:
# Convert everything in the row to string
row_str = [str(x) for x in row]
line = ",".join(row_str)
file.write(line+"\n")
write_csv("revised_election_data_2021.csv", election_data_2021_with_leaders)