HackerRank Data Structure


Stack

class Stack:
    def __init__(self):
        self.data = []

    # for print
    def __iter__(self):
        return self

    def __str__(self):
        values = [x for x in self]
        return ' -> '.join(values)

    def push(self, data):
        self.data.append(data)

    def pop(self):
        if len(self.data) is not 0:
            return self.data.pop()
        else:
            print("stack is empty")

    def print_stack(self):
        print(self.data)

if __name__ == "__main__":

    myStack = Stack()
    myStack.push(1)
    myStack.push(2)
    myStack.push(3)
    myStack.push(4)
    myStack.print_stack()

    print(myStack.pop())
    print(myStack.pop())

    myStack.print_stack()

실행 결과

[1, 2, 3, 4]
4
3
[1, 2]

Queue

class Queue:

    def __init__(self):
        self.items = []

    def enqueue(self, item):
        self.items.insert(0, item)

    def dequeue(self):
        self.items.pop()

    def print_queue(self):
        print(self.items)

if __name__ == "__main__":
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(2)
    queue.enqueue(3)
    queue.enqueue(4)
    queue.print_queue()
    queue.dequeue()
    queue.print_queue()

실행결과

[4, 3, 2, 1]
[4, 3, 2]

LinkedList

class Node(object):
    def __init__(self, data=None, next_node=None, prev_node=None):
        self.data = data
        self.next = next_node
        self.prev = prev_node

    def __str__(self):
        return str(self.data)

class LinkedList:
    def __init__(self, data=None):
        self.head = None
        self.tail = None
        if data is not None:
            self.insert(data)

    # for print
    def __iter__(self):
        current = self.head
        while current:
            yield current
            current = current.next

    def __str__(self):
        values = [str(x) for x in self]
        return ' -> '.join(values)

    def __len__(self):
        result = 0
        node = self.head
        while node:
            result += 1
            node = node.next
        return result

    def insert(self, data):
        if self.head is None:
            self.tail = self.head = Node(data)
        else:
            self.tail.next = Node(data)
            self.tail = self.tail.next
        return self.tail

    def delete(self, data):
        if self.head is None:
            print("It is empty!")
            return
        elif self.head.data is data:
            self.head = self.head.next
        else:
            current = self.head
            while current.next is not None:
                if current.data is data:
                    print("Delete the number: %d"%(data))
                    current.data = current.next.data
                    current.next = current.next.next
                    return
                current = current.next
            print("It is not founded")


if __name__ == "__main__":
    ll = LinkedList()
    ll.insert(1)
    ll.insert(2)
    ll.insert(3)
    ll.insert(4)
    ll.insert(5)
    print(ll)
    ll.delete(3)
    ll.delete(6)
    print(ll)

실행 결과

1 -> 2 -> 3 -> 4 -> 5
Delete the number: 3
It is not founded
1 -> 2 -> 4 -> 5


'Computer Science > Coding Interview' 카테고리의 다른 글

HackerRank Queues: A Tale of Two Stacks  (0) 2017.07.30
Stacks: Balanced Brackets  (0) 2017.07.30
HackerRank MergeSort Counting Inversions  (0) 2017.07.29
Binary Search  (0) 2017.07.28
HackerRank: Basic Problems  (0) 2017.07.25

+ Recent posts