Advertisement
Guest User

isValidTrafficSequence.py

a guest
Jun 9th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. print("Implemented by Pedro Izecksohn.")
  2.  
  3. def isValidTrafficSequence (seq):
  4.     if not seq:
  5.         return True
  6.     i=0
  7.     while i<(len(seq)-1):
  8.         i+=1
  9.         if seq[i-1]=="red":
  10.             if seq[i]=="green":
  11.                 continue
  12.             return False
  13.         if seq[i-1]=="yellow":
  14.             if seq[i]=="red":
  15.                 continue
  16.             return False
  17.         if seq[i-1]=="green":
  18.             if seq[i]=="yellow":
  19.                 continue
  20.             return False
  21.         print("Invalid data.")
  22.         exit()
  23.     return True
  24.  
  25. def main():
  26.     seq=[]
  27.     s="a"
  28.     while len(s):
  29.         s=input()
  30.         if len(s):
  31.             seq.append(s)
  32.     print(isValidTrafficSequence(seq))
  33.  
  34. if __name__=="__main__":
  35.     main()
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement