Skip to content

Instantly share code, notes, and snippets.

@salmanx
salmanx / postgres-cheatsheet.md
Created May 2, 2017 12:23 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

If run with -E flag, it will describe the underlaying queries of the \ commands (cool for learning!).

Most \d commands support additional param of __schema__.name__ and accept wildcards like *.*

@salmanx
salmanx / flatten_array.rb
Created October 3, 2017 10:24
A simple ruby method for array flatten
class FlattenArray
def self.flattify(array,init=[])
array.each do |a|
if a.class==Array
flattify(a,init)
else
init << a
end
end
init
# Sami’s spaceship crashed on Mars! She sends sequential SOS messages to Earth for help.
# Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal
# received by Earth as a string, S , determine how many letters of Sami’s SOS have been changed by radiation.
# Input Format
# There is one line of input: a single string, S .
# Note: As the original message is just SOS repeated n times, S ’s length will be a multiple of 3 .
# Constraints
# 1 ≤ |S| ≤ 99
@salmanx
salmanx / caesar_cipher.rb
Last active November 18, 2017 18:57
Simple ruby program for caesar cipher sollution
# Julius Caesar protected his confidential information by encrypting it in a cipher. Caesar’s cipher rotated every letter
# in a string by a fixed number, K , making it unreadable by his enemies. Given a string, S , and a number, K ,
# encrypt S and print the resulting string.
# Note: The cipher only encrypts letters; symbols, such as − , remain unencrypted.
# Input Format
# The first line contains an integer, N , which is the length of the unencrypted string.
# The second line contains the unencrypted string, S .
# The third line contains the integer encryption key, K , which is the number of letters to rotate.
# Constraints
# 1 ≤ N ≤ 100
@salmanx
salmanx / dropdown.directive.ts
Last active January 2, 2018 06:03
simple angular directive for dropdown menu in navbar
import { Directive, HostBinding, HostListener } from '@angular/core';
@Directive({
selector: '[rbDropdown]'
})
export class DropdownDirective {
private isOpen = false;
@HostBinding('class.open') get opened(){
return this.isOpen;
@salmanx
salmanx / auto-grow.directive.ts
Last active November 23, 2017 07:37
A simple angular directive to expand input field
import { Directive, ElementRef, Renderer } from '@angular/core';
@Directive({
selector: '[autoGrow]',
host: {
'(focus)' : 'onFocus()',
'(blur)' : 'onBlur()'
}
})
export class AutoGrowDirective {
@salmanx
salmanx / seed_db.js
Created November 22, 2017 07:24
seeding data in mongo db client
require('dotenv').config();
const MongoClient = require('mongodb').MongoClient;
const bcrypt = require('bcrypt');
const users = require('./users');
function seedCollection(collectionName, initialRecords){
MongoClient.connect(process.env.DB_CONN, (err, db) => {
console.log("connected to mongo db");
@salmanx
salmanx / excerpt.pipe.ts
Created November 23, 2017 07:42
Custom pipe for showing summary text in angular
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: "excerpt"
})
export class ExcerptPipe implements PipeTransform{
transform(text: string, limit?: number){
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/playground", { useNewUrlParser: true })
.then(() => console.log("Mongo db is connected!"))
.catch((err) => console.error("Could not connect to db ...", err));
const courseSchema = new mongoose.Schema({
name: { type: String, required: true },
author: { type: String, required: true, minlength: 3, maxlength: 255 },
category: {
@salmanx
salmanx / process.stdout.js
Created October 15, 2018 07:14
snippet for standard input output example for node js
const questions = [
"What is your name?",
"What is fav hobby?",
"What is your fav programming language?"
];
const answers = [];
function ask(i){
process.stdout.write(`\n\n ${questions[i]} \n\n`);