Posts: 14
Threads: 1
Joined: Dec 2017
Dec-17-2017, 03:03 PM
(This post was last modified: Dec-17-2017, 03:05 PM by klara.)
I've been working on this for almost a day now and I started in so many wrong ways that I cannot see clearly anymore.
What I did now is:
def number_of_polluted_days(januar):
k = {}
v = 0
for day, data in januar.items():
for city, pollution in data.items():
d = ['Texas', 'Moscow', 'Berlin', 'Athens']
if k[pollution]>=50:
v +=1
return v I'm getting more and more embarrased since I feel like you really tell me something useful and I just don't seem to know how to put it in the program.
Posts: 69
Threads: 0
Joined: Dec 2017
Dec-17-2017, 03:16 PM
(This post was last modified: Dec-17-2017, 03:17 PM by Terafy.)
Please don't feel embarrassed. Everyone who started programming had the same experience at the beginning. You get better over time.
The code you had before was the answer just needed to change pollution.
So keep the
k[city]+=1
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 2,954
Threads: 48
Joined: Sep 2016
Dec-17-2017, 03:24 PM
(This post was last modified: Dec-17-2017, 03:34 PM by wavic.)
def number_of_polluted_days(januar):
k = defaultdict(int)
for day, data in januar.items():
for city, pollution in data.items():
if pollution > 50: # at the first post you say that the condition is if the pollution is above 50? Anyway
# k is an empty dictionary
# The defaultdict means when you assign a value to a key which doesn't exist, it creates that key and a initialize it.
k[city] += 1
return v An example:
In [1]: from collections import defaultdict
In [2]: d = defaultdict(int) # the default value for any key which doesn't exist
...: will be an integer
In [3]: for num in range(1, 6):
...: d[num] += 1 # d[num] doesn't exist but we are adding 1 to its value.
...: It will be created in that moment
...:
In [4]: d
Out[4]: defaultdict(int, {1: 1, 2: 1, 3: 1, 4: 1, 5: 1})
In [5]: for key, value in d.items():
...: print(key, '-', str(value))
...:
1 - 1
2 - 1
3 - 1
4 - 1
5 - 1
Posts: 69
Threads: 0
Joined: Dec 2017
(Dec-17-2017, 03:24 PM)wavic Wrote: def number_of_polluted_days(januar):
k = defaultdict(int)
for day, data in januar.items():
for city, pollution in data.items():
if pollution > 50: # at the first post you say that the condition is if the pollution is above 50? Anyway
k[city] += 1
return v
Except it return k not v. See even the pro make mistakes
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 2,954
Threads: 48
Joined: Sep 2016
Dec-17-2017, 03:35 PM
(This post was last modified: Dec-17-2017, 03:35 PM by wavic.)
Sure :D
I am not a pro. I am on the finances. I code for fun.
Posts: 69
Threads: 0
Joined: Dec 2017
Shh... she need a moral boost. On this thread your a super elite programmer that goes by the name... Wavic
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 14
Threads: 1
Joined: Dec 2017
Hehe thank you Ted and thank you Wavic for such support. I've been silent since I am still struggling. The private tests we have for this task, didn't pass yet. I've been at 2 out of 5 already but I'm back at 0 :'(
Posts: 69
Threads: 0
Joined: Dec 2017
Dec-17-2017, 07:33 PM
(This post was last modified: Dec-17-2017, 07:42 PM by Terafy.)
Well this is the code that worked for me... never ran Wavic one. See how very close you were!
def number_of_polluted_days(januar):
k = {}
for day, data in januar.items():
for city, pollution in data.items():
d = ['Texas', 'Moscow', 'Berlin', 'Athens']
if city not in k:
k[city]=0
if pollution >= 50:
k[city] += 1
return k
januar = {
1: {'Texas': 126, 'Moscow': 64, 'Berlin': 57, 'Athens': 35,},
2: {'Texas': 53, 'Moscow': 56, 'Berlin': 58,},
3: {'Texas': 34, 'Moscow': 71, 'Berlin': 48, 'Athens': 55,}
}
print(number_of_polluted_days(januar))
Don't stay silent! If we're out of the loop we can't help you... so voice your trouble :)
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 14
Threads: 1
Joined: Dec 2017
Dec-17-2017, 08:02 PM
(This post was last modified: Dec-17-2017, 08:02 PM by klara.)
Damn I had no idea I had to align "if city..." and "if pollution...". This is why it wasnt't working. It often confuses me. Ted, do you have any suggestions how to know better in the future? And as much as I've tried to understand your sugestions wavic, I understand much better Ted's suggestions. My knowledge in Python is still very limited obviously and I hope to be able to solve such problems in more than one way in the future.
Now I'm going to part 2 where I need to define average_pollution_level which returns a dictionary where for each city an average of pollution level is caculated. But, if for example there are 3 measurments missing for a certain city in these 31 days I need to consider that and then calculate the average of 28 days. I'm confused how I do this because each city is different and I have 14. I don't want to write a whole snake of code.
Ted, any suggestions?
p.s. I'm sorry for making your sunday extra busy :)
Posts: 69
Threads: 0
Joined: Dec 2017
Dec-17-2017, 08:25 PM
(This post was last modified: Dec-17-2017, 08:25 PM by Terafy.)
"...do you have any suggestions how to know better in the future?"
my usual work out goes like this
1) planning out what you want
2) write a pseudo code (optional)
3) do a small bit at a time
4) run the code to see if you getting what you wanted.
5) repeat 3 and 4 until finish.
"My knowledge in Python is still very limited obviously and I hope to be able to solve such problems in more than one way in the future."
you will! I believe in you!
Part 2... It's a Sunday afternoon when Ted secret identity was found out... (suspenseful music!)
Look at the code you did in part 1. Should be exactly like that but instead of check the pollution level you should add pollution and counting the number of time a city appears.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
|