S

@supriyo_biswas

IPv4 range to IPv4-mapped IPv6 range

Python
1 year ago
import ipaddress def ipv4_to_ipv6_mapped(ipv4_cidr): # Parse the IPv4 CIDR ipv4_network = ipaddress.IPv4Network(ipv4_cidr) # Convert the network address to IPv6-mapped format ipv6_network_address = ipaddress.IPv6Address(f"::ffff:{ipv4_network.network_address}") # Calculate the new subnet mask length for the IPv6 range

Product of array except self

Python
1 year ago
# https://leetcode.com/problems/product-of-array-except-self/ # Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. # The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. # You must write an algorithm that runs in O(n) time and without using the division operation. # Example 1: # Input: nums = [1,2,3,4]

Reverse Vowels of a String

Python
1 year ago
# https://leetcode.com/problems/reverse-vowels-of-a-string/description/ # Given a string s, reverse only all the vowels in the string and return it. # The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once. # Example 1: # Input: s = "hello" # Output: "holle"

Can Place Flowers

Python
1 year ago
# https://leetcode.com/problems/can-place-flowers/description/ # You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots. # Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise. # Example 1: # Input: flowerbed = [1,0,0,0,1

Kids With the Greatest Number of Candies

Python
1 year ago
# https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/description/ # There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have. # Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids,

Greatest common divisor of strings

Python
1 year ago
# https://leetcode.com/problems/greatest-common-divisor-of-strings/description/ # For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times). # Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2. # Example 1:

Euclidean GCD algorithm

Python
1 year ago
# https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations def gcd(a, b): while b != 0: a, b = b, a % b return a print(gcd(10, 5))

Merge Strings Alternately

Python
1 year ago
# https://leetcode.com/problems/merge-strings-alternately/description/ # You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string. # Return the merged string. # Example 1: # Input: word1 = "abc", word2 = "pqr" # Output: "apbqcr" # Explanation: The merged string will be merged as so:

Subtract common whitespace

Python
1 year ago
def subtract_common_whitespace(s): left = 0 while left < len(s) and s[left].isspace(): left += 1 right = len(s) - 1 while right >= 0 and s[right].isspace(): right -= 1 ws_left = 0

Casting objects to arrays

PHP
1 year ago
<?php class Point { protected $x; protected $y; public function __construct($x, $y) { $this->x = $x; $this->y = $y; }

URL encoding key value pairs

Python
1 year ago
import urllib.parse params = { 'key1': 'value1', 'key2': 'value2', 'key3': 'value with spaces', 'key4': 'value&with&special&characters' } encoded_params = urllib.parse.urlencode(params)

Go sample for parsing

Go
1 year ago
package main import "fmt" import ( `math/rand` "time" ) var rng = rand.New(rand.NewSource(time.Now().UnixNano()))

Comparison between webcrypto and crypto APIs for HMAC calculation in Node

NodeJS
1 year ago
const { webcrypto, createHmac } = require('crypto') async function main() { const data = 'The quick brown fox jumps over the lazy dog.' const secret = 'VerySecretKey' const hmac = createHmac('sha256', secret).update(data).digest().toString('hex') console.log(hmac) const hmacSha256 = { name: 'HMAC', hash: 'SHA-256' }

JSON deserialization of embedded structs

Go
1 year ago
package main import ( "fmt" "encoding/json" ) type Base struct { A int `json:"a"` }

scanf() example

C
1 year ago
#include <stdio.h> int main() { int x; scanf("%d", &x); printf("you entered: %d\n", x); return 0; }

input() example

Python
1 year ago
x = input() print('you entered:', x) y = input() print('then you have entered:', y)

Panic inside defer

Go
1 year ago
package main import "fmt" func main() { f() } func f() { defer func() {

Cancelling fractions

Python
1 year ago
def gcd(a, b): while b != 0: a, b = b, a % b return a def cancel_frac(a, b): while True: g = gcd(a, b) if g == 1: break

URL join helper

Deno
2 years ago
type stringOrRecord = string | Record<string, string> export function url (path: string, ...args: stringOrRecord[]): string { const parts = [path] let i = 0 while (i < args.length && typeof args[i] === 'string') { parts.push(args[i] as string) i++ }

strings.LastIndex in Go

Go
2 years ago
package main import ( "fmt" "strings" ) func main() { str := "hello, world!" idx := strings.LastIndex(str, ",")