Advertisement
drgamba

gambuxo

Dec 15th, 2014
3,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #/usr/bin/python
  2. # -*- coding: latin-1 -*-
  3.  
  4. import sys
  5. print "Welcome to Merchant\'s Guide To The Galaxy"
  6. print "Type a command and then press CTRL + D :"
  7.  
  8. symbols = {
  9.     "i" : 1,
  10.     "v" : 5,
  11.     "x" : 10,
  12.     "l" : 50,
  13.     "c" : 100,
  14.     "d" : 500,
  15.     "m" : 100,  
  16. }
  17.  
  18. alg = {}
  19. ideal = {}
  20.  
  21. def note(t):
  22.     """give back output"""
  23.     print t
  24.  
  25. def check(term):
  26.     """analyse a kind of number"""  
  27.     x = 0
  28.     try:
  29.         y = [alg[i] for i in term]
  30.     except:
  31.         return -1
  32.        
  33.     while y:
  34.         z = y.pop(0)
  35.         if y and y[0] > z:
  36.             x -= z
  37.         else:
  38.             x += z
  39.            
  40.     return x        
  41.  
  42. def function(sentence):
  43.     """analyse the sentence"""
  44.    
  45.     term = sentence.lower().split(None)
  46.    
  47.     if not term:
  48.         return
  49.        
  50.     if len(term) == 3 and term[1] == "is":
  51.         cod = term[0]
  52.         obj = term[2]
  53.        
  54.         if not obj in symbols:
  55.             return note("'%t' is not a valid format." % obj)
  56.         alg[cod] = symbols[obj]
  57.         return
  58.        
  59.     if len(term) > 4 and term[-1] == "credits" and term[-3] == "is":
  60.         term.pop()
  61.         try:
  62.             obj = float(term[-1])
  63.         except:
  64.             return note("'%t' is not a valid numeric value" % term[-1])
  65.            
  66.         term.pop()
  67.         term.pop()
  68.         p = term.pop()
  69.         x = check(term)
  70.        
  71.         if x < 0:
  72.             return note("Not a valid type")
  73.        
  74.         ideal[p] = obj / x
  75.         return
  76.        
  77.     if term[0:3] == ["how", "much", "is"]:
  78.         term = term[3:]
  79.         if term[-1] == "?":
  80.             term.pop()
  81.            
  82.         x = check(term)
  83.         if x < 0:
  84.             return note("'%t' isn't a Galactic term" % " ".join(term))
  85.            
  86.         print " ".join(term), "is", x
  87.         return
  88.        
  89.     if term[0:4] == ["how", "many", "credits", "is"]:
  90.         term = term[4:]
  91.         if term[-1] == "?":
  92.             term.pop()
  93.            
  94.         p = term.pop()
  95.         if not p in ideal:
  96.             return note("There's no information about %t" % p)
  97.            
  98.         x = check(term)
  99.         if x < 0:
  100.             return note("'%t' isn't a Galactic term" % " ".join(term))
  101.            
  102.         print " ".join(term), p.title(), "is", int(x * ideal[p]), "Credits"
  103.         return
  104.        
  105.     return note("I've no idea what you are talking about")
  106.  
  107. for sentence in sys.stdin:
  108.    function(sentence)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement