This is a self-made exercise and may be incorrect. You are welcome to discuss and discuss various optimization and reconstruction solutions.
Based on feedback or code review, the updated answers or related content of this article will be added to the comments of this blog.
We will try to ensure that the answer code for each question is complete, not just functions or classes. Open the Python 2.7 IDLE and copy the complete code to debug and run it.
Welcome to Balian's blog home. Http://www.cnblogs.com/balian
13-10.
Stack and queue. Write a class that defines a data structure that can have both stack () and queue () operations. This class is similar to an array in Perl. Four methods are required:
Shift () returns and deletes the first element in the list, similar to the previous dequeue () function.
Unshift () is a new element in the header of the list.
Add a new element to the end of the list, similar to the preceding enqueue () and push () methods.
Pop () returns and deletes the last element in the list, which is exactly the same as the pop () method.
For more information, see exercise 13-8 and exercise 13-9.
[Answer]
The Code is as follows:
#-*-Encoding: UTF-8-*-class ArrayPattern (object): 'defines the array model class 'def _ init _ (self, arrayList): self. arrayList = arrayList def shift (self): headItem = self. arrayList [0] print 'item', headItem, 'is deleted from the head of array. 'self. arrayList = self. arrayList [1:] print 'the updated array is: ', self. arrayList, '\ n' def unshift (self, headItem): tempList = [headItem] for item in self. arrayList: tempList. append (item) self. arrayList = tempList print 'item', headItem, 'is added on the head of array 'print' The updated array is: ', self. arrayList, '\ n' def push (self, endItem): self. arrayList. append (endItem) print 'item', endItem, 'is added on the end of array. 'print 'the updated array is: ', self. arrayList, '\ n' def pop (self): endItem = self. arrayList. pop () print 'item', endItem, 'is poped. 'print 'the updated array is: ', self. arrayList, '\ n' a_array = ArrayPattern ([1, 2, 3, 4, 5, 6, 7, 8]) a_array.shift () a_array.unshift (9) a_array.push (10) a_array.pop ()
[Execution result]
Item 1 is deleted from the head of array.
The updated array is: [2, 3, 4, 5, 6, 7, 8]
Item 9 is added on the head of array
The updated array is: [9, 2, 3, 4, 5, 6, 7, 8]
Item 10 is added on the end of array.
The updated array is: [9, 2, 3, 4, 5, 6, 7, 8, 10]
Item 10 is poped.
The updated array is: [9, 2, 3, 4, 5, 6, 7, 8]
13-11.
E-commerce.
You need to compile a basic e-commerce engine for a B2C (enterprise-to-consumer) retailer. You need to write a class User for the customer, a class Item corresponding to the inventory list, and a class corresponding to the shopping Cart. The goods are placed in the shopping cart. The customer can have multiple shopping cart. At the same time, there can be multiple goods in the shopping cart, including multiple identical goods.
[Unfinished]
I feel difficult. I am temporarily suspended.
13-12.
Chat room.
Because the blog only discusses technology, the question is slightly modified. For the original question, see page 408th. Create a new chat room. You need three types: a Message class, which contains a Message string and other information such as broadcast and unilateral recipients; a User class, contains all the information of a person entering your chat room. There is also a Room class, which shows a more complex chat system. You can create a separate "Room" during the chat and invite others to join. Additional question: Develop a graphical user interface application for the user.
[Unfinished]
I feel difficult. I am temporarily suspended.