Given an bunch of airline tickets with [from, to], for example [MUC, LHR], [CDG, MUC], [SFO, SJC], [LHR, SFO]
please reconstruct the itinerary in order,
[ CDG, MUC, LHR, SFO, SJC ].
Note: tickets can be represented as nodes
#!/usr/bin/env python
import sys
# Reads the given file which contains airline tickets
# and returns 2 hash,
# * (1) with all the ticket info
# * (2) wth the starting and ending points
def getTicketsFromFile(filename):
lines = []
# Read the file
with open(filename, "r") as fp:
lines = lines = fp.readlines()
lines = map(lambda s: s.strip(), lines)
tickets = {}
points = {}
for line in lines:
if line !="":
point = map(lambda s: s.strip(), line[1:-1].split(","))
tickets[point[0]] = point[1]
# create 2 nodes for each ticket (start, end) and
# do this for all the tickets
for p in point:
try:
# Delete the key from the points if it exists to find the start and end
# points for the entire trip.
del points[p]
except:
points[p] = ""
# Find the starting point
for k in points.keys():
if k in tickets.keys():
startPoint = k
break
return (tickets, startPoint)
if __name__ == "__main__":
(tickets, startPoint) = getTicketsFromFile(sys.argv[1])
# print the order
point = startPoint
while True:
print point,
try:
point = tickets[point]
except:
break
ord(c): Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string.
hex(n): Convert an integer number to a hexadecimal string
We do not need first to characters in the hex output.
zfill(witdh): Return the numeric string left filled with zeros in a string of length width.
#!/usr/bin/env python
# Filename: daemon.py
# Make the process to run in the background.
__author__ = "Karthikeyan Periasamy"
__version__ = "0.1"
__all__ = ["daemonize"]
# Standard modules
import os # OS interface functions.
import sys # System specific functions.
from syslog import * # Logging functions.
# Daemon parameters
UMASK = 0 # File mode creation mask for the calling process
WORKDIR = "/" # The Working directory
# Find the null device /dev/null
if (hasattr(os, "devnull")):
NULL_DEVICE = os.devnull
else:
NULL_DEVICE = "/dev/null"
# Function
def daemonize (procname):
openlog (procname, LOG_PID)
try:
# Fork the child
syslog (LOG_INFO, "Daemizing")
syslog (LOG_INFO, "Creating the first child")
pid = os.fork ()
# Exit from the parent
if 0 != pid:
syslog (LOG_INFO, "Parent exiting")
os._exit (0)
# Make the child as the leader of the process
syslog (LOG_INFO, "Child created")
syslog (LOG_INFO, "Creating a new session")
os.setsid ()
# Fork a child again to make sure that no terminal device is connected to it.
syslog (LOG_INFO, "Creating the second child")
pid = os.fork ()
# Exit from the first child
if 0 != pid:
syslog (LOG_INFO, "First child exiting")
os._exit (0)
# Set the calling process's file mode creation mask
syslog (LOG_INFO, "Set the calling process's file mode creation mask")
os.umask (UMASK)
# Changing working directory to '/' to make sure that
# the process does not depend on any mounted partition
# execpt the root of the filesystem
logmsg = 'Changing working directory to "' + WORKDIR + '" to avoid dependencies'
syslog (LOG_INFO, logmsg)
os.chdir (WORKDIR)
# Close standard input, output and error file descriptors
syslog (LOG_INFO, "Closing I/O file descriptors")
os.close (0)
os.close (1)
os.close (2)
# Redirect standard output and errror to /dev/null
os.open(NULL_DEVICE, os.O_RDWR)
os.dup2(0, 1)
os.dup2(0, 2)
except:
logmsg = "Unexpected error:" + repr (sys.exc_info()[1])
syslog (LOG_ERR, logmsg)
os._exit (1)
# ---------------------------------------------------------------------------
# Main program (for testing)
# ---------------------------------------------------------------------------
if __name__ == '__main__':
daemonize("daemon_test")
syslog(LOG_INFO, 'Daemon is sleeping for 10 seconds')
import time
for i in range (10):
syslog(LOG_INFO, 'Sleeping...')
time.sleep(1)
syslog(LOG_INFO, 'Daemon exiting')
sys.exit(0)