Skip to content

Instantly share code, notes, and snippets.

View neerajkumar's full-sized avatar
💭
I may be slow to respond.

Neeraj Kumar neerajkumar

💭
I may be slow to respond.
View GitHub Profile
@neerajkumar
neerajkumar / decorator_pattern.rb
Created June 6, 2018 14:22
Design Pattern: Decorator Pattern - Ruby Solution
## ConcreteComponent
class SimpleWriter
def initialize(path)
@file = File.open(path, 'w')
end
def write_line(line)
@file.print(line)
@file.print('\n')
@neerajkumar
neerajkumar / closure_counter.js
Created June 4, 2018 20:57
Javascript Closure Counter Solution
var add = (function () {
let counter = 0;
return function() {
return counter += 1;
};
})();
console.log(add());
console.log(add());
@neerajkumar
neerajkumar / quick_sort.rb
Last active June 6, 2018 20:57
Quick Sort - Ruby Solution
## Divide and Conquer Method
def partition(a, begining, ending)
pivot, pIndex = a[ending], begining
i = begining
while(i < ending) do
if a[i] <= pivot
## Swap a[i] and a[pIndex] with each other
a[i], a[pIndex] = a[pIndex], a[i]
@neerajkumar
neerajkumar / merge_sort.js
Created June 4, 2018 09:22
Merge Sort - JavaScript Solution
function merge(left, right, a) {
let i = 0;
let j = 0;
let k = 0;
while(i < left.length && j < right.length) {
if(left[i] <= right[j]) {
a[k] = left[i];
i++;
} else {
@neerajkumar
neerajkumar / merge_sort.rb
Created June 4, 2018 07:53
Merge Sort - Ruby Solution
def merge(left, right, a)
i, j, k = 0, 0, 0
while(i < left.length && j < right.length) do
if left[i] <= right[j]
a[k] = left[i]
i += 1
else
a[k] = right[j]
j += 1
@neerajkumar
neerajkumar / super_stack.rb
Last active June 2, 2018 11:48
Hackerrank Super stack problem - ruby solution
# -*- coding: utf-8 -*-
=begin
You are given an empty stack. Your task is to perform following three operations:
push a: Push an integer a, to the top of the stack
pop: Pop the top element from the stack. It is guaranteed that stack is not empty, when performing the pop operation.
inc x d: Add d to bottom x elements of the stack.
After each operation, print the top element of the stack, if after an operation, the stack becomes empty, then print EMPTY.
Input Format:
The first line of the input is n, total number of operations performed on the stack.
@neerajkumar
neerajkumar / superStack.cpp
Last active June 1, 2018 16:10
Hackerrank test for implementation of super stack
/*
You are given an empty stack. Your task is to perform following three operations:
push a: Push an integer a, to the top of the stack
pop: Pop the top element from the stack. It is guaranteed that stack is not empty, when performing the pop operation.
inc x d: Add d to bottom x elements of the stack.
After each operation, print the top element of the stack, if after an operation, the stack becomes empty, then print EMPTY.
Input Format:
The first line of the input is n, total number of operations performed on the stack.
Each of the next n lines is one of the three operations listed above.
@neerajkumar
neerajkumar / ProgrammerString.java
Created May 28, 2018 10:32
Hackerrank Test: Counting programmer string in progxrammerrxproxgrammer, xprogxrmaxemrppprmmograeiruu and programmerprogrammer
public class Solution4 {
static int solve(String str) {
String programmer = "programmer";
String head = programmer;
int i = 0;
// programmer
for (; i < str.length(); i++) {
@neerajkumar
neerajkumar / programmer-string.rb
Last active May 28, 2018 10:29
Hackerrank Test: Counting programmer string in progxrammerrxproxgrammer, xprogxrmaxemrppprmmograeiruu and programmerprogrammer
class String
def maulin! n
slice! n
self
end
def maulin n
dup.maulin! n
end
end
@neerajkumar
neerajkumar / slack-uploader.rb
Created December 15, 2017 05:56
A ruby script to upload any file on any slack channel.
require 'rest-client'
require 'json'
require 'byebug'
def upload
options = {
# Slack channel webhook integration token
token: '123456789-xxxaaaddffggbbb',
file: File.new("error_hotels.csv", 'rb'),
filename: "error_hotels.csv",