Advertisement
Iam_Sandeep

Untitled

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