Skip to content

Instantly share code, notes, and snippets.

View Gosilama's full-sized avatar

God'spower Osilama Gosilama

View GitHub Profile
@Gosilama
Gosilama / LeftRotation.py
Created September 24, 2020 23:14
Perform left rotations on a given array
# A left rotation operation on an array shifts each of the array's elements unit to the left.
# For Example: if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2].
# Given an array of a integers and a number, d, perform d left rotations on the array.
#Return the updated array to be printed as a single line of space-separated integers.
def rotLeft(a, d):
aLen = len(a)
if (aLen == d) : return a
const arrayManipulation = (n, queries) => {
const zeroArr = [];
let max = 0;
for (let i = 0; i < n; i++) {
zeroArr.push(0);
}
queries.forEach((query) => {
zeroArr[query[0] - 1] += query[2];
const repeatedString = (s, n) => {
let strLen = s.length;
if (strLen === 1 && s === strToCheck) return n;
if (strLen === 1 && s != strToCheck) return 0;
const rem = n % strLen;
const quotient = Math.floor(n/strLen);
let count = 0;
@Gosilama
Gosilama / JumpingOnClouds.java
Created July 11, 2020 08:01
Solution to the Hackerrank cloud jumping problem
/**
* Emma is playing a new mobile game that starts with consecutively numbered clouds.
* Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2.
* She must avoid the thunderheads.
* Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud. It is always possible to win the game.
* For each game, Emma will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided.
* For example, c = [0, 1, 0, 0, 0, 1, 0] indexed from 0...6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5.
* She could follow the following two paths: 0->2->4->6 or 0->2->3->4->6. The first path takes 3 jumps while the second takes 4.
*/
public class JumpingOnClouds {
@Gosilama
Gosilama / CountingValleys.java
Last active July 11, 2020 07:49
Count the number of valleys a hiker walked through
/**
* Given a hiker's sequence of up and down steps during his last hike, find and print the number of valleys he walked through.
* For example, if the hiker's path is s = [DDUUUUDD], he first enters a valley units deep. Then he climbs out an up onto a mountain units high. Finally, he returns to sea level and ends his hike.
*/
public class CountingValleys {
static int countingValleys(int n, String s) {
char[] chars = s.toCharArray();
int sum = 0;
int valleys = 0;
int prevSum;
@Gosilama
Gosilama / numberToString2.js
Created June 11, 2020 15:52
Write a function that converts number to text, Passing an int number should return array of text number eg 23498643 => [two, three, four, nine, eight, six, four, three] 40263645 => [four, zero, two, six, three, six, four, four, five] You5:43 PM Update the function to group the numbers by 2s 23498643 => [twenty-three, fourty-nine, eighty-six, fou…
const ds2 = {
unit: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
tens: ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
};
const isUnit = (num) => num % 10 === parseInt(num);
const isTeen = (num) => num[0] === '1' && num.length > 1;
const isTens = (num) => num[0] > 1 && num.length > 1;
const getNumStrArr2 = (num) => {
const parts = num.toString().match(/.{1,2}/g);
const resultArr = [];
@Gosilama
Gosilama / numberToString.js
Created June 11, 2020 15:50
Write a function that converts number to text, Passing an int number should return array of text number eg 23498643 => [two, three, four, nine, eight, six, four, three] 40263645 => [four, zero, two, six, three, six, four, four, five]
const ds = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const getNumStringArr = (num) => {
const numArr = num.toString().split('');
const resultArr = [];
for (let i = 0; i < numArr.length; i++) {
const m = parseInt(numArr[i]);
resultArr.push(ds[m]);
}
return resultArr;
}
@Gosilama
Gosilama / absDiagonalSum.java
Last active May 7, 2020 11:17
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
public static int diagonalDifference(List<List<Integer>> arr) {
int primarySum = 0;
int secondarySum = 0;
for (int i = 0; i < arr.size(); i++) {
primarySum += arr.get(i).get(j);
secondarySum += arr.get(i).get(arr.size() - j - 1);
}
return Math.abs(primarySum - secondarySum);
@Gosilama
Gosilama / findFirstMissingPositiveInteger.js
Last active May 1, 2020 20:30
Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well. For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
const findFirstMissingPositiveInteger = input => {
const sortedInput = input.sort((a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
const nthInput = input.length - 1;
if (sortedInput[nthInput] <= 0) return 1;
@Gosilama
Gosilama / serializeDeserializeBinaryTree.js
Last active May 1, 2020 21:14
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right The following test sh…
/**
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree.
For example, given the following Node class
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right