Skip to content

Instantly share code, notes, and snippets.

View leo-bianchi's full-sized avatar

Leo Bianchi leo-bianchi

  • Alpar Service
  • São Paulo, SP
View GitHub Profile
@leo-bianchi
leo-bianchi / agenda.py
Created October 27, 2020 18:02
I don't wanna cry alone right now
lista = []
agenda = {}
def listAll():
print('----- AGENDA -----')
for i in lista:
for chave, valor in i.items():
print(chave.title(), valor)
print('----- FIM -----')
@leo-bianchi
leo-bianchi / diagonal-difference.js
Last active April 5, 2020 05:14
Diagonal Difference (HackerRank)
// Without reduce
function diagonalDifference(arr) {
let sum = 0;
for (let i = 0; i <= arr.length - 1; i++) {
sum += arr[i][i] - arr[i].reverse()[i];
}
return Math.abs(sum);
}
// With reduce
function stockAndCount(n, arr) {
let pairs = 0; // pairs number
const colors = arr.reduce((acc, val) => {
acc[val] ? (acc[val] += 1) : (acc[val] = 1);
return acc;
}, {}); // return an object with colors (numbers) as keys and quantity as value
Object.keys(colors).forEach((n) => { // for each key in colors
let _pair = parseInt(colors[n] / 2); // looks for even numbers
if (_pair >= 1) pairs += _pair; // total
@leo-bianchi
leo-bianchi / simple-array-sum.js
Created April 5, 2020 01:36
Simple Array Sum (HackerRank)
// Without Reduce
function simpleArraySum(ar) {
let total = 0;
for(let elem in ar) {
total += ar[elem]
}
return total;
}
// With Reduce
@leo-bianchi
leo-bianchi / original.js
Last active December 7, 2019 22:24
Refatorando condições
console.time('test');
const saldosQtd = [
{
qtd: 1
},
{
qtd: 10
}
];
@leo-bianchi
leo-bianchi / verifyMultiRowDuplicates.js
Created November 28, 2019 20:16
Servicenow catalog item onChange client script to verify duplicates inside multi-row variable set
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '' || newValue == 'false') {
return;
}
var multiRowSysId = 'sys_id';
var checkBoxField = 'os_horarios_forao_totalmente_preenchidos';
// Mensagem de erro
@leo-bianchi
leo-bianchi / consolelog.js
Created November 28, 2019 20:04
Js file with good practices tips.
// DEBUG
// Printing objects on console
const foo = {name: 'tom', age: 30};
const bar = {name: 'harry', age: 50};
const baz = {name: 'john', age: 19};
console.log({ foo, bar, baz }) // To show objects names on console.
// Styling with css
console.logo('%c My friends', 'color: orange; font-weight: bold');
// Console table
@leo-bianchi
leo-bianchi / index.js
Last active November 21, 2019 17:34
Javascript ES6 (ES0215) .map(), .reduce(), .filter() usage examples.
// FILTER
/*
If i already have an array but i only want to have items in the array that match certain criteria, use the filter.
Quando eu já tenho um array e quero apenas itens no novo array que correspondem a uma condição, use filter.
*/
// GET JUST EVEN NUMBERS
let arr = [-200, -163, -26, -4, 0, 7, 76];
@leo-bianchi
leo-bianchi / arrayToObject.js
Last active November 4, 2019 18:32
Constructs an object from a array, using even indexes as key and odd indexes as value
/**
* Constructs an object from a array, using even indexes as key and odd indixes as value
*
* @param {Array} myArray - Array to be transformed
* @returns {Object}
*/
function toObject(myArray) {
let r = {};
for (let i = 0; i < myArray.length; i += 2) {
let key = (myArray[i]),
@leo-bianchi
leo-bianchi / fixStr.js
Created November 4, 2019 17:36
Javascript function to remove accents, broken characters, special characters and blank spaces.
/**
* Normalize strings to a custom pattern, tempplate
*
* @param {string} myStr - String to be normalized
* @returns {string} Normalized string
*/
function fixStr(myStr) {
return myStr.normalize('NFD')
.replace(/\uFFFD/g, '')
.replace(/[\u0300-\u036f]/g, '')