codeparrot/xlcost-text-to-code huggingface dataset:
Probability of a key K present in array | Function to find the probability ; find probability upto 2 decimal places ; Driver Code
def kPresentProbability ( a , n , k ) : NEW_LINE INDENT count = a . count ( k ) NEW_LINE return round ( count / n , 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 4 , 7 , 2 , 0 , 8 , 7 , 5 ] NEW_LINE K = 2 NEW_LINE N = len ( A ) NEW_LINE print ( kPresentProbability ( A , N , K ) ) NEW_LINE DEDENT
Largest subarray having sum greater than k | Comparison function used to sort preSum vector . ; Function to find index in preSum vector upto which all prefix sum values are less than or equal to val . ; Starting and ending index of search space . ; To store required index value . ; If middle value is less than or equal to val then index can lie in mid + 1. . n else it lies in 0. . mid - 1. ; Function to find largest subarray having sum greater than or equal to k . ; Length of largest subarray . ; Vector to store pair of prefix sum and corresponding ending index value . ; To store the current value of prefix sum . ; To store minimum index value in range 0. . i of preSum vector . ; Insert values in preSum vector . ; Update minInd array . ; If sum is greater than k , then answer is i + 1. ; If sum is less than or equal to k , then find if there is a prefix array having sum that needs to be added to current sum to make its value greater than k . If yes , then compare length of updated subarray with maximum length found so far . ; Driver code .
def compare ( a , b ) : NEW_LINE INDENT if a [ 0 ] == b [ 0 ] : NEW_LINE INDENT return a [ 1 ] < b [ 1 ] NEW_LINE DEDENT return a [ 0 ] < b [ 0 ] NEW_LINE DEDENT def findInd ( preSum , n , val ) : NEW_LINE INDENT l , h = 0 , n - 1 NEW_LINE ans = - 1 NEW_LINE while l <= h : NEW_LINE INDENT mid = ( l + h ) // 2 NEW_LINE if preSum [ mid ] [ 0 ] <= val : NEW_LINE INDENT ans = mid NEW_LINE l = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT h = mid - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def largestSub ( arr , n , k ) : NEW_LINE INDENT maxlen = 0 NEW_LINE preSum = [ ] NEW_LINE Sum = 0 NEW_LINE minInd = [ None ] * ( n ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Sum = Sum + arr [ i ] NEW_LINE preSum . append ( [ Sum , i ] ) NEW_LINE DEDENT preSum . sort ( ) NEW_LINE minInd [ 0 ] = preSum [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT minInd [ i ] = min ( minInd [ i - 1 ] , preSum [ i ] [ 1 ] ) NEW_LINE DEDENT Sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Sum = Sum + arr [ i ] NEW_LINE if Sum > k : NEW_LINE INDENT maxlen = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT ind = findInd ( preSum , n , Sum - k - 1 ) NEW_LINE if ind != - 1 and minInd [ ind ] < i : NEW_LINE INDENT maxlen = max ( maxlen , i - minInd [ ind ] ) NEW_LINE DEDENT DEDENT DEDENT return maxlen NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 2 , 1 , 6 , - 3 ] NEW_LINE n = len ( arr ) NEW_LINE k = 5 NEW_LINE print ( largestSub ( arr , n , k ) ) NEW_LINE DEDENT
Find the slope of the given number | function to find slope of a number ; to store slope of the given number 'num ; loop from the 2 nd digit up to the 2 nd last digit of the given number 'num ; if the digit is a maxima ; if the digit is a minima ; required slope ; Driver Code
def slopeOfNum ( num , n ) : NEW_LINE ' NEW_LINE INDENT slope = 0 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( num [ i ] > num [ i - 1 ] and num [ i ] > num [ i + 1 ] ) : NEW_LINE INDENT slope += 1 NEW_LINE DEDENT elif ( num [ i ] < num [ i - 1 ] and num [ i ] < num [ i + 1 ] ) : NEW_LINE INDENT slope += 1 NEW_LINE DEDENT DEDENT return slope NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT num = "1213321" NEW_LINE n = len ( num ) NEW_LINE print ( " Slope ▁ = " , slopeOfNum ( num , n ) ) NEW_LINE DEDENT
Sudo Placement | Beautiful Pairs | Python3 code for finding required pairs ; The function to check if beautiful pair exists ; Set for hashing ; Traversing the first array ; Traversing the second array to check for every j corresponding to single i ; x + y = z = > x = y - z ; If such x exists then we return true ; Hash to make use of it next time ; No pair exists ; Driver Code ; If pair exists then 1 else 0 2 nd argument as size of first array fourth argument as sizeof 2 nd array
from typing import List NEW_LINE def pairExists ( arr1 : List [ int ] , m : int , arr2 : List [ int ] , n : int ) -> bool : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( arr2 [ 2 ] - arr1 [ 2 ] ) not in s : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT s . add ( arr1 [ i ] ) NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , 5 , 10 , 8 ] NEW_LINE arr2 = [ 2 , 20 , 13 ] NEW_LINE if ( pairExists ( arr1 , 4 , arr2 , 3 ) ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT DEDENT
Sudo Placement | Placement Tour | Python Program to find the optimal number of elements such that the cumulative value should be less than given number ; This function returns true if the value cumulative according to received integer K is less than budget B , otherwise returns false ; Initialize a temporary array which stores the cumulative value of the original array ; Sort the array to find the smallest K values ; Check if the value is less than budget ; This function prints the optimal number of elements and respective cumulative value which is less than the given number ; Initialize answer as zero as optimal value may not exists ; If the current Mid Value is an optimal value , then try to maximize it ; Call Again to set the corresponding cumulative value for the optimal ans ; Driver Code ; Budget
value = 0 NEW_LINE def canBeOptimalValue ( K : int , arr : list , N : int , B : int ) -> bool : NEW_LINE INDENT global value NEW_LINE tmp = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT tmp [ i ] = ( arr [ i ] + K * ( i + 1 ) ) NEW_LINE DEDENT tmp . sort ( ) NEW_LINE value = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT value += tmp [ i ] NEW_LINE DEDENT return value <= B NEW_LINE DEDENT def findNoOfElementsandValue ( arr : list , N : int , B : int ) : NEW_LINE INDENT global value NEW_LINE ans = 0 NEW_LINE value = 0 NEW_LINE while start <= end : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if canBeOptimalValue ( mid , arr , N , B ) : NEW_LINE INDENT ans = mid NEW_LINE start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT canBeOptimalValue ( ans , arr , N , B ) NEW_LINE print ( ans , value ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 6 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE B = 90 NEW_LINE findNoOfElementsandValue ( arr , N , B ) NEW_LINE DEDENT
Previous greater element | Python 3 program previous greater element A naive solution to print previous greater element for every element in an array . ; Previous greater for first element never exists , so we print - 1. ; Let us process remaining elements . ; Find first element on left side that is greater than arr [ i ] . ; If all elements on left are smaller . ; Driver code
def prevGreater ( arr , n ) : NEW_LINE INDENT print ( " - 1" , end = " , ▁ " ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT flag = 0 NEW_LINE for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if arr [ i ] < arr [ j ] : NEW_LINE INDENT print ( arr [ j ] , end = " , ▁ " ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if j == 0 and flag == 0 : NEW_LINE INDENT print ( " - 1" , end = " , ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 4 , 2 , 20 , 40 , 12 , 30 ] NEW_LINE n = len ( arr ) NEW_LINE prevGreater ( arr , n ) NEW_LINE DEDENT
Previous greater element | Python3 program to print previous greater element An efficient solution to print previous greater element for every element in an array . ; Create a stack and push index of first element to it ; Previous greater for first element is always - 1. ; Traverse remaining elements ; Pop elements from stack while stack is not empty and top of stack is smaller than arr [ i ] . We always have elements in decreasing order in a stack . ; If stack becomes empty , then no element is greater on left side . Else top of stack is previous greater . ; Driver code
import math as mt NEW_LINE def prevGreater ( arr , n ) : NEW_LINE INDENT s = list ( ) ; NEW_LINE s . append ( arr [ 0 ] ) NEW_LINE print ( " - 1 , ▁ " , end = " " ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT while ( len ( s ) > 0 and s [ - 1 ] < arr [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if len ( s ) == 0 : NEW_LINE INDENT print ( " - 1 , ▁ " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s [ - 1 ] , " , ▁ " , end = " " ) NEW_LINE DEDENT s . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT arr = [ 10 , 4 , 2 , 20 , 40 , 12 , 30 ] NEW_LINE n = len ( arr ) NEW_LINE prevGreater ( arr , n ) NEW_LINE
Duplicates in an array in O ( n ) time and by using O ( 1 ) extra space | Set | Function to find repeating elements ; Flag variable used to represent whether repeating element is found or not . ; Check if current element is repeating or not . If it is repeating then value will be greater than or equal to n . ; Check if it is first repetition or not . If it is first repetition then value at index arr [ i ] is less than 2 * n . Print arr [ i ] if it is first repetition . ; Add n to index arr [ i ] to mark presence of arr [ i ] or to mark repetition of arr [ i ] . ; If flag variable is not set then no repeating element is found . So print - 1. ; Driver Function
def printDuplicates ( arr , n ) : NEW_LINE INDENT fl = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ arr [ i ] % n ] >= n ) : NEW_LINE INDENT if ( arr [ arr [ i ] % n ] < 2 * n ) : NEW_LINE INDENT print ( arr [ i ] % n , end = " ▁ " ) NEW_LINE fl = 1 ; NEW_LINE DEDENT DEDENT arr [ arr [ i ] % n ] += n ; NEW_LINE DEDENT if ( fl == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT arr = [ 1 , 6 , 3 , 1 , 3 , 6 , 6 ] ; NEW_LINE arr_size = len ( arr ) ; NEW_LINE printDuplicates ( arr , arr_size ) ; NEW_LINE
Find the smallest positive number missing from an unsorted array | Set 2 | Function to find smallest positive missing number . ; to store next array element in current traversal ; if value is negative or greater than array size , then it cannot be marked in array . So move to next element . ; traverse the array until we reach at an element which is already marked or which could not be marked . ; find first array index which is not marked which is also the smallest positive missing number . ; if all indices are marked , then smallest missing positive number is array_size + 1. ; Driver code
def findMissingNo ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] <= 0 or arr [ i ] > n ) : NEW_LINE INDENT continue NEW_LINE DEDENT val = arr [ i ] NEW_LINE while ( arr [ val - 1 ] != val ) : NEW_LINE INDENT nextval = arr [ val - 1 ] NEW_LINE arr [ val - 1 ] = val NEW_LINE val = nextval NEW_LINE if ( val <= 0 or val > n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != i + 1 ) : NEW_LINE INDENT return i + 1 NEW_LINE DEDENT DEDENT return n + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 ] NEW_LINE arr_size = len ( arr ) NEW_LINE missing = findMissingNo ( arr , arr_size ) NEW_LINE print ( " The ▁ smallest ▁ positive " , " missing ▁ number ▁ is ▁ " , missing ) NEW_LINE DEDENT
Print all triplets with given sum | Prints all triplets in arr [ ] with given sum ; Driver code
def findTriplets ( arr , n , sum ) : NEW_LINE INDENT for i in range ( 0 , n - 2 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] + arr [ j ] + arr [ k ] == sum ) : NEW_LINE INDENT print ( arr [ i ] , " ▁ " , arr [ j ] , " ▁ " , arr [ k ] , sep = " " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT arr = [ 0 , - 1 , 2 , - 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE findTriplets ( arr , n , - 2 ) NEW_LINE
Print all triplets with given sum | Python3 program to find triplets in a given array whose Sum is equal to given sum . ; function to print triplets with given sum ; Find all pairs with Sum equals to " Sum - arr [ i ] " ; Driver code
import math as mt NEW_LINE def findTriplets ( arr , n , Sum ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT s = dict ( ) NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT x = Sum - ( arr [ i ] + arr [ j ] ) NEW_LINE if x in s . keys ( ) : NEW_LINE INDENT print ( x , arr [ i ] , arr [ j ] ) NEW_LINE DEDENT else : NEW_LINE INDENT s [ arr [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 0 , - 1 , 2 , - 3 , 1 ] NEW_LINE Sum = - 2 NEW_LINE n = len ( arr ) NEW_LINE findTriplets ( arr , n , Sum ) NEW_LINE
Maximum product quadruple ( sub | Python3 program to find a maximum product of a quadruple in array of integers ; Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; will contain max product ; Driver Code
import sys NEW_LINE def maxProduct ( arr , n ) : NEW_LINE INDENT if ( n < 4 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT max_product = - sys . maxsize ; NEW_LINE for i in range ( n - 3 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 2 ) : NEW_LINE INDENT for k in range ( j + 1 , n - 1 ) : NEW_LINE INDENT for l in range ( k + 1 , n ) : NEW_LINE INDENT max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] * arr [ l ] ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return max_product ; NEW_LINE DEDENT arr = [ 10 , 3 , 5 , 6 , 20 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE max = maxProduct ( arr , n ) ; NEW_LINE if ( max == - 1 ) : NEW_LINE INDENT print ( " No ▁ Quadruple ▁ Exists " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Maximum ▁ product ▁ is " , max ) ; NEW_LINE DEDENT
Maximum product quadruple ( sub | Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Sort the array in ascending order ; Return the maximum of x , y and z ; Driver Code
def maxProduct ( arr , n ) : NEW_LINE INDENT if ( n < 4 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT arr . sort ( ) NEW_LINE x = ( arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] * arr [ n - 4 ] ) NEW_LINE y = arr [ 0 ] * arr [ 1 ] * arr [ 2 ] * arr [ 3 ] NEW_LINE z = ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] * arr [ n - 2 ] ) NEW_LINE return max ( x , max ( y , z ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 10 , - 3 , 5 , 6 , - 20 ] NEW_LINE n = len ( arr ) NEW_LINE max = maxProduct ( arr , n ) NEW_LINE if ( max == - 1 ) : NEW_LINE INDENT print ( " No ▁ Quadruple ▁ Exists " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Maximum ▁ product ▁ is " , max ) NEW_LINE DEDENT DEDENT
Minimum value of " max ▁ + ▁ min " in a subarray | Python 3 program to find sum of maximum and minimum in any subarray of an array of positive numbers . ; Driver code
def maxSum ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT ans = arr [ 0 ] + arr [ 1 ] NEW_LINE for i in range ( 1 , n - 1 , 1 ) : NEW_LINE INDENT ans = min ( ans , ( arr [ i ] + arr [ i + 1 ] ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 12 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSum ( arr , n ) ) NEW_LINE DEDENT
Stella Octangula Number | Returns value of n * ( 2 * n * n - 1 ) ; Finds if a value of f ( n ) is equal to x where n is in interval [ low . . high ] ; Returns true if x isStella octangula number . Else returns false . ; Find ' high ' for binary search by repeated doubling ; If condition is satisfied for a power of 2. ; Call binary search ; Driver code
def f ( n ) : NEW_LINE INDENT return n * ( 2 * n * n - 1 ) ; NEW_LINE DEDENT def binarySearch ( low , high , x ) : NEW_LINE INDENT while ( low <= high ) : NEW_LINE INDENT mid = int ( ( low + high ) // 2 ) ; NEW_LINE if ( f ( mid ) < x ) : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT elif ( f ( mid ) > x ) : NEW_LINE INDENT high = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT def isStellaOctangula ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT i = 1 ; NEW_LINE while ( f ( i ) < x ) : NEW_LINE INDENT i = i * 2 ; NEW_LINE DEDENT if ( f ( i ) == x ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return binarySearch ( i / 2 , i , x ) ; NEW_LINE DEDENT n = 51 ; NEW_LINE if ( isStellaOctangula ( n ) == True ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Sort the given string using character search | Python 3 implementation to sort the given string without using any sorting technique ; A character array to store the no . of occurrences of each character between ' a ' to 'z ; to store the final sorted string ; To store each occurrence of character by relative indexing ; To traverse the character array and append it to new_str ; Driver program to test above
def sortString ( st , n ) : NEW_LINE ' NEW_LINE INDENT arr = [ 0 ] * 26 NEW_LINE new_str = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT while ( arr [ i ] > 0 ) : NEW_LINE INDENT new_str += chr ( i + ord ( ' a ' ) ) NEW_LINE arr [ i ] -= 1 NEW_LINE DEDENT DEDENT return new_str NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " geeksforgeeks " NEW_LINE n = len ( st ) NEW_LINE print ( sortString ( st , n ) ) NEW_LINE DEDENT
Maximum occurring character in a linked list | Link list node ; Storing element 's frequencies  in a hash table. ; Calculating the first maximum element ; Push a node to linked list . Note that this function changes the head ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = ' ' NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def maxChar ( head ) : NEW_LINE INDENT p = head NEW_LINE hash = [ 0 for i in range ( 256 ) ] NEW_LINE while ( p != None ) : NEW_LINE hash [ ord ( p . data ) ] += 1 NEW_LINE p = p . next NEW_LINE p = head NEW_LINE max = - 1 NEW_LINE res = ' ' NEW_LINE while ( p != None ) : NEW_LINE if ( max < hash [ ord ( p . data ) ] ) : NEW_LINE INDENT res = p . data NEW_LINE max = hash [ ord ( p . data ) ] NEW_LINE DEDENT p = p . next NEW_LINE return res NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE return head_ref NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE str = " skeegforskeeg " NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT head = push ( head , str [ i ] ) NEW_LINE DEDENT print ( maxChar ( head ) ) NEW_LINE DEDENT
Maximum sum of elements from each row in the matrix | Python Program to find row - wise maximum element sum considering elements in increasing order . ; Function to perform given task ; Getting the maximum element from last row ; Comparing it with the elements of above rows ; Maximum of current row . ; If we could not an element smaller than prev_max . ; Driver code
N = 3 NEW_LINE def getGreatestSum ( a ) : NEW_LINE INDENT prev_max = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( prev_max < a [ N - 1 ] [ j ] ) : NEW_LINE INDENT prev_max = a [ N - 1 ] [ j ] NEW_LINE DEDENT DEDENT sum = prev_max NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT curr_max = - 2147483648 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( prev_max > a [ i ] [ j ] and a [ i ] [ j ] > curr_max ) : NEW_LINE INDENT curr_max = a [ i ] [ j ] NEW_LINE DEDENT DEDENT if ( curr_max == - 2147483648 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT prev_max = curr_max NEW_LINE sum = sum + prev_max NEW_LINE DEDENT return sum NEW_LINE DEDENT a = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE print ( getGreatestSum ( a ) ) NEW_LINE b = [ [ 4 , 5 , 6 ] , [ 4 , 5 , 6 ] , [ 4 , 5 , 6 ] ] NEW_LINE print ( getGreatestSum ( b ) ) NEW_LINE
Find the number of times every day occurs in a month | Python program to count occurrence of days in a month ; function to find occurrences ; stores days in a week ; Initialize all counts as 4. ; find index of the first day ; number of days whose occurrence will be 5 ; mark the occurrence to be 5 of n - 28 days ; print the days ; driver program to test the above function
import math NEW_LINE def occurrenceDays ( n , firstday ) : NEW_LINE INDENT days = [ " Monday " , " Tuesday " , " Wednesday " , " Thursday " , " Friday " , " Saturday " , " Sunday " ] NEW_LINE count = [ 4 for i in range ( 0 , 7 ) ] NEW_LINE pos = - 1 NEW_LINE for i in range ( 0 , 7 ) : NEW_LINE INDENT if ( firstday == days [ i ] ) : NEW_LINE INDENT pos = i NEW_LINE break NEW_LINE DEDENT DEDENT inc = n - 28 NEW_LINE for i in range ( pos , pos + inc ) : NEW_LINE INDENT if ( i > 6 ) : NEW_LINE INDENT count [ i % 7 ] = 5 NEW_LINE DEDENT else : NEW_LINE INDENT count [ i ] = 5 NEW_LINE DEDENT DEDENT for i in range ( 0 , 7 ) : NEW_LINE INDENT print ( days [ i ] , " ▁ " , count [ i ] ) NEW_LINE DEDENT DEDENT n = 31 NEW_LINE firstday = " Tuesday " NEW_LINE occurrenceDays ( n , firstday ) NEW_LINE
Value of k | Python3 code to find k - th element after append and insert middle operations ; ans = n Middle element of the sequence ; length of the resulting sequence . ; Updating the middle element of next sequence ; Moving to the left side of the middle element . ; Moving to the right side of the middle element . ; Driver code
import math NEW_LINE def findElement ( n , k ) : NEW_LINE INDENT left = 1 NEW_LINE right = math . pow ( 2 , n ) - 1 NEW_LINE while 1 : NEW_LINE INDENT mid = int ( ( left + right ) / 2 ) NEW_LINE if k == mid : NEW_LINE INDENT print ( ans ) NEW_LINE break NEW_LINE DEDENT ans -= 1 NEW_LINE if k < mid : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT DEDENT DEDENT n = 4 NEW_LINE k = 8 NEW_LINE findElement ( n , k ) NEW_LINE
First common element in two linked lists | Python3 program to find first common element in two unsorted linked list ; Link list node ; A utility function to insert a node at the beginning of a linked list ; Returns the first repeating element in linked list ; Traverse through every node of first list ; If current node is present in second list ; If no common node ; Driver code
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data 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 . data = new_data NEW_LINE new_node . next = head_ref NEW_LINE head_ref = new_node NEW_LINE return head_ref NEW_LINE DEDENT def firstCommon ( head1 , head2 ) : NEW_LINE INDENT while ( head1 != None ) : NEW_LINE INDENT p = head2 NEW_LINE while ( p != None ) : NEW_LINE INDENT if ( p . data == head1 . data ) : NEW_LINE INDENT return head1 . data NEW_LINE DEDENT p = p . next NEW_LINE DEDENT head1 = head1 . next NEW_LINE DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head1 = None NEW_LINE head1 = push ( head1 , 20 ) NEW_LINE head1 = push ( head1 , 5 ) NEW_LINE head1 = push ( head1 , 15 ) NEW_LINE head1 = push ( head1 , 10 ) NEW_LINE head2 = None NEW_LINE head2 = push ( head2 , 10 ) NEW_LINE head2 = push ( head2 , 2 ) NEW_LINE head2 = push ( head2 , 15 ) NEW_LINE head2 = push ( head2 , 8 ) NEW_LINE print ( firstCommon ( head1 , head2 ) ) NEW_LINE DEDENT