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 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. Characteristics Exhaustive Search: Every possible solution is examined without any optimization. Simplicity: Easy to understand and implement. Ineffici...

Computational Approaches to Problem Solving - Introduction

Computational approaches to problem-solving encompass a diverse range of methodologies that leverage computational power to address complex challenges across various domains. We will  explore several fundamental strategies— brute force, divide-and-conquer, dynamic programming, greedy algorithms, and randomized approaches —each offering unique insights and techniques to tackle different classes of problems effectively. The brute-force approach represents simplicity and exhaustive computation.It involves systematically checking every possible solution to find the optimal one, making it ideal for problems like cracking padlocks or guessing passwords.Despite its simplicity, brute-force methods can be computationally expensive,especially for problems with large solution spaces, leading to impractical execution times in real-world applications. In contrast, the divide-and-conquer approach breaks down problems into smaller, more manageable sub-problems until they become simple enough...