Skip to main content

Posts

draw a 4*4 matrix in typescript and exchange two column values based on row index

working link https://stackblitz.com/edit/typescript-omoaq4 code => //draw a matrix and exchange two column values in the matrix based on given row index class matrix { static rm =[]; static rc =[]; static drawMatrix ( r : number , c : number ) { //n2 var k = 0 ; for ( let i = 0 , k = 0 ; i < r ; i ++) { this . rc =[]; for ( let j = 0 ; j < c ; j ++, k ++) { this . rc . push ( String . fromCharCode ( 64 +( k + 1 ))); } this . rm . push ( this . rc ); } } static exchangeRowData ( rowIndex , col1 , col2 ) { let currentRow = this . rm [ rowIndex ]; let temp = currentRow [ this . getIndex ( currentRow , col1 )]; let index = this . getIndex ( currentRow , col2 ); currentRow [ this . getIndex ( currentRow , col1 )]= currentRow [ index ]; currentRow [ index ]= t...

how to get min and max from array and return sum of all elements except min and max in typescript

working link https://stackblitz.com/edit/typescript-ewrnkd code=> //get minimum and maximum and then class Algo { static solve ( arr : Array < number >) { let max = 0 ; let min = 0 ; max = arr [ 0 ]; min = arr [ 0 ]; for ( var i = 0 ; i < arr . length ; i ++) { if ( arr [ i + 1 ]> max ) { max = arr [ i + 1 ] } if ( arr [ i + 1 ]< min ) { min = arr [ i + 1 ] } } return { "minSum" : this . sumWithMaxMin ( arr , max ), "maxSum" : this . sumWithMaxMin ( arr , min )} } static sumWithMaxMin ( arr , n ): number { let sum = 0 ; for ( let i = 0 ; i < arr . length ; i ++) { if ( arr [ i ]!= n ) { sum += arr [ i ]; } } return sum ; } } var o = Algo . solve ([ 1 , 2 , 3 , 4 , 5 ]); console . log ( o . max...

classic merge short in typescript

working example https://stackblitz.com/edit/typescript-lchwhz Please remember this is not the best way to implement merge short in javascript but its a classic way using without any inbuilt API method. more detail about merge sort is=> https://www.youtube.com/watch?v=TzeBrDU-JaY //merge sort in typescript using classic way you can easliy reduce this method to sorter one depends on your requirement class sortAlgo { static sort ( myArray : Array < number >) { //left bound let left = 0 ; //right bound just one less than total array length let right = myArray . length - 1 ; //divide array in to small part this . divideArray ( myArray , left , right ); } static divideArray ( myArray : Array < number >, left : number , right : number ) { //recursive call util left < right if ( left < right ) { //find mid to preventing stack overflow right-1 ...

quicksort in typescript a quick example how to implement quicksort

working link https://stackblitz.com/edit/typescript-vqzrsa //quicksort implementation in typescript //full detail please check https://www.youtube.com/watch?v=COk73cpQbFQ class Quicksort { //pass arry static sort ( arry : Array < number >) { //get start index let start = 0 ; //get end index which is one less tahn total length let end = arry . length - 1 ; //pass arry with start and end index to the function quickSort this . quickSort ( arry , start , end ); } static quickSort ( arry : Array < number >, start : number , end : number ) { //do sorting using divide and conquer if ( start < end ) { //get a pivot index let pindex = this . getPivotIndex ( arry , start , end ); this . quickSort ( arry , start , pindex - 1 ); this . quickSort ( arry , pindex + 1 , end ); } } //swap element static swap ( arry : Array < number >, start : number , index : number ) { let temp = arry [ s...

Insertion Sort in javascript with working link

https://stackblitz.com/edit/js-bbrsjn //insertionSort function insertionSort ( arry ) { var total = arry . length ; var value , index ; for ( var i = 1 ; i < total ; i ++) { value = arry [ i ];    index = i ; while ( index > 0 && value < arry [ index - 1 ]) { arry [ index ]= arry [ index - 1 ]; index = index - 1 ; } arry [ index ]= value ; } return arry ; } var arry =[ 90 , 222 , 998 , 117 , 2 ]; insertionSort ( arry ); console . log ( arry );

small example how to implement bubble short in javascript with time complexity O(n*n)

working example https://stackblitz.com/edit/js-faixur code :- function swap ( myArray , to , from ) { var temp = myArray [ to ]; myArray [ to ]= myArray [ from ]; myArray [ from ]= temp ; } function BubbleSort ( arr ) { for ( let i = 0 ; i < arr . length ; i ++) { for ( let x = 0 ; x < arr . length - 1 ; x ++) { if ( arr [ x ]< arr [ x + 1 ]) { swap ( myArray ,( x ), x + 1 ); } } } } var myArray =[]; for ( var i = 0 ; i < 20 ; i ++) { myArray . push ( Math . round ( Math . random ()* 1000 )); } console . time (); BubbleSort ( myArray ); console . log ( myArray ); console . timeEnd ();

how to solve Fibonacci, prime and composite number and factorial using a single static class in typescirpt.

working link ------------- https://stackblitz.com/edit/typescript-be1art these are some frequently asked question such as prime numbers, factorial and Fibonacci series for an interview purpose. I am trying to solve these question using a single static class in typescript. here the whole code of my static class where I am using a single class to solve these problem's class MathProblem { //Fibonacci series-------------------- static Fibonacci ( max ) { let a : number = 0 ; let b : number = 1 ; let f : number = 1 ; let series =[]; for ( let i = 0 ; i <= max ; i ++) { if ( i === 0 ) { f = 0 ; } else if ( i === 1 ) { f = 1 ; } else { f = a + b ; a = b ; b = f ; } series . push ( f ); } return series ; } //factorial----------------...

generate unique random number between a range using static class in typescript

I am creating a simple class which includes all four methods for different type of random occurrence and based on random range its generate a unique set of the random numbers between ranges. working link=> https://stackblitz.com/edit/typescript-qrmqc4 code=> enum randomType { includeMinMax = 0 , exculdeMin = 1 , exculdeMax = 2 , exculdeMinMax = 3 } //get random number between two range class seedRandom { static maxOccur = 100 ; //min and max include public static getRandomNumber ( min : number , max : number ) { return Math . round ( Math . random ()*( max - min )+ min ); } //if you don't wanna include max value or max exclude public static getRandomNumberNotMax ( min : number , max : number ) { return Math . round ( Math . random ()*( max -( min + 1 ))+ min ); } //if you don't wanna include min value or min exclude public static getRandomNumberNotMin ( min : number , max : number ) { return...

create a custom alert box with multiple layout options in objective c

// //   EGviewAlert.h-------------------------- #import <UIKit/UIKit.h> @interface EGviewAlert : UIView //alert view with heading message and ok button typedef void (^closeAction)( id ); typedef void (^cancelAction)( id ); -( void )Alert:( NSString *)heading withMsg:( NSString *)message withOkBtn:( NSString *)okBtnMsg closeAction:( void (^)( id ))completeBlock; //alert view with heading message and ok button and cancel button -( void )Alert:( NSString *)heading withMsg:( NSString *)message withOkBtn:( NSString *)okBtnMsg withCancelBtn:( NSString *)cancelBtnMsg closeAction:( void (^)( id ))completeBlock cancelAction:( void (^)( id ))cancelAction; //for custom layout -( void )customAlert:( NSDictionary *)messages   closeAction:( void (^)( id ))completeBlock cancelAction:( void (^)( id ))cancelAction; //heading view @property ( nonatomic , strong ) UIView *HeadingView; //heading text @property ( nonatomic , ...