id
stringlengths
18
123
pair
stringclasses
6 values
Code1
stringlengths
41
1.98k
Code2
stringlengths
41
1.98k
instruction
stringclasses
6 values
input
stringlengths
56
2k
cpp-python-PROGRAM_CHECK_DIAGONAL_MATRIX_SCALAR_MATRIX
cpp-python
bool isDiagonalMatrix ( int mat [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; return true ; }
def isDiagonalMatrix ( mat ) : for i in range ( 0 , N ) : for j in range ( 0 , N ) : if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isDiagonalMatrix ( int mat [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; return true ; } </Code>
cpp-python-SORT_3_INTEGERS_WITHOUT_USING_CONDITION_USING_MAX_FUNCTION
cpp-python
void printSorted ( int a , int b , int c ) { int get_max = max ( a , max ( b , c ) ) ; int get_min = - max ( - a , max ( - b , - c ) ) ; int get_mid = ( a + b + c ) - ( get_max + get_min ) ; cout << get_min << " " << get_mid << " " << get_max ; }
def printSorted ( a , b , c ) : get_max = max ( a , max ( b , c ) ) get_min = - max ( - a , max ( - b , - c ) ) get_mid = ( a + b + c ) - ( get_max + get_min ) print ( get_min , " " , get_mid , " " , get_max )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printSorted ( int a , int b , int c ) { int get_max = max ( a , max ( b , c ) ) ; int get_min = - max ( - a , max ( - b , - c ) ) ; int get_mid = ( a + b + c ) - ( get_max + get_min ) ; cout << get_min << " " << get_mid << " " << get_max ; } </Code>
cpp-python-NUMBER_IS_DIVISIBLE_BY_29_OR_NOT
cpp-python
bool isDivisible ( long long int n ) { while ( n / 100 ) { int last_digit = n % 10 ; n /= 10 ; n += last_digit * 3 ; } return ( n % 29 == 0 ) ; }
def isDivisible ( n ) : while ( int ( n / 100 ) ) : last_digit = int ( n % 10 ) n = int ( n / 10 ) n += last_digit * 3 return ( n % 29 == 0 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isDivisible ( long long int n ) { while ( n / 100 ) { int last_digit = n % 10 ; n /= 10 ; n += last_digit * 3 ; } return ( n % 29 == 0 ) ; } </Code>
cpp-python-FIND_THE_ELEMENT_THAT_APPEARS_ONCE
cpp-python
int getSingle ( int arr [ ] , int n ) { int ones = 0 , twos = 0 ; int common_bit_mask ; for ( int i = 0 ; i < n ; i ++ ) { twos = twos
def getSingle ( arr , n ) : ones = 0 twos = 0 for i in range ( n ) : twos = twos
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getSingle ( int arr [ ] , int n ) { int ones = 0 , twos = 0 ; int common_bit_mask ; for ( int i = 0 ; i < n ; i ++ ) { twos = twos </Code>
cpp-python-MINIMUM_SUM_PATH_TRIANGLE
cpp-python
int minSumPath ( vector < vector < int >> & A ) { int memo [ A . size ( ) ] ; int n = A . size ( ) - 1 ; for ( int i = 0 ; i < A [ n ] . size ( ) ; i ++ ) memo [ i ] = A [ n ] [ i ] ; for ( int i = A . size ( ) - 2 ; i >= 0 ; i -- ) for ( int j = 0 ; j < A [ i ] . size ( ) ; j ++ ) memo [ j ] = A [ ...
def minSumPath ( A ) : memo = [ None ] * len ( A ) n = len ( A ) - 1 for i in range ( len ( A [ n ] ) ) : memo [ i ] = A [ n ] [ i ] for i in range ( len ( A ) - 2 , - 1 , - 1 ) : for j in range ( len ( A [ i ] ) ) : memo [ j ] = A [ i ] [ j ] + min ( memo [ j ] , memo [ j + ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minSumPath ( vector < vector < int >> & A ) { int memo [ A . size ( ) ] ; int n = A . size ( ) - 1 ; for ( int i = 0 ; i < A [ n ] . size ( ) ; i ++ ) memo [ i ] = A [ n ] [ i ] ; for ( int i = A . size ( ) - 2 ; i >= 0 ; i -- ) for ( int j = 0 ; j < A [ i ] . size ( ) ; j ++ ) memo [ j ]...
cpp-python-REARRANGE_POSITIVE_AND_NEGATIVE_NUMBERS_PUBLISH
cpp-python
void GFG :: rearrange ( int arr [ ] , int n ) { int i = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < 0 ) { i ++ ; swap ( & arr [ i ] , & arr [ j ] ) ; } } int pos = i + 1 , neg = 0 ; while ( pos < n && neg < pos && arr [ neg ] < 0 ) { swap ( & arr [ neg ] , & arr [ pos...
def rearrange ( arr , n ) : i = - 1 for j in range ( n ) : if ( arr [ j ] < 0 ) : i += 1 arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] pos , neg = i + 1 , 0 while ( pos < n and neg < pos and arr [ neg ] < 0 ) : arr [ neg ] , arr [ pos ] = arr [ pos ] , arr [ neg ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void GFG :: rearrange ( int arr [ ] , int n ) { int i = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < 0 ) { i ++ ; swap ( & arr [ i ] , & arr [ j ] ) ; } } int pos = i + 1 , neg = 0 ; while ( pos < n && neg < pos && arr [ neg ] < 0 ) { swap ( & arr [ neg ] , & ar...
cpp-python-ELEMENTS_TO_BE_ADDED_SO_THAT_ALL_ELEMENTS_OF_A_RANGE_ARE_PRESENT_IN_ARRAY
cpp-python
int countNum ( int arr [ ] , int n ) { int count = 0 ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; }
def countNum ( arr , n ) : count = 0 arr.sort ( ) for i in range ( 0 , n - 1 ) : if ( arr [ i ] != arr [ i + 1 ] and arr [ i ] != arr [ i + 1 ] - 1 ) : count += arr [ i + 1 ] - arr [ i ] - 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countNum ( int arr [ ] , int n ) { int count = 0 ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; } </Code>
cpp-python-LARGEST_SUBSEQUENCE_GCD_GREATER_1
cpp-python
int largestGCDSubsequence ( int arr [ ] , int n ) { int ans = 0 ; int maxele = * max_element ( arr , arr + n ) ; for ( int i = 2 ; i <= maxele ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( arr [ j ] % i == 0 ) ++ count ; } ans = max ( ans , count ) ; } re...
def largestGCDSubsequence ( arr , n ) : ans = 0 maxele = max ( arr ) for i in range ( 2 , maxele + 1 ) : count = 0 for j in range ( n ) : if ( arr [ j ] % i == 0 ) : count += 1 ans = max ( ans , count ) return ans
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int largestGCDSubsequence ( int arr [ ] , int n ) { int ans = 0 ; int maxele = * max_element ( arr , arr + n ) ; for ( int i = 2 ; i <= maxele ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( arr [ j ] % i == 0 ) ++ count ; } ans = max ( ans , count ) ; ...
cpp-python-PROGRAM_CHECK_INPUT_INTEGER_STRING
cpp-python
bool isNumber ( string s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( isdigit ( s [ i ] ) == false ) return false ; return true ; }
def isNumber ( s ) : for i in range ( len ( s ) ) : if s [ i ].isdigit ( ) != True : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isNumber ( string s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( isdigit ( s [ i ] ) == false ) return false ; return true ; } </Code>
cpp-python-COUNT_PAIRS_TWO_SORTED_ARRAYS_WHOSE_SUM_EQUAL_GIVEN_VALUE_X_1
cpp-python
int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; unordered_set < int > us ; for ( int i = 0 ; i < m ; i ++ ) us . insert ( arr1 [ i ] ) ; for ( int j = 0 ; j < n ; j ++ ) if ( us . find ( x - arr2 [ j ] ) != us . end ( ) ) count ++ ; return count ; }
def countPairs ( arr1 , arr2 , m , n , x ) : count = 0 us = set ( ) for i in range ( m ) : us.add ( arr1 [ i ] ) for j in range ( n ) : if x - arr2 [ j ] in us : count += 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; unordered_set < int > us ; for ( int i = 0 ; i < m ; i ++ ) us . insert ( arr1 [ i ] ) ; for ( int j = 0 ; j < n ; j ++ ) if ( us . find ( x - arr2 [ j ] ) != us . end ( ) ) count ++ ; return count ; } </C...
cpp-python-FIND_NUMBER_PERFECT_SQUARES_TWO_GIVEN_NUMBERS
cpp-python
int countSquares ( int a , int b ) { int cnt = 0 ; for ( int i = a ; i <= b ; i ++ ) for ( int j = 1 ; j * j <= i ; j ++ ) if ( j * j == i ) cnt ++ ; return cnt ; }
def CountSquares ( a , b ) : cnt = 0 for i in range ( a , b + 1 ) : j = 1 ; while j * j <= i : if j * j == i : cnt = cnt + 1 j = j + 1 i = i + 1 return cnt
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSquares ( int a , int b ) { int cnt = 0 ; for ( int i = a ; i <= b ; i ++ ) for ( int j = 1 ; j * j <= i ; j ++ ) if ( j * j == i ) cnt ++ ; return cnt ; } </Code>
cpp-python-MINIMIZE_THE_SUM_OF_DIGITS_OF_A_AND_B_SUCH_THAT_A_B_N
cpp-python
int minSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } if ( sum == 1 ) return 10 ; return sum ; }
def minSum ( n ) : sum = 0 while ( n > 0 ) : sum += ( n % 10 ) n //= 10 if ( sum == 1 ) : return 10 return sum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } if ( sum == 1 ) return 10 ; return sum ; } </Code>
cpp-python-NTH_NON_FIBONACCI_NUMBER
cpp-python
int nonFibonacci ( int n ) { int prevPrev = 1 , prev = 2 , curr = 3 ; while ( n > 0 ) { prevPrev = prev ; prev = curr ; curr = prevPrev + prev ; n = n - ( curr - prev - 1 ) ; } n = n + ( curr - prev - 1 ) ; return prev + n ; }
def nonFibonacci ( n ) : prevPrev = 1 prev = 2 curr = 3 while n > 0 : prevPrev = prev prev = curr curr = prevPrev + prev n = n - ( curr - prev - 1 ) n = n + ( curr - prev - 1 ) return prev + n
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int nonFibonacci ( int n ) { int prevPrev = 1 , prev = 2 , curr = 3 ; while ( n > 0 ) { prevPrev = prev ; prev = curr ; curr = prevPrev + prev ; n = n - ( curr - prev - 1 ) ; } n = n + ( curr - prev - 1 ) ; return prev + n ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_3_LONGEST_INCREASING_SUBSEQUENCE_1
cpp-python
int lis ( int arr [ ] , int n ) { int lis [ n ] ; lis [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { lis [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } return * max_element ( lis , lis + n ) ; }
def lis ( arr ) : n = len ( arr ) lis = [ 1 ] * n for i in range ( 1 , n ) : for j in range ( 0 , i ) : if arr [ i ] > arr [ j ] and lis [ i ] < lis [ j ] + 1 : lis [ i ] = lis [ j ] + 1 maximum = 0 for i in range ( n ) : maximum = max ( maximum , lis [ i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lis ( int arr [ ] , int n ) { int lis [ n ] ; lis [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { lis [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } return * max_element ( lis , lis + n ) ; } <...
cpp-python-QUICKLY_FIND_MULTIPLE_LEFT_ROTATIONS_OF_AN_ARRAY
cpp-python
void leftRotate ( int arr [ ] , int n , int k ) { for ( int i = k ; i < k + n ; i ++ ) cout << arr [ i % n ] << " " ; }
def leftRotate ( arr , n , k ) : for i in range ( k , k + n ) : print ( str ( arr [ i % n ] ) , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void leftRotate ( int arr [ ] , int n , int k ) { for ( int i = k ; i < k + n ; i ++ ) cout << arr [ i % n ] << " " ; } </Code>
cpp-python-C_PROGRAM_CONCATENATE_STRING_GIVEN_NUMBER_TIMES
cpp-python
string repeat ( string s , int n ) { string s1 = s ; for ( int i = 1 ; i < n ; i ++ ) s += s1 ; return s ; }
def repeat ( s , n ) : s1 = s for i in range ( 1 , n ) : s += s1 return s
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string repeat ( string s , int n ) { string s1 = s ; for ( int i = 1 ; i < n ; i ++ ) s += s1 ; return s ; } </Code>
cpp-python-SMALLEST_WINDOW_CONTAINS_CHARACTERS_STRING
cpp-python
string findSubString ( string str ) { int n = str . length ( ) ; int dist_count = 0 ; bool visited [ MAX_CHARS ] = { false } ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ str [ i ] ] == false ) { visited [ str [ i ] ] = true ; dist_count ++ ; } } int s...
def findSubString ( str ) : n = len ( str ) dist_count = len ( set ( [ x for x in str ] ) ) count , start , start_index , min_len = 0 , 0 , - 1 , 9999999999 curr_count = defaultdict ( lambda : 0 ) for j in range ( n ) : curr_count [ str [ j ] ] += 1 if curr_count [ str [ j ] ] == 1 :...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string findSubString ( string str ) { int n = str . length ( ) ; int dist_count = 0 ; bool visited [ MAX_CHARS ] = { false } ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ str [ i ] ] == false ) { visited [ str [ i ] ] = true ; dist_count ++ ; } } ...
cpp-python-COUNT_WAYS_REACH_NTH_STAIR_USING_STEP_1_2_3
cpp-python
public : int findStep ( int n ) { if ( n == 1 || n == 0 ) return 1 ; else if ( n == 2 ) return 2 ; else return findStep ( n - 3 ) + findStep ( n - 2 ) + findStep ( n - 1 ) ; }
def findStep ( n ) : if ( n == 1 or n == 0 ) : return 1 elif ( n == 2 ) : return 2 else : return findStep ( n - 3 ) + findStep ( n - 2 ) + findStep ( n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> public : int findStep ( int n ) { if ( n == 1 || n == 0 ) return 1 ; else if ( n == 2 ) return 2 ; else return findStep ( n - 3 ) + findStep ( n - 2 ) + findStep ( n - 1 ) ; } </Code>
cpp-python-SUBARRAYSUBSTRING_VS_SUBSEQUENCE_AND_PROGRAMS_TO_GENERATE_THEM_1
cpp-python
void printSubsequences ( int arr [ ] , int n ) { unsigned int opsize = pow ( 2 , n ) ; for ( int counter = 1 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( counter & ( 1 << j ) ) cout << arr [ j ] << " " ; } cout << endl ; } }
def printSubsequences ( arr , n ) : opsize = math.pow ( 2 , n ) for counter in range ( 1 , ( int ) ( opsize ) ) : for j in range ( 0 , n ) : if ( counter & ( 1 << j ) ) : print ( arr [ j ] , end = " " ) print ( )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printSubsequences ( int arr [ ] , int n ) { unsigned int opsize = pow ( 2 , n ) ; for ( int counter = 1 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( counter & ( 1 << j ) ) cout << arr [ j ] << " " ; } cout << endl ; } } </Code>
cpp-python-MINIMUM_XOR_VALUE_PAIR_1
cpp-python
int minXOR ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int minXor = INT_MAX ; int val = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { val = arr [ i ] ^ arr [ i + 1 ] ; minXor = min ( minXor , val ) ; } return minXor ; }
def minXOR ( arr , n ) : arr.sort ( ) minXor = int ( sys.float_info.max ) val = 0 for i in range ( 0 , n - 1 ) : val = arr [ i ] ^ arr [ i + 1 ] minXor = min ( minXor , val ) return minXor
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minXOR ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int minXor = INT_MAX ; int val = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { val = arr [ i ] ^ arr [ i + 1 ] ; minXor = min ( minXor , val ) ; } return minXor ; } </Code>
cpp-python-DOOLITTLE_ALGORITHM_LU_DECOMPOSITION
cpp-python
void luDecomposition ( int mat [ ] [ MAX ] , int n ) { int lower [ n ] [ n ] , upper [ n ] [ n ] ; memset ( lower , 0 , sizeof ( lower ) ) ; memset ( upper , 0 , sizeof ( upper ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = i ; k < n ; k ++ ) { int sum = 0 ; for ( int j = 0 ;...
def luDecomposition ( mat , n ) : lower = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] upper = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] for i in range ( n ) : for k in range ( i , n ) : sum = 0 for j in range ( i ) : sum += ( lower [ i ] [ j ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void luDecomposition ( int mat [ ] [ MAX ] , int n ) { int lower [ n ] [ n ] , upper [ n ] [ n ] ; memset ( lower , 0 , sizeof ( lower ) ) ; memset ( upper , 0 , sizeof ( upper ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = i ; k < n ; k ++ ) { int sum = 0 ; for ( int ...
cpp-python-BINARY_REPRESENTATION_OF_A_GIVEN_NUMBER_1
cpp-python
void bin ( unsigned n ) { if ( n > 1 ) bin ( n >> 1 ) ; printf ( "%d" , n & 1 ) ; }
def bin ( n ) : if ( n > 1 ) : bin ( n >> 1 ) print ( n & 1 , end = "" )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void bin ( unsigned n ) { if ( n > 1 ) bin ( n >> 1 ) ; printf ( "%d" , n & 1 ) ; } </Code>
cpp-python-COUNT_PALINDROME_SUB_STRINGS_STRING
cpp-python
int CountPS ( char str [ ] , int n ) { int dp [ n ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; bool P [ n ] [ n ] ; memset ( P , false , sizeof ( P ) ) ; for ( int i = 0 ; i < n ; i ++ ) P [ i ] [ i ] = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { P [ ...
def CountPS ( str , n ) : dp = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] P = [ [ False for x in range ( n ) ] for y in range ( n ) ] for i in range ( n ) : P [ i ] [ i ] = True for i in range ( n - 1 ) : if ( str [ i ] == str [ i + 1 ] ) : P [ i ] [ i + 1 ] = True ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int CountPS ( char str [ ] , int n ) { int dp [ n ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; bool P [ n ] [ n ] ; memset ( P , false , sizeof ( P ) ) ; for ( int i = 0 ; i < n ; i ++ ) P [ i ] [ i ] = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { ...
cpp-python-GIVEN_TWO_NUMBERS_B_FIND_X_X_B
cpp-python
void modularEquation ( int a , int b ) { if ( a < b ) { cout << "No solution possible " << endl ; return ; } if ( a == b ) { cout << "Infinite Solution possible " << endl ; return ; } int count = 0 ; int n = a - b ; int y = sqrt ( a - b ) ; for ( int i = 1 ; i <= y ; ++ i ) { if ...
def modularEquation ( a , b ) : if ( a < b ) : print ( "No solution possible " ) return if ( a == b ) : print ( "Infinite Solution possible " ) return count = 0 n = a - b y = ( int ) ( math.sqrt ( a - b ) ) for i in range ( 1 , y + 1 ) : if ( n % i == 0 ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void modularEquation ( int a , int b ) { if ( a < b ) { cout << "No solution possible " << endl ; return ; } if ( a == b ) { cout << "Infinite Solution possible " << endl ; return ; } int count = 0 ; int n = a - b ; int y = sqrt ( a - b ) ; for ( int i = 1 ; i <= y ; ++ i ) { ...
cpp-python-PAPER_CUT_MINIMUM_NUMBER_SQUARES
cpp-python
int minimumSquare ( int a , int b ) { long long result = 0 , rem = 0 ; if ( a < b ) swap ( a , b ) ; while ( b > 0 ) { result += a / b ; long long rem = a % b ; a = b ; b = rem ; } return result ; }
def minimumSquare ( a , b ) : result = 0 rem = 0 if ( a < b ) : a , b = b , a while ( b > 0 ) : result += int ( a / b ) rem = int ( a % b ) a = b b = rem return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minimumSquare ( int a , int b ) { long long result = 0 , rem = 0 ; if ( a < b ) swap ( a , b ) ; while ( b > 0 ) { result += a / b ; long long rem = a % b ; a = b ; b = rem ; } return result ; } </Code>
cpp-python-MAXIMUM_SUM_PATH_MATRIX_TOP_BOTTOM
cpp-python
int maxSum ( int mat [ SIZE ] [ SIZE ] , int n ) { if ( n == 1 ) return mat [ 0 ] [ 0 ] ; int dp [ n ] [ n ] ; int maxSum = INT_MIN , max ; for ( int j = 0 ; j < n ; j ++ ) dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) {...
def maxSum ( mat , n ) : if n == 1 : return mat [ 0 ] [ 0 ] dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] maxSum = INT_MIN for j in range ( n ) : dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] for i in range ( n - 2 , - 1 , - 1 ) : for j in range ( n ) : maxi...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSum ( int mat [ SIZE ] [ SIZE ] , int n ) { if ( n == 1 ) return mat [ 0 ] [ 0 ] ; int dp [ n ] [ n ] ; int maxSum = INT_MIN , max ; for ( int j = 0 ; j < n ; j ++ ) dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j...
cpp-python-MINIMUM_SUM_ABSOLUTE_DIFFERENCE_PAIRS_TWO_ARRAYS
cpp-python
long long int findMinSum ( int a [ ] , int b [ ] , int n ) { sort ( a , a + n ) ; sort ( b , b + n ) ; long long int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + abs ( a [ i ] - b [ i ] ) ; return sum ; }
def findMinSum ( a , b , n ) : a.sort ( ) b.sort ( ) sum = 0 for i in range ( n ) : sum = sum + abs ( a [ i ] - b [ i ] ) return sum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int findMinSum ( int a [ ] , int b [ ] , int n ) { sort ( a , a + n ) ; sort ( b , b + n ) ; long long int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + abs ( a [ i ] - b [ i ] ) ; return sum ; } </Code>
cpp-python-DELETE_ARRAY_ELEMENTS_WHICH_ARE_SMALLER_THAN_NEXT_OR_BECOME_SMALLER
cpp-python
void deleteElements ( int arr [ ] , int n , int k ) { stack < int > s ; s . push ( arr [ 0 ] ) ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) { while ( ! s . empty ( ) && s . top ( ) < arr [ i ] && count < k ) { s . pop ( ) ; count ++ ; } s . push ( arr [ i ] ) ; } int m = s ...
def deleteElements ( arr , n , k ) : st = [ ] st.append ( arr [ 0 ] ) top = 0 count = 0 for i in range ( 1 , n ) : while ( len ( st ) != 0 and count < k and st [ top ] < arr [ i ] ) : st.pop ( ) count += 1 top -= 1 st.append ( arr [ i ] ) t...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void deleteElements ( int arr [ ] , int n , int k ) { stack < int > s ; s . push ( arr [ 0 ] ) ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) { while ( ! s . empty ( ) && s . top ( ) < arr [ i ] && count < k ) { s . pop ( ) ; count ++ ; } s . push ( arr [ i ] ) ; } int...
cpp-python-FIRST_UPPERCASE_LETTER_IN_A_STRING_ITERATIVE_AND_RECURSIVE
cpp-python
char first ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( isupper ( str [ i ] ) ) return str [ i ] ; return 0 ; }
def first ( str ) : for i in range ( 0 , len ( str ) ) : if ( str [ i ].istitle ( ) ) : return str [ i ] return 0
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> char first ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( isupper ( str [ i ] ) ) return str [ i ] ; return 0 ; } </Code>
cpp-python-FIND_THE_MINIMUM_DISTANCE_BETWEEN_TWO_NUMBERS
cpp-python
int minDist ( int arr [ ] , int n , int x , int y ) { int i , j ; int min_dist = INT_MAX ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( ( x == arr [ i ] && y == arr [ j ] || y == arr [ i ] && x == arr [ j ] ) && min_dist > abs ( i - j ) ) { min_dist = abs ( ...
def minDist ( arr , n , x , y ) : min_dist = 99999999 for i in range ( n ) : for j in range ( i + 1 , n ) : if ( x == arr [ i ] and y == arr [ j ] or y == arr [ i ] and x == arr [ j ] ) and min_dist > abs ( i - j ) : min_dist = abs ( i - j ) return min_dist
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minDist ( int arr [ ] , int n , int x , int y ) { int i , j ; int min_dist = INT_MAX ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( ( x == arr [ i ] && y == arr [ j ] || y == arr [ i ] && x == arr [ j ] ) && min_dist > abs ( i - j ) ) { min_dist =...
cpp-python-ARC_LENGTH_ANGLE
cpp-python
double arcLength ( double diameter , double angle ) { double pi = 22.0 / 7.0 ; double arc ; if ( angle >= 360 ) { cout << "Angle cannot" , " be formed" ; return 0 ; } else { arc = ( pi * diameter ) * ( angle / 360.0 ) ; return arc ; } }
def arcLength ( diameter , angle ) : if angle >= 360 : print ( "Angle cannot be formed" ) return 0 else : arc = ( 3.142857142857143 * diameter ) * ( angle / 360.0 ) return arc
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double arcLength ( double diameter , double angle ) { double pi = 22.0 / 7.0 ; double arc ; if ( angle >= 360 ) { cout << "Angle cannot" , " be formed" ; return 0 ; } else { arc = ( pi * diameter ) * ( angle / 360.0 ) ; return arc ; } } </Code>
cpp-python-GIVEN_AN_ARRAY_OF_PAIRS_FIND_ALL_SYMMETRIC_PAIRS_IN_IT
cpp-python
void findSymPairs ( int arr [ ] [ 2 ] , int row ) { unordered_map < int , int > hM ; for ( int i = 0 ; i < row ; i ++ ) { int first = arr [ i ] [ 0 ] ; int sec = arr [ i ] [ 1 ] ; if ( hM . find ( sec ) != hM . end ( ) && hM [ sec ] == first ) cout << "(" << sec << ", " << first << ")" << endl ; ...
def findSymPairs ( arr , row ) : hM = dict ( ) for i in range ( row ) : first = arr [ i ] [ 0 ] sec = arr [ i ] [ 1 ] if ( sec in hM.keys ( ) and hM [ sec ] == first ) : print ( "(" , sec , "," , first , ")" ) else : hM [ first ] = sec
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findSymPairs ( int arr [ ] [ 2 ] , int row ) { unordered_map < int , int > hM ; for ( int i = 0 ; i < row ; i ++ ) { int first = arr [ i ] [ 0 ] ; int sec = arr [ i ] [ 1 ] ; if ( hM . find ( sec ) != hM . end ( ) && hM [ sec ] == first ) cout << "(" << sec << ", " << first << ")" << end...
cpp-python-CHECK_IF_A_STRING_HAS_ALL_CHARACTERS_WITH_SAME_FREQUENCY_WITH_ONE_VARIATION_ALLOWED
cpp-python
bool isValidString ( string str ) { int freq [ CHARS ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) freq [ str [ i ] - 'a' ] ++ ; int i , freq1 = 0 , count_freq1 = 0 ; for ( i = 0 ; i < CHARS ; i ++ ) { if ( freq [ i ] != 0 ) { freq1 = freq [ i ] ; ...
def isValidString ( str ) : freq = [ 0 ] * CHARS for i in range ( len ( str ) ) : freq [ ord ( str [ i ] ) - ord ( 'a' ) ] += 1 freq1 = 0 count_freq1 = 0 for i in range ( CHARS ) : if ( freq [ i ] != 0 ) : freq1 = freq [ i ] count_freq1 = 1 break ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isValidString ( string str ) { int freq [ CHARS ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) freq [ str [ i ] - 'a' ] ++ ; int i , freq1 = 0 , count_freq1 = 0 ; for ( i = 0 ; i < CHARS ; i ++ ) { if ( freq [ i ] != 0 ) { freq1 = freq [ i ] ;...
cpp-python-PRIME_NUMBERS
cpp-python
bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; }
def isPrime ( n ) : if ( n <= 1 ) : return False for i in range ( 2 , n ) : if ( n % i == 0 ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } </Code>
cpp-python-FIND_MAXIMUM_AVERAGE_SUBARRAY_OF_K_LENGTH
cpp-python
int findMaxAverage ( int arr [ ] , int n , int k ) { if ( k > n ) return - 1 ; int * csum = new int [ n ] ; csum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) csum [ i ] = csum [ i - 1 ] + arr [ i ] ; int max_sum = csum [ k - 1 ] , max_end = k - 1 ; for ( int i = k ; i < n ; i ++ ) { int ...
def findMaxAverage ( arr , n , k ) : if k > n : return - 1 csum = [ 0 ] * n csum [ 0 ] = arr [ 0 ] for i in range ( 1 , n ) : csum [ i ] = csum [ i - 1 ] + arr [ i ] max_sum = csum [ k - 1 ] max_end = k - 1 for i in range ( k , n ) : curr_sum = csum [ i ] - csum [ i -...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMaxAverage ( int arr [ ] , int n , int k ) { if ( k > n ) return - 1 ; int * csum = new int [ n ] ; csum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) csum [ i ] = csum [ i - 1 ] + arr [ i ] ; int max_sum = csum [ k - 1 ] , max_end = k - 1 ; for ( int i = k ; i < n ; i ++ ) { ...
cpp-python-DIVIDE_CUBOID_CUBES_SUM_VOLUMES_MAXIMUM
cpp-python
void maximizecube ( int l , int b , int h ) { int side = __gcd ( l , __gcd ( b , h ) ) ; int num = l / side ; num = ( num * b / side ) ; num = ( num * h / side ) ; cout << side << " " << num << endl ; }
def maximizecube ( l , b , h ) : side = gcd ( l , gcd ( b , h ) ) num = int ( l / side ) num = int ( num * b / side ) num = int ( num * h / side ) print ( side , num )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void maximizecube ( int l , int b , int h ) { int side = __gcd ( l , __gcd ( b , h ) ) ; int num = l / side ; num = ( num * b / side ) ; num = ( num * h / side ) ; cout << side << " " << num << endl ; } </Code>
cpp-python-HORNERS_METHOD_POLYNOMIAL_EVALUATION
cpp-python
int horner ( int poly [ ] , int n , int x ) { int result = poly [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) result = result * x + poly [ i ] ; return result ; }
def horner ( poly , n , x ) : result = poly [ 0 ] for i in range ( 1 , n ) : result = result * x + poly [ i ] return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int horner ( int poly [ ] , int n , int x ) { int result = poly [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) result = result * x + poly [ i ] ; return result ; } </Code>
cpp-python-C_PROGRAM_CYCLICALLY_ROTATE_ARRAY_ONE
cpp-python
void rotate ( int arr [ ] , int n ) { int x = arr [ n - 1 ] , i ; for ( i = n - 1 ; i > 0 ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = x ; }
def rotate ( arr , n ) : x = arr [ n - 1 ] for i in range ( n - 1 , 0 , - 1 ) : arr [ i ] = arr [ i - 1 ] arr [ 0 ] = x
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void rotate ( int arr [ ] , int n ) { int x = arr [ n - 1 ] , i ; for ( i = n - 1 ; i > 0 ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = x ; } </Code>
cpp-python-N_BONACCI_NUMBERS_1
cpp-python
void bonacciseries ( long n , int m ) { int a [ m ] = { 0 } ; a [ n - 1 ] = 1 ; a [ n ] = 1 ; for ( int i = n + 1 ; i < m ; i ++ ) a [ i ] = 2 * a [ i - 1 ] - a [ i - n - 1 ] ; for ( int i = 0 ; i < m ; i ++ ) cout << a [ i ] << " " ; }
def bonacciseries ( n , m ) : a = [ 0 for i in range ( m ) ] a [ n - 1 ] = 1 a [ n ] = 1 for i in range ( n + 1 , m ) : a [ i ] = 2 * a [ i - 1 ] - a [ i - n - 1 ] for i in range ( 0 , m ) : print ( a [ i ] , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void bonacciseries ( long n , int m ) { int a [ m ] = { 0 } ; a [ n - 1 ] = 1 ; a [ n ] = 1 ; for ( int i = n + 1 ; i < m ; i ++ ) a [ i ] = 2 * a [ i - 1 ] - a [ i - n - 1 ] ; for ( int i = 0 ; i < m ; i ++ ) cout << a [ i ] << " " ; } </Code>
cpp-python-COUNTING_PAIRS_PERSON_CAN_FORM_PAIR_ONE_1
cpp-python
int numberOfWays ( int x ) { int dp [ x + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; }
def numberOfWays ( x ) : if x == 0 or x == 1 : return 1 else : return ( numberOfWays ( x - 1 ) + ( x - 1 ) * numberOfWays ( x - 2 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numberOfWays ( int x ) { int dp [ x + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; } </Code>
cpp-python-HOW_TO_PRINT_MAXIMUM_NUMBER_OF_A_USING_GIVEN_FOUR_KEYS
cpp-python
int findoptimal ( int N ) { if ( N <= 6 ) return N ; int screen [ N ] ; int b ; int n ; for ( n = 1 ; n <= 6 ; n ++ ) screen [ n - 1 ] = n ; for ( n = 7 ; n <= N ; n ++ ) { screen [ n - 1 ] = max ( 2 * screen [ n - 4 ] , max ( 3 * screen [ n - 5 ] , 4 * screen [ n - 6 ] ) ) ; } return screen...
def findoptimal ( N ) : if ( N <= 6 ) : return N screen = [ 0 ] * N for n in range ( 1 , 7 ) : screen [ n - 1 ] = n for n in range ( 7 , N + 1 ) : screen [ n - 1 ] = max ( 2 * screen [ n - 4 ] , max ( 3 * screen [ n - 5 ] , 4 * screen [ n - 6 ] ) ) return screen [ N - 1 ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findoptimal ( int N ) { if ( N <= 6 ) return N ; int screen [ N ] ; int b ; int n ; for ( n = 1 ; n <= 6 ; n ++ ) screen [ n - 1 ] = n ; for ( n = 7 ; n <= N ; n ++ ) { screen [ n - 1 ] = max ( 2 * screen [ n - 4 ] , max ( 3 * screen [ n - 5 ] , 4 * screen [ n - 6 ] ) ) ; } return...
cpp-python-MAXIMUM_DIFFERENCE_SUM_ELEMENTS_TWO_ROWS_MATRIX
cpp-python
int maxRowDiff ( int mat [ ] [ MAX ] , int m , int n ) { int rowSum [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum += mat [ i ] [ j ] ; rowSum [ i ] = sum ; } int max_diff = rowSum [ 1 ] - rowSum [ 0 ] ; int min_element = rowSum [ 0 ] ; ...
def maxRowDiff ( mat , m , n ) : rowSum = [ 0 ] * m for i in range ( 0 , m ) : sum = 0 for j in range ( 0 , n ) : sum += mat [ i ] [ j ] rowSum [ i ] = sum max_diff = rowSum [ 1 ] - rowSum [ 0 ] min_element = rowSum [ 0 ] for i in range ( 1 , m ) : if ( ro...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxRowDiff ( int mat [ ] [ MAX ] , int m , int n ) { int rowSum [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum += mat [ i ] [ j ] ; rowSum [ i ] = sum ; } int max_diff = rowSum [ 1 ] - rowSum [ 0 ] ; int min_element = rowSum [ 0...
cpp-python-CHECK_VALID_SEQUENCE_DIVISIBLE_M_1
cpp-python
int isPossible ( int n , int index , int modulo , int M , int arr [ ] , int dp [ ] [ MAX ] ) { modulo = ( ( modulo % M ) + M ) % M ; if ( index == n ) { if ( modulo == 0 ) return 1 ; return 0 ; } if ( dp [ index ] [ modulo ] != - 1 ) return dp [ index ] [ modulo ] ; int placeAdd = isPossible ( n , ind...
def isPossible ( n , index , modulo , M , arr , dp ) : modulo = ( ( modulo % M ) + M ) % M if ( index == n ) : if ( modulo == 0 ) : return 1 return 0 if ( dp [ index ] [ modulo ] != - 1 ) : return dp [ index ] [ modulo ] placeAdd = isPossible ( n , index + 1 , modulo ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int isPossible ( int n , int index , int modulo , int M , int arr [ ] , int dp [ ] [ MAX ] ) { modulo = ( ( modulo % M ) + M ) % M ; if ( index == n ) { if ( modulo == 0 ) return 1 ; return 0 ; } if ( dp [ index ] [ modulo ] != - 1 ) return dp [ index ] [ modulo ] ; int placeAdd = isPossible ( ...
cpp-python-FIND_NTH_FIBONACCI_NUMBER_USING_GOLDEN_RATIO
cpp-python
int fib ( int n ) { if ( n < 6 ) return f [ n ] ; int t = 5 , fn = 5 ; while ( t < n ) { fn = round ( fn * PHI ) ; t ++ ; } return fn ; }
def fib ( n ) : if n < 6 : return f [ n ] t = 5 fn = 5 while t < n : fn = round ( fn * PHI ) t += 1 return fn
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int fib ( int n ) { if ( n < 6 ) return f [ n ] ; int t = 5 , fn = 5 ; while ( t < n ) { fn = round ( fn * PHI ) ; t ++ ; } return fn ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_6_MIN_COST_PATH
cpp-python
int minCost ( int cost [ R ] [ C ] , int m , int n ) { int i , j ; int tc [ R ] [ C ] ; tc [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( i = 1 ; i <= m ; i ++ ) tc [ i ] [ 0 ] = tc [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( j = 1 ; j <= n ; j ++ ) tc [ 0 ] [ j ] = tc [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] ; fo...
def minCost ( cost , m , n ) : tc = [ [ 0 for x in range ( C ) ] for x in range ( R ) ] tc [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] for i in range ( 1 , m + 1 ) : tc [ i ] [ 0 ] = tc [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] for j in range ( 1 , n + 1 ) : tc [ 0 ] [ j ] = tc [ 0 ] [ j - 1 ] + cost [ 0 ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minCost ( int cost [ R ] [ C ] , int m , int n ) { int i , j ; int tc [ R ] [ C ] ; tc [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( i = 1 ; i <= m ; i ++ ) tc [ i ] [ 0 ] = tc [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( j = 1 ; j <= n ; j ++ ) tc [ 0 ] [ j ] = tc [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ]...
cpp-python-PUT_SPACES_WORDS_STARTING_CAPITAL_LETTERS
cpp-python
void amendSentence ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) { str [ i ] = str [ i ] + 32 ; if ( i != 0 ) cout << " " ; cout << str [ i ] ; } else cout << str [ i ] ; } }
def amendSentence ( string ) : string = list ( string ) for i in range ( len ( string ) ) : if string [ i ] >= 'A' and string [ i ] <= 'Z' : string [ i ] = chr ( ord ( string [ i ] ) + 32 ) if i != 0 : print ( "" , end = "" ) print ( string [ i ] , end...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void amendSentence ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) { str [ i ] = str [ i ] + 32 ; if ( i != 0 ) cout << " " ; cout << str [ i ] ; } else cout << str [ i ] ; } } </Code>
cpp-python-WAYS_SUM_N_USING_ARRAY_ELEMENTS_REPETITION_ALLOWED
cpp-python
int countWays ( int arr [ ] , int m , int N ) { int count [ N + 1 ] ; memset ( count , 0 , sizeof ( count ) ) ; count [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( i >= arr [ j ] ) count [ i ] += count [ i - arr [ j ] ] ; return count [ N ] ; }
def countWays ( arr , m , N ) : count = [ 0 for i in range ( N + 1 ) ] count [ 0 ] = 1 for i in range ( 1 , N + 1 ) : for j in range ( m ) : if ( i >= arr [ j ] ) : count [ i ] += count [ i - arr [ j ] ] return count [ N ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countWays ( int arr [ ] , int m , int N ) { int count [ N + 1 ] ; memset ( count , 0 , sizeof ( count ) ) ; count [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( i >= arr [ j ] ) count [ i ] += count [ i - arr [ j ] ] ; return count [ N ] ; } </Code>
cpp-python-BREAK_NUMBER_THREE_PARTS
cpp-python
ll count_of_ways ( ll n ) { ll count = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) for ( int k = 0 ; k <= n ; k ++ ) if ( i + j + k == n ) count ++ ; return count ; }
def count_of_ways ( n ) : count = 0 for i in range ( 0 , n + 1 ) : for j in range ( 0 , n + 1 ) : for k in range ( 0 , n + 1 ) : if ( i + j + k == n ) : count = count + 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> ll count_of_ways ( ll n ) { ll count = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) for ( int k = 0 ; k <= n ; k ++ ) if ( i + j + k == n ) count ++ ; return count ; } </Code>
cpp-python-SUM_SERIES_0_6_0_06_0_006_0_0006_N_TERMS
cpp-python
float sumOfSeries ( int n ) { return ( 0.666 ) * ( 1 - 1 / pow ( 10 , n ) ) ; }
def sumOfSeries ( n ) : return ( ( 0.666 ) * ( 1 - 1 / pow ( 10 , n ) ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> float sumOfSeries ( int n ) { return ( 0.666 ) * ( 1 - 1 / pow ( 10 , n ) ) ; } </Code>
cpp-python-PARTITION_NUMBER_TWO_DIVISBLE_PARTS
cpp-python
void findDivision ( string & str , int a , int b ) { int len = str . length ( ) ; vector < int > lr ( len + 1 , 0 ) ; lr [ 0 ] = ( str [ 0 ] - '0' ) % a ; for ( int i = 1 ; i < len ; i ++ ) lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + ( str [ i ] - '0' ) ) % a ; vector < int > rl ( len + 1 , 0 ) ; rl [ len ...
def findDivision ( str , a , b ) : lenn = len ( str ) lr = [ 0 ] * ( lenn + 1 ) lr [ 0 ] = ( int ( str [ 0 ] ) ) % a for i in range ( 1 , lenn ) : lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + \ int ( str [ i ] ) ) % a rl = [ 0 ] * ( lenn + 1 ) rl [ lenn - 1 ] = int ( str [ lenn - 1 ] ) % b ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findDivision ( string & str , int a , int b ) { int len = str . length ( ) ; vector < int > lr ( len + 1 , 0 ) ; lr [ 0 ] = ( str [ 0 ] - '0' ) % a ; for ( int i = 1 ; i < len ; i ++ ) lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + ( str [ i ] - '0' ) ) % a ; vector < int > rl ( len + 1 , 0 ) ; rl...
cpp-python-ROW_WISE_COMMON_ELEMENTS_TWO_DIAGONALS_SQUARE_MATRIX
cpp-python
int countCommon ( int mat [ ] [ MAX ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( mat [ i ] [ i ] == mat [ i ] [ n - i - 1 ] ) res ++ ; return res ; }
def countCommon ( mat , n ) : res = 0 for i in range ( n ) : if mat [ i ] [ i ] == mat [ i ] [ n - i - 1 ] : res = res + 1 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countCommon ( int mat [ ] [ MAX ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( mat [ i ] [ i ] == mat [ i ] [ n - i - 1 ] ) res ++ ; return res ; } </Code>
cpp-python-HEXAGONAL_NUMBER
cpp-python
int hexagonalNum ( int n ) { return n * ( 2 * n - 1 ) ; }
def hexagonalNum ( n ) : return n * ( 2 * n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int hexagonalNum ( int n ) { return n * ( 2 * n - 1 ) ; } </Code>
cpp-python-FIND_PAIR_WITH_GREATEST_PRODUCT_IN_ARRAY
cpp-python
int findGreatest ( int arr [ ] , int n ) { int result = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = max ( result , arr [ i ] ) ; return result ; }
def findGreatest ( arr , n ) : result = - 1 for i in range ( n ) : for j in range ( n - 1 ) : for k in range ( j + 1 , n ) : if ( arr [ j ] * arr [ k ] == arr [ i ] ) : result = max ( result , arr [ i ] ) return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findGreatest ( int arr [ ] , int n ) { int result = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = max ( result , arr [ i ] ) ; return result ; } </Code>
cpp-python-FIND_SQUARE_ROOT_UNDER_MODULO_P_SET_1_WHEN_P_IS_IN_FORM_OF_4I_3
cpp-python
void squareRoot ( int n , int p ) { n = n % p ; for ( int x = 2 ; x < p ; x ++ ) { if ( ( x * x ) % p == n ) { cout << "Square root is " << x ; return ; } } cout << "Square root doesn't exist" ; }
def squareRoot ( n , p ) : n = n % p for x in range ( 2 , p ) : if ( ( x * x ) % p == n ) : print ( "Square root is " , x ) return print ( "Square root doesn't exist" )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void squareRoot ( int n , int p ) { n = n % p ; for ( int x = 2 ; x < p ; x ++ ) { if ( ( x * x ) % p == n ) { cout << "Square root is " << x ; return ; } } cout << "Square root doesn't exist" ; } </Code>
cpp-python-FIND_NUMBER_TIMES_STRING_OCCURS_GIVEN_STRING_1
cpp-python
int count ( string a , string b ) { int m = a . length ( ) ; int n = b . length ( ) ; int lookup [ m + 1 ] [ n + 1 ] = { { 0 } } ; for ( int i = 0 ; i <= n ; ++ i ) lookup [ 0 ] [ i ] = 0 ; for ( int i = 0 ; i <= m ; ++ i ) lookup [ i ] [ 0 ] = 1 ; for ( int i = 1 ; ...
def count ( a , b ) : m = len ( a ) n = len ( b ) lookup = [ [ 0 ] * ( n + 1 ) for i in range ( m + 1 ) ] for i in range ( n + 1 ) : lookup [ 0 ] [ i ] = 0 for i in range ( m + 1 ) : lookup [ i ] [ 0 ] = 1 for i in range ( 1 , m + 1 ) : for j in range ( 1 , n + 1 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int count ( string a , string b ) { int m = a . length ( ) ; int n = b . length ( ) ; int lookup [ m + 1 ] [ n + 1 ] = { { 0 } } ; for ( int i = 0 ; i <= n ; ++ i ) lookup [ 0 ] [ i ] = 0 ; for ( int i = 0 ; i <= m ; ++ i ) lookup [ i ] [ 0 ] = 1 ; for ( int i ...
cpp-python-FIND_INDEX_OF_AN_EXTRA_ELEMENT_PRESENT_IN_ONE_SORTED_ARRAY_2
cpp-python
int find_extra_element_index ( int arrA [ ] , int arrB [ ] , int n , int m ) { int extra_element = sum ( arrA , n ) - sum ( arrB , m ) ; return indexOf ( arrA , extra_element , n ) ; }
def find_extra_element_index ( arrA , arrB ) : extra_element = sum ( arrA ) - sum ( arrB ) return arrA.index ( extra_element )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int find_extra_element_index ( int arrA [ ] , int arrB [ ] , int n , int m ) { int extra_element = sum ( arrA , n ) - sum ( arrB , m ) ; return indexOf ( arrA , extra_element , n ) ; } </Code>
cpp-python-K_TH_MISSING_ELEMENT_INCREASING_SEQUENCE_NOT_PRESENT_GIVEN_SEQUENCE
cpp-python
int find ( int a [ ] , int b [ ] , int k , int n1 , int n2 ) { unordered_set < int > s ; for ( int i = 0 ; i < n2 ; i ++ ) s . insert ( b [ i ] ) ; int missing = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( s . find ( a [ i ] ) == s . end ( ) ) missing ++ ; if ( missing == k ) return a [ i ] ; ...
def find ( a , b , k , n1 , n2 ) : s = set ( ) for i in range ( n2 ) : s.add ( b [ i ] ) missing = 0 for i in range ( n1 ) : if a [ i ] not in s : missing += 1 if missing == k : return a [ i ] return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int find ( int a [ ] , int b [ ] , int k , int n1 , int n2 ) { unordered_set < int > s ; for ( int i = 0 ; i < n2 ; i ++ ) s . insert ( b [ i ] ) ; int missing = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( s . find ( a [ i ] ) == s . end ( ) ) missing ++ ; if ( missing == k ) return a [ ...
cpp-python-FIND_SUM_NODES_GIVEN_PERFECT_BINARY_TREE_1
cpp-python
int sumNodes ( int l ) { int leafNodeCount = pow ( 2 , l - 1 ) ; int sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; int sum = sumLastLevel * l ; return sum ; }
def sumNodes ( l ) : leafNodeCount = math.pow ( 2 , l - 1 ) sumLastLevel = 0 sumLastLevel = ( ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ) sum = sumLastLevel * l return int ( sum )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sumNodes ( int l ) { int leafNodeCount = pow ( 2 , l - 1 ) ; int sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; int sum = sumLastLevel * l ; return sum ; } </Code>
cpp-python-LEONARDO_NUMBER_1
cpp-python
int leonardo ( int n ) { int dp [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + 1 ; return dp [ n ] ; }
def leonardo ( n ) : dp = [ ] dp.append ( 1 ) dp.append ( 1 ) for i in range ( 2 , n + 1 ) : dp.append ( dp [ i - 1 ] + dp [ i - 2 ] + 1 ) return dp [ n ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int leonardo ( int n ) { int dp [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + 1 ; return dp [ n ] ; } </Code>
cpp-python-COUNT_NATURAL_NUMBERS_WHOSE_PERMUTATION_GREATER_NUMBER
cpp-python
int countNumber ( int n ) { int result = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) { stack < int > s ; if ( i <= n ) { s . push ( i ) ; result ++ ; } while ( ! s . empty ( ) ) { int tp = s . top ( ) ; s . pop ( ) ; for ( int j = tp % 10 ; j <= 9 ; j ++ ) { ...
def countNumber ( n ) : result = 0 for i in range ( 1 , 10 ) : s = [ ] if ( i <= n ) : s.append ( i ) result += 1 while len ( s ) != 0 : tp = s [ - 1 ] s.pop ( ) for j in range ( tp % 10 , 10 ) : x = tp * 10 + j ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countNumber ( int n ) { int result = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) { stack < int > s ; if ( i <= n ) { s . push ( i ) ; result ++ ; } while ( ! s . empty ( ) ) { int tp = s . top ( ) ; s . pop ( ) ; for ( int j = tp % 10 ; j <= 9 ; j +...
cpp-python-CHECK_WHETHER_GIVEN_DEGREES_VERTICES_REPRESENT_GRAPH_TREE
cpp-python
bool check ( int degree [ ] , int n ) { int deg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) deg_sum += degree [ i ] ; return ( 2 * ( n - 1 ) == deg_sum ) ; }
def check ( degree , n ) : deg_sum = sum ( degree ) if ( 2 * ( n - 1 ) == deg_sum ) : return True else : return False
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool check ( int degree [ ] , int n ) { int deg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) deg_sum += degree [ i ] ; return ( 2 * ( n - 1 ) == deg_sum ) ; } </Code>
cpp-python-FIND_SUM_EVEN_INDEX_BINOMIAL_COEFFICIENTS_1
cpp-python
int evenbinomialCoeffSum ( int n ) { return ( 1 << ( n - 1 ) ) ; }
def evenbinomialCoeffSum ( n ) : return ( 1 << ( n - 1 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int evenbinomialCoeffSum ( int n ) { return ( 1 << ( n - 1 ) ) ; } </Code>
cpp-python-FIND_MINIMUM_ELEMENT_IN_A_SORTED_AND_ROTATED_ARRAY
cpp-python
int findMin ( int arr [ ] , int low , int high ) { if ( high < low ) return arr [ 0 ] ; if ( high == low ) return arr [ low ] ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return arr [ mid + 1 ] ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return arr [ mid ] ...
def findMin ( arr , low , high ) : if high < low : return arr [ 0 ] if high == low : return arr [ low ] mid = int ( ( low + high ) / 2 ) if mid < high and arr [ mid + 1 ] < arr [ mid ] : return arr [ mid + 1 ] if mid > low and arr [ mid ] < arr [ mid - 1 ] : return ar...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMin ( int arr [ ] , int low , int high ) { if ( high < low ) return arr [ 0 ] ; if ( high == low ) return arr [ low ] ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return arr [ mid + 1 ] ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return arr [...
cpp-python-SQUARE_PYRAMIDAL_NUMBER_SUM_SQUARES
cpp-python
int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n ; if ( sum == s ) return n ; } return - 1 ; }
def findS ( s ) : _sum = 0 n = 1 while ( _sum < s ) : _sum += n * n n += 1 n -= 1 if _sum == s : return n return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n ; if ( sum == s ) return n ; } return - 1 ; } </Code>
cpp-python-RETURN_MAXIMUM_OCCURRING_CHARACTER_IN_THE_INPUT_STRING
cpp-python
char getMaxOccuringChar ( char * str ) { int count [ ASCII_SIZE ] = { 0 } ; int len = strlen ( str ) ; int max = 0 ; char result ; for ( int i = 0 ; i < len ; i ++ ) { count [ str [ i ] ] ++ ; if ( max < count [ str [ i ] ] ) { max = count [ str [ i ] ] ; re...
def getMaxOccuringChar ( str ) : count = [ 0 ] * ASCII_SIZE max = - 1 c = '' for i in str : count [ ord ( i ) ] += 1 ; for i in str : if max < count [ ord ( i ) ] : max = count [ ord ( i ) ] c = i return c
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> char getMaxOccuringChar ( char * str ) { int count [ ASCII_SIZE ] = { 0 } ; int len = strlen ( str ) ; int max = 0 ; char result ; for ( int i = 0 ; i < len ; i ++ ) { count [ str [ i ] ] ++ ; if ( max < count [ str [ i ] ] ) { max = count [ str [ i ] ] ; ...
cpp-python-COUNT_STRINGS_CAN_FORMED_USING_B_C_GIVEN_CONSTRAINTS_1
cpp-python
int countStr ( int n ) { return 1 + ( n * 2 ) + ( n * ( ( n * n ) - 1 ) / 2 ) ; }
def countStr ( n ) : return ( 1 + ( n * 2 ) + ( n * ( ( n * n ) - 1 ) // 2 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countStr ( int n ) { return 1 + ( n * 2 ) + ( n * ( ( n * n ) - 1 ) / 2 ) ; } </Code>
cpp-python-GIVEN_N_X_N_SQUARE_MATRIX_FIND_SUM_SUB_SQUARES_SIZE_K_X_K
cpp-python
void printSumSimple ( int mat [ ] [ n ] , int k ) { if ( k > n ) return ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { for ( int j = 0 ; j < n - k + 1 ; j ++ ) { int sum = 0 ; for ( int p = i ; p < k + i ; p ++ ) for ( int q = j ; q < k + j ; q ++ ) sum += mat [ p ]...
def printSumSimple ( mat , k ) : if ( k > n ) : return for i in range ( n - k + 1 ) : for j in range ( n - k + 1 ) : sum = 0 for p in range ( i , k + i ) : for q in range ( j , k + j ) : sum += mat [ p ] [ q ] print ( sum , ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printSumSimple ( int mat [ ] [ n ] , int k ) { if ( k > n ) return ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { for ( int j = 0 ; j < n - k + 1 ; j ++ ) { int sum = 0 ; for ( int p = i ; p < k + i ; p ++ ) for ( int q = j ; q < k + j ; q ++ ) sum += ma...
cpp-python-COUNT_OF_N_DIGIT_NUMBERS_WHOSE_SUM_OF_DIGITS_EQUALS_TO_GIVEN_SUM
cpp-python
void findCount ( int n , int sum ) { int start = pow ( 10 , n - 1 ) ; int end = pow ( 10 , n ) - 1 ; int count = 0 ; int i = start ; while ( i <= end ) { int cur = 0 ; int temp = i ; while ( temp != 0 ) { cur += temp % 10 ; temp = temp / 10 ; } if ( cur == sum ) { count +...
def findCount ( n , sum ) : start = math.pow ( 10 , n - 1 ) end = math.pow ( 10 , n ) - 1 count = 0 i = start while ( i <= end ) : cur = 0 temp = i while ( temp != 0 ) : cur += temp % 10 temp = temp // 10 if ( cur == sum ) : count =...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findCount ( int n , int sum ) { int start = pow ( 10 , n - 1 ) ; int end = pow ( 10 , n ) - 1 ; int count = 0 ; int i = start ; while ( i <= end ) { int cur = 0 ; int temp = i ; while ( temp != 0 ) { cur += temp % 10 ; temp = temp / 10 ; } if ( cur == sum ) { ...
cpp-python-COUNT_GROUPINGS_NUMBER_SUM_DIGITS_EVERY_SUB_GROUP_LESS_EQUALS_IMMEDIATE_RIGHT_SUB_GROUP_1
cpp-python
int countGroups ( int position , int previous_sum , int length , char * num ) { if ( position == length ) return 1 ; if ( dp [ position ] [ previous_sum ] != - 1 ) return dp [ position ] [ previous_sum ] ; dp [ position ] [ previous_sum ] = 0 ; int res = 0 ; int sum = 0 ; for ( int i = position ; i < leng...
def countGroups ( position , previous_sum , length , num ) : if ( position == length ) : return 1 if ( dp [ position ] [ previous_sum ] != - 1 ) : return dp [ position ] [ previous_sum ] dp [ position ] [ previous_sum ] = 0 res = 0 sum = 0 for i in range ( position , length ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countGroups ( int position , int previous_sum , int length , char * num ) { if ( position == length ) return 1 ; if ( dp [ position ] [ previous_sum ] != - 1 ) return dp [ position ] [ previous_sum ] ; dp [ position ] [ previous_sum ] = 0 ; int res = 0 ; int sum = 0 ; for ( int i = position ; i...
cpp-python-MAXIMUM_POINTS_INTERSECTION_N_CIRCLES
cpp-python
int intersection ( int n ) { return n * ( n - 1 ) ; }
def intersection ( n ) : return n * ( n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int intersection ( int n ) { return n * ( n - 1 ) ; } </Code>
cpp-python-SUM_MATRIX_ELEMENT_ABSOLUTE_DIFFERENCE_ROW_COLUMN_NUMBERS_2
cpp-python
int findSum ( int n ) { n -- ; int sum = 0 ; sum += ( n * ( n + 1 ) ) / 2 ; sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; return sum ; }
def findSum ( n ) : n -= 1 sum = 0 sum += ( n * ( n + 1 ) ) / 2 sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 return int ( sum )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSum ( int n ) { n -- ; int sum = 0 ; sum += ( n * ( n + 1 ) ) / 2 ; sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; return sum ; } </Code>
cpp-python-FIND_REPEATING_ELEMENT_SORTED_ARRAY_SIZE_N
cpp-python
int findRepeatingElement ( int arr [ ] , int low , int high ) { if ( low > high ) return - 1 ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatingElemen...
def findRepeatingElement ( arr , low , high ) : if low > high : return - 1 mid = ( low + high ) / 2 if ( arr [ mid ] != mid + 1 ) : if ( mid > 0 and arr [ mid ] == arr [ mid - 1 ] ) : return mid return findRepeatingElement ( arr , low , mid - 1 ) return findRepeatingE...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findRepeatingElement ( int arr [ ] , int low , int high ) { if ( low > high ) return - 1 ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatin...
cpp-python-SMALLEST_OF_THREE_INTEGERS_WITHOUT_COMPARISON_OPERATORS
cpp-python
int smallest ( int x , int y , int z ) { int c = 0 ; while ( x && y && z ) { x -- ; y -- ; z -- ; c ++ ; } return c ; }
def smallest ( x , y , z ) : c = 0 while ( x and y and z ) : x = x - 1 y = y - 1 z = z - 1 c = c + 1 return c
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int smallest ( int x , int y , int z ) { int c = 0 ; while ( x && y && z ) { x -- ; y -- ; z -- ; c ++ ; } return c ; } </Code>
cpp-python-PATH_MAXIMUM_AVERAGE_VALUE
cpp-python
double maxAverageOfPath ( int cost [ M ] [ M ] , int N ) { int dp [ N + 1 ] [ N + 1 ] ; dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( int j = 1 ; j < N ; j ++ ) dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] ; ...
def maxAverageOfPath ( cost , N ) : dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] for i in range ( 1 , N ) : dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] for j in range ( 1 , N ) : dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double maxAverageOfPath ( int cost [ M ] [ M ] , int N ) { int dp [ N + 1 ] [ N + 1 ] ; dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( int j = 1 ; j < N ; j ++ ) dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [ 0 ] [...
cpp-python-COUNT_PAIRS_DIFFERENCE_EQUAL_K
cpp-python
int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k || arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; }
def countPairsWithDiffK ( arr , n , k ) : count = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if arr [ i ] - arr [ j ] == k or arr [ j ] - arr [ i ] == k : count += 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k || arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; } </Code>
cpp-python-LONGEST_SUBSEQUENCE_DIFFERENCE_ADJACENTS_ONE_SET_2
cpp-python
int longLenSub ( int arr [ ] , int n ) { unordered_map < int , int > um ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( um . find ( arr [ i ] - 1 ) != um . end ( ) && len < um [ arr [ i ] - 1 ] ) len = um [ arr [ i ] - 1 ] ; if ( um . find ( arr [ i ] + 1 ) != um . end ( ...
def longLenSub ( arr , n ) : um = defaultdict ( lambda : 0 ) longLen = 0 for i in range ( n ) : len1 = 0 if ( arr [ i - 1 ] in um and len1 < um [ arr [ i ] - 1 ] ) : len1 = um [ arr [ i ] - 1 ] if ( arr [ i ] + 1 in um and len1 < um [ arr [ i ] + 1 ] ) : len1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int longLenSub ( int arr [ ] , int n ) { unordered_map < int , int > um ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( um . find ( arr [ i ] - 1 ) != um . end ( ) && len < um [ arr [ i ] - 1 ] ) len = um [ arr [ i ] - 1 ] ; if ( um . find ( arr [ i ] + 1 ) != um ....
cpp-python-RECURSIVE_PROGRAM_PRIME_NUMBER
cpp-python
bool isPrime ( int n , int i = 2 ) { if ( n <= 2 ) return ( n == 2 ) ? true : false ; if ( n % i == 0 ) return false ; if ( i * i > n ) return true ; return isPrime ( n , i + 1 ) ; }
def isPrime ( n , i = 2 ) : if ( n <= 2 ) : return True if ( n == 2 ) else False if ( n % i == 0 ) : return False if ( i * i > n ) : return true return isPrime ( n , i + 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPrime ( int n , int i = 2 ) { if ( n <= 2 ) return ( n == 2 ) ? true : false ; if ( n % i == 0 ) return false ; if ( i * i > n ) return true ; return isPrime ( n , i + 1 ) ; } </Code>
cpp-python-FIND_LAST_DIGIT_FACTORIAL_DIVIDES_FACTORIAL_B
cpp-python
int computeLastDigit ( long long int A , long long int B ) { int variable = 1 ; if ( A == B ) return 1 ; else if ( ( B - A ) >= 5 ) return 0 ; else { for ( long long int i = A + 1 ; i <= B ; i ++ ) variable = ( variable * ( i % 10 ) ) % 10 ; return variable % 10 ; } }
def computeLastDigit ( A , B ) : variable = 1 if ( A == B ) : return 1 elif ( ( B - A ) >= 5 ) : return 0 else : for i in range ( A + 1 , B + 1 ) : variable = ( variable * ( i % 10 ) ) % 10 return variable % 10
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int computeLastDigit ( long long int A , long long int B ) { int variable = 1 ; if ( A == B ) return 1 ; else if ( ( B - A ) >= 5 ) return 0 ; else { for ( long long int i = A + 1 ; i <= B ; i ++ ) variable = ( variable * ( i % 10 ) ) % 10 ; return variable % 10 ; } } </Code>
cpp-python-MINIMUM_OPERATIONS_REQUIRED_SET_ELEMENTS_BINARY_MATRIX
cpp-python
int minOperation ( bool arr [ N ] [ M ] ) { int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] == 0 ) { ans ++ ; for ( int k = 0 ; k <= i ; k ++ ) { for ( int h = 0 ; h <= j ; ...
def minOperation ( arr ) : ans = 0 for i in range ( N - 1 , - 1 , - 1 ) : for j in range ( M - 1 , - 1 , - 1 ) : if ( arr [ i ] [ j ] == 0 ) : ans += 1 for k in range ( i + 1 ) : for h in range ( j + 1 ) : if ( arr [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minOperation ( bool arr [ N ] [ M ] ) { int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] == 0 ) { ans ++ ; for ( int k = 0 ; k <= i ; k ++ ) { for ( int h = 0 ; h <=...
cpp-python-NEXT_POWER_OF_2_1
cpp-python
unsigned int nextPowerOf2 ( unsigned int n ) { unsigned int p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; }
def nextPowerOf2 ( n ) : p = 1 if ( n and not ( n & ( n - 1 ) ) ) : return n while ( p < n ) : p <<= 1 return p
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int nextPowerOf2 ( unsigned int n ) { unsigned int p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } </Code>
cpp-python-MINIMIZE_THE_MAXIMUM_DIFFERENCE_BETWEEN_THE_HEIGHTS
cpp-python
int getMinDiff ( int arr [ ] , int n , int k ) { if ( n == 1 ) return 0 ; sort ( arr , arr + n ) ; int ans = arr [ n - 1 ] - arr [ 0 ] ; int small = arr [ 0 ] + k ; int big = arr [ n - 1 ] - k ; if ( small > big ) swap ( small , big ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int subtract = arr [ i...
def getMinDiff ( arr , n , k ) : if ( n == 1 ) : return 0 arr.sort ( ) ans = arr [ n - 1 ] - arr [ 0 ] small = arr [ 0 ] + k big = arr [ n - 1 ] - k if ( small > big ) : small , big = big , small for i in range ( 1 , n - 1 ) : subtract = arr [ i ] - k add = ar...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getMinDiff ( int arr [ ] , int n , int k ) { if ( n == 1 ) return 0 ; sort ( arr , arr + n ) ; int ans = arr [ n - 1 ] - arr [ 0 ] ; int small = arr [ 0 ] + k ; int big = arr [ n - 1 ] - k ; if ( small > big ) swap ( small , big ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int subtract = ...
cpp-python-DIFFERENT_WAYS_SUM_N_USING_NUMBERS_GREATER_EQUAL_M
cpp-python
int numberofways ( int n , int m ) { int dp [ n + 2 ] [ n + 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ n + 1 ] = 1 ; for ( int k = n ; k >= m ; k -- ) { for ( int i = 0 ; i <= n ; i ++ ) { dp [ i ] [ k ] = dp [ i ] [ k + 1 ] ; if ( i - k >= 0 ) dp [ i ] [ k ] = ( dp [ i ] [...
def numberofways ( n , m ) : dp = np.zeros ( ( n + 2 , n + 2 ) ) dp [ 0 ] [ n + 1 ] = 1 for k in range ( n , m - 1 , - 1 ) : for i in range ( n + 1 ) : dp [ i ] [ k ] = dp [ i ] [ k + 1 ] if ( i - k >= 0 ) : dp [ i ] [ k ] = ( dp [ i ] [ k ] + dp [ i - k ] [ k...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numberofways ( int n , int m ) { int dp [ n + 2 ] [ n + 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ n + 1 ] = 1 ; for ( int k = n ; k >= m ; k -- ) { for ( int i = 0 ; i <= n ; i ++ ) { dp [ i ] [ k ] = dp [ i ] [ k + 1 ] ; if ( i - k >= 0 ) dp [ i ] [ k ] = ( dp ...
cpp-python-FIND_WHETHER_GIVEN_INTEGER_POWER_3_NOT
cpp-python
bool check ( int n ) { return 1162261467 % n == 0 ; }
def check ( n ) : return 1162261467 % n == 0
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool check ( int n ) { return 1162261467 % n == 0 ; } </Code>
cpp-python-NEWMAN_CONWAY_SEQUENCE
cpp-python
int sequence ( int n ) { if ( n == 1 || n == 2 ) return 1 ; else return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ; }
def sequence ( n ) : if n == 1 or n == 2 : return 1 else : return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sequence ( int n ) { if ( n == 1 || n == 2 ) return 1 ; else return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ; } </Code>
cpp-python-SMALLEST_DERANGEMENT_SEQUENCE
cpp-python
void generate_derangement ( int N ) { int S [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) S [ i ] = i ; int D [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 2 ) { if ( i == N && i % N != 0 ) { int temp = D [ N ] D [ N ] = D [ N - 1 ] ; D [ N - 1 ] = temp ; } else { D [ i ] = i...
def generate_derangement ( N ) : S = [ 0 ] * ( N + 1 ) for i in range ( 1 , N + 1 ) : S [ i ] = i D = [ 0 ] * ( N + 1 ) for i in range ( 1 , N + 1 , 2 ) : if i == N : D [ N ] = S [ N - 1 ] D [ N - 1 ] = S [ N ] else : D [ i ] = i + 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void generate_derangement ( int N ) { int S [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) S [ i ] = i ; int D [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 2 ) { if ( i == N && i % N != 0 ) { int temp = D [ N ] D [ N ] = D [ N - 1 ] ; D [ N - 1 ] = temp ; } else { D [ ...
cpp-python-NUMBER_OF_TRIANGLES_IN_DIRECTED_AND_UNDIRECTED_GRAPHS
cpp-python
int countTriangle ( int graph [ V ] [ V ] , bool isDirected ) { int count_Triangle = 0 ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { for ( int k = 0 ; k < V ; k ++ ) { if ( graph [ i ] [ j ] && graph [ j ] [ k ] && graph [ k ] [ i ] ) count_Triangl...
def countTriangle ( g , isDirected ) : nodes = len ( g ) count_Triangle = 0 for i in range ( nodes ) : for j in range ( nodes ) : for k in range ( nodes ) : if ( i != j and i != k and j != k and g [ i ] [ j ] and g [ j ] [ k ] and g [ k ] [ i ] ) : cou...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countTriangle ( int graph [ V ] [ V ] , bool isDirected ) { int count_Triangle = 0 ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { for ( int k = 0 ; k < V ; k ++ ) { if ( graph [ i ] [ j ] && graph [ j ] [ k ] && graph [ k ] [ i ] ) count_...
cpp-python-HEIGHT_N_ARY_TREE_PARENT_ARRAY_GIVEN
cpp-python
int findHeight ( int * parent , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int p = i , current = 1 ; while ( parent [ p ] != - 1 ) { current ++ ; p = parent [ p ] ; } res = max ( res , current ) ; } return res ; }
def findHeight ( parent , n ) : res = 0 for i in range ( n ) : p = i current = 1 while ( parent [ p ] != - 1 ) : current += 1 p = parent [ p ] res = max ( res , current ) return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findHeight ( int * parent , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int p = i , current = 1 ; while ( parent [ p ] != - 1 ) { current ++ ; p = parent [ p ] ; } res = max ( res , current ) ; } return res ; } </Code>
cpp-python-LEONARDO_NUMBER
cpp-python
int leonardo ( int n ) { if ( n == 0 || n == 1 ) return 1 ; return leonardo ( n - 1 ) + leonardo ( n - 2 ) + 1 ; }
def leonardo ( n ) : if ( n == 0 or n == 1 ) : return 1 return ( leonardo ( n - 1 ) + leonardo ( n - 2 ) + 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int leonardo ( int n ) { if ( n == 0 || n == 1 ) return 1 ; return leonardo ( n - 1 ) + leonardo ( n - 2 ) + 1 ; } </Code>
cpp-python-FIND_SIZE_OF_THE_LARGEST_FORMED_BY_ALL_ONES_IN_A_BINARY_MATRIX
cpp-python
int findLargestPlus ( int mat [ N ] [ N ] ) { int left [ N ] [ N ] , right [ N ] [ N ] , top [ N ] [ N ] , bottom [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { top [ 0 ] [ i ] = mat [ 0 ] [ i ] ; bottom [ N - 1 ] [ i ] = mat [ N - 1 ] [ i ] ; left [ i ] [ 0 ] = mat [ i ] [ 0 ] ; right [ i ] [...
def findLargestPlus ( mat ) : left = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] right = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] top = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] bottom = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] for i in range ( N ) : t...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLargestPlus ( int mat [ N ] [ N ] ) { int left [ N ] [ N ] , right [ N ] [ N ] , top [ N ] [ N ] , bottom [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { top [ 0 ] [ i ] = mat [ 0 ] [ i ] ; bottom [ N - 1 ] [ i ] = mat [ N - 1 ] [ i ] ; left [ i ] [ 0 ] = mat [ i ] [ 0 ] ; right ...
cpp-python-CENTER_ELEMENT_OF_MATRIX_EQUALS_SUMS_OF_HALF_DIAGONALS
cpp-python
bool HalfDiagonalSums ( int mat [ ] [ MAX ] , int n ) { int diag1_left = 0 , diag1_right = 0 ; int diag2_left = 0 , diag2_right = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { if ( i < n / 2 ) { diag1_left += mat [ i ] [ i ] ; diag2_left += mat [ j ] [ i ] ; } else if ( i >...
def HalfDiagonalSums ( mat , n ) : diag1_left = 0 diag1_right = 0 diag2_left = 0 diag2_right = 0 i = 0 j = n - 1 while i < n : if ( i < n // 2 ) : diag1_left += mat [ i ] [ i ] diag2_left += mat [ j ] [ i ] elif ( i > n // 2 ) : diag1_right...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool HalfDiagonalSums ( int mat [ ] [ MAX ] , int n ) { int diag1_left = 0 , diag1_right = 0 ; int diag2_left = 0 , diag2_right = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { if ( i < n / 2 ) { diag1_left += mat [ i ] [ i ] ; diag2_left += mat [ j ] [ i ] ; } else i...
cpp-python-SUM_NODES_K_TH_LEVEL_TREE_REPRESENTED_STRING
cpp-python
int sumAtKthLevel ( string tree , int k ) { int level = - 1 ; int sum = 0 ; int n = tree . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( tree [ i ] == '(' ) level ++ ; else if ( tree [ i ] == ')' ) level -- ; else { if ( level == k ) sum += ( tree [ i ] - '0' ) ; } } return ...
def sumAtKthLevel ( tree , k ) : level = - 1 sum = 0 n = len ( tree ) for i in range ( n ) : if ( tree [ i ] == '(' ) : level += 1 elif ( tree [ i ] == ')' ) : level -= 1 else : if ( level == k ) : sum += ( ord ( tree [ i ] ) - ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sumAtKthLevel ( string tree , int k ) { int level = - 1 ; int sum = 0 ; int n = tree . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( tree [ i ] == '(' ) level ++ ; else if ( tree [ i ] == ')' ) level -- ; else { if ( level == k ) sum += ( tree [ i ] - '0' ) ; } } ...
cpp-python-LEIBNIZ_HARMONIC_TRIANGLE
cpp-python
void LeibnizHarmonicTriangle ( int n ) { int C [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } for ( int i = ...
def LeibnizHarmonicTriangle ( n ) : C = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] for i in range ( 0 , n + 1 ) : for j in range ( 0 , min ( i , n ) + 1 ) : if ( j == 0 or j == i ) : C [ i ] [ j ] = 1 else : C [ i ] [ j ] = ( C [ i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void LeibnizHarmonicTriangle ( int n ) { int C [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } for ( i...
cpp-python-WAYS_SPLIT_STRING_PARTITION_STARTS_DISTINCT_CHARACTER
cpp-python
int countWays ( string s ) { int count [ 26 ] = { 0 } ; for ( char x : s ) count [ x - 'a' ] ++ ; count [ s [ 0 ] - 'a' ] = 1 ; int ans = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) if ( count [ i ] != 0 ) ans *= count [ i ] ; return ans ; }
def countWays ( s ) : count = [ 0 ] * 26 for x in s : count [ ord ( x ) - ord ( 'a' ) ] = ( count [ ord ( x ) - ord ( 'a' ) ] ) + 1 count [ ord ( s [ 0 ] ) - ord ( 'a' ) ] = 1 ans = 1 for i in range ( 26 ) : if ( count [ i ] != 0 ) : ans *= count [ i ] return ans
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countWays ( string s ) { int count [ 26 ] = { 0 } ; for ( char x : s ) count [ x - 'a' ] ++ ; count [ s [ 0 ] - 'a' ] = 1 ; int ans = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) if ( count [ i ] != 0 ) ans *= count [ i ] ; return ans ; } </Code>
cpp-python-COUNT_NUMBER_WAYS_TILE_FLOOR_SIZE_N_X_M_USING_1_X_M_SIZE_TILES
cpp-python
int countWays ( int n , int m ) { int count [ n + 1 ] ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; }
def countWays ( n , m ) : count = [ ] for i in range ( n + 2 ) : count.append ( 0 ) count [ 0 ] = 0 for i in range ( 1 , n + 1 ) : if ( i > m ) : count [ i ] = count [ i - 1 ] + count [ i - m ] elif ( i < m ) : count [ i ] = 1 else : co...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countWays ( int n , int m ) { int count [ n + 1 ] ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; } </Code>
cpp-python-COMMON_ELEMENTS_IN_ALL_ROWS_OF_A_GIVEN_MATRIX
cpp-python
void printCommonElements ( int mat [ M ] [ N ] ) { unordered_map < int , int > mp ; for ( int j = 0 ; j < N ; j ++ ) mp [ mat [ 0 ] [ j ] ] = 1 ; for ( int i = 1 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mp [ mat [ i ] [ j ] ] == i ) { mp [ mat [ i ] [ j ] ] = i ...
def printCommonElements ( mat ) : mp = dict ( ) for j in range ( N ) : mp [ mat [ 0 ] [ j ] ] = 1 for i in range ( 1 , M ) : for j in range ( N ) : if ( mat [ i ] [ j ] in mp.keys ( ) and mp [ mat [ i ] [ j ] ] == i ) : mp [ mat [ i ] [ j ] ] = i + 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printCommonElements ( int mat [ M ] [ N ] ) { unordered_map < int , int > mp ; for ( int j = 0 ; j < N ; j ++ ) mp [ mat [ 0 ] [ j ] ] = 1 ; for ( int i = 1 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mp [ mat [ i ] [ j ] ] == i ) { mp [ mat [ i ] [ j ]...
cpp-python-FLOOR_IN_A_SORTED_ARRAY
cpp-python
int floorSearch ( int arr [ ] , int n , int x ) { if ( x >= arr [ n - 1 ] ) return n - 1 ; if ( x < arr [ 0 ] ) return - 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > x ) return ( i - 1 ) ; return - 1 ; }
def floorSearch ( arr , low , high , x ) : if ( low > high ) : return - 1 if ( x >= arr [ high ] ) : return high mid = int ( ( low + high ) / 2 ) if ( arr [ mid ] == x ) : return mid if ( mid > 0 and arr [ mid - 1 ] <= x and x < arr [ mid ] ) : return mid - 1 if (...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int floorSearch ( int arr [ ] , int n , int x ) { if ( x >= arr [ n - 1 ] ) return n - 1 ; if ( x < arr [ 0 ] ) return - 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > x ) return ( i - 1 ) ; return - 1 ; } </Code>
cpp-python-SMALLEST_POWER_OF_2_GREATER_THAN_OR_EQUAL_TO_N_1
cpp-python
unsigned int nextPowerOf2 ( unsigned int n ) { unsigned int p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; }
def nextPowerOf2 ( n ) : p = 1 if ( n and not ( n & ( n - 1 ) ) ) : return n while ( p < n ) : p <<= 1 return p
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int nextPowerOf2 ( unsigned int n ) { unsigned int p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } </Code>
cpp-python-KTH_ADJACENT_NODE_GRAPH_VERTEX_WEIGHT
cpp-python
void printkthnode ( vector < pair < int , int >> adj [ ] , int wt [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) sort ( adj [ i ] . begin ( ) , adj [ i ] . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( adj [ i ] . size ( ) >= k ) cout << adj [ i ] [ adj [ i ] . size ( ) - k ] . second ; ...
def printkthnode ( adj , wt , n , k ) : for i in range ( n ) : adj [ i ].sort ( ) for i in range ( n ) : if ( len ( adj [ i ] ) >= k ) : print ( adj [ i ] [ len ( adj [ i ] ) - k ] [ 1 ] , end = " " ) else : print ( "-1" , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printkthnode ( vector < pair < int , int >> adj [ ] , int wt [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) sort ( adj [ i ] . begin ( ) , adj [ i ] . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( adj [ i ] . size ( ) >= k ) cout << adj [ i ] [ adj [ i ] . size ( ) - k ] . se...
cpp-python-LONGEST_SUBARRAY_NOT_K_DISTINCT_ELEMENTS
cpp-python
void longest ( int a [ ] , int n , int k ) { unordered_map < int , int > freq ; int start = 0 , end = 0 , now = 0 , l = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; if ( freq [ a [ i ] ] == 1 ) now ++ ; while ( now > k ) { freq [ a [ l ] ] -- ; if ( freq [ a [ l ] ] == ...
def longest ( a , n , k ) : freq = [ 0 ] * n start = 0 end = 0 now = 0 l = 0 for i in range ( n ) : freq [ a [ i ] ] += 1 if ( freq [ a [ i ] ] == 1 ) : now += 1 while ( now > k ) : freq [ a [ l ] ] -= 1 if ( freq [ a [ l ] ] == 0 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void longest ( int a [ ] , int n , int k ) { unordered_map < int , int > freq ; int start = 0 , end = 0 , now = 0 , l = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; if ( freq [ a [ i ] ] == 1 ) now ++ ; while ( now > k ) { freq [ a [ l ] ] -- ; if ( freq [ a [ l ...
cpp-python-MINIMUM_FLIP_REQUIRED_MAKE_BINARY_MATRIX_SYMMETRIC_1
cpp-python
int minimumflip ( int mat [ ] [ N ] , int n ) { int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) flip ++ ; return flip ; }
def minimumflip ( mat , n ) : flip = 0 for i in range ( n ) : for j in range ( i ) : if mat [ i ] [ j ] != mat [ j ] [ i ] : flip += 1 return flip
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minimumflip ( int mat [ ] [ N ] , int n ) { int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) flip ++ ; return flip ; } </Code>