Monday, 10 April 2023

Reverse of Vovels in Given String:-

Reverse of Vovels in Given String:-


Input: 'Question'

Output: 'Qoisteun' 


 vovelRev(str) {
        let arr = [];
        let strArr = str.split('');
        strArr.forEach((ch, i) => {
            if (ch === 'A' || ch === 'E' || ch === 'I' || ch === 'O' || ch === 'U' || ch === 'a' || ch === 'e' || ch === 'i' || ch === 'o' || ch === 'u')
                arr.push(i)
        });
        let k = arr.length - 1;
        for (let i = 0; i < arr.length / 2; i++) {
            let l = strArr[arr[i]];
            strArr[arr[i]] = strArr[arr[k]];
            strArr[arr[k]] = l;
            k--;
        }
        console.log(strArr.join(''));
    }

Thursday, 22 April 2021

Left Rotation Array

   Left Rotation:

left rotation operation on an array shifts each of the array's elements  unit to the left. For example, if  left rotations are performed on array , then the array would become . Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.

Given an array  of  integers and a number, , perform  left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

Function Description

Complete the function rotLeft in the editor below.

rotLeft has the following parameter(s):

  • int a[n]: the array to rotate
  • int d: the number of rotations

Returns

  • int a'[n]: the rotated array

Input Format

The first line contains two space-separated integers  and , the size of  and the number of left rotations.
The second line contains  space-separated integers, each an .

Constraints

Sample Input

5 4
1 2 3 4 5

Sample Output

5 1 2 3 4

Explanation

When we perform  left rotations, the array undergoes the following sequence of changes:

function rotLeft(a, d) {
    for(let i=0;i<d;i++){
        let lastElement=a[a.length-1];
        for(let j=0;j<a.length-1;j++){
            let updatedIndex=(j+a.length-1)%(a.length);
            a[updatedIndex]=a[j];
        }
         a[a.length-2]=lastElement;
    }
    return a;
}

   //OR

function rotLeft(a, d) {
    if(d==a.length)
        return a;
    else if(d>a.length)
        d=d%a.length;

      let newArr= a.splice(d,a.length)
     newArr= newArr.concat(a);
    return newArr;
}

 

Wednesday, 21 April 2021

Interview Programs


        //input user_name_Km outout userNameKm
    function getVariableName(inp) {
        let output = '';
        if (inp.indexOf('_') != -1) {
            var splitArr = inp.split("_");
            let i = 1;
            if (splitArr[0] === '') {
                i = 2;
                output += splitArr[1].toLowerCase();
            }
            else
                output += splitArr[0].toLowerCase();
            for (let j = ij < splitArr.lengthj++) {
                console.log(splitArr[j], splitArr[j].slice(1splitArr[j].length));
                output += splitArr[j][0].toUpperCase() + splitArr[j].slice(1splitArr[j].length);
            }
            return console.log(inpoutput);
        }
        for (let i = 0i < inp.lengthi++) {
            console.log(inp[i].charCodeAt(), inp[i]);
            if (inp[i].charCodeAt() <= 90)
                output += '_' + inp[i].toUpperCase();
            else output += inp[i];
        }
        console.log(inpoutput);

    }
    getVariableName("user_name_Km");
    getVariableName("_Nname_Km");
    getVariableName("Nname_jm_U");
    getVariableName("useUameKm");
    getVariableName("userName");
    getVariableName("YnameJmU");

    //Binary to Decimal
    var binary = "1101000";
    binaryToDecimal(binary)
    var digit = parseInt(binary2);
    console.log(digit);
    function binaryToDecimal(string) {
        let decimal = 0;
        let bits = 1;
        for(let i = string.length-1i >=0i--) {
            let currNum = (string[i]);
            console.log(currNum,"currNum")
            if(currNum === "1") {
                decimal += bits;
            }
            
            bits *= 2;
            console.log(bits,"bits",decimal)
        }
        console.log(decimal);
    }

Wednesday, 7 November 2018

Interview Programs

// N number of people are standing in a circle and 1st have knife, he kill 2nd then
3rd have knife, he kill 4th and so on then who will safe last


var a = 100;
var arr = [];
for (i = 1i <= ai++) {
    arr.push(i);
}
var i = 1;
while (arr.length > 1) {
    if (i > arr.length)
        i = 1;
    else if (i == arr.length)
        i = 0;
     //console.log(i,"lll",arr[i]);
    arr.splice(i1)
    i = i + 1;
}
console.log(arr[0]);

//Given a number N. The task is to find the largest factor of that number which is a perfect square.
function isNumberPerfectSquare(num) {
i = 1;
square = i * i;
while (num > square) {
i++;
square = i * i;
}
if (num == square)
return true;
return false;
}


function largestPerfectSquare(num, reminder, diviser, factor) {
if (isNumberPerfectSquare(factor)) {
return factor;
}
else {
while (reminder > 0) {
diviser++;
reminder = num % diviser;
}
return largestPerfectSquare(num, 1, diviser, num / diviser)
}
}
console.log("Largest Perfect square of 420 is:", largestPerfectSquare(420, 1, 1, 420));

Tuesday, 24 April 2018

Sorting

1. Selection sort:

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right. This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2), where n is the number of items.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>

<p>Click the button to sort the array in ascending order.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;   

function myFunction() {//Bubble sort works by repeatedly swapping the adjecent  elements if they are in wrong order.
let len=points.length;
for(let i=0; i<len-1; i++){
        for(let j=0; j<len-i-1; j++){
        if(points[j+1]<points[j]){
            let temp=points[j];
                points[j]=points[j+1];
                points[j+1]=temp;
            }
          }
      }   
    //points.sort(function(a, b){return a - b});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>

For Edit You can Use below Link:-

https://jsfiddle.net/OmPrakashPandey/Lpw43xyt/



2. Bubble Sort

Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items

We observe in algorithm that Bubble Sort compares each pair of array element unless the whole array is completely sorted in an ascending order. This may cause a few complexity issues like what if the array needs no more swapping as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any swap has happened or not. If no swap has occurred, i.e. the array requires no more processing to be sorted, it will come out of the loop.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>

<p>Click the button to sort the array in ascending order.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points; 

function myFunction() {//Bubble sort works by repeatedly swapping the adjecent  elements if they are in wrong order.
let len=points.length;
for(let i=0; i<len; i++){
        for(let j=0; j<len-i; j++){
        if(points[j+1]<points[j]){
            let temp=points[j];
                points[j]=points[j+1];
                points[j+1]=temp;
            }
          }
      }   
    //points.sort(function(a, b){return a - b});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>




For Edit You can Use below Link:-

https://jsfiddle.net/OmPrakashPandey/cfgrt2of//



Wednesday, 14 March 2018

Create filter Pipe in Angular5

dfAngular Pipe:-

A pipe takes in data as input and transforms it to a desired output. In this page, you'll use pipes to transform a component's Array of object data into filterd Array order (ascending /descending).

Create pipe using cli

ng g pipe filter-data

write below code in filter-data.pipe.ts file


import { Pipe, PipeTransform } from '@angular/core';
import { retry } from 'rxjs/operator/retry';

@Pipe({
name: 'filterData'
})
export class FilterDataPipe implements PipeTransform {

transform(items: any[], f: any, key?: string, keyF?: string): any[] {
if (items && Array.isArray(items)) {
if (Array.isArray(f)) {
let allFilter = [];
if (keyF) f.forEach((obj) => { allFilter.push(obj[keyF]); });
else allFilter = f;

if (key) {
return items.filter((it) => {
return allFilter.includes(it[key])
});
}
else {
return items.filter((it) => {
return allFilter.includes(it)
})
}
}
else {
if (key) {
return items.filter((it) => {
return it[key] === f
});
}
else {
return items.filter((it) => {
return it === f
})
}
}
}
return [];
}
}

Write below code where you want to sort data(In component html)


  1. Filter in Array of string:- e.g. var arr=['om', 'prakash', 'pandey']
<div class="mdl-cell mdl-cell--4-col">
<mat-form-field class="mat-form-field-custom">
<input matInput placeholder="Search by Audit Name" [(ngModel)]="searchText">
<mat-icon matSuffix>search</mat-icon>
</mat-form-field>
</div>

<div class=" mdl-cell mdl-cell--3-col mdl-cell--8-col-tablet
mdl-cell--12-col-phone matrix-names"
*ngFor="let name of (arr | filterData : searchText)">{{ name }}
</div>
  1. Filter in Array Of Objects:-  e.g. var userList=[{name:'om'}, {name: 'prakash'} , {name: 'pandey'}]
<div class="mdl-cell mdl-cell--4-col">
<mat-form-field class="mat-form-field-custom">
<input matInput placeholder="Search by Audit Name" [(ngModel)]="searchText">
<mat-icon matSuffix>search</mat-icon>
</mat-form-field>
</div>

<div class=" mdl-cell mdl-cell--3-col mdl-cell--8-col-tablet
mdl-cell--12-col-phone matrix-names"
*ngFor="let user of (userList | filterData : searchText : 'name')">{{ user.name }}
</div>
  1. Filter in Array Of Objects:-  e.g. var userList=[{name:'om'}, {name: 'prakash'} , {name: 'pandey'}] and input filter is object then in last parameter define the KEY by which you want to search
  2. Filter in Array Of Objects:-  e.g. var userList=[{name:'om'}, {name: 'prakash'} , {name: 'pandey'}] and input filter is also an array of object(for multi select dropdown) then second parameter is an Array of Object andn last parameter defined the KEY by which you want to search........