codeparrot/xlcost-text-to-code huggingface dataset:
Check whether given three numbers are adjacent primes | Function checks whether given number is prime or not . ; Check if n is a multiple of 2 ; If not , then just check the odds ; Return next prime number ; Start with next number ; Breaks after finding next prime number ; Check given three numbers are adjacent primes are not ; Check given three numbers are primes are not ; Find next prime of a ; If next is not same as 'a ; If next next is not same as 'c ; Driver code for above functions
def isPrime ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 3 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 2 NEW_LINE DEDENT return True NEW_LINE DEDENT def nextPrime ( start ) : NEW_LINE INDENT nxt = start + 1 NEW_LINE while ( isPrime ( nxt ) == False ) : NEW_LINE INDENT nxt = nxt + 1 NEW_LINE DEDENT return nxt NEW_LINE DEDENT def areAdjacentPrimes ( a , b , c ) : NEW_LINE INDENT if ( isPrime ( a ) == False or isPrime ( b ) == False or isPrime ( c ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT nxt = nextPrime ( a ) NEW_LINE DEDENT ' NEW_LINE INDENT if ( nxt != b ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( nextPrime ( b ) != c ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if ( areAdjacentPrimes ( 11 , 13 , 19 ) ) : NEW_LINE INDENT print ( " Yes " ) , NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n | Python3 Program to find sum of series 1 + 2 + 2 + 3 + . . . + n ; Function that find sum of series . ; Driver method
import math NEW_LINE def sumOfSeries ( n ) : NEW_LINE INDENT return ( ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ) NEW_LINE DEDENT n = 10 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE
Maximum binomial coefficient term value | Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Return maximum binomial coefficient term value . ; if n is even ; if n is odd ; Driver Code
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] ; NEW_LINE DEDENT def maxcoefficientvalue ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return binomialCoeff ( n , int ( n / 2 ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return binomialCoeff ( n , int ( ( n + 1 ) / 2 ) ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE print ( maxcoefficientvalue ( n ) ) ; NEW_LINE DEDENT
Smallest n digit number divisible by given three numbers | Python3 code to find smallest n digit number which is divisible by x , y and z . ; LCM for x , y , z ; returns smallest n digit number divisible by x , y and z ; find the LCM ; find power of 10 for least number ; reminder after ; If smallest number itself divides lcm . ; add lcm - reminder number for next n digit number ; this condition check the n digit number is possible or not if it is possible it return the number else return 0 ; driver code ; if number is possible then it print the number
from fractions import gcd NEW_LINE import math NEW_LINE def LCM ( x , y , z ) : NEW_LINE INDENT ans = int ( ( x * y ) / ( gcd ( x , y ) ) ) NEW_LINE return int ( ( z * ans ) / ( gcd ( ans , z ) ) ) NEW_LINE DEDENT def findDivisible ( n , x , y , z ) : NEW_LINE INDENT lcm = LCM ( x , y , z ) NEW_LINE ndigitnumber = math . pow ( 10 , n - 1 ) NEW_LINE reminder = ndigitnumber % lcm NEW_LINE if reminder == 0 : NEW_LINE INDENT return ndigitnumber NEW_LINE DEDENT ndigitnumber += lcm - reminder NEW_LINE if ndigitnumber < math . pow ( 10 , n ) : NEW_LINE INDENT return int ( ndigitnumber ) NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT n = 4 NEW_LINE x = 2 NEW_LINE y = 3 NEW_LINE z = 5 NEW_LINE res = findDivisible ( n , x , y , z ) NEW_LINE if res != 0 : NEW_LINE INDENT print ( res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT
Sum of squares of first n natural numbers | Return the sum of square of first n natural numbers ; Driven Program
def squaresum ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 NEW_LINE DEDENT n = 4 NEW_LINE print ( squaresum ( n ) ) ; NEW_LINE
Program to calculate distance between two points | Python3 program to calculate distance between two points ; Function to calculate distance ; Calculating distance ; Drivers Code
import math NEW_LINE def distance ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT return math . sqrt ( math . pow ( x2 - x1 , 2 ) + math . pow ( y2 - y1 , 2 ) * 1.0 ) NEW_LINE DEDENT print ( " % .6f " % distance ( 3 , 4 , 4 , 3 ) ) NEW_LINE
Check if a large number is divisibility by 15 | to find sum ; function to check if a large number is divisible by 15 ; length of string ; check divisibility by 5 ; Sum of digits ; if divisible by 3 ; Driver Code
def accumulate ( s ) : NEW_LINE INDENT acc = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT acc += ord ( s [ i ] ) - 48 ; NEW_LINE DEDENT return acc ; NEW_LINE DEDENT def isDivisible ( s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE if ( s [ n - 1 ] != '5' and s [ n - 1 ] != '0' ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT sum = accumulate ( s ) ; NEW_LINE return ( sum % 3 == 0 ) ; NEW_LINE DEDENT s = "15645746327462384723984023940239" ; NEW_LINE if isDivisible ( s ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT s = "15645746327462384723984023940235" ; NEW_LINE if isDivisible ( s ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Shortest path with exactly k edges in a directed and weighted graph | Define number of vertices in the graph and inifinite value ; A naive recursive function to count walks from u to v with k edges ; Base cases ; Initialize result ; Go to all adjacents of u and recur ; Driver Code ; Let us create the graph shown in above diagram
V = 4 NEW_LINE INF = 999999999999 NEW_LINE def shortestPath ( graph , u , v , k ) : NEW_LINE INDENT if k == 0 and u == v : NEW_LINE INDENT return 0 NEW_LINE DEDENT if k == 1 and graph [ u ] [ v ] != INF : NEW_LINE INDENT return graph [ u ] [ v ] NEW_LINE DEDENT if k <= 0 : NEW_LINE INDENT return INF NEW_LINE DEDENT res = INF NEW_LINE for i in range ( V ) : NEW_LINE INDENT if graph [ u ] [ i ] != INF and u != i and v != i : NEW_LINE INDENT rec_res = shortestPath ( graph , i , v , k - 1 ) NEW_LINE if rec_res != INF : NEW_LINE INDENT res = min ( res , graph [ u ] [ i ] + rec_res ) NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT INF = 999999999999 NEW_LINE graph = [ [ 0 , 10 , 3 , 2 ] , [ INF , 0 , INF , 7 ] , [ INF , INF , 0 , 6 ] , [ INF , INF , INF , 0 ] ] NEW_LINE u = 0 NEW_LINE v = 3 NEW_LINE k = 2 NEW_LINE print ( " Weight ▁ of ▁ the ▁ shortest ▁ path ▁ is " , shortestPath ( graph , u , v , k ) ) NEW_LINE DEDENT
Shortest path with exactly k edges in a directed and weighted graph | Define number of vertices in the graph and inifinite value ; A Dynamic programming based function to find the shortest path from u to v with exactly k edges . ; Table to be filled up using DP . The value sp [ i ] [ j ] [ e ] will store weight of the shortest path from i to j with exactly k edges ; Loop for number of edges from 0 to k ; for source ; for destination ; initialize value ; from base cases ; go to adjacent only when number of edges is more than 1 ; There should be an edge from i to a and a should not be same as either i or j ; Let us create the graph shown in above diagram
V = 4 NEW_LINE INF = 999999999999 NEW_LINE def shortestPath ( graph , u , v , k ) : NEW_LINE INDENT global V , INF NEW_LINE sp = [ [ None ] * V for i in range ( V ) ] NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT sp [ i ] [ j ] = [ None ] * ( k + 1 ) NEW_LINE DEDENT DEDENT for e in range ( k + 1 ) : NEW_LINE INDENT for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT sp [ i ] [ j ] [ e ] = INF NEW_LINE if ( e == 0 and i == j ) : NEW_LINE INDENT sp [ i ] [ j ] [ e ] = 0 NEW_LINE DEDENT if ( e == 1 and graph [ i ] [ j ] != INF ) : NEW_LINE INDENT sp [ i ] [ j ] [ e ] = graph [ i ] [ j ] NEW_LINE DEDENT if ( e > 1 ) : NEW_LINE INDENT for a in range ( V ) : NEW_LINE INDENT if ( graph [ i ] [ a ] != INF and i != a and j != a and sp [ a ] [ j ] [ e - 1 ] != INF ) : NEW_LINE INDENT sp [ i ] [ j ] [ e ] = min ( sp [ i ] [ j ] [ e ] , graph [ i ] [ a ] + sp [ a ] [ j ] [ e - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT return sp [ u ] [ v ] [ k ] NEW_LINE DEDENT graph = [ [ 0 , 10 , 3 , 2 ] , [ INF , 0 , INF , 7 ] , [ INF , INF , 0 , 6 ] , [ INF , INF , INF , 0 ] ] NEW_LINE u = 0 NEW_LINE v = 3 NEW_LINE k = 2 NEW_LINE print ( " Weight ▁ of ▁ the ▁ shortest ▁ path ▁ is " , shortestPath ( graph , u , v , k ) ) NEW_LINE
Multistage Graph ( Shortest Path ) | Returns shortest distance from 0 to N - 1. ; dist [ i ] is going to store shortest distance from node i to node N - 1. ; Calculating shortest path for rest of the nodes ; Initialize distance from i to destination ( N - 1 ) ; Check all nodes of next stages to find shortest distance from i to N - 1. ; Reject if no edge exists ; We apply recursive equation to distance to target through j . and compare with minimum distance so far . ; Driver code ; Graph stored in the form of an adjacency Matrix
def shortestDist ( graph ) : NEW_LINE INDENT global INF NEW_LINE dist = [ 0 ] * N NEW_LINE dist [ N - 1 ] = 0 NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT dist [ i ] = INF NEW_LINE for j in range ( N ) : NEW_LINE INDENT if graph [ i ] [ j ] == INF : NEW_LINE INDENT continue NEW_LINE DEDENT dist [ i ] = min ( dist [ i ] , graph [ i ] [ j ] + dist [ j ] ) NEW_LINE DEDENT DEDENT return dist [ 0 ] NEW_LINE DEDENT N = 8 NEW_LINE INF = 999999999999 NEW_LINE graph = [ [ INF , 1 , 2 , 5 , INF , INF , INF , INF ] , [ INF , INF , INF , INF , 4 , 11 , INF , INF ] , [ INF , INF , INF , INF , 9 , 5 , 16 , INF ] , [ INF , INF , INF , INF , INF , INF , 2 , INF ] , [ INF , INF , INF , INF , INF , INF , INF , 18 ] , [ INF , INF , INF , INF , INF , INF , INF , 13 ] , [ INF , INF , INF , INF , INF , INF , INF , 2 ] ] NEW_LINE print ( shortestDist ( graph ) ) NEW_LINE
Reverse Level Order Traversal | A binary tree node ; Function to print reverse level order traversal ; Print nodes at a given level ; Compute the height of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node ; Compute the height of each subtree ; Use the larger one ; Let us create trees shown in above diagram
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def reverseLevelOrder ( root ) : NEW_LINE INDENT h = height ( root ) NEW_LINE for i in reversed ( range ( 1 , h + 1 ) ) : NEW_LINE INDENT printGivenLevel ( root , i ) NEW_LINE DEDENT DEDENT def printGivenLevel ( root , level ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if level == 1 : NEW_LINE INDENT print root . data , NEW_LINE DEDENT elif level > 1 : NEW_LINE INDENT printGivenLevel ( root . left , level - 1 ) NEW_LINE printGivenLevel ( root . right , level - 1 ) NEW_LINE DEDENT DEDENT def height ( node ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT lheight = height ( node . left ) NEW_LINE rheight = height ( node . right ) NEW_LINE if lheight > rheight : NEW_LINE INDENT return lheight + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return rheight + 1 NEW_LINE DEDENT DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE print " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is " NEW_LINE reverseLevelOrder ( root ) NEW_LINE
Minimize the number of weakly connected nodes | Set of nodes which are traversed in each launch of the DFS ; Function traversing the graph using DFS approach and updating the set of nodes ; Building a undirected graph ; Computes the minimum number of disconnected components when a bi - directed graph is converted to a undirected graph ; Declaring and initializing a visited array ; We check if each node is visited once or not ; We only launch DFS from a node iff it is unvisited . ; Clearing the set of nodes on every relaunch of DFS ; Relaunching DFS from an unvisited node . ; Iterating over the node set to count the number of nodes visited after making the graph directed and storing it in the variable count . If count / 2 == number of nodes - 1 , then increment count by 1. ; Driver code ; Building graph in the form of a adjacency list
node = set ( ) NEW_LINE Graph = [ [ ] for i in range ( 10001 ) ] NEW_LINE def dfs ( visit , src ) : NEW_LINE INDENT visit [ src ] = True NEW_LINE node . add ( src ) NEW_LINE llen = len ( Graph [ src ] ) NEW_LINE for i in range ( llen ) : NEW_LINE INDENT if ( not visit [ Graph [ src ] [ i ] ] ) : NEW_LINE INDENT dfs ( visit , Graph [ src ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT def buildGraph ( x , y , llen ) : NEW_LINE INDENT for i in range ( llen ) : NEW_LINE INDENT p = x [ i ] NEW_LINE q = y [ i ] NEW_LINE Graph [ p ] . append ( q ) NEW_LINE Graph [ q ] . append ( p ) NEW_LINE DEDENT DEDENT def compute ( n ) : NEW_LINE INDENT visit = [ False for i in range ( n + 5 ) ] NEW_LINE number_of_nodes = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not visit [ i ] ) : NEW_LINE INDENT node . clear ( ) NEW_LINE dfs ( visit , i ) NEW_LINE count = 0 NEW_LINE for it in node : NEW_LINE INDENT count += len ( Graph [ ( it ) ] ) NEW_LINE DEDENT count //= 2 NEW_LINE if ( count == len ( node ) - 1 ) : NEW_LINE number_of_nodes += 1 NEW_LINE DEDENT DEDENT return number_of_nodes NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE m = 4 NEW_LINE x = [ 1 , 1 , 4 , 4 , 0 , 0 , 0 , 0 , 0 ] NEW_LINE y = [ 2 , 3 , 5 , 6 , 0 , 0 , 0 , 0 , 0 ] NEW_LINE buildGraph ( x , y , n ) NEW_LINE print ( str ( compute ( n ) ) + " ▁ weakly ▁ connected ▁ nodes " ) NEW_LINE DEDENT
Karp 's minimum mean (or average) weight cycle algorithm | a struct to represent edges ; vector to store edges @ SuppressWarnings ( " unchecked " ) ; calculates the shortest path ; initializing all distances as - 1 ; shortest distance From first vertex to in tself consisting of 0 edges ; filling up the dp table ; Returns minimum value of average weight of a cycle in graph . ; array to store the avg values ; Compute average values for all vertices using weights of shortest paths store in dp . ; Find minimum value in avg [ ] ; Driver Code
class edge : NEW_LINE INDENT def __init__ ( self , u , w ) : NEW_LINE INDENT self . From = u NEW_LINE self . weight = w NEW_LINE DEDENT DEDENT def addedge ( u , v , w ) : NEW_LINE INDENT edges [ v ] . append ( edge ( u , w ) ) NEW_LINE DEDENT V = 4 NEW_LINE edges = [ [ ] for i in range ( V ) ] NEW_LINE def shortestpath ( dp ) : NEW_LINE INDENT for i in range ( V + 1 ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT dp [ 0 ] [ 0 ] = 0 NEW_LINE for i in range ( 1 , V + 1 ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT for k in range ( len ( edges [ j ] ) ) : NEW_LINE INDENT if ( dp [ i - 1 ] [ edges [ j ] [ k ] . From ] != - 1 ) : NEW_LINE INDENT curr_wt = ( dp [ i - 1 ] [ edges [ j ] [ k ] . From ] + edges [ j ] [ k ] . weight ) NEW_LINE if ( dp [ i ] [ j ] == - 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = curr_wt NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i ] [ j ] , curr_wt ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT def minAvgWeight ( ) : NEW_LINE INDENT dp = [ [ None ] * V for i in range ( V + 1 ) ] NEW_LINE shortestpath ( dp ) NEW_LINE avg = [ - 1 ] * V NEW_LINE for i in range ( V ) : NEW_LINE INDENT if ( dp [ V ] [ i ] != - 1 ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT if ( dp [ j ] [ i ] != - 1 ) : NEW_LINE INDENT avg [ i ] = max ( avg [ i ] , ( dp [ V ] [ i ] - dp [ j ] [ i ] ) / ( V - j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT result = avg [ 0 ] NEW_LINE for i in range ( V ) : NEW_LINE INDENT if ( avg [ i ] != - 1 and avg [ i ] < result ) : NEW_LINE INDENT result = avg [ i ] NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT addedge ( 0 , 1 , 1 ) NEW_LINE addedge ( 0 , 2 , 10 ) NEW_LINE addedge ( 1 , 2 , 3 ) NEW_LINE addedge ( 2 , 3 , 2 ) NEW_LINE addedge ( 3 , 1 , 0 ) NEW_LINE addedge ( 3 , 0 , 8 ) NEW_LINE print ( minAvgWeight ( ) ) NEW_LINE
Frequency of maximum occurring subsequence in given string | Python3 program for the above approach ; Function to find the frequency ; freq stores frequency of each english lowercase character ; dp [ i ] [ j ] stores the count of subsequence with ' a ' + i and ' a ' + j character ; Increment the count of subsequence j and s [ i ] ; Update the frequency array ; For 1 length subsequence ; For 2 length subsequence ; Return the final result ; Given string str ; Function call
import numpy NEW_LINE def findCount ( s ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE dp = [ [ 0 ] * 26 ] * 26 NEW_LINE freq = numpy . zeros ( 26 ) NEW_LINE dp = numpy . zeros ( [ 26 , 26 ] ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT dp [ j ] [ ord ( s [ i ] ) - ord ( ' a ' ) ] += freq [ j ] NEW_LINE DEDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT ans = max ( freq [ i ] , ans ) NEW_LINE DEDENT for i in range ( 0 , 26 ) : NEW_LINE INDENT for j in range ( 0 , 26 ) : NEW_LINE INDENT ans = max ( dp [ i ] [ j ] , ans ) NEW_LINE DEDENT DEDENT return int ( ans ) NEW_LINE DEDENT str = " acbab " NEW_LINE print ( findCount ( str ) ) NEW_LINE
Create an array such that XOR of subarrays of length K is X | Function to construct the array ; Creating a list of size K , initialised with 0 ; Initialising the first element with the given XOR ; Driver code
def constructArray ( N , K , X ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in range ( 0 , K ) : NEW_LINE INDENT ans . append ( 0 ) NEW_LINE DEDENT ans [ 0 ] = X NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT print ( ans [ i % K ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT N = 5 NEW_LINE K = 2 NEW_LINE X = 4 NEW_LINE constructArray ( N , K , X ) NEW_LINE
Two player game in which a player can remove all occurrences of a number | Python3 implementation for two player game in which a player can remove all occurrences of a number ; Function that print whether player1 can wins or loses ; Storing the number of occurrence of elements in unordered map ; Variable to check if the occurrence of repeated elements is >= 4 and multiple of 2 or not ; Count elements which occur more than once ; Driver code
from collections import defaultdict NEW_LINE def game ( v , n ) : NEW_LINE INDENT m = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( v [ i ] not in m ) : NEW_LINE INDENT m [ v [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ v [ i ] ] += 1 NEW_LINE DEDENT DEDENT count = 0 NEW_LINE check = 0 NEW_LINE for i in m . values ( ) : NEW_LINE INDENT if ( i > 1 ) : NEW_LINE INDENT if ( i >= 4 and i % 2 == 0 ) : NEW_LINE INDENT check += 1 NEW_LINE DEDENT count += 1 NEW_LINE DEDENT DEDENT if ( check % 2 != 0 ) : NEW_LINE INDENT flag = False NEW_LINE DEDENT if ( check % 2 != 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT elif ( n % 2 == 0 and count % 2 == 0 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 2 , 3 , 3 , 5 ] NEW_LINE size = len ( arr ) NEW_LINE game ( arr , size ) NEW_LINE DEDENT
Count of elements which form a loop in an Array according to given constraints | Python3 program to number of elements which form a cycle in an array ; Function to count number of elements forming a cycle ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code
import math NEW_LINE mod = 1000000007 NEW_LINE def solve ( A , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE parent = [ - 1 ] * n NEW_LINE vis = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = i NEW_LINE if ( parent [ j ] == - 1 ) : NEW_LINE INDENT while ( parent [ j ] == - 1 ) : NEW_LINE INDENT parent [ j ] = i NEW_LINE j = math . gcd ( j , A [ j ] ) % n NEW_LINE DEDENT if ( parent [ j ] == i ) : NEW_LINE INDENT while ( vis [ j ] == 0 ) : NEW_LINE INDENT vis [ j ] = 1 NEW_LINE cnt += 1 NEW_LINE j = math . gcd ( j , A [ j ] ) % n NEW_LINE DEDENT DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT A = [ 1 , 1 , 6 , 2 ] NEW_LINE n = len ( A ) NEW_LINE print ( solve ( A , n ) ) NEW_LINE
Sum and Product of all even digit sum Nodes of a Singly Linked List | Node of Linked List ; Function to insert a node at the beginning of the singly Linked List ; Insert the data ; Link old list to the new node ; Move head to pothe new node ; Function to find the digit sum for a number ; Return the sum ; Function to find the required sum and product ; Initialise the sum and product to 0 and 1 respectively ; Traverse the given linked list ; If current node has even digit sum then include it in resultant sum and product ; Find the sum and the product ; Print the final Sum and Product ; Driver Code ; Head of the linked list ; Create the linked list 15 . 16 . 8 . 6 . 13 ; Function call
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = head_ref NEW_LINE head_ref = new_node NEW_LINE return head_ref NEW_LINE DEDENT def digitSum ( num ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( num ) : NEW_LINE INDENT sum += ( num % 10 ) NEW_LINE num //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumAndProduct ( head_ref ) : NEW_LINE INDENT prod = 1 NEW_LINE sum = 0 NEW_LINE ptr = head_ref NEW_LINE while ( ptr != None ) : NEW_LINE INDENT if ( not ( digitSum ( ptr . data ) & 1 ) ) : NEW_LINE INDENT prod *= ptr . data NEW_LINE sum += ptr . data NEW_LINE DEDENT ptr = ptr . next NEW_LINE DEDENT print ( " Sum ▁ = " , sum ) NEW_LINE print ( " Product ▁ = " , prod ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 13 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 16 ) NEW_LINE head = push ( head , 15 ) NEW_LINE sumAndProduct ( head ) NEW_LINE DEDENT