Basic data structures and algorithms in python

Basic data structures and algorithms in python Searching algorithms in python Linear search 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 def linear_search(arr, target): for i in range(len(arr)): if arr[i] == target: return i # Return the index if target is found return -1 # Return -1 if the target is not found def main(): arr = [3, 10, 23, 5, 2, 6] target = 5 # Calling the linear_search function result = linear_search(arr, target) # Output the result if result != -1: print(f"Element {target} found at index {result}.") else: print(f"Element {target} not found in the list.") # Calling main to run the program if __name__ == "__main__": main() Time Complexity ...

February 25, 2025 · 6 min · 1105 words · Me

Leetcode solutions

Leetcode solutions Problem 1: Two Sum 1 2 3 4 5 6 7 8 9 10 11 12 class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ for i in range(0, len(nums)): for j in range(0, len(nums)): if nums[i]+nums[j] == target and i!=j: op = [i,j] return op Problem 2: Roman to integer 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900} i = 0 num = 0 while i < len(s): if i+1<len(s) and s[i:i+2] in roman: num+=roman[s[i:i+2]] i+=2 else: #print(i) num+=roman[s[i]] i+=1 return num Problem 3: Pallindrome 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool """ if x < 0: return False # Store the number in a variable number = x # This will store the reverse of the number reverse = 0 while number: reverse = reverse * 10 + number % 10 number //= 10 return x == reverse Problem 4: Longest Common Prefix 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """ size = len(strs) # if size is 0, return empty string if (size == 0): return "" if (size == 1): return strs[0] # sort the array of strings strs.sort() # find the minimum length from # first and last string end = min(len(strs[0]), len(strs[size - 1])) # find the common prefix between # the first and last string i = 0 while (i < end and strs[0][i] == strs[size - 1][i]): i += 1 pre = strs[0][0: i] return pre Problem 5: Valid Parentheses 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ # Stack for left symbols leftSymbols = [] # Loop for each character of the string for c in s: # If left symbol is encountered if c in ['(', '{', '[']: leftSymbols.append(c) # If right symbol is encountered elif c == ')' and len(leftSymbols) != 0 and leftSymbols[-1] == '(': leftSymbols.pop() elif c == '}' and len(leftSymbols) != 0 and leftSymbols[-1] == '{': leftSymbols.pop() elif c == ']' and len(leftSymbols) != 0 and leftSymbols[-1] == '[': leftSymbols.pop() # If none of the valid symbols is encountered else: return False return leftSymbols == [] Problem 6: Largest Substring Between Two Equal Characters 1 2 3 4 5 6 7 8 class Solution: def maxLengthBetweenEqualCharacters(self, s: str) -> int: output = -1 for i in range (0, len(s)): for j in range (i+1, len(s)): if s[i] == s[j]: output = max(output, j-i-1) return output

December 30, 2023 · 3 min · 506 words · Aum Pauskar

Image analysis theory

Image processing Theory jargon Difference between supervised and unsupervised learning Criteria Supervised Learning Unsupervised Learning Data Uses labeled data for training. Uses unlabeled data for training. Goal Predict a label for new data based on past observations. Discover hidden patterns or intrinsic structures within the data. Examples Classification, Regression Clustering, Association Complexity Less complex as it has a clear goal. More complex due to the lack of clear goal. Usage When the outcome of the problem is known. When the outcome of the problem is unknown. Application #1 Spam Detection Customer Segmentation Application #2 Credit Fraud Detection Anomaly Detection EM spectrum The Electromagnetic Spectrum (EM) is the range of all types of EM radiation. Radiation is energy that travels and spreads out as it goes – visible light that comes from a lamp in your house or radio waves from a radio station are two types of electromagnetic radiation. Other examples of EM radiation are microwaves, infrared and ultraviolet light, X-rays, and gamma-rays. ...

December 6, 2023 · 6 min · 1102 words · Aum Pauskar

Image analysis with pytorch

Image Analysis using pytorch Prerequisites This project is built using python in Ubuntu (WSL) and you’ll need to install the following: Any bash terminal (one of the following) Conda WSL Mac OS Any flavour of Linux Python 3 (I’m using 3.10.12) 1 2 sudo apt update sudo apt install python3 Pip 1 2 sudo apt update sudo apt install python3-pip Packages Note: Since I’m using a computer with a CUDA compatable NVIDIA GPU, I’ll be using the GPU version of pytorch. If you don’t have a GPU, you can install the CPU version of pytorch given below. CPU install 1 pip3 install torch torchvision numpy matplotlib GPU install Installing numpy and matplotlib 1 pip3 install numpy matplotlib Installing pytorch Check the pytorch website to see the which library is compatable with your system. In my case I’m using CUDA 11.8 1 pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 Jupyter notebook (optional) 1 pip3 install jupyterlab Or just use Jupyter notebook from VS Code from here Environement I’ve used 1 CPU Ryzen 7 5800H 2 GPU RTX 3060 Laptop 3 RAM 2x8GB DDR4 @ 3200MHz 4 OS Windows 11/Ubuntu 22.4 WSL 5 CUDA 11.8 6 Python 3.10.12 MNIST number dataset The MNIST dataset is a dataset of handwritten digits. It has 60,000 training images and 10,000 test images. We’ll see a code to load the dataset and display the occurances of individual digits in the dataset. Or if you want to run the code from Jupyter notebook you can clone my repository via git. ...

December 3, 2023 · 5 min · 1062 words · Aum Pauskar

Virtualenv

Python virtual environment Python virtual environemt is a program that aids in creating a serperate virtual environment for each project Requiremnets Python 3 Pip Steps Installing virtual environemnt package 1 pip install virtualenv Creating virtual environent Windows - cmd/powershell 1 py -m venv {your_env_name} Linux 1 python3 -m venv myworld Activating virtual environemt Windows - cmd 1 myworld\Scripts\activate.bat Windows - powershell 1 myworld\Scripts\activate.ps1 Linux ...

November 16, 2023 · 1 min · 76 words · Aum Pauskar