#Time Zone intersection problem

1 messages · Page 1 of 1 (latest)

brave raven
#

I thought about making a function w 2 variables and this is what I got so far, idk how to define the x and y to be a time zone

desert viper
#

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]))

brave raven
desert viper
#

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.

brave raven
desert viper
brave raven
#

im assuming the only very bad part is all the elifs maybe?

desert viper
brave raven
desert viper
#

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

desert viper
brave raven
#

i dont have nitro, my code is too long to send as a message 😦

fresh urchin
#

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

desert viper
desert viper
brave raven
#
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"

brave raven
#

thank you all for ur help btw

desert viper
#
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

desert viper
#

;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))
high laurelBOT
#
Program Output
{'Michigan', 'Tennessee', 'Florida', 'Indiana', 'Kentucky'}
brave raven
desert viper
#

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.

brave raven
#

oh okay bet, thank you sm!