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 like basic ar

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 recursive solution is mor

PadLocking

Padlocking using the brute force method refers to the process of systematically attempting every possible combination to unlock a padlock. This approach involves trying each combination one by one until the correct one is found. Steps in Brute Forcing a Padlock: Understanding the Lock's Structure : A typical combination padlock might have a certain number of dials, each with a range of digits (e.g., 0-9). For instance, a lock with 3 dials, each with 10 digits, would have 10^3 = 1,000 possible combinations. Systematic Trial : Starting Point : Begin with the first possible combination, usually all dials set to 0 (e.g., 000). Incrementing Combinations : Progress through each possible combination systematically (001, 002, 003, ..., 999). Checking the Lock : After each combination is set, try to open the lock. Finding the Solution : Continue this process until the lock opens, indicating that the correct combination has been found. Efficiency and Time Considerations : Time Required : The

Brute Force Method of Problem Solving

The brute force method is a straightforward, exhaustive approach to problem-solving that involves systematically exploring all possible solutions to find the correct one. While this method is simple and often guarantees a solution, it is usually inefficient for large or complex problems due to its high computational cost. Here's a detailed description: 1.  Definition and Overview Brute Force  is a problem-solving technique that tries every possible option until it finds the solution. It does not involve any shortcuts, optimizations, or heuristics—just pure trial and error. This method is often used as a baseline or last resort when more sophisticated methods fail or are not available. It is also useful for understanding the nature of the problem and identifying patterns or insights that might lead to more efficient solutions. 2.  How It Works Systematic Exploration : The brute force method considers every possible configuration or combination that could potentially solve the proble

Recursion vs Dynamic Programming

  Recursion vs Dynamic Programming Aspect Recursion Dynamic Programming (DP) Basic Idea Solves a problem by breaking it down into smaller sub-problems, often leading to repeated calculations of the same sub-problems. Solves a problem by breaking it down into smaller sub-problems, but stores the results of these sub-problems to avoid redundant calculations. Overlapping Sub-Problems Sub-problems are often recalculated multiple times because results are not stored. Sub-problems are solved once and their results are stored (usually in a table) for reuse. Optimal Substructure Can be used if the problem exhibits optimal substructure, but does not explicitly take advantage of it unless combined with memoization. Explicitly takes advantage of optimal substructure by building up the solution from the smallest sub-problems. Time Complexity Can be exponential in many cases (e.g., O(2^n) for Fibonacci) due to repeated calculations. Often reduces time complexity to polynomial (e.g., O(n) for Fibona

Dynamic Programming

  What is Dynamic Programming? Dynamic Programming (DP) is an algorithmic approach used to solve problems by breaking them down into simpler sub-problems and storing the results of these sub-problems to avoid redundant computations. It is particularly effective for problems that exhibit overlapping sub-problems and optimal substructure . Key Concepts in Dynamic Programming Dynamic Programming is built on two key principles: Overlapping Sub-Problems : In many problems, the same sub-problems are solved multiple times. Instead of solving the same sub-problem repeatedly, DP solves it once and stores the result for future reference. This reduces the overall computation time. Optimal Substructure : A problem has optimal substructure if an optimal solution to the problem can be constructed from optimal solutions to its sub-problems. This means that the solution to the main problem depends on the solutions to its sub-problems. Steps in Dynamic Programming Dynamic Programming typically involve