Skip to content

Instantly share code, notes, and snippets.

View pallabpain's full-sized avatar
👨‍💻

Pallab Pain pallabpain

👨‍💻
View GitHub Profile
@pallabpain
pallabpain / headscale_grpc_client.go
Last active January 6, 2023 18:24
A sample program to connect to a headscale server over gRPC
// A sample program that connects to the headscale gRPC server
// and lists all the API keys
//
// Usage Example: go run main.go -server=localhost:50443 -apikey=3GbSjH5IqA.1FPG5ZiqB3a8idtvJnhBjCSb_V5pffuaLyFJyYZNPlQ
package main
import (
"context"
"flag"
@pallabpain
pallabpain / squash_commits.md
Last active September 13, 2022 06:40
Squash commits when you have merge commits in between

Squash commits when you have merge commits in between

Let's assume that the feature branch is called feature and the main branch main:

  1. Create a temporary (say temp) branch from main:
git checkout -b temp main
  1. Squash the feature branch in:
@pallabpain
pallabpain / server.go
Created April 2, 2022 11:38
Starting a go http server with cancellable context
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"os/signal"
)
@pallabpain
pallabpain / longest_lex_substr.py
Created November 27, 2021 16:39
Given a string S, print the longest substring P such that P > S lexicographically.
def longestSubstr(s):
for i in range(1, len(s)):
if s[i] > s[0]:
break
# For cases like, AABAB
# BAB is lexicographically greater, however,
# ABAB is the longest such substring
while i > 1 and s[i-1] == s[0]:
i -= 1
@pallabpain
pallabpain / longest_palindromic_substring.py
Created October 2, 2021 09:29
Find the longest palindromic substring
"""
Given a string s, return the longest palindromic substring in s
Example 1:
Input: s = "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
@pallabpain
pallabpain / permutations.py
Created October 2, 2021 05:12
Print all permutations of a given array of integers
"""
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]
@pallabpain
pallabpain / nextGreaterElement.go
Created October 1, 2021 16:25
Print the next greater element
package main
/*
* Given an array, print the Next Greater Element (NGE) for every element.
* The next greater element for an element x is the first greater element
* on the right side of x in the array. Elements for which no greater
* element exists, consider the next greater element as -1.
*/
import "fmt"
@pallabpain
pallabpain / rotateMatrix.py
Created September 10, 2021 13:30
Roate a 2D matrix clockwise by 90 degrees
"""
You are given an n x n 2D matrix that represents an image.
Rotate the image by 90 degrees (clockwise) using O(1) extra space
"""
def rotateImageClockwiseBy90(a):
"""
The solution here is to find the elements to swap. We will do it
in cycles. First, identify the groups:
(x, y), (y, N-1-x), (N-1-x, N-1-y), (N-1-y, x)
@pallabpain
pallabpain / find_key_in_dict.py
Created March 15, 2021 07:26
Find a key in dictionary or a list of dictionaries
def key_in_dict(d, k):
if isinstance(d, list):
for v in d:
return key_in_dict(v, k)
if isinstance(d, dict):
if k in d:
return True
for v in d.values():
return key_in_dict(v, k)
return False
@pallabpain
pallabpain / remove_duplicates.py
Created August 15, 2020 12:58
Given a sorted array, remove the duplicates from the array in-place such that each element appears at most twice, and return the new length.
"""
Given a sorted array, remove the duplicates from the array in-place such that each element appears at most twice, and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example
Given array [1, 1, 1, 3, 5, 5, 7]
The output should be 6, with the first six elements of the array being [1, 1, 3, 5, 5, 7]