#Time Zone intersection problem
1 messages · Page 1 of 1 (latest)
Everything you get from input is a string. So, you need to convert them. In other words, you need to convert t1 and t2.
However, it seems like you're trying to use set.intersection. You might want to try something like this set([x]).intersection(set([y]))
this is what i ended up doing
With all due respect, this is a very bad solution. You can do much better. I encourage you to use this solution and try to make it less repetitive.
But, if it's an assignment and you're in peace with this solution then that's your choice.
I am very much aware hehe... i really want to know how it can be made much more simpler.
Send the code you have now and if I or someone else got time we'll send a simpler version.
Send your code like this
```python
your code here
```
im assuming the only very bad part is all the elifs maybe?
Hard to say if it's the only bad part but yes
mmk bet
no i want you to tell me 🙂 im super new and ion wanna be sugarcoated i need all the criticism i can get as long as i understand in the end
I complety understand and that a super good attitude, I love it. Still, it's hard to say if it's the only bad part but it is the really bad that I can see yes
I love how you're lurking the convo @fresh urchin hahaha
And don't be to hard on yourself either. It's normal to not produce the best solutions at first.
i dont have nitro, my code is too long to send as a message 😦
Lol I do not have much time atm to take part of it and give a solution because I’m at a conference but I am watching ahah
I love thé attitude as well
no prob i appreciate it
Send it in multiple parts or use on of the one of those
NP it was just funny to see you react to the messages without saying anything
def time_zones(x, y):
Eastern = {"Connecticut", "Delaware", "Georgia", "Maine", "Maryland", "Massachusetts", "New Hampshire",
"New Jersey", "New York", "North Carolina", "Ohio", "Pennsylvania", "Rhode Island",
"South Carolina", "Vermont", "Virginia", "West Virginia", "Florida", "Indiana", "Kentucky", "Michigan",
"Tennessee"}
Central = {"Alabama", "Arkansas", "Illinois", "Iowa", "Louisiana", "Minnesota", "Mississippi",
"Missouri", "Oklahoma", "Wisconsin", "Florida", "Indiana", "Kansas", "Kentucky", "Michigan",
"Nebraska", "North Dakota", "South Dakota", "Tennessee", "Texas"}
Mountain = {"Arizona", "Colorado", "Montana", "New Mexico", "Utah", "Wyoming", "Idaho",
"Kansas", "Nebraska", "North Dakota", "Oregon", "South Dakota", "Texas"}
Pacific = {"California", "Washington", "Idaho", "Nevada", "Oregon"}
if x == "Eastern":
x = Eastern
if y == "Eastern":
y = Eastern
elif y == "Central":
y = Central
elif y == "Mountain":
y = Mountain
elif y == "Pacific":
y = Pacific
elif x == "Central":
x = Central
if y == "Eastern":
y = Eastern
elif y == "Central":
y = Central
elif y == "Mountain":
y = Mountain
elif y == "Pacific":
y = Pacific
elif x == "Mountain":
x = Mountain
if y == "Eastern":
y = Eastern
elif y == "Central":
y = Central
elif y == "Mountain":
y = Mountain
elif y == "Pacific":
y = Pacific
elif x == "Pacific":
x = Pacific
if y == "Eastern":
y = Eastern
elif y == "Central":
y = Central
elif y == "Mountain":
y = Mountain
elif y == "Pacific":
y = Pacific
answer = x.intersection(y)
if __name__ == '__main__':
t1 = input("PLease enter time zone 1: ")
t2 = input("Please enter time zone 2: ")
print(time_zones(t1, t2))
this is how my friend did it. obv same concepts i just decided to make it a function and i feel i made it easier on myself by doing only one intersection in the whole thing and just copy/pasting all the elifs for "y"
also just curious what bot is this?
thank you all for ur help btw
ESTEARN = {
"Connecticut", "Delaware", "Georgia", "Maine", "Maryland", "Massachusetts", "New Hampshire",
"New Jersey", "New York", "North Carolina", "Ohio", "Pennsylvania", "Rhode Island",
"South Carolina", "Vermont", "Virginia", "West Virginia", "Florida", "Indiana", "Kentucky",
"Michigan","Tennessee"
}
CENTRAL = {
"Alabama", "Arkansas", "Illinois", "Iowa", "Louisiana", "Minnesota", "Mississippi", "Missouri",
"Oklahoma", "Wisconsin", "Florida", "Indiana", "Kansas", "Kentucky", "Michigan", "Nebraska",
"North Dakota", "South Dakota", "Tennessee", "Texas"
}
MOUNTAIN = {
"Arizona", "Colorado", "Montana", "New Mexico", "Utah", "Wyoming", "Idaho","Kansas", "Nebraska",
"North Dakota", "Oregon", "South Dakota", "Texas"
}
PACIFIC = {"California", "Washington", "Idaho", "Nevada", "Oregon"}
TIME_ZONE_BY_NAMES = {
"eastern": ESTEARN,
"central": CENTRAL,
"mountain": MOUNTAIN,
"pacific": PACIFIC,
}
def time_zones(t1, t2):
time_zone1 = TIME_ZONE_BY_NAMES[t1]
time_zone2 = TIME_ZONE_BY_NAMES[t2]
return time_zone1.intersection(time_zone2)
if __name__ == '__main__':
t1 = input("PLease enter time zone 1: ").lower()
t2 = input("Please enter time zone 2: ").lower()
print(time_zones(t1, t2))
I haven't totally tested but it seems to give the same result so, try and and yea... do whatever you want with that and don't hesitate to ask questions
TIME_ZONE_BY_NAMES is a dictionary. It's a type of data structure that associated a key with a value. Very useful for that kind of situation. https://docs.python.org/3/tutorial/datastructures.html
I wrote some of the variable names in capital letters because they are constants and constants need to be in all caps. https://peps.python.org/pep-0008/#constants
It's built in discord.
;compile
ESTEARN = {
"Connecticut", "Delaware", "Georgia", "Maine", "Maryland", "Massachusetts", "New Hampshire",
"New Jersey", "New York", "North Carolina", "Ohio", "Pennsylvania", "Rhode Island",
"South Carolina", "Vermont", "Virginia", "West Virginia", "Florida", "Indiana", "Kentucky",
"Michigan","Tennessee"
}
CENTRAL = {
"Alabama", "Arkansas", "Illinois", "Iowa", "Louisiana", "Minnesota", "Mississippi", "Missouri",
"Oklahoma", "Wisconsin", "Florida", "Indiana", "Kansas", "Kentucky", "Michigan", "Nebraska",
"North Dakota", "South Dakota", "Tennessee", "Texas"
}
MOUNTAIN = {
"Arizona", "Colorado", "Montana", "New Mexico", "Utah", "Wyoming", "Idaho","Kansas", "Nebraska",
"North Dakota", "Oregon", "South Dakota", "Texas"
}
PACIFIC = {"California", "Washington", "Idaho", "Nevada", "Oregon"}
TIME_ZONE_BY_NAMES = {
"eastern": ESTEARN,
"central": CENTRAL,
"mountain": MOUNTAIN,
"pacific": PACIFIC,
}
def time_zones(t1, t2):
time_zone1 = TIME_ZONE_BY_NAMES[t1]
time_zone2 = TIME_ZONE_BY_NAMES[t2]
return time_zone1.intersection(time_zone2)
if __name__ == '__main__':
t1 = "eastern"
t2 = "central"
print(time_zones(t1, t2))
{'Michigan', 'Tennessee', 'Florida', 'Indiana', 'Kentucky'}
I see now. just learned dictionaries and my next problem i think deals w them, imma have to learn em more online. thanks for that document and for your feedback! I also don't fully understand the all caps constants so i will look further into that link. Also one question, why did you put .lower() after the input for t1 and t2? what does .lower() do
.lower() returns a copy of the string in lowercase. https://docs.python.org/3/library/stdtypes.html#str.lower
I did that because we can't know if the user will write the key correctly, they are case sensitive. That way, whatever the user writes (Ex. Central, CENTRAL, cEnTraL, centraL) it will always work correctly.
As for the all caps thing, it's simply a naming convention.
Python as a styling guide (the link I've sent) that explain how code should be written to make it simpler and easier to read.
oh okay bet, thank you sm!