Ad

Write a function that returns the factorial of a number. As a quick refresher, a factorial of a number is the result of that number multiplied by the number before it, and the number before that number, and so on, until you reach 1. The factorial of 1 is just 1.

Sample:

factorial(5); // 5 * 4 * 3 * 2 * 1 === 120

function factorial(num){
  if(num === 1){
    return 1;
  }
  
  return num * factorial(num - 1)
}

Write a function called power which takes in a base and an exponent. If the exponent is 0, return 1.

Sample:

console.log(power(2, 4)); // 16
console.log(power(2, 3)); // 8
console.log(power(2, 2)); // 4 
console.log(power(2, 1)); // 2
console.log(power(2, 0)); // 1
function power(num,exp){
  if(exp === 0){
    return 1;
  }
  if(exp % 2 === 0){
    let half = Math.floor(exp/2)
    return power(num, half) * power(num, half);
  }else{
    let half = Math.floor((exp-1)/2)
    return num * power(num, half) * power(num, half);
  }
  
}

Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5…. The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on.
Find the number on the n-th position of the sequence.
Input: n = 3
Output: 2

Input: n = 55
Output:10

function solution(num){
  let times = 1;
  let count = 0;
  while(num > 0){
    count = num
    num -= times;
    times++;
  }
  return count;
}

Programming challenge description:
The example sequence
011212201220200112 ... is constructed as
follows:

  1. The first element in the sequence is 0.
  2. For each iteration, repeat the following
    action: take a copy of the entire current
    sequence, replace O with 1, 1 with 2, and 2
    with 0, and place it at the end of the
    current sequence. E.g.
    0 ≥ 01 > 0112 > 01121220 > ...
    Create an algorithm which determines what
    number is at the Nth position in the
    sequence (using -based indexing).
    Input:
    Your program should read lines from
    standard input. Each line contains an
    integer N such that O <= N <=
    3000000000.

Output:
Print out the number which is at the Nth
position in the sequence.
Test 1
Test Input: 5
Test Output: 2

Test 2
Test Input: 101
Test Output: 1

Test 3
Test Input: 25684
Test Output: 0

function solution(num){
  if(num === 0) return 0
  let count = 1;
  while(num >1){
    count++;
    let tmp = Math.round(Math.log2(num))
    num -= Math.pow(tmp,2)
    
  }
  
  if(Math.round(count % 3) === 1){
    return 1
  }
  
  if(Math.round(count % 3) === 2){
    return 2
  }
  
  if(Math.round(count % 3) === 0){
    return 0
  }
}

// let test1 = findNth(0)
// console.log(test1)//0

// let test2 = findNth(5)
// console.log(test2)//2 

// let test3 = findNth(101)
// console.log(test3)//1 

// let test4 = findNth(25684)
// console.log(test4)//0

Royal Morse Code
The Royal British Kingdom has enforced a new decree in the society that all princes and princesses must use a new
form of Morse code for all communications.
We know that Morse Code uses dots (.) and dashes (;) to communicate meaning to the receiver. The following rules
are stated according to this decree:

Any message must begin with a dot (.)
Any message must end with a dash (-)
Every dot (.) must have a corresponding dash (-) after it to close it

If a message follows all the mentioned rules, then it is considered compliant with the Royal Decree and the messa
allowed to be sent. Otherwise, it is considered as defaulted.
The Great Prince of Britain has sent out a set of N messages of the new Morse code. You have to figure out the
number of messages which are compliant and ultimately sent forward.
Input Specification:
input1: N, the number of messages.
input2: Sequence of strings, each representing a new message in the set.
Output Specification:
The number of messages compliant to the new decree
Example 1:
input1: 1
input2: ("..---.-")

Output: 0
Explanation:
The given string starts with a dot(.) and ends with a dash (-), but it fails the third criteria as there are 5 dots but only 4
dashes. Thus, O messages are compliant.
Example 2:
input1: 2
input2: (".-.-.-.-","...---.-")
Output: 2
Explanation:
Both the given strings follow all 3 rules. Thus, both 2 messages are compliant.

import java.util.*;
class MorseCodeChecker{
public int decreeCompliant(int input1, String[] input2){
  int result = 0;
		
		for(int i = 0 ; i < input2.length; i++){
			Deque<Character> stack = new ArrayDeque<>();
			if(input2[i] == null || input2[i].length() == 0){
				continue;
			}
			String cur = input2[i];
			
      if(cur.charAt(0) != '.' && cur.charAt(cur.length()-1) != '-'){
					break;
				}
         int j = 0;
			while(j < cur.length()){
				
				if((cur.charAt(j) == '.')){

					while(j < cur.length() && cur.charAt(j) == '.'){
						j++;
						stack.push('.');
					}
				}
				else{
					if(stack.size() == 0){
						break;
					}
					stack.pop();
					j++;
				}
			}
			if(j == cur.length() && stack.size() == 0){
				result++;
			}
		}
		return result;
	}
  }