2020-08-26

JavaScript - Return the string between the first found set of curly brackets

let regExp = /\{([^}]+)\}/;

let matches = regExp.exec('Now show {this}, and not (that).');

console.log(matches[1]);

JavaScript - Return the string between the first found set of parentheses

let regExp = /\(([^)]+)\)/;

let matches = regExp.exec('Show (this) and not (that).');

console.log(matches[1]);


MacOS - How to forcibly remove a file or folder and all its contents recursively

 sudo rm –R /path/to/file/or/folder


2020-08-14

Python - Ignore the first copy of both the lowest and the highest value in a list

l = [1,2,3,4,4,5,5,5,6,1,6]

duplicates = list(set([

    x for x in l 

    if 

        l.count(x) > 1 

        and (

            x == max(l) 

            or x == min(l) 

        )

]))

for d in duplicates:

l.remove(d)

print(l)


Python - Ignore the first copy/duplicate of the highest value in a list

l = [1,2,3,4,4,5,5,5,6,1,6]

duplicates = list(set(

    [

        

        for x in l 

        if l.count(x) > 1 

            and x == max(l)

    ]

))

for d in duplicates:

l.remove(d)

print(l)

Python - Remove only the first instance of each duplicate value in a list

l = [1,2,3,4,4,5,5,5,6,1]

duplicates = list(set([x for x in l if l.count(x) > 1]))

for d in duplicates:

l.remove(d)

print(l)


Python - Remove duplicates from list

l = [1,1,2,3,1,1,1,4,5,6,1]

l = dict.fromkeys(l).keys()

print(l)


Python - List detect duplicates that occur only "n" or more times

l = [1,2,3,4,4,5,5,6,1,1,1,1]

n = 3

print(list(set([x for x in l if l.count(x) >= n])))


Python - List detect duplicates (distinct)

l = [1,2,3,4,4,5,5,6,1]

print(set([x for x in l if l.count(x) > 1]))

Python - List removal of a given value as many times as it appears

l = [1,2,3,4,5,1,1,1,2]

print(list(filter(lambda x: x!=1, l)))


Python - Remove list duplicate values

l = [1,1,2,3,1,1,1,4,5,6,1]

print(dict.fromkeys(l).keys())



PostgreSQL - Recursive query with hierarchy levels

 CREATE TABLE public.employees (

id int8 NOT NULL,

"name" varchar(50) NULL,

manager_id int8 NULL

);


insert into public.employees(id, name, manager_id) values(1, 'michelle', null);

insert into public.employees(id, name, manager_id) values(2, 'joy', 1);

insert into public.employees(id, name, manager_id) values(3, 'carlos', 2);

insert into public.employees(id, name, manager_id) values(4, 'jane', 3);

insert into public.employees(id, name, manager_id) values(5, 'miguel', 3);

insert into public.employees(id, name, manager_id) values(6, 'josh', 1);


with recursive subordinates as (

-- Anchored element, the person to get all direct

-- and indirect reports to

select 

a.id, a.name, a.manager_id, cast(null as int8) as super_manager_id

, cast(1 as int8) as level

from 

public.employees as a

where 

a.id = 2

-- Recursive elements, all other employees

union -- all 

select 

e.id, e.name, e.manager_id, s.manager_id as super_manager_id

, s.level + 1 as level

from 

public.employees as e 

inner join subordinates as s

on s.id = e.manager_id 

)

select

from 

subordinates

;


PostgreSQL - Qualify alternative (distinct on)

select distinct on (client_id) o.*

from orders as o

order by client_id 

2020-08-05

MySQL - Delimited text field to rows using the JSON_TABLE function

select
  `t`.`DELIMITED_FIELD_NAME`
  , `csv_row`.`author`
from
  SOURCE_TABLE as t
  
  -- Delimited field to rows
  join JSON_TABLE(
    TRIM(REPLACE(json_array(`t`.`DELIMITED_FIELD_NAME`), ';', '","')),
    '$[*]'
    columns (
      `DELIMITED_FIELD_NAME`
          varchar(50)
          path '$'
    )
  ) as `csv_row`
;