Skip to content

Instantly share code, notes, and snippets.

View Gosilama's full-sized avatar

God'spower Osilama Gosilama

View GitHub Profile
@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 / 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 / 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 / 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 {
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;
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];
@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