Posts

Showing posts from May, 2024

numpy basics, creating arrays, indexing and slicing

Image
NumPy (Numerical Python) is a popular Python library for numerical and scientific computing. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. NumPy is a fundamental library for data manipulation and analysis in the Python ecosystem and is widely used in various scientific and engineering applications. Here are some of the key features and capabilities of NumPy: Multidimensional Arrays:  NumPy provides the ndarray object, which is a highly efficient and flexible array data structure. These arrays can have any number of dimensions and are the building blocks for many scientific and mathematical computations. Element-Wise Operations:  NumPy allows you to perform element-wise operations on arrays, making it easy to apply mathematical operations to entire arrays without explicit loops. Mathematical Functions:  NumPy includes a wide range of mathematical functions for operations l...

Fibonacci Series

Image
Credits:Elisheva Elbaz Dynamic programming is a method for solving a complex problem by breaking it up into smaller subproblems, and store the results of the subproblems for later use (to reduce duplication). Wherever we see a recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. Let’s start with the Fibonacci numbers. The Fibonacci numbers is the sequence  0, 1, 1, 2, 3, 5, 8, 13, 21, 34 … where each number in the sequence is found by adding up the two numbers before it. Note: It is sometimes written  1, 1, 2, 3, 5, 8, 13, 21, 34 …  but we will be using the sequence above where  fib(0) = 0 . Let’s write a function that will return the nth Fibonacci number. def fibonacciNoRecursion(n): if (n < 0): return 0 if (n == 0): return 0 previous = 1 sum = 1 for i in range(2,n): temp = sum sum += previous previous = temp return sum This has a linear time complexity —  O(n) . The r...

PadLocking

Padlocking Problem – Brute Force Method The padlocking problem is a simple real-life example that can be used to explain the Brute Force method of problem solving . The problem is: A combination padlock has several number dials. The correct combination is unknown. Find the combination that opens the lock by systematically trying all possible combinations. For classroom teaching, we can use a small hypothetical 3-digit padlock . 1. Understanding the Problem Suppose we have a padlock with 3 dials , and each dial contains digits from 0 to 9 . For example: Dial 1 Dial 2 Dial 3 ↓ ↓ ↓ 0 0 0 Each dial has 10 possible values . Therefore, the total number of possible combinations is: 10 × 10 × 10 = 10 3 = 1000 10 \times 10 \times 10 = 10^3 = 1000 So there are 1,000 possible combinations , ranging from: 000 001 002 003 ... 997 998 999 2. Brute Force Approach We don't have any information about the correct combination. Theref...