<jupyter_start><jupyter_text><jupyter_code>class AS2_Main:

    def __init__(self):
        self.product = MyList()

    def menu(self):
        print("\n")
        print("+------------------------Menu-----------------------+")
        print("|        1. Load data from file and display         |")
        print("|        2. Input & add to the end.                 |")
        print("|        3. Display data                            |")
        print("|        4. Save product list to file.              |")
        print("|        5. Search by ID                            |")
        print("|        6. Delete by ID                            |")
        print("|        7. Sort by ID.                             |")
        print("|        8. Convert to Binary                       |")
        print("|        9. Load to stack and display               |")
        print("|        10. Load to queue and display.             |")
        print("|        0.Exit                                     |")
        print("+---------------------------------------------------+")
        print("\n")

    def choose(self):
        option = int(input("Enter key for select option: "))
        while option != 0:
            if option == 1:
                data = self.product.load_data()
                data.PrintList()
            elif option == 2:
                n = input("Please enter the product (id-name-quantity-price) ").strip().split()
                id, name = str(n[0]), str(n[1])
                quantity = int(n[2])
                price = float(n[3])
                data.push_back([id, name, quantity, price])
            elif option == 3:
                data.PrintList()
            elif option == 4:
                data.write_data()
            elif option == 5:
                data.search_by_id()
            elif option == 6:
                data.del_by_id()
            elif option == 7:
                data.sort_by_id().PrintList()
            elif option == 8:
                n = data.head.data[2]
                print(OperationToProduct().to_binary(n))
            elif option == 9:
                a = self.product.to_linkedlist().to_stack()
                [print(x, "\n") for x in a]
            elif option == 10:
                b = self.product.to_linkedlist().to_queue()
                [print(x, "\n") for x in b]            
            else:
                print("Invalid option!")
            option = int(input("Enter key for select option: "))
        print("Thanks!!!")

a = AS2_Main()
a.menu()
a.choose()
class OperationToProduct:
    def push_back(self, newElement):
        newNode = Node(newElement)
        if(self.head == None):
            self.head = newNode
        else:
            temp = self.head
            while(temp.next != None):
                temp = temp.next
            temp.next = newNode

    def PrintList(self):
        temp = self.head
        if temp != None:
            print("The list contains:", end="\n")
            while temp != None:
                print(temp.data, end="\n")
                temp = temp.next
        else:
            print("The list is empty.")

    def load_data(self):
        file = str(input("Please enter the file path: "))
        linkedlist = MyList()
        with open(file) as doc:
            for line in doc:
                lines = line.strip().split(';')
                id, name = lines[0], lines[1]
                price = float(lines[2])
                quantity = int(lines[3])
                linkedlist.push_back([id, name, quantity, price])
        return linkedlist

    def write_data(self):
        file_output = input("Please enter the output path:")
        f = open(file_output,"w")
        temp = self.head
        if temp != None:
            while (temp != None):
                f.write(';'.join([str(_) for _ in temp.data]) + "\n")
                temp = temp.next
        else:
            f.write("The list is empty.")

    def search_by_id(self):
        id = input("Please enter the ID: ")
        temp = self.head
        found = 0
        if temp != None:
            while temp != None:
                if temp.data[0] == id:
                    found += 1
                    break
                temp = temp.next
            if found == 1:
                print(temp.data)
            else:
                print(-1)
        else:
            print("The list is empty.")

    def del_by_id(self):
        id = input("Please enter the ID: ")
        while self.head != None and self.head.data[0] == id:
            self.head = self.head.next
        temp = self.head
        if temp != None:
            while temp.next != None:
                if temp.next.data[0] == id:
                    temp.next = temp.next.next
                else:
                    temp = temp.next

    def sort_by_id(self):
        sorted_list = MyList()
        values = []
        temp = self.head
        while temp != None:
            values.append(temp.data)
            temp = temp.next
        k = sorted(values, key = lambda values: values[0])
        for x in k:
            sorted_list.push_back(x)
        return sorted_list

    def to_binary(self, n):
        if n == 0: 
            return ''
        else:
            return to_binary(n//2) + str(n % 2)

    def to_linkedlist(self):
        linkedlist = MyList()
        with open("product.txt") as doc:
            for line in doc:
                lines = line.strip().split(';')
                id, name = lines[0], lines[1]
                price = float(lines[3])
                quantity = int(lines[2])
                linkedlist.push_back([id, name, quantity, price])
        return linkedlist

    def to_stack(self):
        temp = self.head
        while temp != None:
            self.stack.push(temp.data)
            temp = temp.next
        return self.stack.items

    def to_queue(self):
        temp = self.head
        while temp != None:
            self.queue.push(temp.data)
            temp = temp.next
        return self.queue.items

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

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

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

    def size(self):
        return len(self.items)

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

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

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

    def size(self):
        return len(self.items)

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

class MyList(OperationToProduct):
    def __init__(self):
        self.head = None
        self.stack = MyStack()
        self.queue = MyQueue()

def to_binary(n):
    if n == 0: 
        return ''
    else:
        return to_binary(n//2) + str(n % 2)

to_binary(139)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13202689171792303, 'fraction_numerical': 0.008153340008582463, 'mean_word_length': 1.2352941176470589, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '200273', 'license': 'Unlicense', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1905, 'n_words': 510, 'path': 'CSD203x_ASM2_thucvhfx10009_funix_edu_vn.ipynb', 'repo_name': 'hocthucv/bt', 'size': '6991'}