Advertisement
Iam_Sandeep

Untitled

Dec 3rd, 2021
1,217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. import bisect
  2. #for the purpose of showing event table
  3. def display(arr):
  4.     print("="*40)
  5.     x,y,z="Event","Task","No of days"
  6.     print(x,"\t",y,"\t",z)
  7.     print("="*40)
  8.     for e,t,n in arr:
  9.         print(e,"\t\t",t,"\t\t",n)
  10.     for i in range(3):
  11.         print()
  12. #show menu
  13. def show():
  14.     print("Critical Path Analysis")
  15.     print("="*40)
  16.     print("1- Display all Tasks")
  17.     print("2- Find the event with longest duration")
  18.     print("3- Compute the time to finish a given event")
  19.     print("4- Compute project completion time")
  20.     print("5- Plot events time chart")
  21.     print("6- Add new task")
  22.     print("7- Exit program")
  23.  
  24. #finds the sum of all events      
  25. def compute_all_events(arr):
  26.     if not arr:
  27.         print("Total Time Taken will be 0")
  28.         return
  29.     l=arr[-1][0]
  30.     sum=0
  31.     for i in range(1,l+1):
  32.         sum+=compute_finish_event(arr,i)
  33.     print("Total time taken will be",sum)
  34. #shows the longest event
  35. def show_longest_event(arr):
  36.     if not arr:
  37.         print("No events going on")
  38.     n=arr[-1][0]
  39.     fts=[None]*n
  40.     for i in range(n):
  41.         fts[i]=compute_finish_event(arr,i+1)
  42.     i=0
  43.     for j in range(n):
  44.         if fts[j]>fts[i]:
  45.             i=j
  46.     print(fts)
  47.     print("Longest Event",i+1)
  48.        
  49. #it computes the finishing time of an event
  50. def compute_finish_event(arr,e):
  51.     if not arr:
  52.         return -1
  53.     elif arr[-1][0]<e:
  54.         return -1
  55.     res=0
  56.     for x,y,z in arr:
  57.         if x==e:
  58.             res=max(res,z)
  59.     return res
  60. #this verifies whether we have to add new task or not
  61. def verify(arr,x,y,z):
  62.     if y<10 or y>30 or z>9 or z<0 or x<1 or x>5:
  63.         return False
  64.        
  65.     for e,t,n in arr:
  66.         if e==x and t==y:
  67.             return False
  68.     return True
  69.  
  70. def add(arr,e,t,n):
  71.     bisect.insort_right(arr,(e,t,n))
  72. #for inserting task
  73. def insert_task(arr):
  74.     while True:
  75.         print("Enter the new task information (event number, task number, number of days)")
  76.         temp=input().split()
  77.         if len(temp)<=2:
  78.             print("Invalid insertion")
  79.             continue
  80.         else:
  81.             e,t,n=map(int,temp)
  82.             if verify(arr,e,t,n)==False:
  83.                 print("Invalid INsertion")
  84.                 continue
  85.             else:
  86.                 add(arr,e,t,n)
  87.                 print("New Event added Successfully")
  88.                 return
  89. #plot graph
  90. def plot(arr):
  91.     n=len(arr)
  92.     if not arr:
  93.         print("No tasks Found")
  94.         return
  95.     print("-"*40)
  96.     last=arr[-1][0]
  97.     for i in range(1,last+1):
  98.         m=compute_finish_event(arr,i)
  99.         print("Event",i,"*"*m,m)
  100.     print("-"*40)
  101.  #generates the data
  102. def generateData(arr):
  103.     events=[1, 1, 1, 1, 2, 2, 3, 4, 5, 5, 5]
  104.     tasks=[25, 22, 24, 16, 15, 22, 17, 14, 14, 11, 20]
  105.     days=[1, 7, 6, 2, 3, 6, 5, 2, 4, 6, 7]
  106.     for i in range(10):
  107.         arr.append((events[i],tasks[i],days[i]))
  108. # main func
  109. def main():
  110.     cons=[]
  111.     generateData(cons)
  112.     while True:
  113.         show()
  114.         c=input()
  115.         if not c.isdigit():
  116.             print("Invalid Choice")
  117.             continue
  118.         c=int(c)
  119.         if c>7 or c<=0:
  120.             continue
  121.         if c==1:
  122.             display(cons)
  123.         elif c==2:
  124.             show_longest_event(cons)
  125.         elif c==3:
  126.             while True:
  127.                 t=int(input(("Enter the event number")))
  128.                 res=compute_finish_event(cons,t)
  129.                 if res==-1:
  130.                     print("Invalid Option")
  131.                     continue
  132.                 else:
  133.                     print("Project",t,"Completed in",res,"days")
  134.                     break
  135.         elif c==4:
  136.             compute_all_events(cons)
  137.         elif c==5:
  138.             plot(cons)
  139.         elif c==6:
  140.             insert_task(cons)
  141.         else:
  142.             yn=input("Are you sure you want to exit the application y/n")
  143.             if yn=='y':
  144.                 print("GoodBye!")
  145.                 break
  146.            
  147.    
  148. main()
  149.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement