Advertisement
go6odn28

11_course_planning

Apr 4th, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. def is_valid(lst, idx):
  2.     return 0 <= idx < len(lst)
  3.  
  4.  
  5. def add_lesson(list_of_lessons, title):
  6.     if title not in list_of_lessons:
  7.         list_of_lessons.append(title)
  8.     return list_of_lessons
  9.  
  10.  
  11. def insert_lesson_title(list_of_lessons, title, idx):
  12.     if is_valid(list_of_lessons, idx):
  13.         if title not in list_of_lessons:
  14.             list_of_lessons.insert(idx, title)
  15.     return list_of_lessons
  16.  
  17.  
  18. def remove(list_of_lessons, title):
  19.     if title in list_of_lessons:
  20.         list_of_lessons.remove(title)
  21.         lesson_exercise = f'{title}-Exercise'
  22.         if lesson_exercise in list_of_lessons:
  23.             list_of_lessons.remove(title)
  24.     return list_of_lessons
  25.  
  26.  
  27. def swap(list_of_lessons, title1, title2):
  28.     if title1 in list_of_lessons and title2 in list_of_lessons:
  29.         first_lesson_index = list_of_lessons.index(title1)
  30.         second_lesson_index = list_of_lessons.index(title2)
  31.         # Swap the lessons
  32.         list_of_lessons[first_lesson_index], list_of_lessons[second_lesson_index] = \
  33.             list_of_lessons[second_lesson_index], list_of_lessons[first_lesson_index]
  34.         first_lesson_exercise = f'{title1}-Exercise'
  35.         second_lesson_exercise = f'{title2}-Exercise'
  36.  
  37.         if first_lesson_exercise in list_of_lessons:
  38.             list_of_lessons.remove(first_lesson_exercise)
  39.             if second_lesson_index + 1 == len(list_of_lessons):
  40.                 list_of_lessons.append(first_lesson_exercise)
  41.             else:
  42.                 list_of_lessons.insert(second_lesson_index + 1, first_lesson_exercise)
  43.  
  44.         if second_lesson_exercise in list_of_lessons:
  45.             list_of_lessons.remove(second_lesson_exercise)
  46.             if first_lesson_index + 1 == len(list_of_lessons):
  47.                 list_of_lessons.append(second_lesson_exercise)
  48.             else:
  49.                 list_of_lessons.insert(first_lesson_index + 1, second_lesson_exercise)
  50.  
  51.     return list_of_lessons
  52.  
  53.  
  54. def exercise(list_of_lessons, title):
  55.     if title not in list_of_lessons:
  56.         list_of_lessons.append(title)
  57.         lesson_exercise = f'{title}-Exercise'
  58.         lesson_title_index = list_of_lessons.index(title)
  59.  
  60.         if lesson_title_index + 1 < len(list_of_lessons):
  61.             list_of_lessons.insert(lesson_title_index + 1, lesson_exercise)
  62.         else:
  63.             list_of_lessons.append(lesson_exercise)
  64.            
  65.     else:
  66.         lesson_title_index = list_of_lessons.index(title)
  67.         lesson_exercise = f'{title}-Exercise'
  68.         if lesson_exercise not in list_of_lessons:
  69.             if lesson_title_index + 1 < len(list_of_lessons):
  70.                 list_of_lessons.insert(lesson_title_index + 1, lesson_exercise)
  71.             else:
  72.                 list_of_lessons.append(lesson_exercise)
  73.  
  74.     return list_of_lessons
  75.  
  76.  
  77. def course_planning(schedule):
  78.     while True:
  79.         command = input()
  80.         if command == "course start":
  81.             break
  82.  
  83.         info = command.split(":")
  84.         action = info[0]
  85.  
  86.         if action == "Add":
  87.             lesson_title_to_add = info[1]
  88.             schedule = add_lesson(schedule, lesson_title_to_add)
  89.  
  90.         elif action == "Insert":
  91.             lesson_title_to_insert, index_to_insert = info[1], int(info[2])
  92.             schedule = insert_lesson_title(schedule, lesson_title_to_insert, index_to_insert)
  93.  
  94.         elif action == "Remove":
  95.             lesson_title_to_remove = info[1]
  96.             schedule = remove(schedule, lesson_title_to_remove)
  97.  
  98.         elif action == "Swap":
  99.             lesson_title1, lesson_title2 = info[1], info[2]
  100.             schedule = swap(schedule, lesson_title1, lesson_title2)
  101.  
  102.         elif action == "Exercise":
  103.             lesson_title_exercise = info[1]
  104.             schedule = exercise(schedule, lesson_title_exercise)
  105.  
  106.     return schedule
  107.  
  108.  
  109. list_schedule = input().split(", ")
  110. course_planning(list_schedule)
  111. for index_, course in enumerate(list_schedule):
  112.     print(f"{index_ + 1}.{course}")
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement