x = 10 y = x print(id(x)) # Output: 140737469306640 print(id(y)) # Output: 140737469306640
x
and y
refer to the same integer object with the memory address 140737469306640
. The reference count of this object is 2.del x print(id(y)) # Output: 140737469306640
x
, the reference count of the integer object becomes 1. Since y
is still pointing to it, the object is not garbage collected.def add_items(n): return n + n + n print add_items(10)
The O(N^2) time complexity means that the running time of an algorithm grows quadratically with the size of the input. It often involves nested loops, where each element in the input is compared with every other element.
def print_items(n): for i in range(n): for j in range(n): print(i,j) print_items(10)
def print_items(n): for i in range(n): for j in range(n): print(i,j) for k in range(n): print(k) print_items(10)
O(n)
for the simple search. It's a guarantee that the simple search will never be slower than O(n) time. In a code where the worst scenario is go thought all the elements the notation is O(n)
def print_items(n): for i in range(n): print(i) print_items(10)
brew install redis
brew services start redis
redis-cli ping
PONG
.// Custom Hook const useProducts = () => { const [products, setProducts] = useState([]); useEffect(() => { fetch("/api/products") .then((res) => res.json()) .then((data) => setProducts(data)); }, []); return products; }; // Container Component function ProductContainer() { const products = useProducts(); return <ProductList products={products} />; } // Presentational Component function ProductList({ products }) { return ( <ul> {products.map((product) => ( <li key={product.id}>{product.name}</li> ))} </ul> ); }
orders
with documents representing individual orders. Each document has fields like order_id
, customer_id
, product_name
, and price
. We want to calculate the average order value for each customer.db.orders.aggregate([ { $group: { _id: "$customer_id", total_spent: { $sum: "$price" }, total_orders: { $count: {} } } }, { $project: { average_order_value: { $divide: ["$total_spent", "$total_orders"] } } } ])
customer_id
.$sum
.$count
.total_spent
by total_orders
.customer_id
and the calculated average_order_value
.