Best JavaScript code snippet using fast-check-monorepo
sort-09-merge-alternate.js
Source:sort-09-merge-alternate.js
1// MERGE SORT (RECURSIVE):2 // divide in half until you have sub-arrays of single elements3 // then merge sorted lists together4 // very useful, easier to understand than others, recursive5 // effective and stable6 // use if you have equivalent elements; it will keep their 7 // original order in array8 // take big list & divide into two half-lists9 // recursively call merge sort on smaller list, which will 10 // in turn do the same11 // base case = list of one12 // on way up recursive calls, you will merge sorted lists together13 // using function walking through both simultaneously & inserts14 // smaller value first, creating bigger sorted list15 16// time complexity: Best O(n log(n)) | Avg O(n log(n)) | Worst O(n log(n))17// space complexity: O(n) because we create new lists as we go18// complete the helper function below to merge 2 sorted arrays 19merge1 = (originalArrayA, originalArrayB) => {20 /*21 procedure merge( var originalArrayA as array, var originalArrayB as array )22 var mergedArray as array23 while ( originalArrayA and originalArrayB have elements )24 if ( originalArrayA[0] > originalArrayB[0] )25 add originalArrayB[0] to the end of mergedArray26 remove originalArrayB[0] from originalArrayB27 else28 add originalArrayA[0] to the end of mergedArray29 remove originalArrayA[0] from originalArrayA30 end while31 while ( originalArrayA has elements )32 add originalArrayA[0] to the end of mergedArray33 remove originalArrayA[0] from originalArrayA34 end while 35 while ( originalArrayB has elements )36 add originalArrayB[0] to the end of mergedArray37 remove originalArrayB[0] from originalArrayB38 end while 39 return mergedArray40 end procedure41 */42 let mergedArray = [];43 while(originalArrayA.length && originalArrayB.length){44 if(originalArrayA[0] <= originalArrayB[0]) mergedArray.push(originalArrayA.shift())45 else mergedArray.push(originalArrayB.shift());46 }47 while(originalArrayA.length) mergedArray.push(originalArrayA.shift());48 49 while(originalArrayB.length) mergedArray.push(originalArrayB.shift());50 return mergedArray51}52// implement the Merge Sort function below USING RECURSION53mergeSort1 = (originalArray) => {54 /*55 procedure mergesort( var a as array )56 if ( n == 1 ) return a57 l1 as array = a[0] ... a[n/2]58 l2 as array = a[n/2+1] ... a[n]59 l1 = mergesort( l1 )60 l2 = mergesort( l2 )61 return merge( l1, l2 )62 end procedure63 */64 const {length:arraySize} = originalArray;65 if (arraySize < 2) return originalArray;66 const midPoint = Math.floor(arraySize/2);67 const leftArray = originalArray.slice(0, midPoint);68 const sortedLeftArray = mergeSort(leftArray);69 const rightArray = originalArray.slice(midPoint, arraySize);70 const sortedRightArray = mergeSort(rightArray);71 return merge(sortedLeftArray, sortedRightArray);72 }73// implement an in-place merge sort algorithm74mergeInPlace1 = (75 originalArray, 76 startPoint=0, 77 midPoint=Math.floor(originalArray.length/2), 78 endPoint=originalArray.length-179) => {80 /*81 procedure merge( var originalArrayA as array, var originalArrayB as array )82 var mergedArray as array83 while ( originalArrayA and originalArrayB have elements )84 if ( originalArrayA[0] > originalArrayB[0] )85 add originalArrayB[0] to the end of mergedArray86 remove originalArrayB[0] from originalArrayB87 else88 add originalArrayA[0] to the end of mergedArray89 remove originalArrayA[0] from originalArrayA90 end while 91 while ( originalArrayA has elements )92 add originalArrayA[0] to the end of mergedArray93 remove originalArrayA[0] from originalArrayA94 end while 95 while ( originalArrayB has elements )96 add originalArrayB[0] to the end of mergedArray97 remove originalArrayB[0] from originalArrayB98 end while 99 return mergedArray100 end procedure101 */102 let midPlus1 = midPoint + 1;103 if (originalArray[midPlus1] >= originalArray[midPoint]) return 104 while (startPoint <= midPoint && endPoint >= midPlus1) {105 if (originalArray[startPoint] <= originalArray[midPlus1]) startPoint += 1106 else {107 currentUpperMidValue = originalArray[midPlus1]108 currentUpperMidIndex = midPlus1109 while (startPoint != currentUpperMidIndex) {110 originalArray[currentUpperMidIndex] = originalArray[currentUpperMidIndex - 1]111 currentUpperMidIndex -= 1112 }113 114 originalArray[startPoint] = currentUpperMidValue115 startPoint += 1116 midPoint += 1117 midPlus1 += 1118 }119 }120 return originalArray121}122mergeSortInPlace1 = (originalArray, leftPoint, rightPoint) => {123 /*124 procedure mergesort( var a as array )125 if ( n == 1 ) return a126 l1 as array = a[0] ... a[n/2]127 l2 as array = a[n/2+1] ... a[n]128 l1 = mergesort( l1 )129 l2 = mergesort( l2 )130 return merge( l1, l2 )131 end procedure132 */133 if (rightPoint > leftPoint) {134 let midPoint = leftPoint + Math.floor((rightPoint - leftPoint) / 2)135 mergeSortInPlace(originalArray, leftPoint, midPoint)136 mergeSortInPlace(originalArray, midPoint + 1, rightPoint)137 mergeInPlace(originalArray, leftPoint, midPoint, rightPoint)138 }139 return originalArray140}141// this function & next from https://www.geeksforgeeks.org/in-place-merge-sort/142function mergeSIP1(arr, start, mid, end)143{144 let start2 = mid + 1;145 146 // If the direct merge is already sorted147 if (arr[mid] <= arr[start2])148 {149 return;150 }151 152 // Two pointers to maintain start153 // of both arrays to merge154 while (start <= mid && start2 <= end)155 {156 157 // If element 1 is in right place158 if (arr[start] <= arr[start2])159 {160 start++;161 }162 else163 {164 let value = arr[start2];165 let index = start2;166 167 // Shift all the elements between element 1168 // element 2, right by 1.169 while (index != start)170 {171 arr[index] = arr[index - 1];172 index--;173 }174 arr[start] = value;175 176 // Update all the pointers177 start++;178 mid++;179 start2++;180 }181 }182}183 184/* l is for left index and r is right index185of the sub-array of arr to be sorted */186function sortInPlace1(arr, l, r)187{188 if (l < r)189 {190 191 // Same as (l + r) / 2, but avoids overflow192 // for large l and r193 let m = l + Math.floor((r - l) / 2);194 195 // Sort first and second halves196 sortInPlace(arr, l, m);197 sortInPlace(arr, m + 1, r);198 199 mergeSIP(arr, l, m, r);200 }201}202merge1 = (originalArrayA, originalArrayB) => {203 let mergedArray = [];204 while (originalArrayA.length && originalArrayB.length) {205 if (originalArrayA[0] <= originalArrayB[0]) mergedArray.push(originalArrayA.shift())206 else mergedArray.push(originalArrayB.shift());207 }208 while (originalArrayA.length) mergedArray.push(originalArrayA.shift());209 while (originalArrayB.length) mergedArray.push(originalArrayB.shift());210 return mergedArray211}212mergeSort1 = (originalArray) => {213 const { length: arraySize } = originalArray;214 if (arraySize < 2) return originalArray;215 const midPoint = Math.floor(arraySize / 2);216 const leftArray = originalArray.slice(0, midPoint);217 const sortedLeftArray = mergeSort(leftArray);218 const rightArray = originalArray.slice(midPoint, arraySize);219 const sortedRightArray = mergeSort(rightArray);220 return merge(sortedLeftArray, sortedRightArray);221}222mergeInPlace1 = (223 originalArray,224 startPoint = 0,225 midPoint = Math.floor(originalArray.length / 2),226 endPoint = originalArray.length - 1227) => {228 let midPlus1 = midPoint + 1;229 if (originalArray[midPlus1] >= originalArray[midPoint]) return;230 while (startPoint <= midPoint && endPoint >= midPlus1) {231 if (originalArray[startPoint] <= originalArray[midPlus1]) startPoint += 1;232 else {233 currentUpperMidValue = originalArray[midPlus1];234 currentUpperMidIndex = midPlus1;235 while (startPoint != currentUpperMidIndex) {236 originalArray[currentUpperMidIndex] = originalArray[currentUpperMidIndex - 1];237 currentUpperMidIndex -= 1;238 }239 originalArray[startPoint] = currentUpperMidValue;240 startPoint += 1;241 midPoint += 1;242 midPlus1 += 1;243 }244 }245 return originalArray;246}247mergeSortInPlace1 = (originalArray, leftPoint, rightPoint) => {248 if (rightPoint > leftPoint) {249 let midPoint = leftPoint + Math.floor((rightPoint - leftPoint) / 2);250 mergeSortInPlace(originalArray, leftPoint, midPoint);251 mergeSortInPlace(originalArray, midPoint + 1, rightPoint);252 mergeInPlace(originalArray, leftPoint, midPoint, rightPoint);253 }254 return originalArray;255}256mergeSIP1 = (arr, start, mid, end) => {257 let start2 = mid + 1;258 if (arr[mid] <= arr[start2]) return;259 while (start <= mid && start2 <= end) {260 if (arr[start] <= arr[start2]) start++;261 else {262 let value = arr[start2];263 let index = start2;264 while (index != start) {265 arr[index] = arr[index - 1];266 index--;267 }268 arr[start] = value;269 start++;270 mid++;271 start2++;272 }273 }274}275sortInPlace1 = (arr, l, r) => {276 if (l < r) {277 let m = l + Math.floor((r - l) / 2);278 sortInPlace(arr, l, m);279 sortInPlace(arr, m + 1, r);280 mergeSIP(arr, l, m, r);281 }282}283merge = (oaa, oab) => {284 let ma = [];285 while (oaa.length && oab.length) {286 if (oaa[0] <= oab[0]) ma.push(oaa.shift())287 else ma.push(oab.shift());288 }289 while (oaa.length) ma.push(oaa.shift());290 while (oab.length) ma.push(oab.shift());291 return ma;292}293mergeSort = (oa) => {294 let asize = oa.length;295 if (asize < 2) return oa;296 const mid = Math.floor(asize / 2);297 const la = oa.slice(0, mid);298 const sla = mergeSort(la);299 const ra = oa.slice(mid, asize);300 const sra = mergeSort(ra);301 return merge(sla, sra);302}303mergeInPlace = (304 oa,305 start = 0,306 mid = Math.floor(oa.length / 2),307 end = oa.length - 1308) => {309 let midp1 = mid + 1;310 if (oa[midp1 >= oa[mid]]) return;311 while (start <= mid && end >= midp1) {312 if (oa[start] <= oa[midp1]) start++313 else {314 cumv = oa[midp1];315 cumi = midp1;316 while (start != cumi) {317 oa[cumi] = oa[cumi - 1];318 cumi--;319 }320 oa[start] = cumv;321 start++;322 mid++;323 midp1++;324 }325 };326 return oa;327}328mergeSortInPlace = (oa, lp, rp) => {329 if (rp > lp) {330 let mid = lp + Math.floor((rp - lp) / 2);331 mergeSortInPlace(oa, lp, mid);332 mergeSortInPlace(oa, mid + 1, rp);333 mergeInPlace(oa, lp, mid, rp);334 }335 return oa;336}337/*338- recursive339merge = (originalArrayA, originalArrayB) => {}340mergeSort = (originalArray) => {}341- iterative342mergeInPlace = (343 originalArray,344 startPoint = 0,345 midPoint = Math.floor(originalArray.length / 2),346 endPoint = originalArray.length - 1347) => {}348mergeSortInPlace = (originalArray, leftPoint, rightPoint) => {}349*/350array = [4, 22, 41, 40, 27, 30, 36, 16, 42, 37, 14, 39, 3, 6, 34, 9, 21, 2, 29, 47]351console.log('--------------------mergeSort (recursion)-------------------------');352console.log(`original array: ${array}`)353var sortedArray = mergeSort(array)354console.log(`sorted array: ${sortedArray}`)355console.log('---------------------mergeSortInPlace (in place)------------------------');356array = [4, 22, 41, 40, 27, 30, 36, 16, 42, 37, 14, 39, 3, 6, 34, 9, 21, 2, 29, 47]357console.log(`original array: ${array}`)358mergeSortInPlace(array, 0, array.length - 1);359console.log(`sorted array: ${sortedArray}`)...
sort-11-tim.js
Source:sort-11-tim.js
1// https://www.geeksforgeeks.org/timsort/2/*3TimSort is a sorting algorithm based on Insertion Sort and Merge Sort.4A stable sorting algorithm works in O(n Log n) time5Used in Javaâs Arrays.sort() as well as Pythonâs sorted() and sort().6First sort small pieces using Insertion Sort, then merges the pieces using 7 merge of merge sort.8We divide the Array into blocks known as Run. 9We sort those runs using insertion sort one by one and then merge those runs 10 using combine function used in merge sort. 11If the size of Array is less than run, then Array get sorted just by using 12 Insertion Sort. 13The size of run may vary from 32 to 64 depending upon the size of the array. 14Note that merge function performs well when sizes subarrays are powers of 2. 15The idea is based on the fact that insertion sort performs well for small arrays.16Recommended: Please try your approach on {IDE} first, before moving on to the solution.17Details of below implementation:18We consider size of run as 32.19We one by one sort pieces of size equal to run20After sorting individual pieces, we merge them one by one. 21We double the size of merged subarrays after every iteration.22*/23// time complexity: Best O(n) | Avg O(n log(n)) | Worst O(n log(n))24// space complexity: O(n)25// Python3 program to perform TimSort. 26RUN = 32 27 28// This function sorts array from left index to 29// to right index which is of size atmost RUN 30insertionSort = (originalArray) => {31 // get array length32 let arrayLength = originalArray.length;33 let currentPosition, currentItem;34 console.log(`original array: ${originalArray}`)35 // loop from 1 since first element is sorted through end of array36 for (let index = 1; index < arrayLength; index++) {37 // set current item as value in array[index]38 currentItem = originalArray[index]39 // set current position as current index40 currentPosition = index41 // while current position is at least 1 and previous item's value is larger 42 // than current item43 while (currentPosition > 0 && originalArray[currentPosition - 1] > currentItem) {44 // set value at current position as previous position45 originalArray[currentPosition] = originalArray[currentPosition -1]46 // set current position as previous position47 currentPosition = currentPosition - 148 }49 // set value of current position as current item50 originalArray[currentPosition] = currentItem51 }52 return originalArray53}54 55// merge function merges the sorted runs 56merge = (originalArray, l, m, r) => {57 58 // original array is broken in two parts 59 // left and right array 60 let len1 = m - l + 161 let len2 = r - m 62 let left = []63 let right = []64 for (let x = 0; x < len1; x++) left.append(originalArray[l + x]) 65 // for (i in range(0, len1)) left.append(originalArray[l + i]) 66 for (let x = 0; x < len2; x++) right.append(originalArray[m + 1 + x]) 67 // for (i in range(0, len2)) right.append(originalArray[m + 1 + i]) 68 69 let i = 070 let j = 071 let k = l 72 // after comparing, we merge those two array 73 // in larger sub array 74 while (i < len1 && j < len2) { 75 76 if (left[i] <= right[j]) {77 originalArray[k] = left[i] 78 i += 1 79 }80 else {81 originalArray[k] = right[j] 82 j += 1 83 }84 k += 185 }86 // copy remaining elements of left, if any 87 while (i < len1) {88 originalArray[k] = left[i] 89 k += 1 90 i += 191 }92 // copy remaining element of right, if any 93 while (j < len2) {94 originalArray[k] = right[j] 95 k += 196 j += 197 }98}99// iterative Timsort function to sort the 100// array[0...n-1] (similar to merge sort) 101timSort = (originalArray, n) => {102 103 // Sort individual subarrays of size RUN 104 for (i in range(0, n, RUN)) insertionSort(originalArray, i, Math.min((i+31), (n-1))) 105 106 // start merging from size RUN (or 32). It will merge 107 // to form size 64, then 128, 256 and so on .... 108 size = RUN 109 while (size < n) {110 111 // pick starting point of left sub array. We 112 // are going to merge originalArray[left..left+size-1] 113 // and originalArray[left+size, left+2*size-1] 114 // After every merge, we increase left by 2*size 115 for (left in range(0, n, 2*size)) {116 117 // find ending point of left sub array 118 // mid+1 is starting point of right sub array 119 mid = left + size - 1 120 right = Math.min((left + 2*size - 1), (n-1)) 121 122 // merge sub array originalArray[left.....mid] & 123 // originalArray[mid+1....right] 124 merge(originalArray, left, mid, right) 125 }126 size = 2*size 127 }128}129// utility function to print the Array 130printArray = (originalArray, n) => {131 for (let x = 0; x < n; x++) console.log(originalArray[x], end = " ") 132 // for (i in range(0, n)) console.log(originalArray[i], end = " ") 133 console.log() 134}135// recreating python range function136// https://stackoverflow.com/questions/8273047/javascript-function-similar-to-python-range137function range(start, stop, step) {138 if (typeof stop == 'undefined') {139 // one param defined140 stop = start;141 start = 0;142 }143 if (typeof step == 'undefined') {144 step = 1;145 }146 if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {147 return [];148 }149 var result = [];150 for (var i = start; step > 0 ? i < stop : i > stop; i += step) {151 result.push(i);152 }153 return result;154};155arr = [5, 21, 7, 23, 19];156n = arr.length;157console.log(`Given Array is:`);158printArray(arr, n)159timSort(arr, n) 160console.log("After Sorting Array is") ...
rotate-array.js
Source:rotate-array.js
1/*function rotateArray(originalArray, rotateNum) {2 let rotated = [];3 if (rotateNum === 0) {4 return originalArray;5 } else if (rotateNum > 0) {6 let begin = originalArray.slice(-rotateNum);7 let end = originalArray.slice(0,(originalArray.length -rotateNum));8 rotated = begin.concat(end);9 } else {10 let begin = originalArray.slice(-1*rotateNum);11 let end = originalArray.slice(0,(-rotateNum));12 rotated = begin.concat(end);13 }14 return rotated;15}16*/17function rotateArray(originalArray, rotateNum) {18 let front = originalArray.slice(-rotateNum);19 let end = originalArray.slice(0, -rotateNum);20 return front.concat(end);...
Using AI Code Generation
1const {originalArray} = require('fast-check');2const {originalArray} = require('fast-check');3const {originalArray} = require('fast-check');4const {originalArray} = require('fast-check');5const {originalArray} = require('fast-check');6const {originalArray} = require('fast-check');
Using AI Code Generation
1var originalArray = require('fast-check').originalArray;2var fc = require('fast-check');3var assert = require('assert');4function test() {5 var result = fc.check(fc.property(fc.array(fc.integer()), function (arr) {6 return arr.length === originalArray(arr).length;7 }), { numRuns: 1000 });8 assert(result === true);9}10test();
Using AI Code Generation
1const fc = require('fast-check');2const { originalArray } = require('fast-check/lib/check/arbitrary/ArrayArbitrary.js');3const myArb = originalArray(fc.integer(), 0, 10);4fc.assert(5 fc.property(myArb, (arr) => {6 return arr.length <= 10;7 })8);9const fc = require('fast-check');10const { originalArray } = require('fast-check/lib/check/arbitrary/ArrayArbitrary.js');11const myArb = originalArray(fc.integer(), 0, 10);12fc.assert(13 fc.property(myArb, (arr) => {14 return arr.length <= 10;15 })16);17const fc = require('fast-check');18const { originalArray } = require('fast-check/lib/check/arbitrary/ArrayArbitrary.js');19const myArb = originalArray(fc.integer(), 0, 10);20fc.assert(21 fc.property(myArb, (arr) => {22 return arr.length <= 10;23 })24);25const fc = require('fast-check');26const { originalArray } = require('fast-check/lib/check/arbitrary/ArrayArbitrary.js');27const myArb = originalArray(fc.integer(), 0, 10);28fc.assert(29 fc.property(myArb, (arr) => {30 return arr.length <= 10;31 })32);33const fc = require('fast-check');34const { originalArray } = require('fast-check/lib/check/arbitrary/ArrayArbitrary.js');35const myArb = originalArray(fc.integer(), 0, 10);36fc.assert(37 fc.property(myArb, (arr) => {38 return arr.length <= 10;39 })40);
Using AI Code Generation
1const fc = require('fast-check');2const { originalArray } = require('fast-check-monorepo');3const myArbitrary = originalArray(4 fc.integer(),5 { minLength: 0, maxLength: 10 }6);7fc.assert(8 fc.property(myArbitrary, (a) => {9 return a.length >= 0 && a.length <= 10;10 })11);12{13 "compilerOptions": {14 }15}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!