S

@supriyo_biswas

Sort A and B list

Python
5 months ago
items = [('A', 1000), ('B', 2000), ('B', 1500), ('A', 1700), ('B', 1000), ('A', 2000)] items.sort(key=lambda x: int(x[0] != 'A') * 1e5 + x[1]) print(items)

simhash

Python
5 months ago
import hashlib def simhash(x: str) -> int: vec = [0] * 128 for i in range(len(x) - 2): shingle = x[i : i + 3] h = int(hashlib.md5(shingle.encode()).hexdigest(), 16) for j in range(128): if h & (1 << j):

3Sum

Python
6 months ago
class Solution: def threeSum(self, nums: list[int]) -> list[list[int]]: result = [] nums.sort() for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: continue

Remove duplicates from sorted array

Python
6 months ago
class Solution: def removeDuplicates(self, nums: list[int]) -> int: if len(nums) <= 1: return len(nums) l = 1 for r in range(1, len(nums)): if nums[r] != nums[r - 1]: nums[l] = num

First missing positive

Python
6 months ago
import sys, json class Solution: def firstMissingPositive(self, nums: list[int]) -> int: nums.sort() missing = 1 i = 0 while i < len(nums): if nums[i] <= 0: i += 1

Longest consecutive sequence

Python
6 months ago
import sys, json class Solution: def longestConsecutive(self, nums: list[int]) -> int: if len(nums) == 0: return 0 nums.sort() results = [0 for _ in range(len(nums))]

Remove element

Python
6 months ago
import sys, json def remove_element(nums: list[int], val: int) -> int: l, r = 0, len(nums) - 1 n = 0 while l <= r: if nums[r] == val: r -= 1 n += 1 elif nums[l] != val:

Traits, inheritance and constants

PHP
1 year ago
<?php // my comment class Foo { protected const value = 10; public function __construct() { echo "Foo", PHP_EOL; }

Longest peak

Python
1 year ago
def longestPeak(arr): i = 0 max_peak_length = 0 while i < len(arr) - 2: j = i while arr[j + 1] > arr[j] and j < len(arr): j += 1 if j > i: k = j while arr[k + 1] < arr[k] and k

Smallest difference

Python
1 year ago
# Write a function that takes in two non-empty arrays of integers, finds the pair # of numbers (one from each array) whose absolute difference is closest to zero, # and returns an array containing these two numbers, with the number from the # fir

Selection sort

Python
1 year ago
# The array will conceptually consist of two parts, a subarray # at the beginning representing a sorted list, and the remainder # which is unsorted. Initially, the entire array is considered # unsorted (this is in contrast to insertion sort). Then,

Insertion sort

Python
1 year ago
# We consider that there is a temporary sorted sublist starting # with the first element and expanding on every pass. We start # from arr[0..1], swapping the last element until it is in the # proper position. Then we consider arr[0..2], arr[0..3] an

Bubble sort

Python
1 year ago
# Move through the entire array swapping elements if they are out of order. # Because of the swaps throughout the entire array, it is ensured that one # pass of these swaps will result in the last index containing the largest # element. # So, in th

Find three largest numbers

Python
1 year ago
# Write a function that takes in an array of at least three integers and, without # sorting the input array, returns a sorted array of the three largest integers # in the input array. # The function should return duplicate integers if necessary;

Tournament winner

Python
1 year ago
# Given an array of pairs representing the teams that have competed # against each other and an array containing the results of each # competition, write a function that returns the winner of the # tournament. The input arrays are named competiti

Left join of users and posts

SQL
1 year ago
PRAGMA foreign_keys = ON; CREATE TABLE users ( id INTEGER PRIMARY KEY NOT NULL, username STRING UNIQUE NOT NULL, disabled INTEGER NOT NULL DEFAULT 0, banned INTEGER NOT NULL DEFAULT 0 ); CREATE TABLE posts (

Struct equality

Go
1 year ago
package main import ( "fmt" "time" ) type SyncState string type SyncRequest struct {

scanf example with floating point inputs

C
1 year ago
#include <stdio.h> int main() { float num; printf("Insira um número:\n"); scanf("%f", &num); printf("A soma é %.2f\n", num + num); return(0);

Listing network interfaces and addresses in Go

Go
1 year ago
package main import ( "fmt" "net" "log" ) func main() { ifaces, err := net.Interfaces()

Prototypal inheritance

NodeJS
1 year ago
function Parent(name) { this.name = name; } Parent.prototype.sayHello = function () { console.log(`I'm ${this.name}!`) } function Child(name, age) { Parent.call(this, name);