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 / anagram_count.rb
Created June 11, 2018 16:46
Counting number of anagram found in a sentence - Ruby Solution
require 'byebug'
class AnagramCountChecker
def initialize(str)
@str = str
end
def sort_word(word)
word.chars.sort_by { |ch| ch.ord }.join('')
@neerajkumar
neerajkumar / anagram.rb
Created June 11, 2018 16:44
Hackerrank test - Counting the number ways an anagram possible - Ruby Solution
=begin
Two words are anagrams of one another if their letters can be rearranged to form the other word.
In this challenge, you will be given a string. You must split it into two contiguous substrings, then determine the minimum number of characters to change to make the two substrings into anagrams of one another.
For example, given the string 'abccde', you would break it into two parts: 'abc' and 'cde'. Note that all letters have been used, the substrings are contiguous and their lengths are equal. Now you can change 'a' and 'b' in the first substring to 'd' and 'e' to have 'dec' and 'cde' which are anagrams. Two changes were necessary.
Input Format
The first line will contain an integer, q, the number of test cases.
Each test case will contain a string s which will be concatenation of both the strings described above in the problem.
@neerajkumar
neerajkumar / es6_inheritence.js
Created June 11, 2018 16:11
ES6 Inheritence - Javascript Solution
// The MIT License (MIT)
// Copyright (c) 2016 Training 4 Developers, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
@neerajkumar
neerajkumar / es5_inheritence_example.js
Last active June 11, 2018 16:10
ES5 Inheritence Example - Javascript Solution
function Vehicle(speed) {
this.speed = speed;
}
function Car(vehicle_type, speed) {
// this._super.call(this, speed);
this.speed = speed;
this.vehicle_type = vehicle_type;
}
@neerajkumar
neerajkumar / singleton_class.js
Created June 11, 2018 10:26
Singleton Class - Javascript Solution
var mySingleton = (function () {
// Instance stores a reference to the Singleton
var instance;
function init() {
// Singleton
// Private methods and variables
function privateMethod(){
console.log( "I am private" );
}
var privateVariable = "Im also private";
@neerajkumar
neerajkumar / compress_string.rb
Created June 9, 2018 17:09
Compressing a string, but without corner cases. - Ruby Solution
## Compressing a string, but without corner cases.
## e.g. aaaaaaaabbbbbbb ==> a8b7
## abcdef => abcdef
def compress_string(str)
count = 1
can_be_compressed = false
compressed_string = ''
i = 1
@neerajkumar
neerajkumar / es5_inheritence.js
Created June 7, 2018 18:35
ES5 Inheritence - Javascript Solution
function Person(first_name, last_name) {
this.first_name = first_name;
this.last_name = last_name;
}
Person.prototype.getFullName = function() {
return this.first_name + ' ' + this.last_name;
}
function Student(studentId, first_name, last_name) {
@neerajkumar
neerajkumar / longest_consecutive_sequence.rb
Last active June 11, 2018 16:27
length of longest consecutive sequence in array of integers
def longest_consecutive_sequence(a)
ans = 0
a.length.times do |i|
unless a.include?(a[i] - 1)
j = a[i]
while(a.include?(j)) do
j += 1
end
@neerajkumar
neerajkumar / factory_pattern.rb
Created June 6, 2018 20:25
Design Pattern: Abstract Factory Pattern - Ruby Solution
# class Duck
# def initialize(name)
# @name = name
# end
# def eat
# puts "Duck #{@name} eats"
# end
@neerajkumar
neerajkumar / decorator_pattern.js
Last active June 6, 2018 16:53
Design Pattern: Decorator Pattern - Javascript Solution
let fs = require('fs');
// ConcreteComponent
class SimpleWriter {
constructor(path) {
this.file_path = path;
}
writeLine(line) {
let self = this