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........



Create Sorting Pipe in Angular5

Angular 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 sorting order(ascending/descending).

Create pipe using cli

ng g pipe order-by

write below code in order-by.pipe.ts file


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

@Pipe({
name: 'orderBy'
})
export class OrderByPipe implements PipeTransform {
transform(items: any[], key?: string, order?: string, typeOf?: string): any[] {

if (items && Array.isArray(items)) {

let asc = (order != 'dsc') ? -1 : 1, c, d;//c,d update only value not reffrence
return items.sort(function (a, b) {
if (key)
c = a[key], d = b[key];
else
c = a, d = b;

if (typeOf === 'date') {
c = typeof (c) === 'string' ? new Date(c) : c;
d = typeof (d) === 'string' ? new Date(d) : d;
}
else if (typeOf === 'number') {
c = parseFloat(c);
d = parseFloat(d);
}
return (c > d) ? (asc * -1) : (c < d ? asc : 0);
});
}
return [];
}
}


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


<div *ngFor="let detail of auditSiteData | orderBy:'audit_date':'asc':'date'">
{{detail.audit_date }}
</div>