def test_flights(self):
     self.st = RedBlackBST()
     schedule = [
         ["09:00:00", "Chicago"],
         ["09:00:03", "Phoenix"],
         ["09:00:13", "Houston"],
         ["09:00:59", "Chicago"],
         ["09:01:10", "Houston"],
         ["09:03:13", "Chicago"],
         ["09:10:11", "Seattle"],
         ["09:10:25", "Seattle"],
         ["09:14:25", "Phoenix"],
         ["09:19:32", "Chicago"],
         ["09:19:46", "Chicago"],
         ["09:21:05", "Chicago"],
         ["09:22:43", "Seattle"],
         ["09:22:54", "Seattle"],
         ["09:25:52", "Chicago"],
         ["09:35:21", "Chicago"],
         ["09:36:14", "Seattle"],
         ["09:37:44", "Phoenix"],
     ]
     for time, city in schedule:
         self.st.put(time, city)
     for time, city in schedule:
         self.assertEqual(city, self.st.get(time))
     self.assertEqual(len(self.st.keys()), len(schedule))
    def setUp(self):
        random.seed(0)

        self.L = random.sample(range(10**6), 10**4)
        self.S = sorted(self.L)
        self.st = RedBlackBST()
        for x in self.L:
            self.st.put(x, x)
Beispiel #3
0
 def __init__(self, preload = []):
     self.li = RedBlackBST()
     for i in preload: # Init is dumb :(
         self.add(self, i)
Beispiel #4
0
# solving kattis problem 'flights'
import sys
from datetime import datetime, timedelta
from itu.algs4.searching.red_black_bst import RedBlackBST

lines = sys.stdin.readlines()
n, m = [int(i) for i in lines[0].strip().split()]

# read flights into ST (with support for operations)
ST = RedBlackBST()
for line in lines[1:n + 1]:
    time, destination = line.strip().split()
    ST.put(datetime.strptime(time, '%H:%M:%S'), destination)

# answer queries
for q in lines[n + 1:len(lines)]:
    query = q.strip().split()
    command = query[0]

    # cancel flight by deleting from ST
    if command == 'cancel':
        ST.delete(datetime.strptime(query[1], '%H:%M:%S'))

    # delay a flight by d amount of seconds
    elif command == 'delay':
        s, d = query[1:]
        time = datetime.strptime(s, '%H:%M:%S')
        val = ST.get(time)
        ST.delete(time)
        updated_t = time + timedelta(seconds=int(d))
        ST.put(updated_t, val)
 def setUp(self):
     self.st = RedBlackBST()
 def setUp(self):
     self.st = RedBlackBST()
     for x in self.L:
         self.st.put(x, x)