Saturday, December 25, 2021

Saturday, December 4, 2021

Interview coding question: Find a pair from two sorted array that sum is nearest to one number

 Q. pair from two sorted array having one element from each and sum is nearest to number X. Solve in O(n) time.


Solution:

let A1=[3,5,8,10,15];
let A2=[0,1,6,20,34];
let X=17;
let diff=Infinity;
let l=0,r=A2.length-1;
let p1=-1,p2=-1;
while(true)
{
if(A1[l]+A2[r]==X)
{
p1=l;
p2=r;
break;
}
let diff_tmp=Math.abs(A1[l]+A2[r]-X);
if(diff_tmp<diff)
{
console.log(`diff = ${diff}`);
p1=l;
p2=r;
diff=diff_tmp;
}
if(A1[l]+A2[r]<X || r<0)
{
l++;
}
else if(A1[l]+A2[r]>X || l>=A1.length)
{
r--;
}
if(l>=A1.length && r<0)
{
break;
}
}
console.log(`p1=${p1} and p2=${p2}`);
console.log(`p1v=${A1[p1]} and p2v=${A2[p2]}`);

Wednesday, December 1, 2021

Interview coding questions: Implement 2-way merge sort using array iteration only

 Answer:

let inputArray=[2,5,8,1,100,3,0,11,26,1,133,6];
let A=inputArray;
let B=Array(A.length);
let S=A;
let T=A;
let l=A.length;
topmost:for(let s=1;;)
{
if(Math.log2(s)%2==0)
{
console.log("even");
S=A;
T=B;
}
else
{
console.log("odd");
S=B;
T=A;
}
console.log("souce="+S);
console.log("target="+T);

//break;
for(let i=0;i<l;)
{
console.log(`i==${i} and s==${s}`);
let p1=i;
let p2=i+s;
let k=0;
console.log(`p1=${p1} p2=${p2}`);
for(let k=0;;)
{
console.log(`k=${k}, p1=${p1} p2=${p2}`);
if(p2 <l && (p2 < i+2*s) && (S[p1]>S[p2] || p1==i+s))
{
T[i+k]=S[p2];
p2=p2+1;
}
else
{
T[i+k]=S[p1];
p1=p1+1;
}
k++;
if(i+k>=l || k>=2*s)
{
break;
}
}
i=i+2*s;
console.log("Target="+T);
//break topmost;
}
s=s*2;
if(s>=l)
{
break;
}
}
A=T;
console.log(A);

Sunday, November 28, 2021

Friday, July 2, 2021

How to import export cron files

 Create the backup (export):

crontab -l > /some/shared/location/crontab.bak

Import it from the new user:

crontab /some/shared/location/crontab.bak

Monday, May 31, 2021

How to revert deleted GIT file

 Use below command to restore deleted file before commiting


git checkout HEAD <filename>

Wednesday, May 12, 2021

Python sort by key, list of dict items

 use below code:

expired_users = sorted(expired_users, key=lambda k: k['expiry_date'],reverse=True)

Tuesday, May 11, 2021

Mongo dump and restore

 MongoDump

 mongodump -d=pp -c=qq --query='{"expiredPremiumInfo":{"$exists":true}}'


mongorestore -d=pp -c=qq <path to bson file>

Monday, May 10, 2021

Email template management - Stripo.email

 Stripo.email is amazing platform for managing Email templates. It has drag and drop email editor also.

Saturday, May 8, 2021

Email automation systems

Below is video comparing all email automation systems like:
1. aweber
2. mautic
3. autoresponder
4. sendy.co

https://youtu.be/1K8nLXf-8_w

Wednesday, April 28, 2021

find and delete all files of a particular size

 find . -type f -size 2572c | xargs rm -rf


use find to list files

c==byte

use xargs to pipe and delete

Wednesday, March 24, 2021

Using RAKE for keyphrase extractions

 

Use MULTI-RAKE for Hindi keyphrases.


RAKE-NLTK

RAKE-NLTK is a modified version that uses the natural language processing toolkit NLTK for some of the calculations.

Installation:

Import, Declare a RAKE-NLTK Object and Extract!
We again extract just the top 10 keywords.

Here’s the output for the same text passage using RAKE-NLTK. For the chosen passage, RAKE and RAKE-NLTK give the same output. But this isn’t always the case. Try it out for yourself on other passages!

Sunday, March 21, 2021

crisp: chat with customer

 crisp is customer chat application and they have widget for android apps as well.



Best tool to create icons for Android and iOS

 appicon.co


This site is amazing the way it create app icons.

Some beautiful examples of Kotlin

 1. UI Callbacks

var uirefreshListener: (()->Unit)? = null
this.uirefreshListener={
showPostLoginUI()
}
@JvmStatic
fun addCustomerPremiumInfo(context: Context, customer_id: Int, callbackUI: (() -> Unit)?) {
}

2. Static fields and methods
companion object {
@JvmStatic
fun syncCustomerData(context: Context, name: String, email: String) {
}

3. Async and runonMainUI and co-routines

GlobalScope.async {
val ackPurchaseResult = withContext(Dispatchers.IO) {
billingClient.acknowledgePurchase(acknowledgePurchaseParams.build(),acknowledgePurchaseResponseListener)
}
}


    

Wednesday, March 17, 2021

How to get Debug Key for Android studio

 Go to ~/.android

run below command

keytool -exportcert -list -v -alias androiddebugkey -keystore debug.keystore

Thursday, March 11, 2021

Django Initial Setup for Django REST API

 python3 -m venv env

source env/bin/activate  # On Windows use `env\Scripts\activate`

# Install Django and Django REST framework into the virtual environment
pip install django
pip install djangorestframework

# Set up a new project with a single application
django-admin startproject tutorial .  # Note the trailing '.' character
cd tutorial
django-admin startapp quickstart
cd ..
python manage.py createsuperuser --email admin@example.com --username admin
manage.py makemigration
manage.py migrate
create token: python manage.py drf_create_token bhola
Give MYSQL settings like below: DATABASES = {
    'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xxx',
'USER': 'root',
'PASSWORD': 'xxxxxx',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
Django filters:pip install django-filter 
filter_backends = [DjangoFilterBackend]
filter_fields = ["email"]

Tutotial on setting up uWSGI with Flask for Python

 Below is amazing tutorial for setting Flask with uWSGI:


Key command is :

uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app


Key file content is :
wsgi.py
from launch import app

if __name__ == "__main__":
    app.run()

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04



Monday, March 8, 2021

Launch of Namaste English website

 My firm HinKhoj has finally launched dedicated website for English learning for Hindi Medium students. In Namaste English, students will get below:

1. Online Live Video courses

2. Recorded classes from English Teacher

3. Grammar practice and spoken english practice

4. Games for increasing English Knowledge


So why wait, click on https://namaste-english.com to visit it.