Skip to content

Instantly share code, notes, and snippets.

View umair-khanzada's full-sized avatar
💡
Under Development

Umair Ahmed umair-khanzada

💡
Under Development
View GitHub Profile
// Note: The time complexity of this solution is O(n)
function getMaxConsecutiveChar(str) {
let char = preChar = str[0];
let occurrence = count = 1;
for(let i = 1; i < str.length; i++){
if(preChar === str[i]) count++;
if(count > occurrence) {
occurrence = count;
char = preChar;
}
@umair-khanzada
umair-khanzada / capitalizeString.js
Created March 25, 2020 14:01
Capitalize string eg: (Title, Name, etc) and remove unnecessary spaces between the words
function capitalizeString(str = "") {
// Break name into word's chunk.
let words = str.split(' ');
// Removed empty string, null, undefined, etc.
let filteredWords = words.filter(word => word);
// Capatilize words.
let capatilizeWords = filteredWords.map(word => `${word[0].toUpperCase()}${word.slice(1)}`);
// Concatenate words.
return capatilizeWords.join(' ');
}
@umair-khanzada
umair-khanzada / index.js
Created September 15, 2019 16:51
Write a function, `persistence`, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
function persistence(num, count = 0){
return num > 9 ? persistence(eval(`${num}`.split('').join('*')), count+1) : count;
}
persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
// and 4 has only one digit
persistence(999) === 4 // because 9*9*9 = 729, 7*2*9 = 126,
// 1*2*6 = 12, and finally 1*2 = 2
import axios from 'axios';
const API_CLIENT = axios.create({
baseURL: 'base_url/api/v1/'
});
// request interceptor
const REQUEST_INTERCEPTOR = API_CLIENT.interceptors.request.use(function (config) {
// TODO: uncomment Authorization header when token is available.
// config.headers['Authorization'] = `Bearer ${localStorage.getItem('access_token')}`;
@umair-khanzada
umair-khanzada / climbingLeaderboard.js
Created January 14, 2019 12:15
climbingLeaderboard hackerrank
function climbingLeaderboard(scores, alice) {
const uniqueScores = [...new Set(scores)]; //for removing duplicate entries.
let preIndex = uniqueScores.length, //starting position of nested loop.
rank = uniqueScores.length + 1, //last rank default.
aliceRanks = [];
for(let i = 0; i < alice.length; i++){
let s = preIndex;
for(s; s--;){
if(alice[i] < uniqueScores[s]){
@umair-khanzada
umair-khanzada / help-desk comparison table in html
Last active June 26, 2019 20:03
Help-desk's comparison.
<link href='https://fonts.googleapis.com/css?family=Lato:100,300,400,700,900' rel='stylesheet' type='text/css'>
<style>
* {
margin: 0;
padding: 0;
font-family: Lato;
}
body {
padding: 50px 0;
/*background: #e5e2e6;*/
@umair-khanzada
umair-khanzada / palindrome.js
Created August 4, 2018 14:08
A simple efficient and faster way of checking palindrome
function palindrome(str) {
str = str.toLowerCase(); //convert to lowercase
let count = Math.ceil(str.length / 2), //limit for itration
flag = true; //palindrome or not
for(var i = 0; i < count; i++){
//checking 0 and the last index are same or not
//if not same break
if(!(str[i] === str[str.length - i - 1])){
flag = false;
@umair-khanzada
umair-khanzada / blockchain-concepts.txt
Last active August 16, 2018 07:30
Building blocks of DApps
Blockchain (A distributed ledger organized in block)
ethereum (A blockchain application platform)
EVM (EVM is the runtime environment for smart contracts)
solidity (Solidity is contract-oriented, high-level programming language for writing smart contracts which run on Ethereum Virtual Machine.)
smartcontract (A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract.)
remix (Web browser based IDE that allows you to write Solidity smart contracts)
web3.js (Ethereum JavaScript API)
DApps (distributed app)
metamask (MetaMask is a bridge that allows you to visit the distributed app in your browser.
It allows you to run Ethereum dApps right in your browser without running a full Ethereum node.)
@umair-khanzada
umair-khanzada / Nested array aggregation query.
Last active December 27, 2017 06:32
Mongodb query for getting count of chapters in collection.
db.getCollection('your collection').aggregate([
{
"$project": {
chapters: "$books.chapters", _id: "$_id"
}
},
{
"$unwind": "$chapters"
},
{