Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@

## Data Compression
* [Burrows Wheeler](data_compression/burrows_wheeler.py)
* [Coordinate Compression](data_compression/coordinate_compression.py)
* [Huffman](data_compression/huffman.py)
* [Lempel Ziv](data_compression/lempel_ziv.py)
* [Lempel Ziv Decompress](data_compression/lempel_ziv_decompress.py)
Expand Down
12 changes: 11 additions & 1 deletion backtracking/combination_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ def combination_sum(candidates: list, target: int) -> list:
>>> combination_sum([-8, 2.3, 0], 1)
Traceback (most recent call last):
...
RecursionError: maximum recursion depth exceeded
ValueError: All elements in candidates must be non-negative
>>> combination_sum([], 1)
Traceback (most recent call last):
...
ValueError: Candidates list should not be empty
"""
if not candidates:
raise ValueError("Candidates list should not be empty")

if any(x < 0 for x in candidates):
raise ValueError("All elements in candidates must be non-negative")

path = [] # type: list[int]
answer = [] # type: list[int]
backtrack(candidates, path, answer, target, 0)
Expand Down
24 changes: 12 additions & 12 deletions blockchain/README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
# Blockchain

A Blockchain is a type of **distributed ledger** technology (DLT) that consists of growing list of records, called **blocks**, that are securely linked together using **cryptography**.
A Blockchain is a type of **distributed ledger** technology (DLT) that consists of a growing list of records, called **blocks**, that are securely linked together using **cryptography**.

Let's breakdown the terminologies in the above definition. We find below terminologies,
Let's break down the terminologies in the above definition. We find below terminologies,

- Digital Ledger Technology (DLT)
- Blocks
- Cryptography

## Digital Ledger Technology

It is otherwise called as distributed ledger technology. It is simply the opposite of centralized database. Firstly, what is a **ledger**? A ledger is a book or collection of accounts that records account transactions.
Blockchain is also called distributed ledger technology. It is simply the opposite of a centralized database. Firstly, what is a **ledger**? A ledger is a book or collection of accounts that records account transactions.

*Why is Blockchain addressed as digital ledger if it can record more than account transactions? What other transaction details and information can it hold?*
*Why is Blockchain addressed as a digital ledger if it can record more than account transactions? What other transaction details and information can it hold?*

Digital Ledger Technology is just a ledger which is shared among multiple nodes. This way there exist no need for central authority to hold the info. Okay, how is it differentiated from central database and what are their benefits?
Digital Ledger Technology is just a ledger that is shared among multiple nodes. This way there exists no need for a central authority to hold the info. Okay, how is it differentiated from a central database and what are their benefits?

There is an organization which has 4 branches whose data are stored in a centralized database. So even if one branch needs any data from ledger they need an approval from database in charge. And if one hacks the central database he gets to tamper and control all the data.
Suppose that there is an organization that has 4 branches whose data are stored in a centralized database. So even if one branch needs any data from the ledger it needs approval from the database in charge. And if one hacks the central database he gets to tamper and control all the data.

Now lets assume every branch has a copy of the ledger and then once anything is added to the ledger by anyone branch it is gonna automatically reflect in all other ledgers available in other branch. This is done using Peer-to-peer network.
Now let's assume every branch has a copy of the ledger and then once anything is added to the ledger by any branch it is gonna automatically reflect in all other ledgers available in other branches. This is done using a peer-to-peer network.

So this means even if information is tampered in one branch we can find out. If one branch is hacked we can be alerted ,so we can safeguard other branches. Now, assume these branches as computers or nodes and the ledger is a transaction record or digital receipt. If one ledger is hacked in a node we can detect since there will be a mismatch in comparison with other node information. So this is the concept of Digital Ledger Technology.
This means that even if information is tampered with in one branch we can find out. If one branch is hacked we can be alerted, so we can safeguard other branches. Now, assume these branches as computers or nodes and the ledger is a transaction record or digital receipt. If one ledger is hacked in a node we can detect since there will be a mismatch in comparison with other node information. So this is the concept of Digital Ledger Technology.

*Is it required for all nodes to have access to all information in other nodes? Wouldn't this require enormous storage space in each node?*

## Blocks

In short a block is nothing but collections of records with a labelled header. These are connected cryptographically. Once a new block is added to a chain, the previous block is connected, more precisely said as locked and hence, will remain unaltered. We can understand this concept once we get a clear understanding of working mechanism of blockchain.
In short, a block is nothing but a collection of records with a labelled header. These are connected cryptographically. Once a new block is added to a chain, the previous block is connected, more precisely said as locked, and hence will remain unaltered. We can understand this concept once we get a clear understanding of the working mechanism of blockchain.

## Cryptography

It is the practice and study of secure communication techniques in the midst of adversarial behavior. More broadly, cryptography is the creation and analysis of protocols that prevent third parties or the general public from accessing private messages.
Cryptography is the practice and study of secure communication techniques amid adversarial behavior. More broadly, cryptography is the creation and analysis of protocols that prevent third parties or the general public from accessing private messages.

*Which cryptography technology is most widely used in blockchain and why?*

So, in general, blockchain technology is a distributed record holder which records the information about ownership of an asset. To define precisely,
So, in general, blockchain technology is a distributed record holder that records the information about ownership of an asset. To define precisely,
> Blockchain is a distributed, immutable ledger that makes it easier to record transactions and track assets in a corporate network.
An asset could be tangible (such as a house, car, cash, or land) or intangible (such as a business) (intellectual property, patents, copyrights, branding). A blockchain network can track and sell almost anything of value, lowering risk and costs for everyone involved.

So this is all about introduction to blockchain technology. To learn more about the topic refer below links....
So this is all about the introduction to blockchain technology. To learn more about the topic refer below links....
* <https://en.wikipedia.org/wiki/Blockchain>
* <https://en.wikipedia.org/wiki/Chinese_remainder_theorem>
* <https://en.wikipedia.org/wiki/Diophantine_equation>
Expand Down
132 changes: 132 additions & 0 deletions data_compression/coordinate_compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""
Assumption:
- The values to compress are assumed to be comparable,
values can be sorted and compared with '<' and '>' operators.
"""


class CoordinateCompressor:
"""
A class for coordinate compression.
This class allows you to compress and decompress a list of values.
Mapping:
In addition to compression and decompression, this class maintains a mapping
between original values and their compressed counterparts using two data
structures: a dictionary `coordinate_map` and a list `reverse_map`:
- `coordinate_map`: A dictionary that maps original values to their compressed
coordinates. Keys are original values, and values are compressed coordinates.
- `reverse_map`: A list used for reverse mapping, where each index corresponds
to a compressed coordinate, and the value at that index is the original value.
Example of mapping:
Original: 10, Compressed: 0
Original: 52, Compressed: 1
Original: 83, Compressed: 2
Original: 100, Compressed: 3
This mapping allows for efficient compression and decompression of values within
the list.
"""

def __init__(self, arr: list[int | float | str]) -> None:
"""
Initialize the CoordinateCompressor with a list.
Args:
arr: The list of values to be compressed.
>>> arr = [100, 10, 52, 83]
>>> cc = CoordinateCompressor(arr)
>>> cc.compress(100)
3
>>> cc.compress(52)
1
>>> cc.decompress(1)
52
"""

# A dictionary to store compressed coordinates
self.coordinate_map: dict[int | float | str, int] = {}

# A list to store reverse mapping
self.reverse_map: list[int | float | str] = [-1] * len(arr)

self.arr = sorted(arr) # The input list
self.n = len(arr) # The length of the input list
self.compress_coordinates()

def compress_coordinates(self) -> None:
"""
Compress the coordinates in the input list.
>>> arr = [100, 10, 52, 83]
>>> cc = CoordinateCompressor(arr)
>>> cc.coordinate_map[83]
2
>>> cc.coordinate_map[80] # Value not in the original list
Traceback (most recent call last):
...
KeyError: 80
>>> cc.reverse_map[2]
83
"""
key = 0
for val in self.arr:
if val not in self.coordinate_map:
self.coordinate_map[val] = key
self.reverse_map[key] = val
key += 1

def compress(self, original: float | str) -> int:
"""
Compress a single value.
Args:
original: The value to compress.
Returns:
The compressed integer, or -1 if not found in the original list.
>>> arr = [100, 10, 52, 83]
>>> cc = CoordinateCompressor(arr)
>>> cc.compress(100)
3
>>> cc.compress(7) # Value not in the original list
-1
"""
return self.coordinate_map.get(original, -1)

def decompress(self, num: int) -> int | float | str:
"""
Decompress a single integer.
Args:
num: The compressed integer to decompress.
Returns:
The original value.
>>> arr = [100, 10, 52, 83]
>>> cc = CoordinateCompressor(arr)
>>> cc.decompress(0)
10
>>> cc.decompress(5) # Compressed coordinate out of range
-1
"""
return self.reverse_map[num] if 0 <= num < len(self.reverse_map) else -1


if __name__ == "__main__":
from doctest import testmod

testmod()

arr: list[int | float | str] = [100, 10, 52, 83]
cc = CoordinateCompressor(arr)

for original in arr:
compressed = cc.compress(original)
decompressed = cc.decompress(compressed)
print(f"Original: {decompressed}, Compressed: {compressed}")
40 changes: 40 additions & 0 deletions maths/prime_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,46 @@ def prime_factors(n: int) -> list[int]:
return factors


def unique_prime_factors(n: int) -> list[int]:
"""
Returns unique prime factors of n as a list.

>>> unique_prime_factors(0)
[]
>>> unique_prime_factors(100)
[2, 5]
>>> unique_prime_factors(2560)
[2, 5]
>>> unique_prime_factors(10**-2)
[]
>>> unique_prime_factors(0.02)
[]
>>> unique_prime_factors(10**241)
[2, 5]
>>> unique_prime_factors(10**-354)
[]
>>> unique_prime_factors('hello')
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of 'int' and 'str'
>>> unique_prime_factors([1,2,'hello'])
Traceback (most recent call last):
...
TypeError: '<=' not supported between instances of 'int' and 'list'
"""
i = 2
factors = []
while i * i <= n:
if not n % i:
while not n % i:
n //= i
factors.append(i)
i += 1
if n > 1:
factors.append(n)
return factors


if __name__ == "__main__":
import doctest

Expand Down