Skip to content

Apps

Introduction

each problem has a solution directly and other defines only the approach in function.


Book

An instance of book in a Library

Source code in algorithms/apps/library.py
class Book:
    """
    An instance of book in a Library
    """

    id_cnt = 1

    def __init__(self, title, author, content, genre):
        self.title = title
        self.content = content
        self.author = author
        self.genre = genre
        self.book_id = self.id_cnt
        Book.id_cnt +=1

    def display(self):
        """
        Displays information about the Book
        """
        print(f'ID: {self.book_id}')
        print(f'Title: {self.title}')
        print(f'Author: {self.author}')
        print(f'Genre: {self.genre}')

display()

Displays information about the Book

Source code in algorithms/apps/library.py
def display(self):
    """
    Displays information about the Book
    """
    print(f'ID: {self.book_id}')
    print(f'Title: {self.title}')
    print(f'Author: {self.author}')
    print(f'Genre: {self.genre}')

Library

A Library that stores the books, authors and genre

Source code in algorithms/apps/library.py
class Library:
    """
    A Library that stores the books, authors and genre
    """

    def __init__(self):
        self.books = {}
        self.recently_read = {}
        self.genre = {}
        self.author = {}

    def add_book(self, book):
        """
        Adds a Book to the Library
        :type book: Book
        """
        self.books[book.book_id] = book

        if book.genre not in self.genre:
            self.genre[book.genre] = []
        self.genre[book.genre].append(book)

        if book.autor not in self.author:
            self.author[book.author] = []
        self.author[book.autor].append(book)

        print(f'{book.title} has been added to Library successfully')

    def recently_read_book(self, book):
        """
        Marks a book as recently read
        :type book: Book
        """
        self.recently_read[book.book_id] = book
        print(f'{book.title} is your recently read book')

    def display_genre(self):
        """
        Display books grouped by Genre
        """
        for genre, books in self.genre.items():
            print(f'{genre} Genre:')
            for book in books:  
                book.display()

    def display_by_author(self):
        """
        Display books grouped by Authors
        """
        for author, books in self.author.items():
            print(f'{author} Author:')
            for book in books:
                book.display()

    def books_by_author(self, author):
        """
        Display books of specific author
        :type author: str
        """
        if author in self.author:
            print(f'books by {author}:')
            for book in self.author[author]:
                book.display()
        print(f'No books related to {author}')

add_book(book)

Adds a Book to the Library :type book: Book

Source code in algorithms/apps/library.py
def add_book(self, book):
    """
    Adds a Book to the Library
    :type book: Book
    """
    self.books[book.book_id] = book

    if book.genre not in self.genre:
        self.genre[book.genre] = []
    self.genre[book.genre].append(book)

    if book.autor not in self.author:
        self.author[book.author] = []
    self.author[book.autor].append(book)

    print(f'{book.title} has been added to Library successfully')

books_by_author(author)

Display books of specific author :type author: str

Source code in algorithms/apps/library.py
def books_by_author(self, author):
    """
    Display books of specific author
    :type author: str
    """
    if author in self.author:
        print(f'books by {author}:')
        for book in self.author[author]:
            book.display()
    print(f'No books related to {author}')

display_by_author()

Display books grouped by Authors

Source code in algorithms/apps/library.py
def display_by_author(self):
    """
    Display books grouped by Authors
    """
    for author, books in self.author.items():
        print(f'{author} Author:')
        for book in books:
            book.display()

display_genre()

Display books grouped by Genre

Source code in algorithms/apps/library.py
def display_genre(self):
    """
    Display books grouped by Genre
    """
    for genre, books in self.genre.items():
        print(f'{genre} Genre:')
        for book in books:  
            book.display()

recently_read_book(book)

Marks a book as recently read :type book: Book

Source code in algorithms/apps/library.py
def recently_read_book(self, book):
    """
    Marks a book as recently read
    :type book: Book
    """
    self.recently_read[book.book_id] = book
    print(f'{book.title} is your recently read book')

MyHashMap

Bases: object

Source code in algorithms/apps/hashmap.py
class MyHashMap(object):

    def __init__(self, capacity=16):
        self.capacity = capacity
        self.buckets = [None]*capacity

    def _hash(self,key):
        return hash(key)%self.capacity

    def put(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: None
        """
        index = self._hash(key)
        if self.buckets[index] is None:
            self.buckets[index] = []
        bucket = self.buckets[index]

        for i,(existing_key,_) in enumerate(bucket):
            if existing_key == key:
                bucket[i] = (key,value)
                return 
        bucket.append((key,value))

    def get(self, key):
        """
        :type key: int
        :rtype: int
        """
        index = self._hash(key)
        if self.buckets[index] is not None:
            for existing_key, value in self.buckets[index]:
                if existing_key == key:
                    return value
        return None

    def remove(self, key):
        """
        :type key: int
        :rtype: None
        """
        index = self._hash(key)

        if self.buckets[index] is not None:
            for i, (existing_key, _) in enumerate(self.buckets[index]):
                if existing_key == key:
                    del self.buckets[index][i]
                    return 

get(key)

:type key: int :rtype: int

Source code in algorithms/apps/hashmap.py
def get(self, key):
    """
    :type key: int
    :rtype: int
    """
    index = self._hash(key)
    if self.buckets[index] is not None:
        for existing_key, value in self.buckets[index]:
            if existing_key == key:
                return value
    return None

put(key, value)

:type key: int :type value: int :rtype: None

Source code in algorithms/apps/hashmap.py
def put(self, key, value):
    """
    :type key: int
    :type value: int
    :rtype: None
    """
    index = self._hash(key)
    if self.buckets[index] is None:
        self.buckets[index] = []
    bucket = self.buckets[index]

    for i,(existing_key,_) in enumerate(bucket):
        if existing_key == key:
            bucket[i] = (key,value)
            return 
    bucket.append((key,value))

remove(key)

:type key: int :rtype: None

Source code in algorithms/apps/hashmap.py
def remove(self, key):
    """
    :type key: int
    :rtype: None
    """
    index = self._hash(key)

    if self.buckets[index] is not None:
        for i, (existing_key, _) in enumerate(self.buckets[index]):
            if existing_key == key:
                del self.buckets[index][i]
                return 

RandomizedSet

Source code in algorithms/apps/getRandom.py
class RandomizedSet:
    def __init__(self):
        self.randomset = set()

    def insert(self,val):
        """
        :type val: int
        :rtype : bool
        """
        if val not in self.randomset:
            self.randomset.add(val)
            return True
        return False

    def remove(self,val):
        """
        :type val: int
        :rtype : bool
        """
        if val in self.randomset:
            self.randomset.remove(val)
            return True
        return False

    def get_random(self):
        """
        :rtype : int
        """
        if len(self.randomset) >=1:
            return random.choice(list(self.randomset))

get_random()

:rtype : int

Source code in algorithms/apps/getRandom.py
def get_random(self):
    """
    :rtype : int
    """
    if len(self.randomset) >=1:
        return random.choice(list(self.randomset))

insert(val)

:type val: int :rtype : bool

Source code in algorithms/apps/getRandom.py
def insert(self,val):
    """
    :type val: int
    :rtype : bool
    """
    if val not in self.randomset:
        self.randomset.add(val)
        return True
    return False

remove(val)

:type val: int :rtype : bool

Source code in algorithms/apps/getRandom.py
def remove(self,val):
    """
    :type val: int
    :rtype : bool
    """
    if val in self.randomset:
        self.randomset.remove(val)
        return True
    return False