Skip to content

Instantly share code, notes, and snippets.

@agmezr
agmezr / heroku_postgres.txt
Created June 27, 2018 04:43
Useful commands for heroku and postgres
# A collection of commands I found useful while deploying a postgres database.
# To avoid specifing the app use the commands on the path where your heroku app is located
# To enable postgres add-no
heroku addons:create heroku-postgresql:<PLAN_NAME>
# Create backup
heroku pg:backups:capture
# List backups
@agmezr
agmezr / database.js
Last active June 27, 2018 05:14
An example of a server configuration for using node and postgres on heroku
// Since It's not a good idea to have everything on the server.js I moved the pool creation to this file
// Require the pg module
const { Pool } = require('pg');
// You can either do this or check if we are in production like in server.js
// In this case if the DATABASE_URL is declared we use it to the connection
const { DATABASE_URL } = process.env;
if (DATABASE_URL){
console.log("Using database url");
@agmezr
agmezr / reverseParentheses.py
Last active August 3, 2018 03:32
Reverse the strings contained in each pair of matching parentheses.
"""
Reverse the strings contained in each pair of matching parentheses, starting from the innermost pair.
The results string should not contain any parentheses.
"""
def reverseParentheses(s):
stack = [""]
for letter in s:
if letter == "(":
stack.append("")
@agmezr
agmezr / database.js
Created September 7, 2018 01:20
An example of connecting a postgres database with node using node-postgres
const { Pool } = require('pg');
const { DATABASE_URL } = process.env;
// if the url for the database is in process use that,
// usually when using heroku or similar to deploy
if (DATABASE_URL){
console.log("Using database url");
var pool = new Pool({
connectionString: DATABASE_URL
@agmezr
agmezr / min_distance.py
Created November 29, 2018 21:14
Find the minimum distance or difference between two sorted list on n + m
def get_min_difference(m, n):
"""
A quick function for finding the min difference between two list (already sorted) on n + m.
Because I always forget about it and end up using n * m
"""
# pointer for m
i = 0
# pointer for n
j = 0
@agmezr
agmezr / mysql_cpp.md
Last active February 4, 2020 20:28
Steps to connect to mysql using cpp (Linux)
  1. Install mysqlconn-dev library, example for Debian: apt-get install libmysqlcppconn-dev
  2. Download mysqlconn library
  3. Download the generic tar or install directly for your favorite flavor
  4. Download and install boost. This lib is needed by mysqlconn.
  5. If using generic, uncompress and take note of where is located.
  6. Create a cpp file to test. You can use this example
  7. Compile including the needed libraries. For example if you used used the generic lib, it should be something like this:
g++ -o test -Ipath/to/connlib/include/jdbc -I/path/to/boost test.cpp -lmysqlcppconn
@agmezr
agmezr / humanSize.py
Created September 1, 2020 19:46
Human friendly format without using math libraries
"""
Write a function in Python called ‘humanSize’ that takes a non-negative number
of bytes and returns a string with the equivalent number of ‘kB’, ‘MB’, ‘GB’,
‘TB’, ‘PB’, ‘EB’, ‘ZB’, or ‘YB’,between [0, 1000), with at most 1 digit of
precision after the decimal.
If the number of bytes is >= 1000 YB, return this number of YB, for example 5120 YB.
"""
from collections import OrderedDict
# using ordered dict to make sure it evaluates first the biggest one.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define X_SYMBOL 'X'
#define O_SYMBOL 'O'
#define STALE_SYMBOL 'S'
struct TicTacToe {
char board[3][3];