Skip to content

Instantly share code, notes, and snippets.

@ShilpiMaurya
Last active September 25, 2020 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShilpiMaurya/3852153e62cbf47007ba2c55fe83b7c4 to your computer and use it in GitHub Desktop.
Save ShilpiMaurya/3852153e62cbf47007ba2c55fe83b7c4 to your computer and use it in GitHub Desktop.
// Problem 1: fizzbuzzChallenge
// write a function that accepts a number and
// 1. returns a string "fizz" if it's divisible by 3,
// 2. returns string "buzz" if divisible by 5,
// 3. returns "fizzbuzz" of divisible by both 3 and 5
// 4. Otherwise returns the input number (if above conditions are not matched)
const fizzbuzzChallenge = num => {
if (typeof num !== "number") {
return null;
}
if (num % 3 === 0 && num % 5 === 0) {
return "fizzbuzz";
} else if (num % 3 === 0) {
return "fizz";
} else if (num % 5 === 0) {
return "buzz";
} else {
return num;
}
};
//Time-complexity: Constant O(1)
//Test fizzbuzzChallenge
console.log(fizzbuzzChallenge(9) === "fizz");
console.log(fizzbuzzChallenge(10) === "buzz");
console.log(fizzbuzzChallenge(15) === "fizzbuzz");
console.log(fizzbuzzChallenge(7) === 7);
console.log(fizzbuzzChallenge(0) === "fizzbuzz");
console.log(fizzbuzzChallenge() === null);
console.log(fizzbuzzChallenge("string") === null);
// Problem 2: reverseChallenge
//write a function that accepts an array and returns another array containing values in reverse order
const reverseChallenge = array => {
const newArray = [];
const m = array.length / 2;
const n = array.length;
for (let i = 0; i < m; i++) {
newArray[i] = array[n - i - 1];
newArray[n - i - 1] = array[i];
}
return newArray;
};
//Time-complexity: O(n-1)
//Test reverseChallenge
const isArrayEqual = (array1, array2) => {
if (array1.length !== array2.length) {
return false;
}
for (let j = 0; j < array1.length; j++) {
if (array1[j] !== array2[j]) {
return false;
} else {
return true;
}
}
};
const isEqual = (a, b) => {
if (Array.isArray(a) && Array.isArray(b)) {
return isArrayEqual(a, b);
} else {
return a === b;
}
};
console.log(isEqual(reverseChallenge([1, 2, 3]), [3, 2, 1]));
console.log(isEqual(reverseChallenge([1]), [1]));
console.log(isEqual(reverseChallenge([0]), [0]));
console.log(isEqual(reverseChallenge(["a", "b", "c"]), ["c", "b", "a"]));
console.log(
isEqual(reverseChallenge(["apple", "banana", "cherry"]), [
"cherry",
"banana",
"apple"
])
);
console.log(isEqual(reverseChallenge(["apple"]), ["apple"]));
@vbkmr
Copy link

vbkmr commented Nov 6, 2019

Problem 3.1:

Write a function which accepts an array as input and returns true if it has no-duplicates otherwise false.

Problem 5:

Write a function which accepts a string as input, the string would contain ' characters, returns true if all the quotes have been closed otherwise false.

test1: input: "asfdsfs'sdfds'sdfsd", output: true
test2: input: "asfdsfs'sdf'ds'sdfsd", output: false

@ShilpiMaurya
Copy link
Author

ShilpiMaurya commented Nov 15, 2019

// Write a function which accepts an array as input and returns true
// if it has no-duplicates otherwise false.

const hasDuplicates = arr => {
  if (!Array.isArray(arr) || !arr.length) {
    return null;
  }
  const count = {};
  for (let i = 0; i < arr.length; i++) {
    const key = arr[i];
    if (count[key]) {
      return true;
    } else {
      count[key] = 1;
    }
  }
  return false;
};

//Time-Complexity: O(n)
//tests

console.assert(hasDuplicates([1, 2, 2, 3, 4]) === true, "test 1");
console.assert(hasDuplicates([1, 2, 2, 3, 3, 4]) === true, "test 2");
console.assert(hasDuplicates([1, 2, 3, 4]) === false, "test 3");
console.assert(hasDuplicates([]) === null, "test 4");
console.assert(hasDuplicates(1, 2, 3) === null, "test 5");

// Write a function which accepts a string as input,
// the string would contain ' characters, returns true
// if all the quotes have been closed otherwise false.
// test1: input: "asfdsfs'sdfds'sdfsd", output: true
// test2: input: "asfdsfs'sdf'ds'sdfsd", output: false

const isAllQuotesClosed = input => {
  if (typeof input !== "string") {
    return null;
  }
  let count = 0;
  for (let i = 0; i < input.length; i++) {
    const str = input[i];
    if (str === "'") {
      count = count + 1;
    }
  }
  if (count % 2 === 0) {
    return true;
  } else {
    return false;
  }
};

//Time-Complexity: O(n)
//tests

console.assert(isAllQuotesClosed("asfdsfs'sdfds'sdfsd") === true, "test 6");
console.assert(isAllQuotesClosed("asfdsfs'sdf'ds'sdfsd") === false, "test 7");
console.assert(isAllQuotesClosed(1234) === null, "test 8");
console.assert(isAllQuotesClosed() === null, "test 9");

@vbkmr
Copy link

vbkmr commented Nov 16, 2019

Programming Challenge 6

Write a function

function solution(A);
that, given an array A consisting of N integers, returns the number of distinct values in array A.

For example, given array A consisting of six elements such that:

A[0] = 2 A[1] = 1 A[2] = 1
A[3] = 2 A[4] = 3 A[5] = 1
the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [0..100,000];
each element of array A is an integer within the range [−1,000,000..1,000,000].

@ShilpiMaurya
Copy link
Author

ShilpiMaurya commented Nov 17, 2019

// Write a function

// function solution(A);
// that, given an array A consisting of N integers,
// returns the number of distinct values in array A.

// For example, given array A consisting of six elements such that:

// A[0] = 2 A[1] = 1 A[2] = 1
// A[3] = 2 A[4] = 3 A[5] = 1
// the function should return 3, because there are 3 distinct values
// appearing in array A, namely 1, 2 and 3.

// Write an efficient algorithm for the following assumptions:

// N is an integer within the range [0..100,000];
// each element of array A is an integer within the range
// [−1,000,000..1,000,000].

const distinctValues = arr => {
  if (!Array.isArray(arr) || !arr.length) {
    return null;
  }
  const count = {};
  const result = [];
  arr.forEach(item => {
    if (count[item]) {
      result.push(item);
    } else {
      count[item] = "s";
    }
  });
  return arr.length - result.length;
};

//Time-complexity: O(n)
// tests

console.assert(distinctValues([1, 2, 3, 4, 56, 76, 53, 53, 76]) === 7);
console.assert(distinctValues([]) === null);
console.assert(distinctValues(1, 2, 3) === null);

// Other method

const distinctValues = arr => {
  if (!Array.isArray(arr) || !arr.length) {
    return null;
  }
  let count = 0;
  let obj = {};
  arr.forEach(item => {
    if (!obj[item]) {
      obj[item] = 1;
      count = count + 1;
    }
  });
  return count;
};

//Time-complexity:O(n)
//tests

console.assert(distinctValues([1, 2, 3, 4, 56, 76, 53, 53, 76]) === 7);
console.assert(distinctValues([]) === null);
console.assert(distinctValues(1, 2, 3) === null);

@vbkmr
Copy link

vbkmr commented Nov 17, 2019

Please refresh your js string manipulation here (or someplace else):
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods

Next challenge:

Programming challenge 7.1

Create a function called fixStart. It should take a single argument, a string, and return a version where all occurences of its first character have been replaced with '*', except for the first character itself. You can assume that the string is at least one character long. For example:

fixStart('babble'): 'ba**le'

Programming challenge 7.2

Create a function called notBad that takes a single argument, a string.

  • It should find the first appearance of the substring 'not' and 'bad'.
  • If the 'bad' follows the 'not', then it should replace the whole 'not'...'bad' substring with 'good' and return the result.
  • If it doesn't find 'not' and 'bad' in the right sequence (or at all), just return the original sentence.
    For example:
  notBad('This dinner is not that bad!'): 'This dinner is good!'
  notBad('This movie is not so bad!'): 'This movie is good!'
  notBad('This dinner is bad!'): 'This dinner is bad!'

Programming challenge 7.3

How to find the maximum occurring character in given String?
If two or more characters have same occurrence then return the first appeared-character in the string.
For example:

solution("Today is Monday"): 'a'

Programming challenge 7.4

How do you count a number of vowels and consonants in a given string?

Programming challenge 7.5

How do you check if a given String contains valid parentheses?
For example:

isValid("this is (properly closed)"): true
isValid("this is) not ((properly closed)"): false
isValid("(())"): true
isValid(")(())"): false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment