codeparrot/xlcost-text-to-code huggingface dataset:
Remove " b " and " ac " from a given string | C # program to remove " b " and ' ac ' from input string ; The main function that removes occurrences of " a " and " bc " in input string ; previous character ; current character ; check if current and next character forms ac ; if current character is b ; if current char is ' c ▁ & & ▁ last ▁ char ▁ in ▁ output ▁ ▁ ▁ is ▁ ' a ' so delete both  ; else copy curr char to output string ; Driver Code
using System ; class GfG { static void stringFilter ( char [ ] str ) { int n = str . Length ; int i = - 1 ; int j = 0 ; while ( j < n ) { if ( j < n - 1 && str [ j ] == ' a ' && str [ j + 1 ] == ' c ' ) j += 2 ; else if ( str [ j ] == ' b ' ) j ++ ; else if ( i >= 0 && str [ j ] == ' c ' && str [ i ] == ' a ' ) { i -- ; j ++ ; } else str [ ++ i ] = str [ j ++ ] ; } Console . WriteLine ( new String ( str ) ) ; } public static void Main ( ) { String str1 = " ad " ; stringFilter ( str1 . ToCharArray ( ) ) ; String str2 = " acbac " ; stringFilter ( str2 . ToCharArray ( ) ) ; String str3 = " aaac " ; stringFilter ( str3 . ToCharArray ( ) ) ; String str4 = " react " ; stringFilter ( str4 . ToCharArray ( ) ) ; String str5 = " aa " ; stringFilter ( str5 . ToCharArray ( ) ) ; String str6 = " ababaac " ; stringFilter ( str6 . ToCharArray ( ) ) ; } }
Write your own atoi ( ) | A simple C # program for implementation of atoi ; if whitespaces then ignore . ; sign of number ; checking for valid input ; handling overflow test case ; Driver code ; Function call
using System ; class GFG { static int myAtoi ( char [ ] str ) { int sign = 1 , Base = 0 , i = 0 ; while ( str [ i ] == ' ▁ ' ) { i ++ ; } if ( str [ i ] == ' - ' str [ i ] == ' + ' ) { sign = 1 - 2 * ( str [ i ++ ] == ' - ' ? 1 : 0 ) ; } while ( i < str . Length && str [ i ] >= '0' && str [ i ] <= '9' ) { if ( Base > int . MaxValue / 10 || ( Base == int . MaxValue / 10 && str [ i ] - '0' > 7 ) ) { if ( sign == 1 ) return int . MaxValue ; else return int . MinValue ; } Base = 10 * Base + ( str [ i ++ ] - '0' ) ; } return Base * sign ; } public static void Main ( String [ ] args ) { char [ ] str = " ▁ - 123" . ToCharArray ( ) ; int val = myAtoi ( str ) ; Console . Write ( " { 0 } ▁ " , val ) ; } }
Check whether two strings are anagram of each other | C # program to check if two strings are anagrams of each other ; function to check if two strings are anagrams of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different Length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array ; Driver code ; Function call
using System ; class GFG { static int NO_OF_CHARS = 256 ; static bool areAnagram ( char [ ] str1 , char [ ] str2 ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str1 . Length ; i ++ ) { count [ str1 [ i ] - ' a ' ] ++ ; count [ str2 [ i ] - ' a ' ] -- ; } if ( str1 . Length != str2 . Length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) { return false ; } return true ; } public static void Main ( String [ ] args ) { char [ ] str1 = " geeksforgeeks " . ToCharArray ( ) ; char [ ] str2 = " forgeeksgeeks " . ToCharArray ( ) ; if ( areAnagram ( str1 , str2 ) ) Console . Write ( " The ▁ two ▁ strings ▁ are ▁ " + " anagram ▁ of ▁ each ▁ other " ) ; else Console . Write ( " The ▁ two ▁ strings ▁ are ▁ " + " not ▁ anagram ▁ of ▁ each ▁ other " ) ; } }
Length of the longest substring without repeating characters | C # program to find the length of the longest substring without repeating characters ; This function returns true if all characters in str [ i . . j ] are distinct , otherwise returns false ; Note : Default values in visited are false ; Returns length of the longest substring with all distinct characters . ; Result ; Driver code
using System ; class GFG { public static bool areDistinct ( string str , int i , int j ) { bool [ ] visited = new bool [ 26 ] ; for ( int k = i ; k <= j ; k ++ ) { if ( visited [ str [ k ] - ' a ' ] == true ) return false ; visited [ str [ k ] - ' a ' ] = true ; } return true ; } public static int longestUniqueSubsttr ( string str ) { int n = str . Length ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( areDistinct ( str , i , j ) ) res = Math . Max ( res , j - i + 1 ) ; return res ; } public static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( " The ▁ input ▁ string ▁ is ▁ " + str ) ; int len = longestUniqueSubsttr ( str ) ; Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ longest ▁ " + " non - repeating ▁ character ▁ " + " substring ▁ is ▁ " + len ) ; } }
Given a string , find its first non | C # program to find first non - repeating character ; this function return the index of first non - repeating character if found , or else it returns - 1 ; initializing all elements to - 1 ; sets all repeating characters to - 2 and non - repeating characters contain the index where they occur ; If this character is not - 1 or - 2 then it means that this character occurred only once so find the min index of all characters that occur only once , that 's our first index ; if res remains Integer . MAX_VALUE , it means there are no characters that repeat only once or the string is empy
using System ; public class GFG { public static int firstNonRepeating ( string str ) { int [ ] fi = new int [ 256 ] ; for ( int i = 0 ; i < 256 ; i ++ ) fi [ i ] = - 1 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( fi [ str [ i ] ] == - 1 ) { fi [ str [ i ] ] = i ; } else { fi [ str [ i ] ] = - 2 ; } } int res = Int32 . MaxValue ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( fi [ i ] >= 0 ) res = Math . Min ( res , fi [ i ] ) ; } if ( res == Int32 . MaxValue ) return - 1 ; else return res ; } public static void Main ( ) { string str ; str = " geeksforgeeks " ; int firstIndex = firstNonRepeating ( str ) ; if ( firstIndex == - 1 ) Console . WriteLine ( " Either ▁ all ▁ characters ▁ are ▁ repeating ▁ or ▁ string ▁ is ▁ empty " ) ; else Console . WriteLine ( " First ▁ non - repeating ▁ character ▁ is ▁ " + str [ firstIndex ] ) ; } }
Remove duplicates from a given string | C # program to create a unique string ; Function to make the string unique ; loop to traverse the string and check for repeating chars using IndexOf ( ) method in Java ; character at i 'th index of s ; if c is present in str , it returns the index of c , else it returns - 1 ; adding c to str if - 1 is returned ; Driver code ; Input string with repeating chars
using System ; public class IndexOf { public static String unique ( String s ) { String str = " " ; int len = s . Length ; for ( int i = 0 ; i < len ; i ++ ) { char c = s [ i ] ; if ( str . IndexOf ( c ) < 0 ) { str += c ; } } return str ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; Console . WriteLine ( unique ( s ) ) ; } }
Sum of all subsets whose sum is a Perfect Number from a given array | C # program for the above approach ; Function to check is a given number is a perfect number or not ; Stores the sum of its divisors ; Add all divisors of x to sum_div ; If the sum of divisors is equal to the given number , return true ; Otherwise , return false ; Function to find sum of all subsets from an array whose sum is a perfect number ; Print the current subset sum if it is a perfect number ; Check if sum is a perfect number or not ; Calculate sum of the subset including arr [ l ] ; Calculate sum of the subset excluding arr [ l ] ; Driver code
using System ; class GFG { static int isPerfect ( int x ) { int sum_div = 1 ; for ( int i = 2 ; i <= x / 2 ; ++ i ) { if ( x % i == 0 ) { sum_div += i ; } } if ( sum_div == x ) { return 1 ; } else return 0 ; } static void subsetSum ( int [ ] arr , int l , int r , int sum = 0 ) { if ( l > r ) { if ( isPerfect ( sum ) != 0 ) { Console . Write ( sum + " ▁ " ) ; } return ; } subsetSum ( arr , l + 1 , r , sum + arr [ l ] ) ; subsetSum ( arr , l + 1 , r , sum ) ; } static void Main ( ) { int [ ] arr = { 5 , 4 , 6 } ; int N = arr . Length ; subsetSum ( arr , 0 , N - 1 ) ; } }
Print all possible ways to split an array into K subsets | C # program for the above approach ; Utility function to find all possible ways to split array into K subsets ; If count of elements in K subsets are greater than or equal to N ; If count of subsets formed is equal to K ; Print K subsets by splitting array into K subsets ; Print current subset ; If current element is the last element of the subset ; Otherwise ; If any subset is occupied , then push the element in that first ; Recursively do the same for remaining elements ; Backtrack ; Otherwise , push it in an empty subset and increase the subset count by 1 ; Break to avoid the case of going in other empty subsets , if available , and forming the same combination ; Function to to find all possible ways to split array into K subsets ; Stores K subset by splitting array into K subsets ; Size of each subset must be less than the number of elements ; Driver Code ; Given array ; Given K ; Size of the array ; Prints all possible splits into subsets
using System ; using System . Collections . Generic ; class GFG { static void PartitionSub ( int [ ] arr , int i , int N , int K , int nos , List < List < int > > v ) { if ( i >= N ) { if ( nos == K ) { for ( int x = 0 ; x < v . Count ; x ++ ) { Console . Write ( " { ▁ " ) ; for ( int y = 0 ; y < v [ x ] . Count ; y ++ ) { Console . Write ( v [ x ] [ y ] ) ; if ( y == v [ x ] . Count - 1 ) { Console . Write ( " ▁ " ) ; } else { Console . Write ( " , ▁ " ) ; } } if ( x == v . Count - 1 ) { Console . Write ( " } " ) ; } else { Console . Write ( " } , ▁ " ) ; } } Console . Write ( " STRNEWLINE " ) ; } return ; } for ( int j = 0 ; j < K ; j ++ ) { if ( v [ j ] . Count > 0 ) { v [ j ] . Add ( arr [ i ] ) ; PartitionSub ( arr , i + 1 , N , K , nos , v ) ; v [ j ] . RemoveAt ( v [ j ] . Count - 1 ) ; } else { v [ j ] . Add ( arr [ i ] ) ; PartitionSub ( arr , i + 1 , N , K , nos + 1 , v ) ; v [ j ] . RemoveAt ( v [ j ] . Count - 1 ) ; break ; } } } static void partKSubsets ( int [ ] arr , int N , int K ) { List < List < int > > v = new List < List < int > > ( ) ; for ( int i = 0 ; i < K ; i ++ ) v . Add ( new List < int > ( ) ) ; if ( K == 0 K > N ) { Console . WriteLine ( " Not ▁ Possible " ) ; } else { Console . WriteLine ( " The ▁ Subset ▁ Combinations ▁ are : ▁ " ) ; PartitionSub ( arr , 0 , N , K , 0 , v ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 2 ; int N = arr . Length ; partKSubsets ( arr , N , K ) ; } }
Count the number of Prime Cliques in an undirected graph | C # implementation to count the number of Prime Cliques in an undirected graph ; Stores the vertices ; Graph ; Degree of the vertices ; To store the count of prime cliques ; Function to create Sieve to check primes ; False here indicates that it is not prime ; Condition if prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to check if the given set of vertices in store array is a clique or not ; Run a loop for all set of edges ; If any edge is missing ; Function to find the count of all the cliques having prime size ; Check if any vertices from i + 1 can be inserted ; Add the vertex to store ; If the graph is not a clique of size k then it cannot be a clique by adding another edge ; Increase the count of prime cliques if the size of current clique is prime ; Check if another edge can be added ; Driver code
using System ; class GFG { static readonly int MAX = 100 ; static int [ ] store = new int [ MAX ] ; static int n ; static int [ , ] graph = new int [ MAX , MAX ] ; static int [ ] d = new int [ MAX ] ; static int ans ; static void SieveOfEratosthenes ( bool [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static bool is_clique ( int b ) { for ( int i = 1 ; i < b ; i ++ ) { for ( int j = i + 1 ; j < b ; j ++ ) if ( graph [ store [ i ] , store [ j ] ] == 0 ) return false ; } return true ; } static void primeCliques ( int i , int l , bool [ ] prime ) { for ( int j = i + 1 ; j <= n ; j ++ ) { store [ l ] = j ; if ( is_clique ( l + 1 ) ) { if ( prime [ l ] ) ans ++ ; primeCliques ( j , l + 1 , prime ) ; } } } public static void Main ( String [ ] args ) { int [ , ] edges = { { 1 , 2 } , { 2 , 3 } , { 3 , 1 } , { 4 , 3 } , { 4 , 5 } , { 5 , 3 } } ; int size = edges . GetLength ( 0 ) ; n = 5 ; bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , n ) ; for ( int i = 0 ; i < size ; i ++ ) { graph [ edges [ i , 0 ] , edges [ i , 1 ] ] = 1 ; graph [ edges [ i , 1 ] , edges [ i , 0 ] ] = 1 ; d [ edges [ i , 0 ] ] ++ ; d [ edges [ i , 1 ] ] ++ ; } ans = 0 ; primeCliques ( 0 , 1 , prime ) ; Console . WriteLine ( ans ) ; } }
Construct a Doubly linked linked list from 2D Matrix | C # program to construct a Doubly linked linked list from 2D Matrix ; define dimension of matrix ; struct node of doubly linked list with four pointer next , prev , up , down ; function to create a new node ; function to construct the doubly linked list ; Create Node with value contain in matrix at index ( i , j ) ; Assign address of curr into the prev pointer of temp ; Assign address of curr into the up pointer of temp ; Recursive call for next pointer ; Recursive call for down pointer ; Return newly constructed node whose all four node connected at it 's appropriate position ; Function to construct the doubly linked list ; function call for construct the doubly linked list ; function for displaying doubly linked list data ; pointer to move right ; pointer to move down ; loop till node . down is not null ; loop till node . right is not null ; driver code ; initialise matrix
using System ; class GFG { static int dim = 3 ; public class Node { public int data ; public Node next , prev , up , down ; } ; static Node createNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = null ; temp . prev = null ; temp . up = null ; temp . down = null ; return temp ; } static Node constructDoublyListUtil ( int [ , ] mtrx , int i , int j , Node curr ) { if ( i >= dim j >= dim ) { return null ; } Node temp = createNode ( mtrx [ i , j ] ) ; temp . prev = curr ; temp . up = curr ; temp . next = constructDoublyListUtil ( mtrx , i , j + 1 , temp ) ; temp . down = constructDoublyListUtil ( mtrx , i + 1 , j , temp ) ; return temp ; } static Node constructDoublyList ( int [ , ] mtrx ) { return constructDoublyListUtil ( mtrx , 0 , 0 , null ) ; } static void display ( Node head ) { Node rPtr ; Node dPtr = head ; while ( dPtr != null ) { rPtr = dPtr ; while ( rPtr != null ) { Console . Write ( rPtr . data + " ▁ " ) ; rPtr = rPtr . next ; } Console . Write ( " STRNEWLINE " ) ; dPtr = dPtr . down ; } } public static void Main ( ) { int [ , ] mtrx = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; Node list = constructDoublyList ( mtrx ) ; display ( list ) ; } }
Count of exponential paths in a Binary Tree | C # program to find the count exponential paths in Binary Tree ; Structure of a Tree node ; Function to create a new node ; Function to find x ; Take log10 of n ; Log ( n ) with base i ; Raising i to the power p ; Function to check whether the given node equals to x ^ y for some y > 0 ; Take logx ( n ) with base x ; Utility function to count the exponent path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is not a number of pow ( x , y ) ; Increment count when encounter leaf node ; Left recursive call save the value of count ; Right recursive call and return value of count ; Function to count exponential paths ; Driver code ; Create Tree ; Retrieve the value of x ; Function call
using System ; class GFG { public class Node { public int key ; public Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static int find_x ( int n ) { if ( n == 1 ) return 1 ; double num , den , p ; num = Math . Log10 ( n ) ; int x = 0 , no = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { den = Math . Log10 ( i ) ; p = num / den ; no = ( int ) ( Math . Pow ( i , ( int ) p ) ) ; if ( Math . Abs ( no - n ) < 0.000001 ) { x = i ; break ; } } return x ; } static bool is_key ( int n , int x ) { double p ; p = Math . Log10 ( n ) / Math . Log10 ( x ) ; int no = ( int ) ( Math . Pow ( x , ( int ) p ) ) ; if ( n == no ) return true ; return false ; } static int evenPaths ( Node node , int count , int x ) { if ( node == null || ! is_key ( node . key , x ) ) { return count ; } if ( node . left == null && node . right == null ) { count ++ ; } count = evenPaths ( node . left , count , x ) ; return evenPaths ( node . right , count , x ) ; } static int countExpPaths ( Node node , int x ) { return evenPaths ( node , 0 , x ) ; } public static void Main ( string [ ] args ) { Node root = newNode ( 27 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 81 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 10 ) ; root . right . left = newNode ( 70 ) ; root . right . right = newNode ( 243 ) ; root . right . right . left = newNode ( 81 ) ; root . right . right . right = newNode ( 909 ) ; int x = find_x ( root . key ) ; Console . Write ( countExpPaths ( root , x ) ) ; } }
Number of pairs such that path between pairs has the two vertices A and B | C # program to find the number of pairs such that the path between every pair contains two given vertices ; Function to perform DFS on the given graph by fixing the a vertex ; To mark a particular vertex as visited ; Variable to store the count of the vertices which can be reached from a ; Performing the DFS by iterating over the visited array ; If the vertex is not visited and removing the vertex b ; Function to return the number of pairs such that path between any two pairs consists of the given two vertices A and B ; Initializing the visited array and assigning it with 0 's ; Initially , the count of vertices is 0 ; Performing DFS by removing the vertex B ; Count the vertices which cannot be reached after removing the vertex B ; Again reinitializing the visited array ; Setting the count of vertices to 0 to perform the DFS again ; Performing the DFS by removing the vertex A ; Count the vertices which cannot be reached after removing the vertex A ; Multiplying both the vertices set ; Driver code ; Loop to store the graph
using System ; using System . Collections . Generic ; class GFG { static int N = 1000001 ; static int c , n , m , a , b ; static void dfs ( int a , int b , List < int > [ ] v , int [ ] vis ) { vis [ a ] = 1 ; c ++ ; foreach ( int i in v [ a ] ) { if ( vis [ i ] == 0 && i != b ) dfs ( i , b , v , vis ) ; } } static void Calculate ( List < int > [ ] v ) { int [ ] vis = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) vis [ i ] = 0 ; c = 0 ; dfs ( a , b , v , vis ) ; int ans1 = n - c - 1 ; for ( int i = 0 ; i < n + 1 ; i ++ ) vis [ i ] = 0 ; c = 0 ; dfs ( b , a , v , vis ) ; int ans2 = n - c - 1 ; Console . Write ( ans1 * ans2 + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { n = 7 ; m = 7 ; a = 3 ; b = 5 ; int [ , ] edges = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } , { 5 , 6 } , { 6 , 7 } , { 7 , 5 } } ; List < int > [ ] v = new List < int > [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { v [ i ] = new List < int > ( ) ; } for ( int i = 0 ; i < m ; i ++ ) { v [ edges [ i , 0 ] ] . Add ( edges [ i , 1 ] ) ; v [ edges [ i , 1 ] ] . Add ( edges [ i , 0 ] ) ; } Calculate ( v ) ; } }
Travelling Salesman Problem implementation using BackTracking | C # implementation of the approach ; Function to find the minimum weight Hamiltonian Cycle ; If last node is reached and it has a link to the starting node i . e the source then keep the minimum value out of the total cost of traversal and " ans " Finally return to check for more possible values ; BACKTRACKING STEP Loop to traverse the adjacency list of currPos node and increasing the count by 1 and cost by graph [ currPos , i ] value ; Mark as visited ; Mark ith node as unvisited ; Driver code ; n is the number of nodes i . e . V ; Boolean array to check if a node has been visited or not ; Mark 0 th node as visited ; Find the minimum weight Hamiltonian Cycle ; ans is the minimum weight Hamiltonian Cycle
using System ; class GFG { static int tsp ( int [ , ] graph , bool [ ] v , int currPos , int n , int count , int cost , int ans ) { if ( count == n && graph [ currPos , 0 ] > 0 ) { ans = Math . Min ( ans , cost + graph [ currPos , 0 ] ) ; return ans ; } for ( int i = 0 ; i < n ; i ++ ) { if ( v [ i ] == false && graph [ currPos , i ] > 0 ) { v [ i ] = true ; ans = tsp ( graph , v , i , n , count + 1 , cost + graph [ currPos , i ] , ans ) ; v [ i ] = false ; } } return ans ; } static void Main ( ) { int n = 4 ; int [ , ] graph = { { 0 , 10 , 15 , 20 } , { 10 , 0 , 35 , 25 } , { 15 , 35 , 0 , 30 } , { 20 , 25 , 30 , 0 } } ; bool [ ] v = new bool [ n ] ; v [ 0 ] = true ; int ans = int . MaxValue ; ans = tsp ( graph , v , 0 , n , 1 , 0 , ans ) ; Console . Write ( ans ) ; } }
Generate all the binary strings of N bits | C # implementation of the above approach : ; Function to print the output ; Function to generate all binary strings ; First assign "0" at ith position and try for all other permutations for remaining positions ; And then assign "1" at ith position and try for all other permutations for remaining positions ; Driver Code ; Print all binary strings
using System ; class GFG { static void printTheArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } static void generateAllBinaryStrings ( int n , int [ ] arr , int i ) { if ( i == n ) { printTheArray ( arr , n ) ; return ; } arr [ i ] = 0 ; generateAllBinaryStrings ( n , arr , i + 1 ) ; arr [ i ] = 1 ; generateAllBinaryStrings ( n , arr , i + 1 ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = new int [ n ] ; generateAllBinaryStrings ( n , arr , 0 ) ; } }