Open in app

Sign In

Write

Sign In

sohesh doshi
sohesh doshi

51 Followers

Home

About

Dec 20, 2022

Singleton Design Pattern

A Singleton is a design pattern that restricts the instantiation of a class to a single object. This is useful when exactly one object is needed to coordinate actions across the system. To implement a Singleton in Python, we can use a metaclass or a decorator. Here is an example…

Python

2 min read

Python

2 min read


Jul 3, 2021

Python:- The best way to get file-type.

bad code to get file-type:- filename = "abc/sample.txt" filename_split_arr = filename.split(".") print(filename_split_arr[-1]) best practice to get file-type:- import magic file_type = magic.from_file("abc/sample.txt", mime=True) print(file_type) python-magic is a Python interface to the libmagic file type identification library. libmagic identifies file types by checking their headers according to a predefined list of file types. This functionality is exposed to the command line by the Unix command file.

Python

1 min read

Python

1 min read


Jul 27, 2020

Python-DB Connection in Class Wrapper.

This class uses pymysql package to do the database interaction. from pymysql import connect from pymysql.cursors import DictCursor class Database: def __init__(self, host_name, user_name, password, charset, port): self._conn = connect(host=host_name, user=user_name, password=password, db=self.db, charset=charset, port=port…

Dbms

1 min read

Dbms

1 min read


Jan 7, 2020

DP Amazon Interview Quetion

A frog jumps either 1, 2 or 3 steps to go to top. In how many ways can it reach the top. def solution(num): if num==1 or num==2 or num==3: return num return 1+solution(num-1)+solution(num-2)+solution(num-3) Let’s Define Dp solution using recursive def Dp_Solution(num): temp=[0 for i in range(num+1)] temp[0]=temp[1]=1 temp[2]=2 temp[3]=3 if num>3: for i in range(4,num+1): temp[i]=1+temp[i-1]+temp[i-2]+temp[i-3]

Python3

1 min read

Python3

1 min read


Jan 5, 2020

Walmart Interview Question Solve using DP

Given an M X N matrix with your initial position at the top-left cell, find the number of possible unique paths to reach the bottom-right cell of the matrix from the initial position. Note: Possible moves can be either down or right at any point in time, i.e., we can move to matrix[i+1][j] or matrix[i][j+1] from matrix[i][j]. Recursive Solution for Above Question def solution(m,n): if m==1 or n==1: return 1 return solution(m-1,n)+solution(m,n-1) print(solution(3,4))

Programming

1 min read

Programming

1 min read


Jan 5, 2020

DP Problem (Count distinct occurrences as a subsequence)

Given a two strings S and T, find count of distinct occurrences of T in S as a subsequence. # Python3 program to count number of times # S appears as a subsequence in T def findSubsequenceCount(S, T): m = len(T) n = len(S) # T can't appear as a…

Programming

1 min read

Programming

1 min read


Jan 4, 2020

DP Problem

Given a number n, we can divide it into only three parts n/2, n/3, and n/4 (we will consider only integer part). The task is to find the maximum sum we can make by dividing the number into three parts recursively and summing up them together. Note: Sometimes, the maximum sum can be obtained by not dividing n. start with the minimum solution n=0 then max sum=0 now n=1 , maxsum=1 so on. so we find a recursive solution like max(n//2+n//3+n//4,n).

Programming

1 min read

Programming

1 min read


Jan 4, 2020

DP Solution for Number of subsequences of the form a^i b^j c^k

Number of subsequences of the form a^i b^j c^k Given a string, count number of subsequences of the form a^ib^jc^k, i.e., it consists of i ’a’ characters, followed by j ’b’ characters, followed by k ’c’ characters where i >= 1, j >=1 and k >= 1. from collections import Counter def solution(totalcount): temp_ar=[0 for i in range(totalcount+1)] temp_ar[3]=1 for i in range(4,totalcount+1): temp_ar[i]=temp_ar[i-1]*3

Programming

1 min read

Programming

1 min read

sohesh doshi

sohesh doshi

51 Followers
Following
  • Fahim ul Haq

    Fahim ul Haq

  • Pinterest Engineering

    Pinterest Engineering

  • javinpaul

    javinpaul

  • Julie Zhuo

    Julie Zhuo

  • Arslan Ahmad

    Arslan Ahmad

See all (110)

Help

Status

Writers

Blog

Careers

Privacy

Terms

About

Text to speech

Teams