Skip to content

Instantly share code, notes, and snippets.

@gozzoo
Last active February 23, 2024 15:10
Show Gist options
  • Save gozzoo/34b239f7c0257966a720e03c9217b860 to your computer and use it in GitHub Desktop.
Save gozzoo/34b239f7c0257966a720e03c9217b860 to your computer and use it in GitHub Desktop.

Задача 1. клониране на масив

да се разработи функция deepcopy която получава масив от елменти и връща друг масив който съдържа копия на елементите от оригиналния масив

Изисквания:

  • функцията да работи като copy.deepcopy()
  • елементите в масива може да бъдат от различен тип

Пример:

a = [ Employee("ivan", age=24), Manager("georgi"), [1, 2, 3], Intern("stoyan"), "hello"]

b = deepcopy(a)

Задача 2. circular queue

да се разработи клас circular_queue който представлява опашка която ползва вътрешно кръгов буфер. това е масив с фиксирана дължина. За достъп до елементите се ползват два индекса head и tail. Единия сочи към първия валиден елемент в масива, а другия към последния. Когато някой от индексите достигне последния елемент от масива на следваща стъпка се превърта и отива на първия.

Методи на класа:

  • circular_queue(n) - конструктор. n е капацитета на опашката, т.е. тя не може да съдържа повече от n елемента
  • add(e) - Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an NoSpaceError if no space is currently available.
  • element() - Retrieves, but does not remove, the head of this queue.
  • offer(e) Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. Returns true if the element was added to this queue, else false
  • peek() - Retrieves, but does not remove, the head of this queue, or returns None if this queue is empty.
  • poll() - Retrieves and removes the head of this queue, or returns None if this queue is empty.
  • remove() - Retrieves and removes the head of this queue.
  • size() - връща брой елементи в опашката
Action throws Error Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()

Пример 1:

queue = circular_queue(10)

queue.add("apple")
queue.add("banana")
queue.add("cherry")

# print the queue
print("Queue: ", queue)

# remove the element at the front of the queue
front = queue.remove()
print("Removed element: ", front)

# print the updated queue
print("Queue after removal: ", queue)

# add another element to the queue
queue.add("date")

# peek at the element at the front of the queue
peeked = queue.peek()
print("Peeked element: ", peeked)

# print the updated queue
print("Queue after peek: ", queue)

Резултат

Queue: [apple, banana, cherry]
Removed element: apple
Queue after removal: [banana, cherry]
Peeked element: banana
Queue after peek: [banana, cherry, date]

Пример 2:

queue = circular_queue(10)

a = [0, 1, 2, 3, 4]

for i in a:
    q.add(i)

# display contents of the queue.
print("Elements of queue ", q)

# remove the head of queue.
removedele = q.remove()
print("removed element-", removedele)

print(q)

# head of queue
head = q.peek()
print("head of queue-", head)

size = q.size()
print("Size of queue-", size)

Резултат

Elements of queue [0, 1, 2, 3, 4]
removed element-0
[1, 2, 3, 4]
head of queue-1
Size of queue-4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment