Skip to content

Instantly share code, notes, and snippets.

package main
import (
"fmt"
"strings"
)
func main() {
word := "elephant"
@amitk
amitk / remove_duplicates.js
Last active April 13, 2020 17:31
remove duplicate elements from an array in javascript
arr = [5, 1, 3, 1, 4, 4, 5]
// using Set
/* Set is a new data structure introduced in ES6 which contains only unique elements */
unique_arr = [...new Set(arr)]
// expanding the process
a_set = new Set(arr) // {5, 1, 3, 4}
unique_arr = Array.from(a_set) //[5, 1, 3, 4]
// using filter
@amitk
amitk / async-foreach.js
Created April 26, 2020 06:00 — forked from Atinux/async-foreach.js
JavaScript: async/await with forEach()
const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
const start = async () => {
await asyncForEach([1, 2, 3], async (num) => {
await waitFor(50)
@amitk
amitk / database.yml
Created May 10, 2020 04:21
connect to aws database from local environment in rails using database.yml
default: &default
adapter: postgresql
database: bs_development
host: localhost
encoding: unicode
pool: 5
stage: &stage-reactui
adapter: postgresql
database: bs_react_demo_apis_staging
@amitk
amitk / linked_list.cpp
Created May 13, 2020 03:17
A simple linked list in c++
#include<bits/stdc++.h>
using namespace std;
//Define structure of linked list. i.e each block in linked list
struct Node {
int data;
Node* link;
};
/* head tail
@amitk
amitk / linked_list.go
Created May 13, 2020 12:20
A simple linked list in go
package main
import (
"fmt"
)
type Node struct {
data int
next *Node
}
@amitk
amitk / prime.sql
Created May 22, 2020 13:13
function to check whether a number is prime or not
-- 1 is treated as prime as per this function.
create function is_prime(num integer) returns boolean as $$
begin -- begin block;
for i in 2..(num/2) loop
if num % i = 0 then
return false; -- return false if it's get divided by any number smaller than half of it or half itself.
end if;
end loop;
return true;
@amitk
amitk / install_docker_on_ubuntu
Created July 11, 2020 14:05
A way to install docker on a ubuntu system
# Uninstall Docker
sudo apt-get remove docker docker-engine docker-ce docker.io
# Update the apt package index
sudo apt-get update
# Allow apt to use a repository over HTTPS
sudo apt-get install \
apt-transport-https \
ca-certificates \
@amitk
amitk / .bash_profile
Created October 22, 2020 12:11
Add branch name to you terminal mac/Linux
# in you home direactory in the bash profile to display branch name in terminal add parsing script
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
@amitk
amitk / database.yml
Created November 11, 2020 11:01
sample file for database.yml of a typical rails application
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg