S

@supriyo_biswas

Sort A and B list

Python
3 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
4 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): vec[j] += 1

3Sum

Python
4 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 j, k = i + 1, len(nums) - 1

Remove duplicates from sorted array

Python
4 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] = nums[r] l += 1

First missing positive

Python
4 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
4 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
4 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 < len(arr):

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 # first array in the first position. # Note that the absolute difference of two integers is the distance between them # on the real number line. For example, the absolute difference of -5 and 5 is # 10, and the absolute difference of -5 and -4 is 1. #

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, in each # pass, the minimum element is chosen and swapped to the index i # where i = 0..len(arr)-2. def selectionSort(arr): for i in range(len(arr) - 1): min_idx = i for j in range(i + 1, len(arr)):

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] and so # on until we reach arr[0..(n-1)], at which point the entire # array is sorted def insertionSort(arr): for i in range(1, len(arr)): for j in range(i, 0, -1):

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 the next passes, we exclude the last element from these swaps. # If there are no swaps that have taken place in this pass, it means the # entire array is sorted and we can break early. def bubbleSort(arr): for j in range(len(arr) - 1, -1, -1):

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; for example, it # should return [10, 10, 12] for an input array of [10, 5, 9, 10, 12]. # Sample Input # array = [141, 1, 17, -7, -17, -27, 18, 541, 8, 7, 7] # Sample Output

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 competitions and results, # respectively. The competitions array has elements in the form of # [homeTeam, awayTeam], where each team is a string of at most 30 # characters representing the name of the team. The results array # contains information about t

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);