Содержание
- 2. What We’ll Be Seeing Today Linked lists Debugger Ex 9 Student enrollment system
- 3. Linked Lists Separate the logical order of items from their physical order in memory Each item
- 4. Important! The two most common mistakes regarding lists are: Disconnecting without keeping a pointer to the
- 5. Class Node 5 class Node: def __init__(self, data=None, next=None): self.__data = data self.__next = next def
- 6. class Linked List 17 class LinkedList: def __init__(self, head=None): self.__head = head def get_head(self): return self.__head
- 7. class Linked List (cont) class LinkedList: def __len__(self): current = self.__head count = 0 while current
- 8. Reversing a list (O(n) complexity) class LinkedList: def rev_list1(self): current = self.__head self.__head = None while
- 9. Can we do any better? Improved implementation of linked lists Add: a length variable in init:
- 10. Doubly-linked list with a tail Sometimes it is convenient that we can add and remove items
- 11. A doubly-linked list with a tail class Node: def __init__(self, data, prev=None, next=None): self.data = data
- 12. A doubly-linked list with a tail def add_first(self, node): if self.__head is None: # list was
- 13. A doubly-linked list with a tail def add_last(self, node): if self.__tail is None: # list was
- 14. A doubly-linked list with a tail def remove_first(self): d = self.__head.data self.__head = self.__head.next if self.__head
- 15. A doubly-linked list with a tail def remove_last(self): d = self.__tail.data self.__tail = self.__tail.prev if self.__tail
- 16. Linked Lists vs. Python’s List
- 17. Debugging with debugger def mult_nums_in_list(numbers, mult): for num in numbers: num *= mult return numbers def
- 18. Debugging with debugger def mult_nums_in_list(numbers, mult): for num in numbers: #should be for num in range(len(numbers))
- 19. Add a break point
- 20. Run Debugger
- 21. List of Variables
- 22. Add variable to be watched
- 23. Step over/into/out Step over
- 24. Step over/into/out Step into
- 25. Step over/into/out Step out
- 26. Ex 9
- 27. Ex 9 – Playing With Objects You will only be given a Screen class that handles
- 28. Ex 9 – Game Loop We can think that everything happens in the same time! All
- 29. Question from the exam 2015: 29
- 30. Q from the exam 30
- 31. Q from the exam 31
- 32. Q from the exam 32
- 33. Q from the exam 33
- 34. Q from the exam 34
- 35. Q from the exam 35
- 36. Stacks and Queues A stack is a last in, first out (LIFO) data structure Items are
- 37. Queues API Queues (LIFO): What would be the API? what are the functions required in order
- 38. Implementing Queue class MyQueue: def __init__(self): self.__head = None self.__tail = None self.__size = 0 def
- 39. Adding new item to the queue def enqueue(self, item): if self.__tail == None: self.__head = Node(item)
- 40. Removing an item from the queue def dequeue(self, item): result = self.__head.data self.__head = self.__head.next if
- 41. Student Enrollment System We want to design a system where students could register to courses What
- 42. Thinking In Interfaces We need to think about the properties of our enrollment class: Available courses?
- 43. Application Programming Interface An “Application Programming Interface” (API) is a way for us to expose the
- 44. Student Enrollment System - API class EnrollmentSystem: __init__(self) add_student(self, student): add_course(self, course) get_available_courses(self) register_student(self, student, course_id)
- 45. Enrollment System Class class EnrollmentSystem: def __init__(self): self.__courses = {} self.__students = {} def add_student(self, student):
- 46. Enrollment System Class class EnrollmentSystem: def __init__(self): self.__courses = {} self.__students = {} … def get_available_courses(self):
- 47. Enrollment System Class What about registration? class EnrollmentSystem: def __init__(self): self.__courses = {} self.__students = {}
- 48. What did we assume so far? Course get_id() is_full() register(sid) Student get_id() add_course(cid) Do the EnrollmentSystem
- 49. Student Class class Student: def __init__(self, student_id): self.__enrolled_courses = set() self.__id = student_id def get_id(self): return
- 50. Course Class class Course: def __init__(self, course_id, course_capacity): self.__enrolled_students = set() self.__id = course_id self.__capacity =
- 51. Putting things together We want to create an actual program using our enrollment system A user
- 52. Our main loop from student import Student from course import Course from system import EnrollmentSystem def
- 53. What’s next? Well a lot is missing from the system Actual implementation of the methods ☺
- 55. Скачать презентацию