Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Last active February 22, 2023 17:31
Show Gist options
  • Save rajivnarayana/b12105107756d5410aa345176dccbf87 to your computer and use it in GitHub Desktop.
Save rajivnarayana/b12105107756d5410aa345176dccbf87 to your computer and use it in GitHub Desktop.
Problems
/**
* Write a program to encode a given number in roman numeral form
// I - 1
// V - 5
// X - 10
// L - 50
// C - 100
// D - 500
// M - 1000
@param {*} number
function encode(number) {
//Write your code here.
//return "XV";
}
// encode(247) === "CCXLVII";
// encode(298) === "CCXCVIII";
// encode(1000) === "M";
// encode(900) === "CM";
// encode(55) === "LV";
// encode(3999) === "MMMCMXCIX";
*/

Problem description

A road of infinite length has been painted many times. [2,4] means it has been painted from 2 meters to 4 meters [5.5,6.75] means it has been painted from 5.5 meters to 6.75 and so on.

Given a list of painted pairs, figure out how much of the road has been painted.

  1. [[2,4],[5.5,6.75]] => 3.25
  2. [[2,4],[3,5]] => 3 (the part [3,4] has been painted twice and should not be considered twice)
function getTotalPaintedLength(paints) {
    //Write your code here
    return 0;
}

Test cases

  1. expect(getTotalPaintedLength([[2,4],[5.5,6.75]])).toBe(3.25)
  2. expect(getTotalPaintedLength([[2,4],[3,5]])).toBe(3)
  3. expect(getTotalPaintedLength([[2,4],[5.5,6.75]])).toBe(3.25);
  4. expect(getTotalPaintedLength([[2,4],[3,5]])).toBe(3);
  5. expect(getTotalPaintedLength([[2,4],[3,5],[4,7]])).toBe(5);
  6. expect(getTotalPaintedLength([[2,4],[3,5],[6.75,8]])).toBe(4.25);
  7. expect(getTotalPaintedLength([[7,10],[15,20],[24,27]])).toBe(11);
  8. expect(getTotalPaintedLength([[4, 7], [1, 5], [3, 10]])).toBe(9);
  9. expect(getTotalPaintedLength([[4,15],[7,12],[15,18],[28,36]])).toBe(22);
  10. expect(getTotalPaintedLength([[3,7],[7,12],[9,18],[100,104]])).toBe(19);
  11. expect(getTotalPaintedLength([[8,9],[17,19],[23,37],[28,36]])).toBe(17);
  12. expect(getTotalPaintedLength([[0,2],[1,3],[2,4]])).toBe(4);
  13. expect(getTotalPaintedLength([[0,3],[0,2],[1,2.5]])).toBe(3);

Write a program that prints all permutations of a given number.

  1. Ex: 123
123
132
213
231
312
321
  1. Ex: 1443
1443
1434
1344
4143
4134
4413
4431
4314
4341
3144
3414
3441

Write the function number_cardinality that takes an integer and returns a string, as follows:

  1. Return the string zero if the number ends in a 0
  2. Return the string five if the number ends in a 5
  3. Return the string even if the number is even (divisible by 2) AND does not end with a 0
  4. Return the string odd if the number is odd (not divisible by 2) AND does not end with a 5

Make sure your return string is exactly as above, using lowercase letters.

Examples:

  • Input: 100 Output: zero
  • Input: 88 Output: even
  • Input: 155 Output: five
  • Input: 99 Output: odd

Solution:

export function number_cardinality( my_number ) {
	 //Insert your code here 
}
@m7mdessa
Copy link

export function number_cardinality( my_number ) {
if (my_number % 10 === 0) {
return "zero";
}
else if (my_number % 10 === 5) {
return "five";
}
else if (my_number % 2 === 0) {
return "even";
}
else {
return "odd";
}
}
console.log(number_cardinality(100)); // "zero"
console.log(number_cardinality(88)); // "even"
console.log(number_cardinality(155)); // "five"
console.log(number_cardinality(99)); // "odd"

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