# vacation.py - Find out when two people can take a vacation during some # 31-day month, based on their vacation schedules. # This program illustrates how to use a bit vector to store a "set" of # information. An integer consists of bits, and we'll use bits 1-31 # to see which day(s) we can both take off. # If a bit = 1, it's available for vacation; 0 means we have to work. begin = int(input("Starting date of your vacation (1-31): ")) end = int(input("Ending date of your vacation (1-31): ")) # For each day in the range the user listed, turn on the appropriate bit. userVacation = 0 for day in range(begin, end+1): userVacation |= 1 << day # Repeat procedure for a friend. begin = int(input("Starting date of friend's vacation (1-31): ")) end = int(input("Ending date of friend's vacation (1-31): ")) friendVacation = 0 for day in range(begin, end+1): friendVacation |= 1 << day # ------------------------------------------------------------------- # Print out when our 2 vacations are. print("\nHere is when you can days off. Y = you, F = friend") print(" 1111111111222222222233") print("1234567890123456789012345678901") for day in range(1, 32): if userVacation & (1 << day) != 0: print("Y", end = "") else: print(" ", end = "") print(" your vacation days") for day in range(1, 32): if friendVacation & (1 << day) != 0: print("Y", end = "") else: print(" ", end = "") print(" friend's vacation days\n") # ------------------------------------------------------------------- # Now check for overlap. We use '&' for the intersection of 2 sets. combinedVacation = userVacation & friendVacation if combinedVacation == 0: print("Sorry, your vacation dates don't overlap.\n") else: print("Here is when your vacation dates overlap:") for day in range(1, 32): if combinedVacation & (1 << day) != 0: print(day, end = " ") print("\n") # ------------------------------------------------------------------- # Let's say your friend is someone you'd rather not hang around with # on your time off. You want to be sure to take your vacation when # your friend has to work. userAlone = userVacation & ~ friendVacation if userAlone == 0: print("Sorry, you can't take a vacation while your friend is working.\n") else: print("Here's when you can take a vacation by yourself:") for day in range(1, 32): if userAlone & (1 << day) != 0: print(day, end = " ") print("\n")