Skip to main content

Posts

Getting Started with NumPy: A Comprehensive Guide

  NumPy is a fundamental library for scientific computing with Python. It provides support for large, multi-dimensional arrays and matrices, along with mathematical functions to operate on these arrays. This tutorial will introduce you to the basics of NumPy and its core functionalities. Installing NumPy Before diving into NumPy, make sure you have it installed. You can install NumPy using the following command: pip install numpy pip install numpy Importing NumPy Once installed, you can import NumPy in your Python script or Jupyter notebook as follows: import numpy as np Now, let's explore some of the essential features of NumPy. NumPy Arrays NumPy's primary data structure is the array. An array is a grid of values, and it can be one-dimensional or multi-dimensional. Here's how you can create a simple one-dimensional array: import numpy as np # Creating a one-dimensional array arr1 = np.array([1, 2, 3, 4, 5]) print(arr1) For multi-dimensional arrays, you can use nested ...
Recent posts

Difference between Numpy and Python list

  NumPy and Python lists are both data structures used to store collections of data, but they have several key differences: 1. Homogeneity:    - NumPy arrays are homogeneous, meaning that all elements in a NumPy array must have the same data type (e.g., all integers, all floating-point numbers). This homogeneity allows for efficient, element-wise operations.    - Python lists can contain elements of different data types, providing more flexibility but potentially sacrificing performance. 2. Performance:    - NumPy is optimized for numerical operations and is typically faster than Python lists when performing element-wise operations (e.g., addition, multiplication) on large datasets. This is due to the homogeneous nature of NumPy arrays and the fact that NumPy operations are implemented in C and can take advantage of low-level optimizations.    - Python lists are more versatile but are generally slower for numerical computations compared to Nu...

Valid parentheses leet code

  class Solution :     def isValid ( self , s : str ) -> bool :         d={ "(" : ")" , "{" : "}" , "[" : "]" }         stack=[]         for p in s:             if p in d: #check each paremnthasis if it is opening one                 stack.append(d[p]) # append closing of that to stack             elif stack and stack[- 1 ]==p: #if p is closing parethasis and                 stack.pop() #it is last ele in stack pop that ele             else :                 return False         return True if not stack else False             #time complexity O(n) # memeory complexity O(n)

Merge sorted Array

  class Solution :     def merge ( self , nums1 : List[ int ], m : int , nums2 : List[ int ], n : int ) -> None :         last=m+n- 1         while m> 0 and n> 0 :             if nums1[m- 1 ]>nums2[n- 1 ]:                 nums1[last]=nums1[m- 1 ]                 m=m- 1             else :                 nums1[last]=nums2[n- 1 ]                 n=n- 1             last=last- 1         while n> 0 :             nums1[last]=nums2[n- 1 ]             n=n- 1             last=last- 1         """         Do not return anything, ...

Reverse integer leetcode solution

  class Solution :     def reverse ( self , x : int ) -> int :         if x< 0 :             sign=- 1         else :             sign= 1         s= str (x)                   if s[ 0 ]== "-" :             ss=sign* int (s[:- len (s):- 1 ])                         elif s[- 1 ]== "0" :             ss= int (s[:- len (s)- 1 :- 1 ])                         else :             ss= int (s[::- 1 ])         if ss>- 2 ** 31 and ss<( 2 ** 31 )- 1 :             return ss         else :             re...

Roman to integer Leetcode Solution

  class Solution :     def romanToInt ( self , s : str ) -> int :         roman={ "I" : 1 , "V" : 5 , "X" : 10 , "L" : 50 , "C" : 100 , "D" : 500 , "M" : 1000 }         total= 0         for i in range ( len (s)):             if i+ 1 < len (s) and roman[s[i]]<roman[s[i+ 1 ]]:                 total=total-(roman[s[i]])             else :                 total=total+(roman[s[i]])         return total