Advertisement
Guest User

PythonQuestion

a guest
Dec 5th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. import sys
  2.  
  3. listDic = {}
  4. data = ["Bird eats Prawn","Bird eats Mussels","Bird eats Crab","Bird eats Limpets","Bird eats Whelk","Crab eats Mussels","Crab eats Limpets",
  5.         "Fish eats Prawn","Limpets eats Seaweed","Lobster eats Crab",
  6.         "Lobster eats Mussels","Lobster eats Limpets","Lobster eats Whelk","Mussels eats Phytoplankton",
  7.         "Mussels eats Zooplankton","Prawn eats Zooplankton","Whelk eats Limpets","Whelk eats Mussels","Zooplankton eats Phytoplankton"]
  8.  
  9. def readFile():
  10.         v = []
  11.         if (len(sys.argv) == 2):
  12.                 fileName = sys.argv[1]
  13.         else:
  14.                 fileName = input("Enter the file name: ")
  15.         inf = open(fileName, "r")
  16.         line = inf.readline().rstrip()
  17.         while (line != ""):
  18.                 v.append(line)
  19.                 line = inf.readline().rstrip()
  20.         return v
  21. def part1(data):
  22.         for i in range (0,len(data)):
  23.                 splitLine = data[i].split("eats")
  24.                 if splitLine[0] in listDic:
  25.                         listDic[splitLine[0]].append(splitLine[1])
  26.                 else:
  27.                         l = [splitLine[1]]
  28.                         listDic[splitLine[0]] = (l)
  29.         for key in listDic.keys() :
  30.                 l = listDic.get(key)
  31.                 print (key,"eats",end=":")
  32.                 for z in range (0,len(l)):
  33.                         if ((len(l) -z) ==1):
  34.                                 print (l[z],end="")
  35.                         else:
  36.                                 print (l[z],end=",")
  37.                                
  38.                 print()
  39.        
  40.  
  41. def part2():
  42.         listPredators = listDic.keys()
  43.         for i in range (0,len(listPredators)):
  44.                 for key in (listDic.keys()):
  45.                         listPrey = listDic.get(key)
  46.                         #print("Predator: ",list(listPredators)[i])
  47.                         #print("Prey: ",listPrey)
  48.                         #print(list(listPredators))
  49.                         for m in range (0,len(listPrey)):
  50.                                 z =(list(listPrey)[m].split(" "))
  51.                                 print("Predators ", list(listPredators)[i])
  52.                                 print(z[1])
  53.                                 if (list(listPredators)[i] == z[1]):
  54.                                         print("Hello")
  55.                
  56.                                
  57.                                
  58. def main():
  59.         part1(data)
  60.         part2()
  61.  
  62. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement