Skip to content

Instantly share code, notes, and snippets.

View hassan-maavan's full-sized avatar
😏
what else?

Hassan Raza hassan-maavan

😏
what else?
View GitHub Profile
@hassan-maavan
hassan-maavan / clean.sh
Created October 16, 2020 09:39 — forked from Iman/clean.sh
Free up disk space on Ubuntu - clean log, cache, archive packages/apt archives, orphaned packages, old kernel and remove the trash
#!/bin/sh
#Check the Drive Space Used by Cached Files
du -sh /var/cache/apt/archives
#Clean all the log file
#for logs in `find /var/log -type f`; do > $logs; done
logs=`find /var/log -type f`
for i in $logs
@hassan-maavan
hassan-maavan / Diagonals sum
Created September 21, 2020 14:01
Create a function that receives a (square) matrix and calculates the sum of both diagonals (main and secondary)
// [
// [ 1, 2, 3 ],
// [ 4, 5, 6 ],
// [ 7, 8, 9 ]
// ]
function sum(matrix) {
return matrix.reduce((sum, value, index) => sum + value[index] + value[value.length - index -1], 0);
}
@hassan-maavan
hassan-maavan / find element that appers even number of times.
Created September 19, 2020 14:14
Write a function that finds the first element that appers an even number of times in an unsorted array.
Write a function that finds the first element that appers an even number of times in an unsorted array.
// input: [5,3,5,1,5,1,3]
// output: 3
let arr = [5,3,5,1,5,1,3];
let value = '';
for(let i = 0; i < arr.length; i++){
let val = arr[i];
if(arr.filter((v) => v === val).length % 2 == 0){
value = val;
@hassan-maavan
hassan-maavan / Divide into two parts
Created September 19, 2020 13:34
Given an array nof integers, divide the array into two parts so that the sum of integers in one part is the same as the sum in other part.
// input [1,2,4,2,7,2]
//output [1,2,4,2] == 9
[7,2] == 9
const sum = (array) => array.reduce((total, val) => total + val);
const arr = [1,2,4,2,7,2];
const arraySum = sum(arr) / 2;
let arr1 = [], arr2 = [];
for(i =0; i < arr.length; i++) {
if(sum([...arr1, arr[i]]) <= arraySum)
@hassan-maavan
hassan-maavan / Sum of Digits - Digital Root
Created September 19, 2020 07:52
Digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers. https://www.codewars.com/kata/541c8630095125aba6000c00
function digital_root(n) {
let values = convertIntToArray(n);
let sum = 0;
sum = values.reduce(doSum);
if((convertIntToArray(n)).length > 1) {
return digital_root(sum);
} else {
return sum;
}
@hassan-maavan
hassan-maavan / Two to One
Created September 19, 2020 06:52
Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters, each taken only once - coming from s1 or s2.
Example:
// a = "xyaabbbccccdefww"
// b = "xxxxyyyyabklmopq"
// longest(a, b) -> "abcdefklmopqwxy"
// a = "abcdefghijklmnopqrstuvwxyz"
// longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"
function longest(s1, s2) {
@hassan-maavan
hassan-maavan / Stop gninnipS My sdroW!
Created September 19, 2020 06:50
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present. Examples: spinWords( "Hey fellow warriors" ) => r…
function spinWords(word){
return (word.split(' ').map(reverseWord)).join(' ');
}
function reverseWord(word) {
if((word.split('')).length > 4) {
return (word.split('').reverse()).join('');
} else {
return word;
}
}
@hassan-maavan
hassan-maavan / Equal Sides Of An Array
Created September 19, 2020 06:50
You are going to be given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1. https://www.codewars.com/kata/5679aa472b8f57fb8c000047
function findEvenIndex(arr)
{
let i;
for(i = 0; i < arr.length; i++){
let slic1 = arr.slice(0,i).reduce((num1, num2) => num1 + num2, 0);
let slic2 = arr.slice((i + 1), arr.length).reduce((num1, num2) => num1 + num2, 0);
if(slic1 == slic2){
return i;
}
}
@hassan-maavan
hassan-maavan / Take a Ten Minute Walk
Created September 19, 2020 06:48
You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter string…
function isValidWalk(walk) {
var dx = 0
var dy = 0
var dt = walk.length
for (var i = 0; i < walk.length; i++) {
switch (walk[i]) {
case 'n': dy--; break
case 's': dy++; break
case 'w': dx--; break
@hassan-maavan
hassan-maavan / List Filtering
Created September 19, 2020 06:48
In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. https://www.codewars.com/kata/53dbd5315a3c69eed20002dd
function filter_list(l) {
return l.filter(a=>Number.isInteger(a));
}