codeparrot/xlcost-text-to-code huggingface dataset:
Generating numbers that are divisor of their right | Function to generate m - digit numbers which are divisor of their right - rotation ; Driver code
function generateNumbers ( m ) { let numbers = [ ] ; let k_max , x ; for ( let y = 0 ; y < 10 ; y ++ ) { k_max = Math . floor ( ( Math . pow ( 10 , m - 2 ) * ( 10 * y + 1 ) ) / Math . floor ( Math . pow ( 10 , m - 1 ) + y ) ) ; for ( let k = 1 ; k <= k_max ; k ++ ) { x = Math . floor ( ( y * ( Math . pow ( 10 , m - 1 ) - k ) ) / ( 10 * k - 1 ) ) ; if ( Math . floor ( ( y * ( Math . pow ( 10 , m - 1 ) - k ) ) % ( 10 * k - 1 ) ) == 0 ) numbers . push ( 10 * x + y ) ; } } numbers . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < numbers . length ; i ++ ) document . write ( numbers [ i ] + " " ) ; } let m = 3 ; generateNumbers ( m ) ;
Check if an array is sorted and rotated | Function to check if an array is sorted and rotated clockwise function checkIfSortRotated ( arr , n ) { let minEle = Number . MAX_VALUE ; int maxEle = int . MinValue ; ; Find the minimum element and it 's index ; Check if all elements before minIndex are in increasing order ; Check if all elements after minIndex are in increasing order ; Check if last element of the array is smaller than the element just before the element at minIndex starting element of the array for arrays like [ 3 , 4 , 6 , 1 , 2 , 5 ] - not circular array ; Driver code ; Function Call
let minIndex = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < minEle ) { minEle = arr [ i ] ; minIndex = i ; } } let flag1 = true ; for ( let i = 1 ; i < minIndex ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { flag1 = false ; break ; } } let flag2 = true ; for ( let i = minIndex + 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { flag2 = false ; break ; } } if ( flag1 && flag2 && ( arr [ n - 1 ] < arr [ 0 ] ) ) document . write ( " " ) ; else document . write ( " " ) ; } let arr = [ 3 , 4 , 5 , 1 , 2 ] ; let n = arr . length ; checkIfSortRotated ( arr , n ) ;
Rotate a matrix by 90 degree in clockwise direction without using any extra space | javascript implementation of above approach ; Function to rotate the matrix 90 degree clockwise ; printing the matrix on the basis of observations made on indices . ; Driver code
var N = 4 ; function rotate90Clockwise ( arr ) { for ( j = 0 ; j < N ; j ++ ) { for ( i = N - 1 ; i >= 0 ; i -- ) document . write ( arr [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } var arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; rotate90Clockwise ( arr ) ;
Rotate a matrix by 90 degree in clockwise direction without using any extra space |  ; rotate function ; First rotation with respect to main diagonal ; Second rotation with respect to middle column ; to print matrix ; Driver code
let N = 4 function rotate ( arr ) { for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < i ; ++ j ) { let temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; } } for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < N / 2 ; ++ j ) { let temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ i ] [ N - j - 1 ] ; arr [ i ] [ N - j - 1 ] = temp ; } } } function print ( arr ) { for ( let i = 0 ; i < N ; ++ i ) { for ( let j = 0 ; j < N ; ++ j ) document . write ( arr [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; rotate ( arr ) ; print ( arr ) ;
Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Sort the array ; Check for first element ; Check for all the elements if it is different its adjacent elements ; Check for the last element ; Driver code
function occurredOnce ( arr , n ) { arr . sort ( function ( a , b ) { return a - b ; } ) ; if ( arr [ 0 ] != arr [ 1 ] ) document . write ( arr [ 0 ] + " " ) ; for ( let i = 1 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i - 1 ] ) document . write ( arr [ i ] + " " ) ; if ( arr [ n - 2 ] != arr [ n - 1 ] ) document . write ( arr [ n - 1 ] + " " ) ; } let arr = [ 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ] ; let n = arr . length ; occurredOnce ( arr , n ) ;
Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Store all the elements in the map with their occurrence ; Traverse the map and print all the elements with occurrence 1 ; Driver code
function occurredOnce ( arr , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , 1 + mp . get ( arr [ i ] ) ) ; else mp . set ( arr [ i ] , 1 ) ; } for ( let [ key , value ] of mp . entries ( ) ) { if ( value == 1 ) document . write ( key + " " ) ; } } let arr = [ 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ] ; let n = arr . length ; occurredOnce ( arr , n ) ;
Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Check if the first and last element is equal . If yes , remove those elements ; Start traversing the remaining elements ; Check if current element is equal to the element at immediate previous index If yes , check the same for next element ; Else print the current element ; Check for the last element ; Driver code
function occurredOnce ( arr , n ) { var i = 1 , len = n ; if ( arr [ 0 ] == arr [ len - 1 ] ) { i = 2 ; len -- ; } for ( ; i < n ; i ++ ) if ( arr [ i ] == arr [ i - 1 ] ) i ++ ; else document . write ( arr [ i - 1 ] + " " ) ; if ( arr [ n - 1 ] != arr [ 0 ] && arr [ n - 1 ] != arr [ n - 2 ] ) document . write ( arr [ n - 1 ] ) ; } var arr = [ 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ] ; var n = arr . length ; occurredOnce ( arr , n ) ;
Split the array and add the first part to the end | Set 2 | Function to reverse arr [ ] from index start to end ; Function to print an array ; Function to left rotate arr [ ] of size n by k ; Driver program to test above functions ; Function calling
function rvereseArray ( arr , start , end ) { while ( start < end ) { let temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; } function splitArr ( arr , k , n ) { rvereseArray ( arr , 0 , n - 1 ) ; rvereseArray ( arr , 0 , n - k - 1 ) ; rvereseArray ( arr , n - k , n - 1 ) ; } let arr = new Array ( 12 , 10 , 5 , 6 , 52 , 36 ) ; let n = arr . length ; let k = 2 ; splitArr ( arr , k , n ) ; printArray ( arr , n ) ;
Check if strings are rotations of each other or not | Set 2 | javascript program to check if two strings are rotations of each other . ; create lps that will hold the longest prefix suffix values for pattern ; length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; the loop calculates lps [ i ] for i = 1 to n - 1 ; match from that rotating point ; Driver code
function isRotation ( a , b ) { var n = a . length ; var m = b . length ; if ( n != m ) return false ; var lps = Array . from ( { length : n } , ( _ , i ) => 0 ) ; var len = 0 ; var i = 1 ; lps [ 0 ] = 0 ; while ( i < n ) { if ( a . charAt ( i ) == b . charAt ( len ) ) { lps [ i ] = ++ len ; ++ i ; } else { if ( len == 0 ) { lps [ i ] = 0 ; ++ i ; } else { len = lps [ len - 1 ] ; } } } i = 0 ; for ( k = lps [ n - 1 ] ; k < m ; ++ k ) { if ( b . charAt ( k ) != a . charAt ( i ++ ) ) return false ; } return true ; } var s1 = " " ; var s2 = " " ; document . write ( isRotation ( s1 , s2 ) ? " " : " " ) ;
Count rotations divisible by 8 | Function to count of all rotations divisible by 8 ; For single digit number ; For two - digit numbers ( considering all pairs ) ; first pair ; second pair ; Considering all three - digit sequences ; Considering the number formed by the last digit and the first two digits ; Considering the number formed by the last two digits and the first digit ; Required count of rotations ; Driver Code
function countRotationsDivBy8 ( n ) { let len = n . length ; let count = 0 ; if ( len == 1 ) { let oneDigit = n [ 0 ] - ' ' ; if ( oneDigit % 8 == 0 ) return 1 ; return 0 ; } if ( len == 2 ) { let first = ( n [ 0 ] - ' ' ) * 10 + ( n [ 1 ] - ' ' ) ; let second = ( n [ 1 ] - ' ' ) * 10 + ( n [ 0 ] - ' ' ) ; if ( first % 8 == 0 ) count ++ ; if ( second % 8 == 0 ) count ++ ; return count ; } let threeDigit ; for ( let i = 0 ; i < ( len - 2 ) ; i ++ ) { threeDigit = ( n [ i ] - ' ' ) * 100 + ( n [ i + 1 ] - ' ' ) * 10 + ( n [ i + 2 ] - ' ' ) ; if ( threeDigit % 8 == 0 ) count ++ ; } threeDigit = ( n [ len - 1 ] - ' ' ) * 100 + ( n [ 0 ] - ' ' ) * 10 + ( n [ 1 ] - ' ' ) ; if ( threeDigit % 8 == 0 ) count ++ ; threeDigit = ( n [ len - 2 ] - ' ' ) * 100 + ( n [ len - 1 ] - ' ' ) * 10 + ( n [ 0 ] - ' ' ) ; if ( threeDigit % 8 == 0 ) count ++ ; return count ; } let n = " " ; document . write ( " " + countRotationsDivBy8 ( n ) ) ;
Minimum move to end operations to make all strings equal | Returns minimum number of moves to end operations to make all strings same . ; Consider s [ i ] as target string and count rotations required to make all other strings same as str [ i ] . ; find function returns the index where we found arr [ i ] which is actually count of move - to - front operations . ; If any two strings are not rotations of each other , we can 't make them same. ; Driver code
function minimunMoves ( arr , n ) { let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { let curr_count = 0 ; let tmp = " " ; for ( let j = 0 ; j < n ; j ++ ) { tmp = arr [ j ] + arr [ j ] ; let index = tmp . indexOf ( arr [ i ] ) ; if ( index == arr [ i ] . length ) return - 1 ; curr_count += index ; } ans = Math . min ( curr_count , ans ) ; } return ans ; } let arr = [ " " , " " , " " , " " ] ; let n = arr . length ; document . write ( minimunMoves ( arr , n ) ) ;
Count rotations in sorted and rotated linked list | Linked list node ; Function that count number of rotation in singly linked list . ; declare count variable and assign it 1. ; declare a min variable and assign to data of head node . ; check that while head not equal to null . ; if min value is greater then head . data then it breaks the while loop and return the value of count . ; head assign the next value of head . ; Function to push element in linked list . ; Allocate dynamic memory for newNode . ; Assign the data into newNode . ; newNode . next assign the address of head node . ; newNode become the headNode . ; Display linked list . ; Create a node and initialize with null ; push ( ) insert node in linked list . 15.18 .5 . 8.11 . 12 ; Function call countRotation ( )
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function countRotation ( head ) { let count = 0 ; let min = head . data ; while ( head != null ) { if ( min > head . data ) break ; count ++ ; head = head . next ; } return count ; } function push ( head , data ) { let newNode = new Node ( ) ; newNode . data = data ; newNode . next = ( head ) ; ( head ) = newNode ; return head ; } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } } let head = null ; head = push ( head , 12 ) ; head = push ( head , 11 ) ; head = push ( head , 8 ) ; head = push ( head , 5 ) ; head = push ( head , 18 ) ; head = push ( head , 15 ) ; printList ( head ) ; document . write ( " " ) ; document . write ( " " ) ; document . write ( countRotation ( head ) + " " ) ;
Rotate Linked List block wise | Link list node ; Recursive function to rotate one block ; Rotate Clockwise ; Rotate anti - Clockwise ; Function to rotate the linked list block wise ; If length is 0 or 1 return head ; if degree of rotation is 0 , return head ; Traverse upto last element of this block ; storing the first node of next block ; If nodes of this block are less than k . Rotate this block also ; Append the new head of next block to the tail of this block ; return head of updated Linked List ; Function to push a node ; Function to print linked list ; Start with the empty list ; create a list 1.2 . 3.4 .5 . 6.7 . 8.9 . null ; k is block size and d is number of rotations in every block .
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } var tail ; function rotateHelper ( blockHead , blockTail , d , k ) { if ( d == 0 ) return blockHead ; if ( d > 0 ) { var temp = blockHead ; for ( i = 1 ; temp . next . next != null && i < k - 1 ; i ++ ) temp = temp . next ; blockTail . next = blockHead ; tail = temp ; return rotateHelper ( blockTail , temp , d - 1 , k ) ; } if ( d < 0 ) { blockTail . next = blockHead ; tail = blockHead ; return rotateHelper ( blockHead . next , blockHead , d + 1 , k ) ; } return blockHead ; } function rotateByBlocks ( head , k , d ) { if ( head == null head . next == null ) return head ; if ( d == 0 ) return head ; var temp = head ; tail = null ; var i ; for ( i = 1 ; temp . next != null && i < k ; i ++ ) temp = temp . next ; var nextBlock = temp . next ; if ( i < k ) head = rotateHelper ( head , temp , d % k , i ) ; else head = rotateHelper ( head , temp , d % k , k ) ; tail . next = rotateByBlocks ( nextBlock , k , d % k ) ; return head ; } function push ( head_ref , new_data ) { var new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } function printList ( node ) { while ( node != null ) { document . write ( node . data + " " ) ; node = node . next ; } } var head = null ; for ( i = 9 ; i > 0 ; i -= 1 ) head = push ( head , i ) ; document . write ( " " ) ; printList ( head ) ; var k = 3 , d = 2 ; head = rotateByBlocks ( head , k , d ) ; document . write ( " " ) ; printList ( head ) ;
Check if two numbers are bit rotations of each other or not | function to check if two numbers are equal after bit rotation ; x64 has concatenation of x with itself . ; comapring only last 32 bits ; right shift by 1 unit ; driver code to test above function
function isRotation ( x , y ) { var x64 = x | ( x << 32 ) ; while ( x64 >= y ) { if ( x64 == y ) { return true ; } x64 >>= 1 ; } return false ; } var x = 122 ; var y = 2147483678 ; if ( isRotation ( x , y ) == false ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Count rotations divisible by 4 | Returns count of all rotations divisible by 4 ; For single digit number ; At - least 2 digit number ( considering all pairs ) ; Considering the number formed by the pair of last digit and 1 st digit ; Driver Code
function countRotations ( n ) { let len = n . length ; if ( len == 1 ) { let oneDigit = n [ 0 ] - ' ' ; if ( oneDigit % 4 == 0 ) return 1 ; return 0 ; } let twoDigit ; let count = 0 ; for ( let i = 0 ; i < ( len - 1 ) ; i ++ ) { twoDigit = ( n [ i ] - ' ' ) * 10 + ( n [ i + 1 ] - ' ' ) ; if ( twoDigit % 4 == 0 ) count ++ ; } twoDigit = ( n [ len - 1 ] - ' ' ) * 10 + ( n [ 0 ] - ' ' ) ; if ( twoDigit % 4 == 0 ) count ++ ; return count ; } let n = " " ; document . write ( " " + countRotations ( n ) ) ;
Check if a string can be obtained by rotating another string 2 places | Method to check if string2 is obtained by string 1 ; Initialize string as anti - clockwise rotation ; Initialize string as clock wise rotation ; Check if any of them is equal to string1 ; Driver code
function isRotated ( str1 , str2 ) { if ( str1 . length != str2 . length ) return false ; if ( str1 . length < 2 ) { return str1 . localeCompare ( str2 ) ; } let clock_rot = " " ; let anticlock_rot = " " ; let len = str2 . length ; anticlock_rot = anticlock_rot + str2 . substring ( len - 2 , len + 1 ) + str2 . substring ( 0 , len - 1 ) ; clock_rot = clock_rot + str2 . substring ( 2 , str2 . length - 2 + 1 ) + str2 . substring ( 0 , 2 + 1 ) ; return ( str1 . localeCompare ( clock_rot ) || str1 . localeCompare ( anticlock_rot ) ) ; } let str1 = " " ; let str2 = " " ; document . write ( isRotated ( str1 , str2 ) ? " " : " " ) ;
Lexicographically minimum string rotation | Set 1 | This functionr return lexicographically minimum rotation of str ; Find length of given String ; Create an array of strings to store all rotations ; Create a concatenation of String with itself ; One by one store all rotations of str in array . A rotation is obtained by getting a substring of concat ; Sort all rotations ; Return the first rotation from the sorted array ; Driver code
function minLexRotation ( str ) { let n = str . length ; let arr = new Array ( n ) ; let concat = str + str ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = concat . substring ( i , i + n ) ; } arr . sort ( ) ; return arr [ 0 ] ; } document . write ( minLexRotation ( " " ) + " " ) ; document . write ( minLexRotation ( " " ) + " " ) ; document . write ( minLexRotation ( " " ) + " " ) ;
Next Greater Element in a Circular Linked List | Javascript program for the above approach ; Node structure of the circular Linked list ; Constructor ; Function to print the elements of a Linked list ; Iterate the linked list ; Print the data ; Function to find the next greater element for each node ; Stores the head of circular Linked list ; Stores the head of the resulting linked list ; Stores the temporary head of the resulting Linked list ; Iterate until head is not equal to H ; Used to iterate over the circular Linked List ; Stores if there exist any next Greater element ; Iterate until head is not equal to curr ; If the current node is smaller ; Update the value of Val ; Update curr ; If res is Null ; Create new Node with value curr . data ; Assign address of res to tempList ; Update tempList ; Assign address of the next node to tempList ; Update the value of head node ; Print the resulting Linked list ; Driver Code
let head ; class Node { constructor ( data ) { this . data = data ; this . next = null ; } } function print ( head ) { let temp = head ; while ( temp != null ) { document . write ( temp . data + " " ) ; temp = temp . next ; } } function NextGreaterElement ( head ) { let H = head ; let res = null ; let tempList = null ; do { let curr = head ; let Val = - 1 ; do { if ( head . data < curr . data ) { Val = curr . data ; break ; } curr = curr . next ; } while ( curr != head ) ; if ( res == null ) { res = new Node ( Val ) ; tempList = res ; } else { tempList . next = new Node ( Val ) ; tempList = tempList . next ; } head = head . next ; } while ( head != H ) ; print ( res ) ; } head = new Node ( 1 ) ; head . next = new Node ( 5 ) ; head . next . next = new Node ( 12 ) ; head . next . next . next = new Node ( 10 ) ; head . next . next . next . next = new Node ( 0 ) ; head . next . next . next . next . next = head ; NextGreaterElement ( head ) ;
Types of Linked List | Node of a doubly linked list
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } }
Types of Linked List | Structure of Node ; Function to print the content of linked list starting from the given node ; Iterate till n reaches null ; Print the data ; Driver Code ; Allocate 3 nodes in the heap ; Assign data in first node ; Link first node with second ; Assign data to second node ; Assign data to third node
class Node { constructor ( ) { this . data = 0 ; this . next = null ; } } function printList ( n ) { while ( n != null ) { document . write ( n . data + " " ) ; n = n . next ; } } let head = null ; let second = null ; let third = null ; head = new Node ( ) ; second = new Node ( ) ; third = new Node ( ) ; head . data = 1 ; head . next = second ; second . data = 2 ; second . next = third ; third . data = 3 ; third . next = null ; printList ( head ) ;
Remove all even parity nodes from a Doubly and Circular Singly Linked List | Structure for a node ; Function to insert a node at the beginning of a Circular linked list ; Create a new node and make head as next of it . ; If linked list is not null then set the next of last node ; Find the node before head and update next of it . ; Point for the first node ; Function to delete the node from a Circular Linked list ; If node to be deleted is head node ; Traverse list till not found delete node ; Copy the address of the node ; Finally , free the memory occupied by del ; Function that returns true if count of set bits in x is even ; Parity will store the count of set bits ; Function to delete all the Even Parity Nodes from the singly circular linked list ; Traverse the list till the end ; If the node ' s ▁ data ▁ has ▁ ▁ even ▁ parity , ▁ delete ▁ node ▁ ' ptr ' ; Point to the next node ; Function to prvar nodes in a given Circular linked list ; Initialize lists as empty ; Created linked list will be 11.9 .34 .6 .13 .21
class Node { constructor ( val ) { this . data = val ; this . next = null ; } } function push ( head_ref , data ) { var ptr1 = new Node ( ) ; var temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } function deleteNode ( head_ref , del ) { if ( head_ref == del ) head_ref = del . next ; var temp = head_ref ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; return ; } function isEvenParity ( x ) { var parity = 0 ; while ( x != 0 ) { if ( ( x & 1 ) != 0 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } function deleteEvenParityNodes ( head ) { if ( head == null ) return ; if ( head == head . next ) { if ( isEvenParity ( head . data ) ) head = null ; return ; } var ptr = head ; var next ; do { next = ptr . next ; if ( isEvenParity ( ptr . data ) ) deleteNode ( head , ptr ) ; ptr = next ; } while ( ptr != head ) ; if ( head == head . next ) { if ( isEvenParity ( head . data ) ) head = null ; return ; } } function printList ( head ) { if ( head == null ) { document . write ( " " ) ; return ; } var temp = head ; if ( head != null ) { do { document . write ( temp . data + " " ) ; temp = temp . next ; } while ( temp != head ) ; } } var head = null ; head = push ( head , 21 ) ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 34 ) ; head = push ( head , 9 ) ; head = push ( head , 11 ) ; deleteEvenParityNodes ( head ) ; printList ( head ) ;