Community chat: https://t.me/hamster_kombat_chat_2
Twitter: x.com/hamster_kombat
YouTube: https://www.youtube.com/@HamsterKombat_Official
Bot: https://t.me/hamster_kombat_bot
Game: https://t.me/hamster_kombat_bot/
Last updated 4 months ago
Your easy, fun crypto trading app for buying and trading any crypto on the market.
📱 App: @Blum
🆘 Help: @BlumSupport
ℹ️ Chat: @BlumCrypto_Chat
Last updated 3 months, 3 weeks ago
Turn your endless taps into a financial tool.
Join @tapswap_bot
Collaboration - @taping_Guru
Last updated 1 week, 1 day ago
How AI is Changing Coding and Education
FREE STANFORD WEBINAR
October 9, 2024 | 11:00-11:45 am PT
Less known features of C
https://jorenar.com/blog/less-known-c
Synchronizing Numba and NumPy RNG States for Consistent BehaviorThe Numba RNG state and NumPy RNG states are entirely separate. Therefore calling np.random.seed() will only impact the NumPy RNG seed and this explains the behaviour in the above. To make Numba's RNG state the same as NumPy's, the np.random.seed() function needs calling from within a JIT compiled region, for example:
```
python
import numpy as np
from numba import jit
from numba.extending import register_jitable
@register_jitable # This will run in JIT mode only if called from a JIT function
def set_seed_compat(x):
np.random.seed(x)
@jit(nopython=True)
def get_random():
set_seed_compat(42)
print(np.random.rand(3))
def get_radnom2():
print(np.random.rand(3))
get_random()
get_random.py_func()
get_radnom2()
#[0.37454012 0.95071431 0.73199394]
#[0.37454012 0.95071431 0.73199394]
#[0.59865848 0.15601864 0.15599452]
```
The third should be different, but still repeatable by re-execution of the script.
Python doesn't have built-in tree structures, but you can implement one using classes.
```
# Node class for a binary tree
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function to perform an in-order traversal (left, root, right)
def inorder(root):
if root:
inorder(root.left)
print(root.data, end=" ")
inorder(root.right)
# Example usage:
if __name__ == "__main__":
# Create the binary tree
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
\# In\-order traversal
inorder(root)
```
You can use dictionaries or lists to represent graphs in Python.
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
Python doesn't have a built-in linked list, but you can implement one using classes.
```
# Node class to represent a single node in the linked list
class Node:
def __init__(self, data):
self.data = data # Data held by the node
self.next = None # Pointer to the next node in the list
# LinkedList class to manage the linked list
class LinkedList:
def __init__(self):
self.head = None # Initialize the head of the list as None
\# Method to append a new node to the end of the list
def append(self, data):
new\_node = Node(data) \# Create a new node with the given data
if self.head is None:
self.head = new\_node \# If the list is empty, set the new node as the head
return
last = self.head
while last.next: \# Traverse to the end of the list
last = last.next
last.next = new\_node \# Set the next of the last node to the new node
\# Method to print the linked list
def print\_list(self):
current = self.head
while current:
print(current.data, end=" \-> ")
current = current.next
print("None")
\# Method to delete the first occurrence of a node with the given data
def delete(self, data):
current = self.head
\# If the head node holds the data to be deleted
if current and current.data == data:
self.head = current.next \# Change the head to the next node
current = None \# Free the old head node
return
\# Search for the node to be deleted, keeping track of the previous node
prev = None
while current and current.data != data:
prev = current
current = current.next
\# If the data was not present in the list
if current is None:
print("Node not found in the list.")
return
\# Unlink the node from the list
prev.next = current.next
current = None
# Example usage:
if __name__ == "__main__":
# Create a linked list
linked_list = LinkedList()
\# Append some elements to the list
linked\_list.append(1)
linked\_list.append(2)
linked\_list.append(3)
\# Print the list
print("Original Linked List:")
linked\_list.print\_list()
\# Delete a node
linked\_list.delete(2)
\# Print the list after deletion
print("Linked List after deleting 2:")
linked\_list.print\_list()
```
Python’s heapq module implements a binary heap.
import heapq
heap = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
heapq.heapify(heap)
smallest = heapq.heappop(heap)
# Python Data Structures
## 1. Primitive Data Types
- int: Integer values.
x = 10
float: Floating-point numbers.
y = 10.5
bool: Boolean values (True/False).
z = True
str: Strings (immutable sequences of Unicode characters).
s = "Hello, World!"
a. List
A mutable, ordered collection of elements.
lst = [1, 2, 3, 4]
lst.append(5)
b. Tuple
An immutable, ordered collection of elements.
tpl = (1, 2, 3)
c. Range
A sequence of numbers, often used in loops.
r = range(1, 10)
d. String
A sequence of characters, treated as a list of characters.
s = "Hello"
first\_char = s[0]
e. Byte/Bytearray
Immutable (bytes) or mutable (bytearray) sequences of bytes.
b = bytes([65, 66, 67]) \# A, B, C
ba = bytearray([65, 66, 67])
a. Set
An unordered, mutable collection of unique elements.
s = {1, 2, 3, 4}
s.add(5)
b. Frozen Set
An immutable version of a set.
fs = frozenset([1, 2, 3])
Dictionary
An unordered, mutable collection of key-value pairs.
d = {'name': 'Alice', 'age': 25}
d['location'] = 'Wonderland'
a. Deque (Double-Ended Queue)
A thread-safe, general-purpose, double-ended queue.
from collections import deque
dq = deque([1, 2, 3])
dq.appendleft(0)
b. Defaultdict
A dictionary that provides a default value for non-existent keys.
from collections import defaultdict
dd = defaultdict(int)
dd['a'] += 1 \# If 'a' doesn't exist, its value will default to 0
c. OrderedDict
A dictionary that remembers the order of key insertions (introduced in Python 3.1).
from collections import OrderedDict
od = OrderedDict()
od['a'] = 1
od['b'] = 2
a. Stack (List-based)
A last-in, first-out (LIFO) structure.
stack = []
stack.append(10)
stack.pop()
b. Queue (List or deque-based)
A first-in, first-out (FIFO) structure.
queue = deque()
queue.append(10)
queue.popleft()
Here are the Colored images:
https://t.me/QajarC
Community chat: https://t.me/hamster_kombat_chat_2
Twitter: x.com/hamster_kombat
YouTube: https://www.youtube.com/@HamsterKombat_Official
Bot: https://t.me/hamster_kombat_bot
Game: https://t.me/hamster_kombat_bot/
Last updated 4 months ago
Your easy, fun crypto trading app for buying and trading any crypto on the market.
📱 App: @Blum
🆘 Help: @BlumSupport
ℹ️ Chat: @BlumCrypto_Chat
Last updated 3 months, 3 weeks ago
Turn your endless taps into a financial tool.
Join @tapswap_bot
Collaboration - @taping_Guru
Last updated 1 week, 1 day ago