Skip to content

Instantly share code, notes, and snippets.

@vncsna
vncsna / bash_strict_mode.md
Created June 6, 2021 01:59 — forked from mohanpedala/bash_strict_mode.md
set -e, -u, -o, -x pipefail explanation

set -e, -u, -o, -x pipefail

The set lines

  • These lines deliberately cause your script to fail. Wait, what? Believe me, this is a good thing.
  • With these settings, certain common errors will cause the script to immediately fail, explicitly and loudly. Otherwise, you can get hidden bugs that are discovered only when they blow up in production.
  • set -euxo pipefail is short for:
set -e
set -u
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
let dominoes = [[1,1],[2,2],[1,1],[1,2],[1,2],[1,1]];
// add tests
suite.add('sort-join', function() {
for (let d of dominoes) {
[...d].sort((a, b) => a - b).join('');
}
})
from collections import deque
def doodledPassword(digits):
n = len(digits)
res = [deque(digits) for _ in range(n)]
deque(map(lambda x: res[x[0]].rotate(-x[0]),[(x,y) for x,y in enumerate(res)]), 0)
return [list(d) for d in res]
@david-hoze
david-hoze / checksum_calculation.md
Last active February 22, 2024 10:33
How to Calculate IP/TCP/UDP Checksum
# Component way of adding redis
# vendor/dry/system/components.rb
Dry::System.register_provider(
:persistence,
boot_path: Pathname(__dir__).join('components').realpath()
)
# vendor/dry/system/components/redis.rb
Dry::System.register_component(:redis, provider: :persistence) do
init do
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
cur = head
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
s = "".join([str(d) for d in digits])
i = int(s)
i = i + 1
s = str(i)
return [int(si) for si in s]
@lbvf50mobile
lbvf50mobile / README.md
Last active July 2, 2019 09:55
Greetings to @Morozzzko, the Ruby is neat.

Hi folks here I show some Ruby magic.

  • a.group_by{|x| x} is similar to a.group_by(&:itself)
  • a.group_by(&:itself) is a short cut for a.group_by(&:itself.to_proc)
  • and a.group_by(&:itself.to_proc) just use new Proc a.group_by(&Proc.new{|o| o.send :itself})
  • a.group_by(&Proc.new{|o| o.send :itself}) use & to treat a Proc as block like this a.group_by{|o| o.send :itself}
  • a.group_by{|o| o.send :itself} here it's possible to substitute o.send :itself to o.itself: a.gruop_by{|o| o.itself}
  • a.group_by{|o| o.itself} here o.itself it's just an o so it's equal to a.group_by{|o| o}
  • Hey and the magic is a.group_by{|o| o} is just a a.group_by{|x| x}
@cdesch
cdesch / rails_generator_cheat_sheet.md
Last active March 30, 2024 09:07
Rails Generator CheatSheet

Cheat Sheets are greate but they are not a substitute for learning the framework and reading the documentation as we most certainly have not covered every potential example here. Please refer to the Rails Command Line Docs for more information.

Command Line Generator Info

Reference

You can get all of this information on the command line.

rails generate with no generator name will output a list of all available generators and some information about global options. rails generate GENERATOR --help will list the options that can be passed to the specified generator.

@flaviocopes
flaviocopes / check-substring-starts-with.go
Last active April 16, 2023 03:02
Go: check if a string starts with a substring #golang
package main
import (
"strings"
)
func main() {
strings.HasPrefix("foobar", "foo") // true
}