Code Not Working Properly - Trying To Create A Simple Graph - python

I'm trying to create a simple program where my inputs about data relating to daily COVID-19 cases will then be tabulated and created into a small graph. For example, I'll first input (primary input) will be: 7 20200401 20200403, which represents the # of inputs after my primary input, and from what dates the cases are from. Then I'll go onto input which the hospital, the # of cases from that hospital, and the day of the report. The number of cases per day will be represented by a * . When I go to run my program, it just shows me what the last # of cases inputted was for all 7 days. Is there any way to fix it, and have the program properly display the correct amount of cases per day?
Just to help you understand, here is what a sample input and output should be for this program:
Input:
7 20200401 20200403
HP1 20200401 1
HP2 20200401 1
HP3 20200401 1
HP4 20200402 1
HP5 20200402 1
HP6 20200403 1
HP7 20200403 1
Output:
20200401:***
20200402:**
20200403:**
But instead, I get this:
20200401:*
20200402:*
20200403:*
Here is my code:
CoronaCaseNumber = input("")
CoronaList = CoronaCaseNumber.split(" ")
LuckyNumber = CoronaList[0]
Date = CoronaList[1]
Date2 = CoronaList[2]
LuckyNumero = int(LuckyNumber)
DateList = []
CaseNumberList = []
for case in range(LuckyNumero):
CoronaCaseData = input()
CoronaList2 = CoronaCaseData.split(" ")
InfoDate = CoronaList2[1]
DateList.append(InfoDate)
CaseNumber = CoronaList2[2]
CaseNumberList.append(CaseNumber)
EmptySet = []
for i in DateList:
if i >= Date and i <= Date2:
if i not in EmptySet:
EmptySet.append(i)
for i in range(0, len(CaseNumberList)):
CaseNumberList[i] = int(CaseNumberList[i])
EmptySet.sort()
for i in range(len(EmptySet)):
print("{}{}{}".format(EmptySet[i], ":", "*" * CaseNumberList[i]))

I'm way too lazy to type in all that data everytime I run your script, so I automated that part to make development and testing of it easier. Likewise, I think the easiest thing to do would be to use the collections module's defaultdict class to keep track of what dates have been seen and the total number of cases seen on each of them. Here's what I mean:
from collections import defaultdict
#CoronaCaseNumber = input("")
#CoronaList = CoronaCaseNumber.split(" ")
#LuckyNumber = CoronaList[0]
#Date = CoronaList[1]
#Date2 = CoronaList[2]
LuckyNumber, Date, Date2 = "8 20200401 20200404".split(" ")
data = """\
HP4 20200402 1
HP5 20200402 1
HP1 20200401 1
HP2 20200401 1
HP3 20200401 1
HP6 20200403 0
HP6 20200404 1
HP7 20200404 1
""".splitlines()
LuckyNumero = int(LuckyNumber)
DateList = []
CaseNumberList = []
for case in range(LuckyNumero):
CoronaCaseData = data[case]
CoronaList2 = CoronaCaseData.split(" ")
InfoDate = CoronaList2[1]
DateList.append(InfoDate)
CaseNumber = CoronaList2[2]
CaseNumberList.append(CaseNumber)
DailyCases = defaultdict(int)
for i, d in enumerate(DateList):
if Date <= d <= Date2: # Valid date?
DailyCases[d] += int(CaseNumberList[i])
# Print daily cases sorted by date (i.e. the dictionary's keys).
for date in sorted(DailyCases, key=lambda d: int(d)):
print("{}:{}".format(date, '*' * DailyCases[date]))
Output:
20200401:***
20200402:**
20200403:
20200404:**

Related

Calculating the maximum numbers of people in a room, when given entry and exit times (both object type)

this has proven to be a challenging task for me so would really appreciate any help:
We have two columns in a data frame: start_time, end_time (both object type hh:mm:ss) which I converted into seconds (float64).
An example of our data (out of 20000 rows):
start_time=["00:01:14", "00:01:15", "00:01:30"]
end_time=["00:01:39", "00:02:25", "00:02:10"]
I am running the following code, but I am not convinced it's correct:
def findMaxPassengers(arrl, exit, n):# define function
arrl.sort() # Sort arrival and exit arrays
exit.sort()
passengers_in = 1
max_passengers = 1
time = arrl[0]
i = 1
j = 0
while (i < n and j < n):
if (arrl[i] <= exit[j]): # if the next event in sorted order is an arrival, then add 1
passengers_in = passengers_in + 1
# Update max_passengers if needed
if(passengers_in > max_passengers):
max_passengers = passengers_in
time = arrl[i]
i = i + 1
else:
passengers_in = passengers_in - 1
j = j + 1
print("Maximum Number of passengers =", max_passengers, "at time", time)
df = pd.read_excel("Venue_Capacity.xlsx")
arrl = list(df.loc[:,"start_time"]);
exit = list(df.loc[:,"end_time"]);
n = len(arrl);
findMaxPassengers(arrl, exit, n);
Is the thinking/code structure behind it correct?
I am not sure if the way the code&time works, if it's adding 1 or subtracting one correctly. The code is running ok and is giving out:
Maximum Number of Passengers = 402 at time 12:12:09
but I am unable to check a dataset of 20000+ rows.

Dictionary task in Python: Elections

The first line gives the number of entries. Further, each entry contains the name of the candidate and the number of votes cast for him in one of the states. Summarize the results of the elections: for each candidate, determine the number of votes cast for him. Use dictionaries to complete the tasks.
Input:
Number of voting records (integer number), then pairs <> - <>
Output:
Print the solution of the problem.
Example:
Input:
5
McCain 10
McCain 5
Obama 9
Obama 8
McCain 1
Output:
McCain 16
Obama 17
My problem is at the step when I have to sum keys with same names but different values.
My code is:
cand_n = int(input())
count = 0
countd = 0
cand_list = []
cand_f = []
num = []
surname = []
edict = {}
while count < cand_n:
cand = input()
count += 1
cand_f = cand.split(' ')
cand_list.append(cand_f)
for k in cand_list:
for i in k:
if i.isdigit():
num.append(int(i))
else: surname.append(i)
while countd < cand_n:
edict[surname[countd]] = num[countd]
countd += 1
print(edict)
You can add the name and vote to the dictionary directly instead of using one more for() and while().
If the name does not exist in the dictionary, you add the name and vote. If the name exists in the dictionary, increase the vote.
cand_n = int(input())
count = 0
cand_list = []
cand_f = []
edict = {}
while count < cand_n:
cand = input()
count += 1
cand_f = cand.split(' ')
if cand_f[0] in edict:
edict[cand_f[0]] += int(cand_f[1])
else:
edict[cand_f[0]] = int(cand_f[1])
print(edict)

code not working for advent of code 2020 day 2 part 1

my output wasn't the answer and I also had the valids list to check all the valids and I can confirm that my way of checking if something is valid is most definitely wrong, but I don't know how to fix it
the question: https://adventofcode.com/2020/day/2
my output is 450 and this is the puzzle input: https://pastebin.com/MBHaMr7m
#Day 2 part 1
count = 0
valid = 0
#valids = []
with open('inputs\input2.txt') as f:
data = f.read()
data = data.replace(" ", "")
data = data.replace("-", "")
data = data.replace(":", "")
data = [str(x) for x in data.splitlines()]
data = [list(x) for x in data]
for i in range(len(data)):
for j in data[i][3:]:
if data[i][2] == j:
count += 1
if count >= int(data[i][0]) and count <= int(data[i][1]):
valid += 1
#valids.append(data[i])
count = 0
else:
count = 0
#print(valids)
print(valid)
When I printed valids, the passwords were all invalid, my way of checking if it is correct is most likely wrong and I still can't figure out what is wrong with my code, please help.
(couldnt find a good title for the question btw)
You are making invalid assumptions about the input, in particular that each range consists of two single-digit numbers. Try something like
with open(r'inputs\input2.txt') as f:
for line in f:
lowhigh, character, password = line.strip().split()
low, high = lowhigh.split('-')
low = int(low)
high = int(high)
# Now check if <character> occurs between <low> and <high> times in <password>

Print pairs of values from two lists

Everything is working for me in this program except for one thing; I need my output to group the names and values together in the print(names, "\n" values) section.
For example: Pat : €99, Kev : €55, Dermot : €100.
My code is below:
salesperson = int(input("How many salesperson's do you wish to account
for:"))
names = []
values = []
counter = 0
total_sales = 0
total_value = 0
if salesperson > counter:
while salesperson != counter:
name = input("Please enter in the salesperson's name:")
value = float(input("Please enter in the value of sales for the
salesperson:"))
salesperson -= 1
names.append(name)
values.append(value)
total_value += value
from statistics import mean
average_values = mean(values)
minimum = min(values)
maximum = max(values)
print(names,"\n",values)
print(average_values)
print(total_value)
print(minimum)
print(maximum)
You can do this using zip(), and f-strings:
print([f"{x}: £{y}" for x, y in zip(names,values)])
Output:
['Pat: £1.0', 'Dermot: £2.0', 'Kev: £3.0', 'Boris: £4.0', 'Jeremy: £5.0']

Adding simple moving average as an additional column to python DataFrame

I have sales data in sales_training.csv that looks like this -
time_period sales
1 127
2 253
3 123
4 253
5 157
6 105
7 244
8 157
9 130
10 221
11 132
12 265
I want to add 3rd column that contains the moving average. My code -
import pandas as pd
df = pd.read_csv("./Sales_training.csv", index_col="time_period")
periods = df.index.tolist()
period = int(input("Enter a period for the moving average :"))
sum1 = 0
for i in periods:
if i < period:
df['forecast'][i] = i
else:
for j in range(period):
sum1 += df['sales'][i-j]
df['forecast'][i] = sum1/period
sum1 = 0
print(df)
df.to_csv("./forecast_mannual.csv")
This is giving KeyError: 'forecast' at the line df['forecast'][i] = i. What is the issue?
one simple solution for it, just df['forecast'] = df['sales']
import pandas as pd
df = pd.read_csv("./Sales_training.csv", index_col="time_period")
periods = df.index.tolist()
period = int(input("Enter a period for the moving average :"))
sum1 = 0
df['forecast'] = df['sales'] # add one line
for i in periods:
if i < period:
df['forecast'][i] = i
else:
for j in range(period):
sum1 += df['sales'][i-j]
df['forecast'][i] = sum1/period
sum1 = 0
print(df)
df.to_csv("./forecast_mannual.csv")
Your code is giving 'keyerror' because of incorrect way of referencing column value for 'forecast'.Because the first time your code runs,'forecast' column is not yet created and when it tries to reference df'forecast' for first iteration then it gives key error.
Here,our task is to update values in dynamically created new column called 'forecast'. Therefore, instead of df['forecast'][i] you can write df.at[i,'forecast'].
There is another issue in the code.When value of i is less than period you are assigning 'i' to forecast which to my understanding is not correct.It should not display any thing in such case.
Here is my version of corrected code:
import pandas as pd
df = pd.read_csv("./sales.csv", index_col="time_period")
periods = df.index.tolist()
period = int(input("Enter a period for the moving average :"))
sum1 = 0
for i in periods:
print(i)
if i < period:
df.at[i,'forecast'] = ''
else:
for j in range(period):
sum1 += df['sales'][i-j]
df['forecast'][i] = sum1/period
sum1 = 0
print(df)
df.to_csv("./forecast_mannual.csv")
Output when I entered period=2 to calculate moving average:
Hope this helps.

Categories

Resources