Dutch national flag problem in Javascript

Image
Dutch national flag problem and solution in Javascript Problem statement:   The Dutch national flag (DNF) problem is one of the most popular programming problems proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange balls of white, red, and blue such that balls of the same color are placed together. Now, let's consider an array with 3 distinct values say 0, 1 and 2. We won't be using any sort method and we need to sort this array in 0(n). Input Array :  let   arr  = [ 0 ,  2 ,  1 ,  0 ,  1 ,  2 ,  0 ,  2 ]; Expected Output: [ 0, 0, 0, 1, 1, 2, 2, 2 ] Solution Approach : When we see expected output, we can clearly see that sorted array is divided into 3 sections having values 0 , 1 and 2. So, let's divide the array in 3 sections: a) from 0th index to left boundary b) from left boundary to right boundary c) from right boundary to last index. Now we will create 2 pointers : left (starting from 0

Javascript Codewars Problem: "Simple Pig Latin"

Codewars Problem and solution with approach explained 

 
 

Objective: 

In this challenge, we will solve the “Simple Pig Latin” Codewars puzzle using javascript. 

Problem statement: 

Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched. 

Examples: 

Input : pigIt('Pig latin is cool'); 

Output : igPay atinlay siay oolcay


Input : pigIt('Hello world !');  

OutputelloHay orldway !


Input : pigIt('Pig latin is cool !'); 

Output : igPay atinlay siay oolcay !


Solution Approach: 

Step 1: capture each word of the string. Also check if there are no special characters. In case of special character that needs to be returned without any changes. 

Step 2 (a): if no special character, then for each word, get the first letter and remove it from the word and add it behind the word with the string "ay". 

Step 2 (b): repeat step 2 for each word and then return the final string.

Step 3 : if special characters found, skip the word and return it without any transformation. 


Hint: we will use regex to find out if the word contains any special character.  

Keeping these  points in mind, let’s jump to the solution now: 

  

  

Solution  : 


function pigIt(str){

  var inputArr = str.split(' ');

  var finalElement='';

  inputArr.forEach((element, index, inputArr) => {

      var regex = /[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/g;

      if (!regex.test(element)){

      var elementArr = element.split('');

      var firstElement = elementArr.shift();

    if((index === inputArr.length - 1))

      finalElement= finalElement+elementArr.join("")+firstElement+"ay";

    else

    finalElement= finalElement+elementArr.join("")+firstElement+"ay "

    

        }

    else{

       finalElement= finalElement+element;

    }

     

    

    }

  )

  return finalElement;

}

Comments

Post a Comment

Popular posts from this blog

Ice Cream Parlor : Hackerrank Problem and Solution

Disemvowel Trolls || Codewars problem and solution in Javascript || Topic : Strings and RegEx

Descending Order || CodeWars Problem and solution in javascript.