Untitled

 avatar
shinta0x01
plain_text
17 days ago
28 kB
4
Indexable
Never
class Node:
    def __init__(self, order_id, item, due_time):
        self.order_id = order_id
        self.item = item
        self.due_time = due_time
        self.next = None

class Delivery:
    def __init__(self):
        self.front = self.rear = None

    def place_order(self, order_id, item, due_time):
        node = Node(order_id, item, due_time)
        if self.front is None:
            self.front = self.rear = node
        else:
            self.rear.next = node
            self.rear = node

    def place_order_front(self, order_id, item, due_time):
        node = Node(order_id, item, due_time)
        if self.front is None:
            self.front = self.rear = node
        else:
            node.next = self.front
            self.front = node

    def delete(self, order_id):
        current = self.front
        if current.order_id == order_id:
            self.front = self.front.next
            return
        while current.next:
            if current.next.order_id == order_id:
                if current.next is None:
                    current.next = None
                    return
                current.next = current.next.next
            current = current.next


    def dispatch_front(self):
        if self.front is None:
            print('no order yet')
        else:
            current = self.front
            while current:
                current.due_time -= 1
                current = current.next
            order_id = self.front.order_id
            item = self.front.item
            due_time = self.front.due_time
            self.front = self.front.next
            print(f'{order_id} {item} {due_time} is dispatched!')

    def dispatch_rear(self):
        if self.front is None:
            print('no order yet!')
        else:
            current = self.front
            while current:
                prev = current
                current.due_time -= 1
                current = current.next
            order_id = self.rear.order_id
            item = self.rear.item
            due_time = self.rear.due_time
            prev.next = None
            print(f'{order_id} {item} {due_time} dispatched!')

    def late_order(self):
        current = self.front
        while current:
            if current.due_time < 0:
                order_id = current.order_id
                item = current.item
                due_time = current.due_time
                self.delete(order_id)
                self.place_order_front(order_id, item, due_time)
                print(f'{order_id} {item} {due_time} ')
            current = current.next

    def process_order(self):
        if self.front is None:
            print('no order yet')
        else:
            self.dispatch_front()
            self.dispatch_rear()

    def deliver_time(self, order_id):
        time = 1
        current = self.front
        if self.front is None:
            print('no order yet')
        else:
            while current:
                if current.order_id == order_id:
                    print(f'deliver order and time : {current.order_id} {time}')
                else:
                    time+=1
                current = current.next
            print(f'{order_id} not in the queue')

    def display(self):
        current = self.front
        while current:
            print(current.order_id, current.item, current.due_time, ' ', end='\n')
            current = current.next

deliver = Delivery()
print('Orders:')
deliver.place_order(1,'item1',3)
deliver.place_order(2,'item2',2)
deliver.place_order(4,'item2',1)
deliver.place_order(5,'item2',0)
deliver.place_order(3,'item3',7)
deliver.display()

print('\nOrder dispatched front:')
deliver.dispatch_front()
print('\nOrder dispatched rear')
deliver.dispatch_rear()

print('Late orders')
deliver.late_order()
print()
deliver.display()
print()
print('Processed order from front and rear:')
deliver.process_order()
deliver.display()
print()
deliver.deliver_time(3)

############## Priority

class Node:
    def __init__(self, data, priority=None):
        self.data = data
        self.priority = priority
        self.next = None

class Prio:
    def __init__(self):
        self.front = self.rear = None

    def enqueue(self, data, priority=None):
        node = Node(data, priority)
        if self.front is None:
            self.front = self.rear = node
        else:
            if self.front.priority is None or self.rear.priority and priority < self.front.priority:
                node.next = self.front
                self.front = node

            elif priority is None or self.rear.priority is not None and priority > self.rear.priority:
                self.rear.next = node
                self.rear = node
            else:
                current = self.front
                while current.next:
                    if current.next.priority is None or current.next.priority > priority:
                        node.next = current.next
                        current.next = node
                        break
                    current = current.next

    def display(self):
        current = self.front
        while current:
            print(current.data, current.priority, ' ', end='')
            current = current.next

p = Prio()
p.enqueue(12,2)
p.enqueue(13,3)
p.enqueue(132,1)
p.enqueue(1324,4)
p.enqueue(13214,0)
p.display()

######## BackTrack

class Node:
    def __init__(self,data):
        self.data = data
        self.next = None

class Backtrack:
    def __init__(self):
        self.top = None

    def push(self, data):
        node = Node(data)
        if self.top is None:
            self.top = node
        else:
            node.next = self.top
            self.top = node

    def pop(self):
        if self.top is None:
            return None
        else:
            popped_data = self.top.data
            self.top = self.top.next
            return popped_data

    def bt(self, data):
        if data >= 0:
            self.push(data)
        else:
            for i in range(5):
                print(self.pop(), end=' > ')

b = Backtrack()
b.bt(1)
b.bt(2)
b.bt(3)
b.bt(4)
b.bt(5)
b.bt(-1)
b.bt(1)
b.bt(2)
b.bt(3)
b.bt(4)
b.bt(5)
b.bt(6)
b.bt(7)
b.bt(8)
b.bt(9)
b.bt(10)
b.bt(-2)
b.bt(11)
b.bt(12)
b.bt(-3)
b.bt(1)
b.bt(2)
b.bt(3)
b.bt(4)
b.bt(5)

####### Final exam


class Node:
    def __init__(self, title, comp_name, job_desc, qualification):
        self.title = title
        self.comp_name = comp_name
        self.job_desc = job_desc
        self.qualification = qualification
        self.next = None

class Node2:
    def __init__(self, job_title, name, age, degree, address):
        self.job_title = job_title
        self.name = name
        self.age = age
        self.degree = degree
        self.address = address
        self.next = None

class Job: #stack for job post
    def __init__(self):
        self.top = None

    def post_job(self, title, comp_name, job_desc, qualification):
        node = Node(title, comp_name, job_desc, qualification)
        node.next = self.top #add new application to top
        self.top = node

    def view_latest_job_posting(self):
        latest_job = self.top.title #get the latest job in the top
        print(latest_job)

    def close_job_opening(self):
        close_job = self.top.title
        self.top = self.top.next # rmove job from the top
        return close_job

    def display(self): #display all the job posting
        current = self.top
        while current:
            print(current.title, current.comp_name, current.job_desc, current.qualification, '\n', end='')
            current = current.next


class Stack2: #stack for application
    def __init__(self):
        self.top = None

    def apply_for_job(self, job_title, name, age, degree, address):
        node = Node2(job_title, name, age, degree, address)
        node.next = self.top #adding new application and assign to top
        self.top = node

    def process_job_application(self):
        popped = self.top.name
        self.top = self.top.next
        return popped

    def view_latest_job_application(self): #get the application in the top
        job_title = self.top.job_title #assign job title to job_title
        name = self.top.name

        print(job_title, name) #display job title and application name

    def check_job_application_status(self, name):
        current = self.top
        while current:
            #check the application if qualified
            if current.name == name and current.degree == 'College graduate':
                print(f'{current.name}\'s Application', 'Accepted') #execute if qualified
                return
            else:
                print('Not accepted') #else execute this
                break

    def display(self): #display all applications
        current = self.top
        while current:
            print(current.job_title, current.name, current.age, current.degree, current.address, '\n', end='')
            current = current.next


posting = Job()
posting.post_job('Cyber Security', 'Evil Corp', 'penetration testing', 'college graduate')
posting.post_job('Cyber Security 2', 'Evil Corp 2', 'penetration testing', 'college graduate')
posting.post_job('Cyber Security 3', 'Evil Corp 3', 'penetration testing', 'college graduate')
print('All Jobs')
posting.display()
print('\nLatest job posting')
posting.view_latest_job_posting()
print()

application = Stack2()
print('All Applications')
application.apply_for_job('Cyber Security', 'Kenneth', 21, 'College graduate', 'Lilo-an')
application.apply_for_job('Cyber Security 2', 'James', 41, 'College graduate', 'Lilo-an')
application.apply_for_job('Cyber Security 3', 'James', 32, 'College graduate', 'Lilo-an')
application.process_job_application()
application.display()
print('\nLatest job application')
application.view_latest_job_application()
print('\nApplication status')
application.check_job_application_status('James')

####### Stack and Queue Coquilla


# class to store the name, sched, details, and the reminders
class Node:
    def __init__(self, name, schedule=None, details=None, reminders=None):
        self.name = name
        self.schedule = schedule
        self.details = details
        self.reminders = reminders
        self.next = None

# class for queue
class Queue:
    def __init__(self):
        self.front = self.rear = None

    # method to enqueue a candidate
    def enqueue(self, name, schedule=None, details=None, reminders=None):
        node = Node(name, schedule, details, reminders)

        if self.front is None:
            self.front = self.rear = node
            return
        else:
            self.rear.next = node
            self.rear = self.rear.next

    # method to dequeue a candidate
    def dequeue(self):
        if self.front is None:
            return None
        else:
            dequeue_data = self.front.name
            self.front = self.front.next
            return dequeue_data

    # method to display the name of a candidate
    def display(self):
        current = self.front

        while current:
            print(current.name, end=" - ")
            current = current.next
        print()

    # method to display the detailed information of a candidate
    def detailed_display(self):
        current = self.front

        while current:
            print(f'Name: {current.name}\tSchedule: {current.schedule}\tDetails: {current.details}\tReminders: {current.reminders}')
            current = current.next


# class for stack
class Stack:
    def __init__(self):
        self.top = None

    # method to push a candidate
    def push(self, name, schedule=None, details=None, reminders=None):
        node = Node(name, schedule, details, reminders)

        if self.top is None:
            self.top = node
            return
        else:
            node.next = self.top
            self.top = node

    # method to pop a candidate
    def pop(self):
        if self.top is None:
            return None
        else:
            pop_data = self.top.name
            self.top = self.top.next
            return pop_data

    # method to display the name of a candidate
    def display(self):
        current = self.top

        while current:
            print(current.name, end=" - ")
            current = current.next
        print()

    # method to display the detailed information a candidate
    def detailed_display(self):
        current = self.top

        while current:
            print(f'Name: {current.name}\tSchedule: {current.schedule}\tDetails: {current.details}\tReminders: {current.reminders}')
            current = current.next


# class for the barista. this class is where the processes will take place
class Barista:
    # method to transfer the promising candidates from queue to stacks
    def review_stack(self, inbox, review, promising_candidate):
        current = inbox.front

        while current.next:
            if promising_candidate in current.next.name:
                review.push(current.next.name)
                current.next = current.next.next
                continue
            current = current.next

    # method to move the chosen candidates from stacks to queue
    def interview_queue(self, review, interview):
        current = review.top

        while current:
            interview.enqueue(review.pop())
            current = current.next

    # method to add the schedule, details, and reminders to the interview candidates
    def schedule_interviews(self, interview, sched_interview, schedule, details, reminders):
        current = interview.front

        while current:
            sched_interview.push(current.name, schedule, details, reminders)
            interview.dequeue()
            current = current.next

    # method to select a candidate then it through the stacks
    def hiring_decision(self, sched_interview, hiring, name):
        current = sched_interview.top

        while current:
            if current.name == name:
                hiring.enqueue(current.name, current.schedule, current.details, current.reminders)
                sched_interview.pop()
                break
            sched_interview.pop()
            current = current.next



bar = Barista()
inbox = Queue()
review = Stack()
interview = Queue()
sched_interview = Stack()
hiring = Queue()

print()
print('Inbox: ')
inbox.enqueue('aivy')
inbox.enqueue('jomary')
inbox.enqueue('kay shamir')
inbox.enqueue('denise')
inbox.enqueue('kenny')
inbox.enqueue('shamenamena')
inbox.display()

print()
print('Review:')
bar.review_stack(inbox, review, 'sham')
review.display()

print()
print('Interview:')
bar.interview_queue(review, interview)
interview.display()

print()
print('Schedule:')
bar.schedule_interviews(interview, sched_interview, '10am', 'Cebu City', 'Bring Valid ID')
sched_interview.detailed_display()

print()
print('Hire:')
bar.hiring_decision(sched_interview, hiring, 'kay shamir')
hiring.display()



####### concatenate, palindrome

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


class Stack:
    def __init__(self):
        self.top = None

    def push(self, data):
        node = Node(data)
        node.next = self.top
        self.top = node

    def pop(self):
        popped_data = self.top.data
        self.top = self.top.next
        return popped_data

    def peek(self):
        if self.top is None:
            return None
        else:
            return self.top.data

    def is_palindrome(self, str1):
        for i in str1:
            self.push(i)

        for i in str1:
            if i == self.peek():
                self.pop()
            else:
                self.pop()
                return False
        return True

    def backtrack(self):
        current = self.top
        while current:
            if current.data < 0:
                for i in range(5):
                    self.pop()
            current = current.next

    def concatenate(self, other_stack):
        if other_stack.top is None:
            return

        # Traverse to the end of the current stack
        current = self.top
        while current.next:
            current = current.next

        # Concatenate the other stack to the end of the current stack
        current.next = other_stack.top

        # Clear the other stack after concatenation
        other_stack.top = None

    def display(self):
        current = self.top
        while current:
            print(current.data, '', end='')
            current = current.next

    def min(self):
        current = self.top
        minimum = self.top.data
        while current:
            if minimum < current.data:
                minimum = minimum
            else:
                minimum = current.data
            current = current.next

        return minimum

    def max(self):
        current = self.top
        maximum = self.top.data
        while current:
            if maximum > current.data:
                maximum = maximum
            else:
                maximum = current.data
            current = current.next
        return maximum

    def is_empty(self):
        return self.top is None

    def size(self):
        count = 0
        current = self.top
        while current:
            count += 1
            current = current.next
        return count

    def clear(self):
        self.top = None

    def search(self, data):
        current = self.top
        position = 0
        while current:
            if current.data == data:
                return position
            position += 1
            current = current.next
        return None

    def reverse(self):
        temp_stack = Stack()
        while self.top:
            temp_stack.push(self.pop())
        self.top = temp_stack.top

    def copy(self):
        copied_stack = Stack()
        current = self.top

        while current is not None:
            copied_stack.push(current.data)
            current = current.next

        return copied_stack


stack = Stack()
stack1 = Stack()
stack.push(0)
stack.push(2)
stack.push(4)
stack.push(8)
stack.display()
print()
print('Minimum', stack.min())
print('Maximum', stack.max())


######## Is Balance

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


class Stack:
    def __init__(self):
        self.top = None

    def push(self, data):
        node = Node(data)
        node.next = self.top
        self.top = node

    def pop(self):
        if self.top is None:
            return None
        popped_data = self.top.data
        self.top = self.top.next
        return popped_data

    def peek(self):
        if self.top is None:
            return None
        else:
            return self.top.data


def is_balanced_parentheses(s):
    stack = Stack()

    # Iterate through each character in the string
    for char in s:
        if char in "({[":
            stack.push(char)
        elif char in ")}]":
            # Check if the stack is empty (unmatched closing parenthesis)
            if stack.peek() is None:
                return False
            # Check if the current closing parenthesis matches the top of the stack
            if (char == ")" and stack.peek() == "(") or \
               (char == "}" and stack.peek() == "{") or \
               (char == "]" and stack.peek() == "["):
                stack.pop()
            else:
                return False

    # Check if there are unmatched opening parentheses
    return stack.peek() is None


# Test cases
print(is_balanced_parentheses("()"))        # True
print(is_balanced_parentheses("()[]{}"))    # True
print(is_balanced_parentheses("(]"))        # False
print(is_balanced_parentheses("([)]"))      # False
print(is_balanced_parentheses("{[]}"))      # True





##### infix to postfix






class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class Stack:
    def __init__(self):
        self.top = None

    def push(self, data):
        node = Node(data)
        node.next = self.top
        self.top = node

    def pop(self):
        popped = self.top.data
        self.top = self.top.next
        return popped

    def peek(self):
        if self.top is None:
            return None
        else:
            return self.top.data

    def display(self):
        current = self.top
        while current:
            print(current.data, '', end='')
            current = current.next

    def is_balance(self, str1):
        open_brackets = '[{('
        close_brackets = ']})'

        for i in str1:
            if i in open_brackets:
                self.push(i)
            elif i in close_brackets:
                if i == ')' and self.peek() == '(' or i == '}' and self.peek() == '{' or i == ']' and self.peek() == '[':
                    self.pop()

                else:
                    return False
        if self.peek() is None:
            return True
        else:
            return False

    def remove_wspace(self, str1):
        new = ''
        for i in str1:
            if i != ' ':
                new += i
        return new

    def infix_postfix(self, str1):
        if self.is_balance(str1):
            nospace = self.remove_wspace(str1)
            operator = '+-*/()^'
            expression = ''
            for i in nospace:
                if i in operator:
                    if (i == '-' or i == '+') and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'):
                        while self.peek() is not None and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'):
                            temp = self.pop()
                            expression += temp
                            if (i == '-' or i == '+') and (self.peek() == '-' or self.peek() == '+'):
                                while self.peek() is not None and (self.peek() == '-' or self.peek() == '+'):
                                    temp = self.pop()
                                    expression += temp
                        self.push(i)
                    elif i == ')':
                        while self.peek() is not None and self.peek() != '(':
                            temp = self.pop()
                            expression += temp
                        self.pop()  # Pop the open parenthesis
                    elif (i == '-' or i == '+') and (self.peek() == '-' or self.peek() == '+'):
                        while self.peek() is not None and (self.peek() == '-' or self.peek() == '+'):
                            temp = self.pop()
                            expression += temp
                        self.push(i)
                    elif (i == '*' or i == '/') and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'):
                        while self.peek() is not None and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'):
                            temp = self.pop()
                            expression += temp
                        self.push(i)
                    elif i == '^' and self.peek() == '^':
                        self.push(i)
                    else:
                        self.push(i)
                else:
                    expression += i

            while self.peek() is not None:
                temp = self.pop()
                expression += temp

            return expression
        else:
            print('Not balance')
            return False


stack = Stack()
print(stack.infix_postfix('((f*g-h)+(i*(j/k-l))+(m*n/o)'))





###### infix to postfix





class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
class Stack:
    def __init__(self):
        self.top = None

    def push(self, data):
        node = Node(data)
        node.next = self.top
        self.top = node

    def pop(self):
        popped = self.top.data
        self.top = self.top.next
        return popped

    def peek(self):
        if self.top is None:
            return None
        else:
            return self.top.data

    def display(self):
        current = self.top
        while current:
            print(current.data, '', end='')
            current = current.next

    def is_balance(self, str1):
        open_brackets = '[{('
        close_brackets = ']})'

        for i in str1:
            if i in open_brackets:
                self.push(i)
            elif i in close_brackets:
                if i == ')' and self.peek() == '(' or i == '}' and self.peek() == '{' or i == ']' and self.peek() == '[':
                    self.pop()

                else:
                    return False
        if self.peek() is None:
            return True
        else:
            return False

    def reverse(self, str1):
        new = ''
        prev = ''
        for i in str1:
            if prev == ')' and i == '(':
                new = i + '*' +new
            else:
                new = i + new

            prev = i
        return new

    def remove_space(self, str1):
        new = ''
        for i in str1:
            if i != ' ':
                new += i
        return new

    def infix_prefix(self, str1):
        if self.is_balance(str1):
            final = self.remove_space(str1)
            reverse = self.reverse(final)
            operator = '+-*/()^[]{}'
            expression = ''
            for i in reverse:
                if i in operator:
                    if (i == '-' or i == '+') and (self.peek() == '*' or self.peek() == '/' or self.peek() == '^'):
                        while self.peek() == '*' or self.peek() == '/' or self.peek() == '^':
                            temp = self.pop()
                            expression += temp
                        self.push(i)
                    elif (i == '*' or i == '/' or i == '^') and (self.peek() == '^'):
                        temp = self.pop()
                        expression += temp
                        self.push(i)

                    elif i == '(':
                        while self.peek() != ')':
                            temp = self.pop()
                            expression += temp
                        self.pop()
                    elif i == '[':
                        while self.peek() != ']':
                            temp = self.pop()
                            expression += temp
                        self.pop()
                    elif i == '{':
                        while self.peek() != '}':
                            temp = self.pop()
                            expression += temp
                        self.pop()
                    else:
                        self.push(i)

                else:
                    expression += i

            while self.peek() is not None:
                temp = self.pop()
                expression += temp

            return self.reverse(expression)
        else:
            print('Not balance')
            return False


stack = Stack()
print('Infix to Prefix\n', stack.infix_prefix('((a+b)*c)-(d*e/(f+g))-(h*i)'))
stack.display()
Leave a Comment