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 / ArrayFlatten.rb
Last active December 15, 2017 05:53
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
def flatten_array(array)
array.each_with_object([]) do |element, flat|
flat.push *(element.is_a?(Array) ? flatten_array(element) : element)
end
end
@neerajkumar
neerajkumar / 404-checker.rb
Created December 15, 2017 05:51
A ruby code to check the 404 (Not Found) responses from any URL.
require 'byebug'
require 'csv'
require 'open3'
urls = []
## 1.csv is the path your CSV file which contains the URLs to check.
CSV.foreach("1.csv") do |row|
stdout, stderr, status = Open3.capture3("curl #{row.first}")
urls << row.first if stdout == 'Not Found'
end
@neerajkumar
neerajkumar / active_model.rb
Created December 15, 2017 05:52
Performing various kind of validations (as provided into any Rails project) using ruby codes only, outside of rails framework.
require 'active_model'
## includes AttributeMethods, Dirty, Validations, Callbacks, Conversion, Model, Naming, Serialization, Translation
class Person
include ActiveModel::AttributeMethods
attribute_method_prefix 'reset_'
attribute_method_suffix '_highest?'
define_attribute_methods 'age', 'firstname'
@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",
@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 / 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 / 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 / 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 / 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 / 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 {