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-WAYS_REMOVE_ONE_ELEMENT_BINARY_STRING_XOR_BECOMES_ZERO
cpp-python
int xorZero ( string str ) { int one_count = 0 , zero_count = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == '1' ) one_count ++ ; else zero_count ++ ; if ( one_count % 2 == 0 ) return zero_count ; return one_count ; }
def xorZero ( str ) : one_count = 0 zero_count = 0 n = len ( str ) for i in range ( 0 , n , 1 ) : if ( str [ i ] == '1' ) : one_count += 1 else : zero_count += 1 if ( one_count % 2 == 0 ) : return zero_count return one_count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int xorZero ( string str ) { int one_count = 0 , zero_count = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == '1' ) one_count ++ ; else zero_count ++ ; if ( one_count % 2 == 0 ) return zero_count ; return one_count ; } </Code>
cpp-python-N_TH_ROOT_NUMBER
cpp-python
double nthRoot ( int A , int N ) { double xPre = rand ( ) % 10 ; double eps = 1 e - 3 ; double delX = INT_MAX ; double xK ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / pow ( xPre , N - 1 ) ) / ( double ) N ; delX = abs ( xK - xPre ) ; xPre = xK ; } return xK ; }
def nthRoot ( A , N ) : xPre = random.randint ( 1 , 101 ) % 10 eps = 0.001 delX = 2147483647 xK = 0.0 while ( delX > eps ) : xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N delX = abs ( xK - xPre ) xPre = xK return xK
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double nthRoot ( int A , int N ) { double xPre = rand ( ) % 10 ; double eps = 1 e - 3 ; double delX = INT_MAX ; double xK ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / pow ( xPre , N - 1 ) ) / ( double ) N ; delX = abs ( xK - xPre ) ; xPre = xK ; } return xK ; } <...
cpp-python-RETURN_PREVIOUS_ELEMENT_IN_AN_EXPANDING_MATRIX
cpp-python
string findLeft ( string str ) { int n = str . length ( ) ; while ( n -- ) { if ( str [ n ] == 'd' ) { str [ n ] = 'c' ; break ; } if ( str [ n ] == 'b' ) { str [ n ] = 'a' ; break ; } if ( str [ n ] == 'a' ) str [ n ] = 'b' ; else if ( str [ n ] == 'c' ) str [ n ] = ...
def findLeft ( str ) : n = len ( str ) - 1 while ( n > 0 ) : if ( str [ n ] == 'd' ) : str = str [ 0 : n ] + 'c' + str [ n + 1 : ] break if ( str [ n ] == 'b' ) : str = str [ 0 : n ] + 'a' + str [ n + 1 : ] break if ( str [ n ] == 'a' ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string findLeft ( string str ) { int n = str . length ( ) ; while ( n -- ) { if ( str [ n ] == 'd' ) { str [ n ] = 'c' ; break ; } if ( str [ n ] == 'b' ) { str [ n ] = 'a' ; break ; } if ( str [ n ] == 'a' ) str [ n ] = 'b' ; else if ( str [ n ] == 'c' ) str [...
cpp-python-MINIMUM_NUMBER_SUBSETS_DISTINCT_ELEMENTS
cpp-python
int subset ( int ar [ ] , int n ) { int res = 0 ; sort ( ar , ar + n ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; for ( ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) count ++ ; else break ; } res = max ( res , count ) ; } return res ; }
def subset ( ar , n ) : res = 0 ar.sort ( ) for i in range ( 0 , n ) : count = 1 for i in range ( n - 1 ) : if ar [ i ] == ar [ i + 1 ] : count += 1 else : break res = max ( res , count ) return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int subset ( int ar [ ] , int n ) { int res = 0 ; sort ( ar , ar + n ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; for ( ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) count ++ ; else break ; } res = max ( res , count ) ; } return res ; } </Code...
cpp-python-SMALLEST_SUBARRAY_K_DISTINCT_NUMBERS
cpp-python
void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { unordered_set < int > s ; int j ; for ( j = i ; j < n ; j ++ ) { s . insert ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; ...
def minRange ( arr , n , k ) : l = 0 r = n for i in range ( n ) : s = [ ] for j in range ( i , n ) : s.append ( arr [ j ] ) if ( len ( s ) == k ) : if ( ( j - i ) < ( r - l ) ) : r = j l = i break...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { unordered_set < int > s ; int j ; for ( j = i ; j < n ; j ++ ) { s . insert ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r ...
cpp-python-OVERLAPPING_SUM_TWO_ARRAY
cpp-python
int findSum ( int A [ ] , int B [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) { hash [ A [ i ] ] ++ ; hash [ B [ i ] ] ++ ; } int sum = 0 ; for ( auto x : hash ) if ( x . second == 1 ) sum += x . first ; return sum ; }
def findSum ( A , B , n ) : Hash = defaultdict ( lambda : 0 ) for i in range ( 0 , n ) : Hash [ A [ i ] ] += 1 Hash [ B [ i ] ] += 1 Sum = 0 for x in Hash : if Hash [ x ] == 1 : Sum += x return Sum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSum ( int A [ ] , int B [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) { hash [ A [ i ] ] ++ ; hash [ B [ i ] ] ++ ; } int sum = 0 ; for ( auto x : hash ) if ( x . second == 1 ) sum += x . first ; return sum ; } </Code>
cpp-python-LONGEST_PALINDROME_SUBSTRING_SET_1
cpp-python
void printSubStr ( string str , int low , int high ) { for ( int i = low ; i <= high ; ++ i ) cout << str [ i ] ; }
def printSubStr ( st , low , high ) : sys.stdout.write ( st [ low : high + 1 ] ) sys.stdout.flush ( ) return ''
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printSubStr ( string str , int low , int high ) { for ( int i = low ; i <= high ; ++ i ) cout << str [ i ] ; } </Code>
cpp-python-FIND_SUM_EVEN_INDEX_BINOMIAL_COEFFICIENTS
cpp-python
int evenSum ( int n ) { int C [ n + 1 ] [ n + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( 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 ] ; } } int sum = 0 ; for ( int...
def evenSum ( 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 - 1 ] [ j - 1 ] + C [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int evenSum ( int n ) { int C [ n + 1 ] [ n + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( 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 ] ; } } int sum = 0 ; fo...
cpp-python-MINIMUM_NUMBER_OF_SQUARES_WHOSE_SUM_EQUALS_TO_GIVEN_NUMBER_N_1
cpp-python
int getMinSquares ( int n ) { int * dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; for ( int i = 4 ; i <= n ; i ++ ) { dp [ i ] = i ; for ( int x = 1 ; x <= ceil ( sqrt ( i ) ) ; x ++ ) { int temp = x * x ; if ( temp > i ) break ; e...
def getMinSquares ( n ) : dp = [ 0 , 1 , 2 , 3 ] for i in range ( 4 , n + 1 ) : dp.append ( i ) for x in range ( 1 , int ( ceil ( sqrt ( i ) ) ) + 1 ) : temp = x * x ; if temp > i : break else : dp [ i ] = min ( dp [ i ] , 1 + d...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getMinSquares ( int n ) { int * dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; for ( int i = 4 ; i <= n ; i ++ ) { dp [ i ] = i ; for ( int x = 1 ; x <= ceil ( sqrt ( i ) ) ; x ++ ) { int temp = x * x ; if ( temp > i ) break ; ...
cpp-python-SEGREGATE_EVEN_ODD_NUMBERS_SET_3
cpp-python
void arrayEvenAndOdd ( int arr [ ] , int n ) { int i = - 1 , j = 0 ; int t ; while ( j != n ) { if ( arr [ j ] % 2 == 0 ) { i ++ ; swap ( arr [ i ] , arr [ j ] ) ; } j ++ ; } for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " " ; }
def arrayEvenAndOdd ( arr , n ) : i = - 1 j = 0 while ( j != n ) : if ( arr [ j ] % 2 == 0 ) : i = i + 1 arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] j = j + 1 for i in arr : print ( str ( i ) + " " , end = '' )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void arrayEvenAndOdd ( int arr [ ] , int n ) { int i = - 1 , j = 0 ; int t ; while ( j != n ) { if ( arr [ j ] % 2 == 0 ) { i ++ ; swap ( arr [ i ] , arr [ j ] ) ; } j ++ ; } for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " " ; } </Code>
cpp-python-SQUARES_OF_MATRIX_DIAGONAL_ELEMENTS
cpp-python
void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) { cout << "Diagonal one : " ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < column ; j ++ ) if ( i == j ) cout << mat [ i ] [ j ] * mat [ i ] [ j ] << " " ; } cout << " \n\nDiagonal two : " ; for ( int i = 0 ; i ...
def diagonalsquare ( mat , row , column ) : print ( "Diagonal one : " , end = "" ) for i in range ( 0 , row ) : for j in range ( 0 , column ) : if ( i == j ) : print ( "{} ".format ( mat [ i ] [ j ] * mat [ i ] [ j ] ) , end = "" ) print ( " \n\nDiagonal two : " , end = "...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) { cout << "Diagonal one : " ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < column ; j ++ ) if ( i == j ) cout << mat [ i ] [ j ] * mat [ i ] [ j ] << " " ; } cout << " \n\nDiagonal two : " ; for ( int i = 0...
cpp-python-CHECK_SUMS_TH_ROW_TH_COLUMN_MATRIX
cpp-python
bool areSumSame ( int a [ ] [ MAX ] , int n , int m ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 = 0 , sum2 = 0 ; for ( int j = 0 ; j < m ; j ++ ) { sum1 += a [ i ] [ j ] ; sum2 += a [ j ] [ i ] ; } if ( sum1 == sum2 ) return true ; } return false...
def areSumSame ( a , n , m ) : sum1 = 0 sum2 = 0 for i in range ( 0 , n ) : sum1 = 0 sum2 = 0 for j in range ( 0 , m ) : sum1 += a [ i ] [ j ] sum2 += a [ j ] [ i ] if ( sum1 == sum2 ) : return 1 return 0
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool areSumSame ( int a [ ] [ MAX ] , int n , int m ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 = 0 , sum2 = 0 ; for ( int j = 0 ; j < m ; j ++ ) { sum1 += a [ i ] [ j ] ; sum2 += a [ j ] [ i ] ; } if ( sum1 == sum2 ) return true ; } retur...
cpp-python-GIVEN_SORTED_ARRAY_NUMBER_X_FIND_PAIR_ARRAY_WHOSE_SUM_CLOSEST_X
cpp-python
void printClosest ( int arr [ ] , int n , int x ) { int res_l , res_r ; int l = 0 , r = n - 1 , diff = INT_MAX ; while ( r > l ) { if ( abs ( arr [ l ] + arr [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = abs ( arr [ l ] + arr [ r ] - x ) ; } if ( arr [ l ] + arr [ r ] > x ) r...
def printClosest ( arr , n , x ) : res_l , res_r = 0 , 0 l , r , diff = 0 , n - 1 , MAX_VAL while r > l : if abs ( arr [ l ] + arr [ r ] - x ) < diff : res_l = l res_r = r diff = abs ( arr [ l ] + arr [ r ] - x ) if arr [ l ] + arr [ r ] > x : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printClosest ( int arr [ ] , int n , int x ) { int res_l , res_r ; int l = 0 , r = n - 1 , diff = INT_MAX ; while ( r > l ) { if ( abs ( arr [ l ] + arr [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = abs ( arr [ l ] + arr [ r ] - x ) ; } if ( arr [ l ] + arr [ r ] ...
cpp-python-GIVEN_BINARY_STRING_COUNT_NUMBER_SUBSTRINGS_START_END_1_1
cpp-python
int countSubStr ( char str [ ] ) { int m = 0 ; for ( int i = 0 ; str [ i ] != '\0' ; i ++ ) { if ( str [ i ] == '1' ) m ++ ; } return m * ( m - 1 ) / 2 ; }
def countSubStr ( st , n ) : m = 0 for i in range ( 0 , n ) : if ( st [ i ] == '1' ) : m = m + 1 return m * ( m - 1 ) // 2
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSubStr ( char str [ ] ) { int m = 0 ; for ( int i = 0 ; str [ i ] != '\0' ; i ++ ) { if ( str [ i ] == '1' ) m ++ ; } return m * ( m - 1 ) / 2 ; } </Code>
cpp-python-COUNT_SUBSTRINGS_WITH_SAME_FIRST_AND_LAST_CHARACTERS
cpp-python
int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( s [ i ] == s [ j ] ) result ++ ; return result ; }
def countSubstringWithEqualEnds ( s ) : result = 0 n = len ( s ) for i in range ( n ) : for j in range ( i , n ) : if ( s [ i ] == s [ j ] ) : result = result + 1 return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( s [ i ] == s [ j ] ) result ++ ; return result ; } </Code>
cpp-python-CALCULATE_MAXIMUM_VALUE_USING_SIGN_TWO_NUMBERS_STRING
cpp-python
int calcMaxValue ( string str ) { int res = str [ 0 ] - '0' ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == '0' || str [ i ] == '1' || res < 2 ) res += ( str [ i ] - '0' ) ; else res *= ( str [ i ] - '0' ) ; } return res ; }
def calcMaxValue ( str ) : res = ord ( str [ 0 ] ) - 48 for i in range ( 1 , len ( str ) ) : if ( str [ i ] == '0' or str [ i ] == '1' or res < 2 ) : res += ord ( str [ i ] ) - 48 else : res *= ord ( str [ i ] ) - 48 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int calcMaxValue ( string str ) { int res = str [ 0 ] - '0' ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == '0' || str [ i ] == '1' || res < 2 ) res += ( str [ i ] - '0' ) ; else res *= ( str [ i ] - '0' ) ; } return res ; } </Code>
cpp-python-PROGRAM_BEST_FIT_ALGORITHM_MEMORY_MANAGEMENT
cpp-python
void bestFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int bestIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { ...
def bestFit ( blockSize , m , processSize , n ) : allocation = [ - 1 ] * n for i in range ( n ) : bestIdx = - 1 for j in range ( m ) : if blockSize [ j ] >= processSize [ i ] : if bestIdx == - 1 : bestIdx = j elif blockSize [ bestId...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void bestFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int bestIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i...
cpp-python-FREQUENCY_ELEMENT_UNSORTED_ARRAY_1
cpp-python
void countFreq ( int a [ ] , int n ) { unordered_map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; int cumul = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumul += hm [ a [ i ] ] ; if ( hm [ a [ i ] ] ) { cout << a [ i ] << "->" << cumul << endl ; } hm [ a [ i ...
def countFreq ( a , n ) : hm = dict ( ) for i in range ( n ) : hm [ a [ i ] ] = hm.get ( a [ i ] , 0 ) + 1 cumul = 0 for i in range ( n ) : cumul += hm [ a [ i ] ] if ( hm [ a [ i ] ] > 0 ) : print ( a [ i ] , "->" , cumul ) hm [ a [ i ] ] = 0
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void countFreq ( int a [ ] , int n ) { unordered_map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; int cumul = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumul += hm [ a [ i ] ] ; if ( hm [ a [ i ] ] ) { cout << a [ i ] << "->" << cumul << endl ; } hm [...
cpp-python-GIVEN_LARGE_NUMBER_CHECK_SUBSEQUENCE_DIGITS_DIVISIBLE_8
cpp-python
bool isSubSeqDivisible ( string str ) { for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i ; j < l ; j ++ ) { for ( int k = j ; k < l ; k ++ ) { if ( arr [ i ] % 8 == 0 ) return true ; else if ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 && i != j ) return true ; ...
def isSubSeqDivisible ( st ) : l = len ( st ) arr = [ 0 ] * l for i in range ( 0 , l ) : for j in range ( i , l ) : for k in range ( j , l ) : if ( arr [ i ] % 8 == 0 ) : return True elif ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 and i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isSubSeqDivisible ( string str ) { for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i ; j < l ; j ++ ) { for ( int k = j ; k < l ; k ++ ) { if ( arr [ i ] % 8 == 0 ) return true ; else if ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 && i != j ) return true ; ...
cpp-python-COMPUTE_THE_INTEGER_ABSOLUTE_VALUE_ABS_WITHOUT_BRANCHING
cpp-python
unsigned int getAbs ( int n ) { int const mask = n >> ( sizeof ( int ) * CHARBIT - 1 ) ; return ( ( n + mask ) ^ mask ) ; }
def getAbs ( n ) : mask = n >> ( SIZE_INT * CHARBIT - 1 ) return ( ( n + mask ) ^ mask )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int getAbs ( int n ) { int const mask = n >> ( sizeof ( int ) * CHARBIT - 1 ) ; return ( ( n + mask ) ^ mask ) ; } </Code>
cpp-python-COUNT_PAIRS_WHOSE_PRODUCTS_EXIST_IN_ARRAY
cpp-python
int countPairs ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ k ] == product ) { result ++ ; b...
def countPairs ( arr , n ) : result = 0 ; for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : product = arr [ i ] * arr [ j ] ; for k in range ( 0 , n ) : if ( arr [ k ] == product ) : result = result + 1 ; break ; ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countPairs ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ k ] == product ) { result ++ ; ...
cpp-python-CLUSTERINGPARTITIONING_AN_ARRAY_SUCH_THAT_SUM_OF_SQUARE_DIFFERENCES_IS_MINIMUM_1
cpp-python
int minCost ( int a [ ] , int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= k ; j ++ ) dp [ i ] [ j ] = inf ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= k ; j ++ ) for ( int m = i - 1 ; m >= 0 ; m -- ) d...
def minCost ( a , n , k ) : dp = [ [ inf for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] dp [ 0 ] [ 0 ] = 0 for i in range ( 1 , n + 1 ) : for j in range ( 1 , k + 1 ) : for m in range ( i - 1 , - 1 , - 1 ) : dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ m ] [ j - 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minCost ( int a [ ] , int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= k ; j ++ ) dp [ i ] [ j ] = inf ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= k ; j ++ ) for ( int m = i - 1 ; m >= 0 ; m...
cpp-python-CHECK_GRAPHS_CYCLE_ODD_LENGTH
cpp-python
bool containsOdd ( int G [ ] [ V ] , int src ) { int colorArr [ V ] ; for ( int i = 0 ; i < V ; ++ i ) colorArr [ i ] = - 1 ; colorArr [ src ] = 1 ; queue < int > q ; q . push ( src ) ; while ( ! q . empty ( ) ) { int u = q . front ( ) ; q . pop ( ) ; if ( G [ u ] [ u ] == 1 ) return true ; ...
def containsOdd ( G , src ) : global V colorArr = [ - 1 ] * V colorArr [ src ] = 1 q = queue.Queue ( ) q.put ( src ) while ( not q.empty ( ) ) : u = q.get ( ) if ( G [ u ] [ u ] == 1 ) : return True for v in range ( V ) : if ( G [ u ] [ v ] and col...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool containsOdd ( int G [ ] [ V ] , int src ) { int colorArr [ V ] ; for ( int i = 0 ; i < V ; ++ i ) colorArr [ i ] = - 1 ; colorArr [ src ] = 1 ; queue < int > q ; q . push ( src ) ; while ( ! q . empty ( ) ) { int u = q . front ( ) ; q . pop ( ) ; if ( G [ u ] [ u ] == 1 ) return ...
cpp-python-COUNT_SET_BITS_IN_AN_INTEGER_3
cpp-python
int countSetBits ( int n ) { if ( n == 0 ) return 0 ; else return 1 + countSetBits ( n & ( n - 1 ) ) ; }
def countSetBits ( n ) : if ( n == 0 ) : return 0 else : return 1 + countSetBits ( n & ( n - 1 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSetBits ( int n ) { if ( n == 0 ) return 0 ; else return 1 + countSetBits ( n & ( n - 1 ) ) ; } </Code>
cpp-python-CHECK_STRING_FOLLOWS_ANBN_PATTERN_NOT
cpp-python
bool isAnBn ( string str ) { int n = str . length ( ) ; int i ; for ( i = 0 ; i < n ; i ++ ) if ( str [ i ] != 'a' ) break ; if ( i * 2 != n ) return false ; int j ; for ( j = i ; j < n ; j ++ ) if ( str [ j ] != 'b' ) return false ; return true ; }
def isAnBn ( str ) : n = len ( str ) for i in range ( n ) : if ( str [ i ] != 'a' ) : break if ( i * 2 != n ) : return False for j in range ( i , n ) : if ( str [ j ] != 'b' ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isAnBn ( string str ) { int n = str . length ( ) ; int i ; for ( i = 0 ; i < n ; i ++ ) if ( str [ i ] != 'a' ) break ; if ( i * 2 != n ) return false ; int j ; for ( j = i ; j < n ; j ++ ) if ( str [ j ] != 'b' ) return false ; return true ; } </Code>
cpp-python-MINIMUM_NUMBER_OF_SQUARES_WHOSE_SUM_EQUALS_TO_GIVEN_NUMBER_N
cpp-python
int getMinSquares ( unsigned int n ) { if ( sqrt ( n ) - floor ( sqrt ( n ) ) == 0 ) return 1 ; if ( n <= 3 ) return n ; int res = n ; for ( int x = 1 ; x <= n ; x ++ ) { int temp = x * x ; if ( temp > n ) break ; else res = min ( res , 1 + getMinSquares ( n - temp ) ) ; } return res ; }
def getMinSquares ( n ) : if n <= 3 : return n ; res = n for x in range ( 1 , n + 1 ) : temp = x * x ; if temp > n : break else : res = min ( res , 1 + getMinSquares ( n - temp ) ) return res ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getMinSquares ( unsigned int n ) { if ( sqrt ( n ) - floor ( sqrt ( n ) ) == 0 ) return 1 ; if ( n <= 3 ) return n ; int res = n ; for ( int x = 1 ; x <= n ; x ++ ) { int temp = x * x ; if ( temp > n ) break ; else res = min ( res , 1 + getMinSquares ( n - temp ) ) ; } return res ...
cpp-python-MAXIMUM_DISTANCE_TWO_OCCURRENCES_ELEMENT_ARRAY
cpp-python
int maxDistance ( int arr [ ] , int n ) { unordered_map < int , int > mp ; int max_dist = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = i ; else max_dist = max ( max_dist , i - mp [ arr [ i ] ] ) ; } return max_dist ; }
def maxDistance ( arr , n ) : mp = { } maxDict = 0 for i in range ( n ) : if arr [ i ] not in mp.keys ( ) : mp [ arr [ i ] ] = i else : maxDict = max ( maxDict , i - mp [ arr [ i ] ] ) return maxDict
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxDistance ( int arr [ ] , int n ) { unordered_map < int , int > mp ; int max_dist = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = i ; else max_dist = max ( max_dist , i - mp [ arr [ i ] ] ) ; } return max_dist ; } </Code>
cpp-python-MAXIMUM_SUBSEQUENCE_SUM_SUCH_THAT_NO_THREE_ARE_CONSECUTIVE
cpp-python
int maxSumWO3Consec ( int arr [ ] , int n ) { int sum [ n ] ; if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ; if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ; if ( n > 2 ) sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = max ( max ...
def maxSumWO3Consec ( arr , n ) : sum = [ 0 for k in range ( n ) ] if n >= 1 : sum [ 0 ] = arr [ 0 ] if n >= 2 : sum [ 1 ] = arr [ 0 ] + arr [ 1 ] if n > 2 : sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) for i in range ( 3 , n ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSumWO3Consec ( int arr [ ] , int n ) { int sum [ n ] ; if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ; if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ; if ( n > 2 ) sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = max...
cpp-python-EULERS_FOUR_SQUARE_IDENTITY_1
cpp-python
void checkEulerFourSquareIdentity ( int a , int b ) { int ab = a * b ; bool flag = false ; int i = 0 ; while ( i * i <= ab ) { int j = i ; while ( i * i + j * j <= ab ) { int k = j ; while ( i * i + j * j + k * k <= ab ) { double l = sqrt ( ab - ( i * i + j * j + k * k ) ) ; ...
def checkEulerFourSquareIdentity ( a , b ) : ab = a * b flag = False i = 0 while i * i <= ab : j = i while i * i + j * j <= ab : k = j while i * i + j * j + k * k <= ab : l = ( ab - ( i * i + j * j + k * k ) ) ** ( 0.5 ) if l == int...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void checkEulerFourSquareIdentity ( int a , int b ) { int ab = a * b ; bool flag = false ; int i = 0 ; while ( i * i <= ab ) { int j = i ; while ( i * i + j * j <= ab ) { int k = j ; while ( i * i + j * j + k * k <= ab ) { double l = sqrt ( ab - ( i * i + j * j + k * k ) ) ; ...
cpp-python-NOBLE_INTEGERS_IN_AN_ARRAY_COUNT_OF_GREATER_ELEMENTS_IS_EQUAL_TO_VALUE_1
cpp-python
int nobleInteger ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) continue ; if ( arr [ i ] == n - i - 1 ) return arr [ i ] ; } if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ; return - 1 ; }
def nobleInteger ( arr ) : arr.sort ( ) n = len ( arr ) for i in range ( n - 1 ) : if arr [ i ] == arr [ i + 1 ] : continue if arr [ i ] == n - i - 1 : return arr [ i ] if arr [ n - 1 ] == 0 : return arr [ n - 1 ] return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int nobleInteger ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) continue ; if ( arr [ i ] == n - i - 1 ) return arr [ i ] ; } if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ; return - 1 ; } </Code>
cpp-python-COUNT_NUMBER_ISLANDS_EVERY_ISLAND_SEPARATED_LINE
cpp-python
int countIslands ( int mat [ ] [ N ] ) { int count = 0 ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] == 'X' ) { if ( ( i == 0 || mat [ i - 1 ] [ j ] == 'O' ) && ( j == 0 || mat [ i ] [ j - 1 ] == 'O' ) ) count ++ ; } } } retu...
def countIslands ( mat ) : count = 0 for i in range ( 0 , M ) : for j in range ( 0 , N ) : if ( mat [ i ] [ j ] == 'X' ) : if ( ( i == 0 or mat [ i - 1 ] [ j ] == 'O' ) and ( j == 0 or mat [ i ] [ j - 1 ] == 'O' ) ) : count = count + 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countIslands ( int mat [ ] [ N ] ) { int count = 0 ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] == 'X' ) { if ( ( i == 0 || mat [ i - 1 ] [ j ] == 'O' ) && ( j == 0 || mat [ i ] [ j - 1 ] == 'O' ) ) count ++ ; } } }...
cpp-python-SELECT_A_RANDOM_NUMBER_FROM_STREAM_WITH_O1_SPACE
cpp-python
int selectRandom ( int x ) { static int res ; static int count = 0 ; count ++ ; if ( count == 1 ) res = x ; else { int i = rand ( ) % count ; if ( i == count - 1 ) res = x ; } return res ; }
def selectRandom ( x ) : res = 0 count = 0 count += 1 if ( count == 1 ) : res = x else : i = random.randrange ( count ) if ( i == count - 1 ) : res = x return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int selectRandom ( int x ) { static int res ; static int count = 0 ; count ++ ; if ( count == 1 ) res = x ; else { int i = rand ( ) % count ; if ( i == count - 1 ) res = x ; } return res ; } </Code>
cpp-python-A_PRODUCT_ARRAY_PUZZLE_1
cpp-python
void productArray ( int arr [ ] , int n ) { if ( n == 1 ) { cout << 0 ; return ; } int i , temp = 1 ; int * prod = new int [ ( sizeof ( int ) * n ) ] ; memset ( prod , 1 , n ) ; for ( i = 0 ; i < n ; i ++ ) { prod [ i ] = temp ; temp *= arr [ i ] ; } temp = 1 ; for ( i = n - 1 ; ...
def productArray ( arr , n ) : if n == 1 : print ( 0 ) return i , temp = 1 , 1 prod = [ 1 for i in range ( n ) ] for i in range ( n ) : prod [ i ] = temp temp *= arr [ i ] temp = 1 for i in range ( n - 1 , - 1 , - 1 ) : prod [ i ] *= temp temp *= a...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void productArray ( int arr [ ] , int n ) { if ( n == 1 ) { cout << 0 ; return ; } int i , temp = 1 ; int * prod = new int [ ( sizeof ( int ) * n ) ] ; memset ( prod , 1 , n ) ; for ( i = 0 ; i < n ; i ++ ) { prod [ i ] = temp ; temp *= arr [ i ] ; } temp = 1 ; for ( i = n -...
cpp-python-SORT_1_N_SWAPPING_ADJACENT_ELEMENTS
cpp-python
bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) { int i , j ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] ) { j = i ; while ( B [ j ] ) j ++ ; sort ( A + i , A + 1 + j ) ; i = j ; } } for ( i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return false ; ...
def sortedAfterSwap ( A , B , n ) : for i in range ( 0 , n - 1 ) : if ( B [ i ] == 1 ) : j = i while ( B [ j ] == 1 ) : j = j + 1 A = A [ 0 : i ] + sorted ( A [ i : j + 1 ] ) + A [ j + 1 : ] i = j for i in range ( 0 , n ) : if ( A [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) { int i , j ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] ) { j = i ; while ( B [ j ] ) j ++ ; sort ( A + i , A + 1 + j ) ; i = j ; } } for ( i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return fal...
cpp-python-FIND_THE_MAXIMUM_SUBARRAY_XOR_IN_A_GIVEN_ARRAY
cpp-python
int maxSubarrayXOR ( int arr [ ] , int n ) { int ans = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_xor = curr_xor ^ arr [ j ] ; ans = max ( ans , curr_xor ) ; } } return ans ; }
def maxSubarrayXOR ( arr , n ) : ans = - 2147483648 for i in range ( n ) : curr_xor = 0 for j in range ( i , n ) : curr_xor = curr_xor ^ arr [ j ] ans = max ( ans , curr_xor ) return ans
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSubarrayXOR ( int arr [ ] , int n ) { int ans = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_xor = curr_xor ^ arr [ j ] ; ans = max ( ans , curr_xor ) ; } } return ans ; } </Code>
cpp-python-MINIMUM_COST_MAKE_ARRAY_SIZE_1_REMOVING_LARGER_PAIRS
cpp-python
int cost ( int a [ ] , int n ) { return ( n - 1 ) * ( * min_element ( a , a + n ) ) ; }
def cost ( a , n ) : return ( ( n - 1 ) * min ( a ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int cost ( int a [ ] , int n ) { return ( n - 1 ) * ( * min_element ( a , a + n ) ) ; } </Code>
cpp-python-REMOVE_MINIMUM_ELEMENTS_EITHER_SIDE_2MIN_MAX
cpp-python
int minRemovalsDP ( int arr [ ] , int n ) { int longest_start = - 1 , longest_end = 0 ; for ( int start = 0 ; start < n ; start ++ ) { int min = INT_MAX , max = INT_MIN ; for ( int end = start ; end < n ; end ++ ) { int val = arr [ end ] ; if ( val < min ) min = val ; if ( val ...
def minRemovalsDP ( arr , n ) : longest_start = - 1 longest_end = 0 for start in range ( n ) : min = sys.maxsize max = - sys.maxsize for end in range ( start , n ) : val = arr [ end ] if ( val < min ) : min = val if ( val > max ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minRemovalsDP ( int arr [ ] , int n ) { int longest_start = - 1 , longest_end = 0 ; for ( int start = 0 ; start < n ; start ++ ) { int min = INT_MAX , max = INT_MIN ; for ( int end = start ; end < n ; end ++ ) { int val = arr [ end ] ; if ( val < min ) min = val ; if...
cpp-python-COUNT_INVERSIONS_OF_SIZE_THREE_IN_A_GIVE_ARRAY_1
cpp-python
int getInvCount ( int arr [ ] , int n ) { int invcount = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int small = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ; int great = 0 ; for ( int j = i - 1 ; j >= 0 ; j -- ) if ( arr [ i ] < arr [ j ] ) grea...
def getInvCount ( arr , n ) : invcount = 0 for i in range ( 1 , n - 1 ) : small = 0 for j in range ( i + 1 , n ) : if ( arr [ i ] > arr [ j ] ) : small += 1 great = 0 for j in range ( i - 1 , - 1 , - 1 ) : if ( arr [ i ] < arr [ j ] ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getInvCount ( int arr [ ] , int n ) { int invcount = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int small = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ; int great = 0 ; for ( int j = i - 1 ; j >= 0 ; j -- ) if ( arr [ i ] < arr [ j ]...
cpp-python-PROGRAM_FOR_SURFACE_AREA_OF_OCTAHEDRON
cpp-python
double surface_area_octahedron ( double side ) { return ( 2 * ( sqrt ( 3 ) ) * ( side * side ) ) ; }
def surface_area_octahedron ( side ) : return ( 2 * ( math.sqrt ( 3 ) ) * ( side * side ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double surface_area_octahedron ( double side ) { return ( 2 * ( sqrt ( 3 ) ) * ( side * side ) ) ; } </Code>
cpp-python-GNOME_SORT_A_STUPID_ONE
cpp-python
void gnomeSort ( int arr [ ] , int n ) { int index = 0 ; while ( index < n ) { if ( index == 0 ) index ++ ; if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ; else { swap ( arr [ index ] , arr [ index - 1 ] ) ; index -- ; } } return ; }
def gnomeSort ( arr , n ) : index = 0 while index < n : if index == 0 : index = index + 1 if arr [ index ] >= arr [ index - 1 ] : index = index + 1 else : arr [ index ] , arr [ index - 1 ] = arr [ index - 1 ] , arr [ index ] index = index -...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void gnomeSort ( int arr [ ] , int n ) { int index = 0 ; while ( index < n ) { if ( index == 0 ) index ++ ; if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ; else { swap ( arr [ index ] , arr [ index - 1 ] ) ; index -- ; } } return ; } </Code>
cpp-python-COUNT_FREQUENCY_K_MATRIX_SIZE_N_MATRIXI_J_IJ
cpp-python
int find ( int n , int k ) { if ( n + 1 >= k ) return ( k - 1 ) ; else return ( 2 * n + 1 - k ) ; }
def find ( n , k ) : if ( n + 1 >= k ) : return ( k - 1 ) else : return ( 2 * n + 1 - k )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int find ( int n , int k ) { if ( n + 1 >= k ) return ( k - 1 ) ; else return ( 2 * n + 1 - k ) ; } </Code>
cpp-python-UNIQUE_CELLS_BINARY_MATRIX
cpp-python
int countUnique ( int mat [ ] [ MAX ] , int n , int m ) { int rowsum [ n ] , colsum [ m ] ; memset ( colsum , 0 , sizeof ( colsum ) ) ; memset ( rowsum , 0 , sizeof ( rowsum ) ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] ) { rowsum [ i ] ++ ; colsum ...
def countUnique ( mat , n , m ) : rowsum = [ 0 ] * n colsum = [ 0 ] * m for i in range ( n ) : for j in range ( m ) : if ( mat [ i ] [ j ] != 0 ) : rowsum [ i ] += 1 colsum [ j ] += 1 uniquecount = 0 for i in range ( n ) : for j in range ( ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countUnique ( int mat [ ] [ MAX ] , int n , int m ) { int rowsum [ n ] , colsum [ m ] ; memset ( colsum , 0 , sizeof ( colsum ) ) ; memset ( rowsum , 0 , sizeof ( rowsum ) ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] ) { rowsum [ i ] ++ ; ...
cpp-python-GENERATE_BINARY_PERMUTATIONS_1S_0S_EVERY_POINT_PERMUTATIONS
cpp-python
void generate ( int ones , int zeroes , string str , int len ) { if ( len == str . length ( ) ) { cout << str << " " ; return ; } generate ( ones + 1 , zeroes , str + "1" , len ) ; if ( ones > zeroes ) generate ( ones , zeroes + 1 , str + "0" , len ) ; }
def generate ( ones , zeroes , str , len1 ) : if ( len1 == len ( str ) ) : print ( str , end = " " ) return generate ( ones + 1 , zeroes , str + "1" , len1 ) if ( ones > zeroes ) : generate ( ones , zeroes + 1 , str + "0" , len1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void generate ( int ones , int zeroes , string str , int len ) { if ( len == str . length ( ) ) { cout << str << " " ; return ; } generate ( ones + 1 , zeroes , str + "1" , len ) ; if ( ones > zeroes ) generate ( ones , zeroes + 1 , str + "0" , len ) ; } </Code>
cpp-python-MID_POINT_CIRCLE_DRAWING_ALGORITHM
cpp-python
void midPointCircleDraw ( int x_centre , int y_centre , int r ) { int x = r , y = 0 ; cout << "(" << x + x_centre << ", " << y + y_centre << ") " ; if ( r > 0 ) { cout << "(" << x + x_centre << ", " << - y + y_centre << ") " ; cout << "(" << y + x_centre << ", " << x + y_centre << ") " ; cout << "(" <...
def midPointCircleDraw ( x_centre , y_centre , r ) : x = r y = 0 print ( "(" , x + x_centre , ", " , y + y_centre , ")" , sep = "" , end = "" ) if ( r > 0 ) : print ( "(" , x + x_centre , ", " , - y + y_centre , ")" , sep = "" , end = "" ) print ( "(" , y + x_centre , ", " , x + y_centre...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void midPointCircleDraw ( int x_centre , int y_centre , int r ) { int x = r , y = 0 ; cout << "(" << x + x_centre << ", " << y + y_centre << ") " ; if ( r > 0 ) { cout << "(" << x + x_centre << ", " << - y + y_centre << ") " ; cout << "(" << y + x_centre << ", " << x + y_centre << ") " ; cout <...
cpp-python-FIND_FACTORIAL_NUMBERS_LESS_EQUAL_N
cpp-python
void printFactorialNums ( int n ) { int fact = 1 ; int x = 2 ; while ( fact <= n ) { cout << fact << " " ; fact = fact * x ; x ++ ; } }
def printFactorialNums ( n ) : fact = 1 x = 2 while fact <= n : print ( fact , end = " " ) fact = fact * x x += 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printFactorialNums ( int n ) { int fact = 1 ; int x = 2 ; while ( fact <= n ) { cout << fact << " " ; fact = fact * x ; x ++ ; } } </Code>
cpp-python-FRIENDS_PAIRING_PROBLEM
cpp-python
int countFriendsPairings ( int n ) { int dp [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i <= 2 ) dp [ i ] = i ; else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ n ] ; }
def countFriendsPairings ( n ) : dp = [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) : if ( i <= 2 ) : dp [ i ] = i else : dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] return dp [ n ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countFriendsPairings ( int n ) { int dp [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i <= 2 ) dp [ i ] = i ; else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ n ] ; } </Code>
cpp-python-PROGRAM_FIND_REMAINDER_LARGE_NUMBER_DIVIDED_11
cpp-python
int remainder ( string str ) { int len = str . length ( ) ; int num , rem = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = rem * 10 + ( str [ i ] - '0' ) ; rem = num % 11 ; } return rem ; }
def remainder ( st ) : ln = len ( st ) rem = 0 for i in range ( 0 , ln ) : num = rem * 10 + ( int ) ( st [ i ] ) rem = num % 11 return rem
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int remainder ( string str ) { int len = str . length ( ) ; int num , rem = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = rem * 10 + ( str [ i ] - '0' ) ; rem = num % 11 ; } return rem ; } </Code>
cpp-python-N_TH_NUMBER_WHOSE_SUM_OF_DIGITS_IS_TEN
cpp-python
int findNth ( int n ) { int count = 0 ; for ( int curr = 1 ; ; curr ++ ) { int sum = 0 ; for ( int x = curr ; x > 0 ; x = x / 10 ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } return - 1 ; }
def findNth ( n ) : count = 0 for curr in itertools.count ( ) : sum = 0 x = curr while ( x ) : sum = sum + x % 10 x = x // 10 if ( sum == 10 ) : count = count + 1 if ( count == n ) : return curr return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findNth ( int n ) { int count = 0 ; for ( int curr = 1 ; ; curr ++ ) { int sum = 0 ; for ( int x = curr ; x > 0 ; x = x / 10 ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } return - 1 ; } </Code>
cpp-python-SORT_1_N_SWAPPING_ADJACENT_ELEMENTS_1
cpp-python
bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] ) { if ( A [ i ] != i + 1 ) swap ( A [ i ] , A [ i + 1 ] ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return false ; } return true ; }
def sortedAfterSwap ( A , B , n ) : for i in range ( 0 , n - 1 ) : if B [ i ] : if A [ i ] != i + 1 : A [ i ] , A [ i + 1 ] = A [ i + 1 ] , A [ i ] for i in range ( n ) : if A [ i ] != i + 1 : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] ) { if ( A [ i ] != i + 1 ) swap ( A [ i ] , A [ i + 1 ] ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return false ; } return true ; } </Code>
cpp-python-MAXIMIZE_ARRAY_SUN_AFTER_K_NEGATION_OPERATIONS
cpp-python
int maximumSum ( int arr [ ] , int n , int k ) { for ( int i = 1 ; i <= k ; i ++ ) { int min = INT_MAX ; int index = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < min ) { min = arr [ j ] ; index = j ; } } if ( min == 0 ) break ; arr [ index ]...
def maximumSum ( arr , n , k ) : for i in range ( 1 , k + 1 ) : min = + 2147483647 index = - 1 for j in range ( n ) : if ( arr [ j ] < min ) : min = arr [ j ] index = j if ( min == 0 ) : break arr [ index ] = - arr [ ind...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maximumSum ( int arr [ ] , int n , int k ) { for ( int i = 1 ; i <= k ; i ++ ) { int min = INT_MAX ; int index = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < min ) { min = arr [ j ] ; index = j ; } } if ( min == 0 ) break ; arr [ ...
cpp-python-PRINT_SQUARES_FIRST_N_NATURAL_NUMBERS_WITHOUT_USING_1
cpp-python
void printSquares ( int n ) { int square = 0 , odd = 1 ; for ( int x = 0 ; x < n ; x ++ ) { cout << square << " " ; square = square + odd ; odd = odd + 2 ; } }
def printSquares ( n ) : square = 0 odd = 1 for x in range ( 0 , n ) : print ( square , end = " " ) square = square + odd odd = odd + 2
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printSquares ( int n ) { int square = 0 , odd = 1 ; for ( int x = 0 ; x < n ; x ++ ) { cout << square << " " ; square = square + odd ; odd = odd + 2 ; } } </Code>
cpp-python-CHECK_REVERSING_SUB_ARRAY_MAKE_ARRAY_SORTED_1
cpp-python
bool checkReverse ( int arr [ ] , int n ) { if ( n == 1 ) return true ; int i ; for ( i = 1 ; i < n && arr [ i - 1 ] < arr [ i ] ; i ++ ) ; if ( i == n ) return true ; int j = i ; while ( arr [ j ] < arr [ j - 1 ] ) { if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) return false ; j ++ ; } if ( j =...
def checkReverse ( arr , n ) : if ( n == 1 ) : return True i = 1 for i in range ( 1 , n ) : if arr [ i - 1 ] < arr [ i ] : if ( i == n ) : return True j = i while ( arr [ j ] < arr [ j - 1 ] ) : if ( i > 1 and arr [ j ] < arr [ i - 2 ] ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkReverse ( int arr [ ] , int n ) { if ( n == 1 ) return true ; int i ; for ( i = 1 ; i < n && arr [ i - 1 ] < arr [ i ] ; i ++ ) ; if ( i == n ) return true ; int j = i ; while ( arr [ j ] < arr [ j - 1 ] ) { if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) return false ; j ++ ; } i...
cpp-python-PROGRAM_CALCULATE_VOLUME_OCTAHEDRON
cpp-python
double vol_of_octahedron ( double side ) { return ( ( side * side * side ) * ( sqrt ( 2 ) / 3 ) ) ; }
def vol_of_octahedron ( side ) : return ( ( side * side * side ) * ( math.sqrt ( 2 ) / 3 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double vol_of_octahedron ( double side ) { return ( ( side * side * side ) * ( sqrt ( 2 ) / 3 ) ) ; } </Code>
cpp-python-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX
cpp-python
int numberOfPaths ( int m , int n ) { if ( m == 1 || n == 1 ) return 1 ; return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 ) ; }
def numberOfPaths ( m , n ) : if ( m == 1 or n == 1 ) : return 1 return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numberOfPaths ( int m , int n ) { if ( m == 1 || n == 1 ) return 1 ; return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 ) ; } </Code>
cpp-python-BINARY_SEARCH
cpp-python
int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; }
def binarySearch ( arr , l , r , x ) : if r >= l : mid = l + ( r - l ) // 2 if arr [ mid ] == x : return mid elif arr [ mid ] > x : return binarySearch ( arr , l , mid - 1 , x ) else : return binarySearch ( arr , mid + 1 , r , x ) else : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } </Code>
cpp-python-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_1
cpp-python
int numberOfPaths ( int m , int n ) { int count [ m ] [ n ] ; for ( int i = 0 ; i < m ; i ++ ) count [ i ] [ 0 ] = 1 ; for ( int j = 0 ; j < n ; j ++ ) count [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) count [ i ] [ j ] = count [ i - 1 ] [ j ] + ...
def numberOfPaths ( m , n ) : count = [ [ 0 for x in range ( m ) ] for y in range ( n ) ] for i in range ( m ) : count [ i ] [ 0 ] = 1 ; for j in range ( n ) : count [ 0 ] [ j ] = 1 ; for i in range ( 1 , m ) : for j in range ( n ) : count [ i ] [ j ] = count [ i - 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numberOfPaths ( int m , int n ) { int count [ m ] [ n ] ; for ( int i = 0 ; i < m ; i ++ ) count [ i ] [ 0 ] = 1 ; for ( int j = 0 ; j < n ; j ++ ) count [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) count [ i ] [ j ] = count [ i - 1 ] [...
cpp-python-NUMBER_N_DIGIT_STEPPING_NUMBERS
cpp-python
long long answer ( int n ) { int dp [ n + 1 ] [ 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; else if ( j == 9...
def answer ( n ) : dp = [ [ 0 for x in range ( 10 ) ] for y in range ( n + 1 ) ] if ( n == 1 ) : return 10 for j in range ( 10 ) : dp [ 1 ] [ j ] = 1 for i in range ( 2 , n + 1 ) : for j in range ( 10 ) : if ( j == 0 ) : dp [ i ] [ j ] = dp [ i - 1 ] [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long answer ( int n ) { int dp [ n + 1 ] [ 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; else if (...
cpp-python-PARTITION_INTO_TWO_SUBARRAYS_OF_LENGTHS_K_AND_N_K_SUCH_THAT_THE_DIFFERENCE_OF_SUMS_IS_MAXIMUM
cpp-python
int maxDifference ( int arr [ ] , int N , int k ) { int M , S = 0 , S1 = 0 , max_difference = 0 ; for ( int i = 0 ; i < N ; i ++ ) S += arr [ i ] ; sort ( arr , arr + N , greater < int > ( ) ) ; M = max ( k , N - k ) ; for ( int i = 0 ; i < M ; i ++ ) S1 += arr [ i ] ; max_difference = S1 - ( S - S1...
def maxDifference ( arr , N , k ) : S = 0 S1 = 0 max_difference = 0 for i in range ( N ) : S += arr [ i ] arr.sort ( reverse = True ) M = max ( k , N - k ) for i in range ( M ) : S1 += arr [ i ] max_difference = S1 - ( S - S1 ) return max_difference
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxDifference ( int arr [ ] , int N , int k ) { int M , S = 0 , S1 = 0 , max_difference = 0 ; for ( int i = 0 ; i < N ; i ++ ) S += arr [ i ] ; sort ( arr , arr + N , greater < int > ( ) ) ; M = max ( k , N - k ) ; for ( int i = 0 ; i < M ; i ++ ) S1 += arr [ i ] ; max_difference = S1 - (...
cpp-python-MAXIMUM_SUM_SUBARRAY_REMOVING_ONE_ELEMENT
cpp-python
int maxSumSubarrayRemovingOneEle ( int arr [ ] , int n ) { int fw [ n ] , bw [ n ] ; int cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ; fw [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { cur_max = max ( arr [ i ] , cur_max + arr [ i ] ) ; max_so_far = max ( max_so_far , cur_max ) ; fw [ i...
def maxSumSubarrayRemovingOneEle ( arr , n ) : fw = [ 0 for k in range ( n ) ] bw = [ 0 for k in range ( n ) ] cur_max , max_so_far = arr [ 0 ] , arr [ 0 ] for i in range ( n ) : cur_max = max ( arr [ i ] , cur_max + arr [ i ] ) max_so_far = max ( max_so_far , cur_max ) fw [ i ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSumSubarrayRemovingOneEle ( int arr [ ] , int n ) { int fw [ n ] , bw [ n ] ; int cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ; fw [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { cur_max = max ( arr [ i ] , cur_max + arr [ i ] ) ; max_so_far = max ( max_so_far , cur_max ) ; ...
cpp-python-MOVE_SPACES_FRONT_STRING_SINGLE_TRAVERSAL
cpp-python
void moveSpaceInFront ( char str [ ] ) { int i = strlen ( str ) ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ' ) str [ i -- ] = str [ j ] ; while ( i >= 0 ) str [ i -- ] = ' ' ; }
def moveSpaceInFront ( s ) : i = len ( s ) - 1 for j in range ( i , - 1 , - 1 ) : if ( s [ j ] != ' ' ) : s = s [ : i ] + s [ j ] + s [ i + 1 : ] i -= 1 while ( i >= 0 ) : s = s [ : i ] + ' ' + s [ i + 1 : ] i -= 1 return s
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void moveSpaceInFront ( char str [ ] ) { int i = strlen ( str ) ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ' ) str [ i -- ] = str [ j ] ; while ( i >= 0 ) str [ i -- ] = ' ' ; } </Code>
cpp-python-RECAMANS_SEQUENCE
cpp-python
int recaman ( int n ) { int arr [ n ] ; arr [ 0 ] = 0 ; printf ( "%d, " , arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int curr = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == curr ) || curr < 0 ) { curr = arr [ i - 1 ] + i ; bre...
def recaman ( n ) : arr = [ 0 ] * n arr [ 0 ] = 0 print ( arr [ 0 ] , end = ", " ) for i in range ( 1 , n ) : curr = arr [ i - 1 ] - i for j in range ( 0 , i ) : if ( ( arr [ j ] == curr ) or curr < 0 ) : curr = arr [ i - 1 ] + i break ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int recaman ( int n ) { int arr [ n ] ; arr [ 0 ] = 0 ; printf ( "%d, " , arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int curr = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == curr ) || curr < 0 ) { curr = arr [ i - 1 ] + i ; ...
cpp-python-MAXIMUM_POINTS_COLLECTED_BY_TWO_PERSONS_ALLOWED_TO_MEET_ONCE
cpp-python
int findMaxPoints ( int A [ ] [ M ] ) { int P1S [ M + 1 ] [ N + 1 ] , P1E [ M + 1 ] [ N + 1 ] ; memset ( P1S , 0 , sizeof ( P1S ) ) ; memset ( P1E , 0 , sizeof ( P1E ) ) ; int P2S [ M + 1 ] [ N + 1 ] , P2E [ M + 1 ] [ N + 1 ] ; memset ( P2S , 0 , sizeof ( P2S ) ) ; memset ( P2E , 0 , sizeof ( P2E ) ) ; fo...
def findMaxPoints ( A ) : P1S = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ] P1E = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ] P2S = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ] P2E = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ] for i in r...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMaxPoints ( int A [ ] [ M ] ) { int P1S [ M + 1 ] [ N + 1 ] , P1E [ M + 1 ] [ N + 1 ] ; memset ( P1S , 0 , sizeof ( P1S ) ) ; memset ( P1E , 0 , sizeof ( P1E ) ) ; int P2S [ M + 1 ] [ N + 1 ] , P2E [ M + 1 ] [ N + 1 ] ; memset ( P2S , 0 , sizeof ( P2S ) ) ; memset ( P2E , 0 , sizeof ( P2E ) )...
cpp-python-SPACE_OPTIMIZED_DP_SOLUTION_0_1_KNAPSACK_PROBLEM
cpp-python
int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int mat [ 2 ] [ W + 1 ] ; memset ( mat , 0 , sizeof ( mat ) ) ; int i = 0 ; while ( i < n ) { int j = 0 ; if ( i % 2 != 0 ) { while ( ++ j <= W ) { if ( wt [ i ] <= j ) mat [ 1 ] [ j ] = max ( val [ i ] + mat [ 0 ] [ j - wt [ i ] ...
def KnapSack ( val , wt , n , W ) : mat = [ [ 0 for i in range ( W + 1 ) ] for i in range ( 2 ) ] i = 0 while i < n : j = 0 if i % 2 == 0 : while j < W : j += 1 if wt [ i ] <= j : mat [ 1 ] [ j ] = max ( val [ i ] + mat [ 0 ] [ ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int mat [ 2 ] [ W + 1 ] ; memset ( mat , 0 , sizeof ( mat ) ) ; int i = 0 ; while ( i < n ) { int j = 0 ; if ( i % 2 != 0 ) { while ( ++ j <= W ) { if ( wt [ i ] <= j ) mat [ 1 ] [ j ] = max ( val [ i ] + mat [ 0 ] [ j - wt...
cpp-python-MULTIPLICATIVE_INVERSE_UNDER_MODULO_M
cpp-python
int modInverse ( int a , int m ) { a = a % m ; for ( int x = 1 ; x < m ; x ++ ) if ( ( a * x ) % m == 1 ) return x ; }
def modInverse ( a , m ) : a = a % m for x in range ( 1 , m ) : if ( ( a * x ) % m == 1 ) : return x return 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int modInverse ( int a , int m ) { a = a % m ; for ( int x = 1 ; x < m ; x ++ ) if ( ( a * x ) % m == 1 ) return x ; } </Code>
cpp-python-LENGTH_OF_THE_LONGEST_ARITHMATIC_PROGRESSION_IN_A_SORTED_ARRAY
cpp-python
int lenghtOfLongestAP ( int set [ ] , int n ) { if ( n <= 2 ) return n ; int L [ n ] [ n ] ; int llap = 2 ; for ( int i = 0 ; i < n ; i ++ ) L [ i ] [ n - 1 ] = 2 ; for ( int j = n - 2 ; j >= 1 ; j -- ) { int i = j - 1 , k = j + 1 ; while ( i >= 0 && k <= n - 1 ) { if ( set [ i ] + set [...
def lenghtOfLongestAP ( set , n ) : if ( n <= 2 ) : return n L = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] llap = 2 for i in range ( n ) : L [ i ] [ n - 1 ] = 2 for j in range ( n - 2 , 0 , - 1 ) : i = j - 1 k = j + 1 while ( i >= 0 and k <= n - 1 ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lenghtOfLongestAP ( int set [ ] , int n ) { if ( n <= 2 ) return n ; int L [ n ] [ n ] ; int llap = 2 ; for ( int i = 0 ; i < n ; i ++ ) L [ i ] [ n - 1 ] = 2 ; for ( int j = n - 2 ; j >= 1 ; j -- ) { int i = j - 1 , k = j + 1 ; while ( i >= 0 && k <= n - 1 ) { if ( set [ i ] ...
cpp-python-COUNT_BINARY_DIGIT_NUMBERS_SMALLER_N
cpp-python
int countOfBinaryNumberLessThanN ( int N ) { queue < int > q ; q . push ( 1 ) ; int cnt = 0 ; int t ; while ( ! q . empty ( ) ) { t = q . front ( ) ; q . pop ( ) ; if ( t <= N ) { cnt ++ ; q . push ( t * 10 ) ; q . push ( t * 10 + 1 ) ; } } return cnt ; }
def countOfBinaryNumberLessThanN ( N ) : q = deque ( ) q.append ( 1 ) cnt = 0 while ( q ) : t = q.popleft ( ) if ( t <= N ) : cnt = cnt + 1 q.append ( t * 10 ) q.append ( t * 10 + 1 ) return cnt
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countOfBinaryNumberLessThanN ( int N ) { queue < int > q ; q . push ( 1 ) ; int cnt = 0 ; int t ; while ( ! q . empty ( ) ) { t = q . front ( ) ; q . pop ( ) ; if ( t <= N ) { cnt ++ ; q . push ( t * 10 ) ; q . push ( t * 10 + 1 ) ; } } return cnt ; } </Code>
cpp-python-FIND_INDEX_GIVEN_FIBONACCI_NUMBER_CONSTANT_TIME
cpp-python
int findIndex ( int n ) { if ( n <= 1 ) return n ; int a = 0 , b = 1 , c = 1 ; int res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; }
def findIndex ( n ) : if ( n <= 1 ) : return n a = 0 b = 1 c = 1 res = 1 while ( c < n ) : c = a + b res = res + 1 a = b b = c return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findIndex ( int n ) { if ( n <= 1 ) return n ; int a = 0 , b = 1 , c = 1 ; int res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } </Code>
cpp-python-CONVERT_SUBSTRINGS_LENGTH_K_BASE_B_DECIMAL_1
cpp-python
int substringConversions ( string str , int k , int b ) { int i = 0 , sum = 0 , counter = k - 1 ; for ( i ; i < k ; i ++ ) { sum = sum + ( ( str . at ( i ) - '0' ) * pow ( b , counter ) ) ; counter -- ; } cout << sum << " " ; int prev = sum ; sum = 0 , counter = 0 ; for ( i ; i < str . size ...
def substringConversions ( str1 , k , b ) : for i in range ( 0 , len ( str1 ) - k + 1 ) : sub = str1 [ i : k + i ] Sum = 0 counter = 0 for i in range ( len ( sub ) - 1 , - 1 , - 1 ) : Sum = ( Sum + ( ( ord ( sub [ i ] ) - ord ( '0' ) ) * pow ( b , counter ) ) ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int substringConversions ( string str , int k , int b ) { int i = 0 , sum = 0 , counter = k - 1 ; for ( i ; i < k ; i ++ ) { sum = sum + ( ( str . at ( i ) - '0' ) * pow ( b , counter ) ) ; counter -- ; } cout << sum << " " ; int prev = sum ; sum = 0 , counter = 0 ; for ( i ; i < str ...
cpp-python-MAXIMUM_SUM_PAIRS_SPECIFIC_DIFFERENCE_1
cpp-python
int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int k ) { int maxSum = 0 ; sort ( arr , arr + N ) ; for ( int i = N - 1 ; i > 0 ; -- i ) { if ( arr [ i ] - arr [ i - 1 ] < k ) { maxSum += arr [ i ] ; maxSum += arr [ i - 1 ] ; -- i ; } } return maxSum ; }
def maxSumPairWithDifferenceLessThanK ( arr , N , k ) : maxSum = 0 arr.sort ( ) i = N - 1 while ( i >= 0 ) : if ( arr [ i ] - arr [ i - 1 ] < k ) : maxSum += arr [ i ] maxSum += arr [ i - 1 ] i -= 1 i -= 1 return maxSum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int k ) { int maxSum = 0 ; sort ( arr , arr + N ) ; for ( int i = N - 1 ; i > 0 ; -- i ) { if ( arr [ i ] - arr [ i - 1 ] < k ) { maxSum += arr [ i ] ; maxSum += arr [ i - 1 ] ; -- i ; } } return maxSum ; } </C...
cpp-python-LONGEST_REPEATED_SUBSEQUENCE_1
cpp-python
string longestRepeatedSubSeq ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] &...
def longestRepeatedSubSeq ( str ) : n = len ( str ) dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ] for i in range ( 1 , n + 1 ) : for j in range ( 1 , n + 1 ) : if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) : dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string longestRepeatedSubSeq ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) if ( str [ i - 1 ] == str [ j ...
cpp-python-HOW_TO_CHECK_IF_A_GIVEN_ARRAY_REPRESENTS_A_BINARY_HEAP_1
cpp-python
bool isHeap ( int arr [ ] , int n ) { for ( int i = 0 ; i <= ( n - 2 ) / 2 ; i ++ ) { if ( arr [ 2 * i + 1 ] > arr [ i ] ) return false ; if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) return false ; } return true ; }
def isHeap ( arr , n ) : for i in range ( int ( ( n - 2 ) / 2 ) + 1 ) : if arr [ 2 * i + 1 ] > arr [ i ] : return False if ( 2 * i + 2 < n and arr [ 2 * i + 2 ] > arr [ i ] ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isHeap ( int arr [ ] , int n ) { for ( int i = 0 ; i <= ( n - 2 ) / 2 ; i ++ ) { if ( arr [ 2 * i + 1 ] > arr [ i ] ) return false ; if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) return false ; } return true ; } </Code>
cpp-python-MINIMUM_COST_SORT_MATRIX_NUMBERS_0_N2_1
cpp-python
int calculateEnergy ( int mat [ SIZE ] [ SIZE ] , int n ) { int i_des , j_des , q ; int tot_energy = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { q = mat [ i ] [ j ] / n ; i_des = q ; j_des = mat [ i ] [ j ] - ( n * q ) ; tot_energy += abs ( i_d...
def calculateEnergy ( mat , n ) : tot_energy = 0 for i in range ( n ) : for j in range ( n ) : q = mat [ i ] [ j ] // n i_des = q j_des = mat [ i ] [ j ] - ( n * q ) tot_energy += ( abs ( i_des - i ) + abs ( j_des - j ) ) return tot_energy
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int calculateEnergy ( int mat [ SIZE ] [ SIZE ] , int n ) { int i_des , j_des , q ; int tot_energy = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { q = mat [ i ] [ j ] / n ; i_des = q ; j_des = mat [ i ] [ j ] - ( n * q ) ; tot_energy += ab...
cpp-python-POSITIVE_ELEMENTS_EVEN_NEGATIVE_ODD_POSITIONS
cpp-python
void rearrange ( int a [ ] , int size ) { int positive = 0 , negative = 1 ; while ( true ) { while ( positive < size && a [ positive ] >= 0 ) positive += 2 ; while ( negative < size && a [ negative ] <= 0 ) negative += 2 ; if ( positive < size && negative < size ) swap ( a [ positive ] , a [ negative ] ...
def rearrange ( a , size ) : positive = 0 negative = 1 while ( True ) : while ( positive < size and a [ positive ] >= 0 ) : positive = positive + 2 while ( negative < size and a [ negative ] <= 0 ) : negative = negative + 2 if ( positive < size and negative < ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void rearrange ( int a [ ] , int size ) { int positive = 0 , negative = 1 ; while ( true ) { while ( positive < size && a [ positive ] >= 0 ) positive += 2 ; while ( negative < size && a [ negative ] <= 0 ) negative += 2 ; if ( positive < size && negative < size ) swap ( a [ positive ] , a [ nega...
cpp-python-PRINT_MATRIX_ANTISPIRAL_FORM
cpp-python
void antiSpiralTraversal ( int m , int n , int a [ R ] [ C ] ) { int i , k = 0 , l = 0 ; stack < int > stk ; while ( k <= m && l <= n ) { for ( i = l ; i <= n ; ++ i ) stk . push ( a [ k ] [ i ] ) ; k ++ ; for ( i = k ; i <= m ; ++ i ) stk . push ( a [ i ] [ n ] ) ; n -- ; if (...
def antiSpiralTraversal ( m , n , a ) : k = 0 l = 0 stk = [ ] while ( k <= m and l <= n ) : for i in range ( l , n + 1 ) : stk.append ( a [ k ] [ i ] ) k += 1 for i in range ( k , m + 1 ) : stk.append ( a [ i ] [ n ] ) n -= 1 if ( k <= m ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void antiSpiralTraversal ( int m , int n , int a [ R ] [ C ] ) { int i , k = 0 , l = 0 ; stack < int > stk ; while ( k <= m && l <= n ) { for ( i = l ; i <= n ; ++ i ) stk . push ( a [ k ] [ i ] ) ; k ++ ; for ( i = k ; i <= m ; ++ i ) stk . push ( a [ i ] [ n ] ) ; n -- ; ...
cpp-python-RETURN_A_PAIR_WITH_MAXIMUM_PRODUCT_IN_ARRAY_OF_INTEGERS_1
cpp-python
void maxProduct ( int arr [ ] , int n ) { if ( n < 2 ) { cout << "No pairs exists\n" ; return ; } if ( n == 2 ) { cout << arr [ 0 ] << " " << arr [ 1 ] << endl ; return ; } int posa = INT_MIN , posb = INT_MIN ; int nega = INT_MIN , negb = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { ...
def maxProduct ( arr , n ) : if ( n < 2 ) : print ( "No pairs exists" ) return if ( n == 2 ) : print ( arr [ 0 ] , " " , arr [ 1 ] ) return posa = 0 posb = 0 nega = 0 negb = 0 for i in range ( n ) : if ( arr [ i ] > posa ) : posb = posa ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void maxProduct ( int arr [ ] , int n ) { if ( n < 2 ) { cout << "No pairs exists\n" ; return ; } if ( n == 2 ) { cout << arr [ 0 ] << " " << arr [ 1 ] << endl ; return ; } int posa = INT_MIN , posb = INT_MIN ; int nega = INT_MIN , negb = INT_MIN ; for ( int i = 0 ; i < n ; i ++...
cpp-python-MAXIMIZE_ARRAY_ELEMENTS_UPTO_GIVEN_NUMBER
cpp-python
int findMaxVal ( int arr [ ] , int n , int num , int maxLimit ) { int ind ; int val ; int dp [ n ] [ maxLimit + 1 ] ; for ( ind = 0 ; ind < n ; ind ++ ) { for ( val = 0 ; val <= maxLimit ; val ++ ) { if ( ind == 0 ) { if ( num - arr [ ind ] == val || num + arr [ ind ] == val ) { ...
def findMaxVal ( arr , n , num , maxLimit ) : ind = - 1 val = - 1 dp = [ [ 0 for i in range ( maxLimit + 1 ) ] for j in range ( n ) ] for ind in range ( n ) : for val in range ( maxLimit + 1 ) : if ( ind == 0 ) : if ( num - arr [ ind ] == val or num + arr [ ind ] == v...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMaxVal ( int arr [ ] , int n , int num , int maxLimit ) { int ind ; int val ; int dp [ n ] [ maxLimit + 1 ] ; for ( ind = 0 ; ind < n ; ind ++ ) { for ( val = 0 ; val <= maxLimit ; val ++ ) { if ( ind == 0 ) { if ( num - arr [ ind ] == val || num + arr [ ind ] == val...
cpp-python-PROGRAM_FOR_DEADLOCK_FREE_CONDITION_IN_OPERATING_SYSTEM
cpp-python
int Resources ( int process , int need ) { int minResources = 0 ; minResources = process * ( need - 1 ) + 1 ; return minResources ; }
def Resources ( process , need ) : minResources = 0 minResources = process * ( need - 1 ) + 1 return minResources
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int Resources ( int process , int need ) { int minResources = 0 ; minResources = process * ( need - 1 ) + 1 ; return minResources ; } </Code>
cpp-python-BUBBLE_SORT
cpp-python
void bubbleSort ( int arr [ ] , int n ) { int i , j ; for ( i = 0 ; i < n - 1 ; i ++ ) for ( j = 0 ; j < n - i - 1 ; j ++ ) if ( arr [ j ] > arr [ j + 1 ] ) swap ( & arr [ j ] , & arr [ j + 1 ] ) ; }
def bubbleSort ( arr ) : n = len ( arr ) for i in range ( n ) : for j in range ( 0 , n - i - 1 ) : if arr [ j ] > arr [ j + 1 ] : arr [ j ] , arr [ j + 1 ] = arr [ j + 1 ] , arr [ j ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void bubbleSort ( int arr [ ] , int n ) { int i , j ; for ( i = 0 ; i < n - 1 ; i ++ ) for ( j = 0 ; j < n - i - 1 ; j ++ ) if ( arr [ j ] > arr [ j + 1 ] ) swap ( & arr [ j ] , & arr [ j + 1 ] ) ; } </Code>
cpp-python-SHORTEST_COMMON_SUPERSEQUENCE_1
cpp-python
int superSeq ( char * X , char * Y , int m , int n ) { int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( ! i ) dp [ i ] [ j ] = j ; else if ( ! j ) dp [ i ] [ j ] = i ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) dp [ i ] [ j ] = 1...
def superSeq ( X , Y , m , n ) : dp = [ [ 0 ] * ( n + 2 ) for i in range ( m + 2 ) ] for i in range ( m + 1 ) : for j in range ( n + 1 ) : if ( not i ) : dp [ i ] [ j ] = j elif ( not j ) : dp [ i ] [ j ] = i elif ( X [ i - 1 ] == Y [ j - 1 ] ) : dp [ ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int superSeq ( char * X , char * Y , int m , int n ) { int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( ! i ) dp [ i ] [ j ] = j ; else if ( ! j ) dp [ i ] [ j ] = i ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) dp [ i ] [ ...
cpp-python-NUMBER_NON_NEGATIVE_INTEGRAL_SOLUTIONS_B_C_N
cpp-python
int countIntegralSolutions ( int n ) { int result = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n - i ; j ++ ) for ( int k = 0 ; k <= ( n - i - j ) ; k ++ ) if ( i + j + k == n ) result ++ ; return result ; }
def countIntegralSolutions ( n ) : result = 0 for i in range ( n + 1 ) : for j in range ( n + 1 ) : for k in range ( n + 1 ) : if i + j + k == n : result += 1 return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countIntegralSolutions ( int n ) { int result = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n - i ; j ++ ) for ( int k = 0 ; k <= ( n - i - j ) ; k ++ ) if ( i + j + k == n ) result ++ ; return result ; } </Code>
cpp-python-MINIMUM_NUMBER_OF_JUMPS_TO_REACH_END_OF_A_GIVEN_ARRAY_2
cpp-python
int minJumps ( int arr [ ] , int n ) { int * jumps = new int [ n ] ; int min ; jumps [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) jumps [ i ] = INT_MAX ; else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ; else { min = INT_MAX ; for ( int j = i + 1 ;...
def minJumps ( arr , n ) : jumps = [ 0 for i in range ( n ) ] for i in range ( n - 2 , - 1 , - 1 ) : if ( arr [ i ] == 0 ) : jumps [ i ] = float ( 'inf' ) elif ( arr [ i ] >= n - i - 1 ) : jumps [ i ] = 1 else : min = float ( 'inf' ) for j ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minJumps ( int arr [ ] , int n ) { int * jumps = new int [ n ] ; int min ; jumps [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) jumps [ i ] = INT_MAX ; else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ; else { min = INT_MAX ; for ( int j = ...
cpp-python-FIND_MINIMUM_ADJUSTMENT_COST_OF_AN_ARRAY
cpp-python
int minAdjustmentCost ( int A [ ] , int n , int target ) { int dp [ n ] [ M + 1 ] ; for ( int j = 0 ; j <= M ; j ++ ) dp [ 0 ] [ j ] = abs ( j - A [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= M ; j ++ ) { dp [ i ] [ j ] = INT_MAX ; for ( int k = max ( j - tar...
def minAdjustmentCost ( A , n , target ) : dp = [ [ 0 for i in range ( M + 1 ) ] for i in range ( n ) ] for j in range ( M + 1 ) : dp [ 0 ] [ j ] = abs ( j - A [ 0 ] ) for i in range ( 1 , n ) : for j in range ( M + 1 ) : dp [ i ] [ j ] = 100000000 for k in range ( ma...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minAdjustmentCost ( int A [ ] , int n , int target ) { int dp [ n ] [ M + 1 ] ; for ( int j = 0 ; j <= M ; j ++ ) dp [ 0 ] [ j ] = abs ( j - A [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= M ; j ++ ) { dp [ i ] [ j ] = INT_MAX ; for ( int k = max ( ...
cpp-python-KTH_SMALLESTLARGEST_ELEMENT_UNSORTED_ARRAY
cpp-python
int kthSmallest ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; return arr [ k - 1 ] ; }
def kthSmallest ( arr , n , k ) : arr.sort ( ) return arr [ k - 1 ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int kthSmallest ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; return arr [ k - 1 ] ; } </Code>
cpp-python-FIND_THE_MINIMUM_COST_TO_REACH_A_DESTINATION_WHERE_EVERY_STATION_IS_CONNECTED_IN_ONE_DIRECTION
cpp-python
INT_MAX int minCost ( int cost [ ] [ N ] ) { int dist [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dist [ i ] = INF ; dist [ 0 ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [ j ] ; re...
def minCost ( cost ) : dist = [ 0 for i in range ( N ) ] for i in range ( N ) : dist [ i ] = INF dist [ 0 ] = 0 for i in range ( N ) : for j in range ( i + 1 , N ) : if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) : dist [ j ] = dist [ i ] + cost [ i ] [ j ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> INT_MAX int minCost ( int cost [ ] [ N ] ) { int dist [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dist [ i ] = INF ; dist [ 0 ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [ j ]...
cpp-python-REVERSE_STRING_WITHOUT_USING_ANY_TEMPORARY_VARIABLE
cpp-python
string reversingString ( string str , int start , int end ) { while ( start < end ) { str [ start ] ^= str [ end ] ; str [ end ] ^= str [ start ] ; str [ start ] ^= str [ end ] ; ++ start ; -- end ; } return str ; }
def reversingString ( str , start , end ) : while ( start < end ) : str = ( str [ : start ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ start + 1 : ] ) str = ( str [ : end ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ end + 1 : ] ) str = ( str [ : start ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string reversingString ( string str , int start , int end ) { while ( start < end ) { str [ start ] ^= str [ end ] ; str [ end ] ^= str [ start ] ; str [ start ] ^= str [ end ] ; ++ start ; -- end ; } return str ; } </Code>
cpp-python-MINIMAL_MOVES_FORM_STRING_ADDING_CHARACTERS_APPENDING_STRING
cpp-python
int minimalSteps ( string s , int n ) { int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = INT_MAX ; string s1 = "" , s2 = "" ; dp [ 0 ] = 1 ; s1 += s [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s1 += s [ i ] ; s2 = s . substr ( i + 1 , i + 1 ) ; dp [ i ] = min ( dp [ i ] , dp [ i...
def minimalSteps ( s , n ) : dp = [ INT_MAX for i in range ( n ) ] s1 = "" s2 = "" dp [ 0 ] = 1 s1 += s [ 0 ] for i in range ( 1 , n ) : s1 += s [ i ] s2 = s [ i + 1 : i + 1 + i + 1 ] dp [ i ] = min ( dp [ i ] , dp [ i - 1 ] + 1 ) if ( s1 == s2 ) : dp ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minimalSteps ( string s , int n ) { int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = INT_MAX ; string s1 = "" , s2 = "" ; dp [ 0 ] = 1 ; s1 += s [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s1 += s [ i ] ; s2 = s . substr ( i + 1 , i + 1 ) ; dp [ i ] = min ( dp [ i ] ,...
cpp-python-MINIMUM_COST_TO_FILL_GIVEN_WEIGHT_IN_A_BAG
cpp-python
int MinimumCost ( int cost [ ] , int n , int W ) { vector < int > val , wt ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( cost [ i ] != - 1 ) { val . push_back ( cost [ i ] ) ; wt . push_back ( i + 1 ) ; size ++ ; } } n = size ; int min_cost [ n + 1 ] [ W + 1 ] ; fo...
def MinimumCost ( cost , n , W ) : val = list ( ) wt = list ( ) size = 0 for i in range ( n ) : if ( cost [ i ] != - 1 ) : val.append ( cost [ i ] ) wt.append ( i + 1 ) size += 1 n = size min_cost = [ [ 0 for i in range ( W + 1 ) ] for j in range ( n +...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int MinimumCost ( int cost [ ] , int n , int W ) { vector < int > val , wt ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( cost [ i ] != - 1 ) { val . push_back ( cost [ i ] ) ; wt . push_back ( i + 1 ) ; size ++ ; } } n = size ; int min_cost [ n + 1 ] [ W + 1 ]...
cpp-python-SORT_EVEN_PLACED_ELEMENTS_INCREASING_ODD_PLACED_DECREASING_ORDER
cpp-python
void bitonicGenerator ( int arr [ ] , int n ) { vector < int > evenArr ; vector < int > oddArr ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( i % 2 ) ) evenArr . push_back ( arr [ i ] ) ; else oddArr . push_back ( arr [ i ] ) ; } sort ( evenArr . begin ( ) , evenArr . end ( ) ) ; sort ( oddArr . ...
def bitonicGenerator ( arr , n ) : evenArr = [ ] oddArr = [ ] for i in range ( n ) : if ( ( i % 2 ) == 0 ) : evenArr.append ( arr [ i ] ) else : oddArr.append ( arr [ i ] ) evenArr = sorted ( evenArr ) oddArr = sorted ( oddArr ) oddArr = oddArr [ : : - 1 ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void bitonicGenerator ( int arr [ ] , int n ) { vector < int > evenArr ; vector < int > oddArr ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( i % 2 ) ) evenArr . push_back ( arr [ i ] ) ; else oddArr . push_back ( arr [ i ] ) ; } sort ( evenArr . begin ( ) , evenArr . end ( ) ) ; sort ( od...
cpp-python-COUNT_DIVISIBLE_PAIRS_ARRAY
cpp-python
int countDivisibles ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] % arr [ j ] == 0 || arr [ j ] % arr [ i ] == 0 ) res ++ ; return res ; }
def countDivisibles ( arr , n ) : res = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] % arr [ j ] == 0 or arr [ j ] % arr [ i ] == 0 ) : res += 1 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countDivisibles ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] % arr [ j ] == 0 || arr [ j ] % arr [ i ] == 0 ) res ++ ; return res ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_3_LONGEST_INCREASING_SUBSEQUENCE
cpp-python
int lis ( int arr [ ] , int n ) { int max = 1 ; _lis ( arr , n , & max ) ; return max ; }
def lis ( arr ) : global maximum n = len ( arr ) maximum = 1 _lis ( arr , n ) return maximum
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 max = 1 ; _lis ( arr , n , & max ) ; return max ; } </Code>
cpp-python-FIND_FIRST_REPEATING_ELEMENT_ARRAY_INTEGERS
cpp-python
void printFirstRepeating ( int arr [ ] , int n ) { int min = - 1 ; set < int > myset ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( myset . find ( arr [ i ] ) != myset . end ( ) ) min = i ; else myset . insert ( arr [ i ] ) ; } if ( min != - 1 ) cout << "The first repeating element is " << arr [ m...
def printFirstRepeating ( arr , n ) : Min = - 1 myset = dict ( ) for i in range ( n - 1 , - 1 , - 1 ) : if arr [ i ] in myset.keys ( ) : Min = i else : myset [ arr [ i ] ] = 1 if ( Min != - 1 ) : print ( "The first repeating element is" , arr [ Min ] ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printFirstRepeating ( int arr [ ] , int n ) { int min = - 1 ; set < int > myset ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( myset . find ( arr [ i ] ) != myset . end ( ) ) min = i ; else myset . insert ( arr [ i ] ) ; } if ( min != - 1 ) cout << "The first repeating element is " << ...
cpp-python-FIND_MINIMUM_NUMBER_OF_COINS_THAT_MAKE_A_CHANGE
cpp-python
int minCoins ( int coins [ ] , int m , int V ) { if ( V == 0 ) return 0 ; int res = INT_MAX ; for ( int i = 0 ; i < m ; i ++ ) { if ( coins [ i ] <= V ) { int sub_res = minCoins ( coins , m , V - coins [ i ] ) ; if ( sub_res != INT_MAX && sub_res + 1 < res ) res = sub_res + 1 ; } } ret...
def minCoins ( coins , m , V ) : if ( V == 0 ) : return 0 res = sys.maxsize for i in range ( 0 , m ) : if ( coins [ i ] <= V ) : sub_res = minCoins ( coins , m , V - coins [ i ] ) if ( sub_res != sys.maxsize and sub_res + 1 < res ) : res = sub_res + 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minCoins ( int coins [ ] , int m , int V ) { if ( V == 0 ) return 0 ; int res = INT_MAX ; for ( int i = 0 ; i < m ; i ++ ) { if ( coins [ i ] <= V ) { int sub_res = minCoins ( coins , m , V - coins [ i ] ) ; if ( sub_res != INT_MAX && sub_res + 1 < res ) res = sub_res + 1 ; } ...
cpp-python-FIND_PAIRS_GIVEN_SUM_ELEMENTS_PAIR_DIFFERENT_ROWS
cpp-python
void pairSum ( int mat [ ] [ MAX ] , int n , int sum ) { for ( int i = 0 ; i < n ; i ++ ) sort ( mat [ i ] , mat [ i ] + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int left = 0 , right = n - 1 ; while ( left < n && right >= 0 ) { if ( ...
def pairSum ( mat , n , sum ) : for i in range ( n ) : mat [ i ].sort ( ) for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : left = 0 right = n - 1 while ( left < n and right >= 0 ) : if ( ( mat [ i ] [ left ] + mat [ j ] [ right ] ) == ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void pairSum ( int mat [ ] [ MAX ] , int n , int sum ) { for ( int i = 0 ; i < n ; i ++ ) sort ( mat [ i ] , mat [ i ] + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int left = 0 , right = n - 1 ; while ( left < n && right >= 0 ) { ...
cpp-python-MINIMUM_COST_CONNECT_WEIGHTED_NODES_REPRESENTED_ARRAY
cpp-python
int minimum_cost ( int a [ ] , int n ) { int mn = INT_MAX ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mn = min ( a [ i ] , mn ) ; sum += a [ i ] ; } return mn * ( sum - mn ) ; }
def minimum_cost ( a , n ) : mn = sys.maxsize sum = 0 for i in range ( n ) : mn = min ( a [ i ] , mn ) sum += a [ i ] return mn * ( sum - mn )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minimum_cost ( int a [ ] , int n ) { int mn = INT_MAX ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mn = min ( a [ i ] , mn ) ; sum += a [ i ] ; } return mn * ( sum - mn ) ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_12_LONGEST_PALINDROMIC_SUBSEQUENCE
cpp-python
int lps ( char * str ) { int n = strlen ( str ) ; int i , j , cl ; int L [ n ] [ n ] ; for ( i = 0 ; i < n ; i ++ ) L [ i ] [ i ] = 1 ; for ( cl = 2 ; cl <= n ; cl ++ ) { for ( i = 0 ; i < n - cl + 1 ; i ++ ) { j = i + cl - 1 ; if ( str [ i ] == str [ j ] && cl == 2 ) L [ i ] [...
def lps ( str ) : n = len ( str ) L = [ [ 0 for x in range ( n ) ] for x in range ( n ) ] for i in range ( n ) : L [ i ] [ i ] = 1 for cl in range ( 2 , n + 1 ) : for i in range ( n - cl + 1 ) : j = i + cl - 1 if str [ i ] == str [ j ] and cl == 2 : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lps ( char * str ) { int n = strlen ( str ) ; int i , j , cl ; int L [ n ] [ n ] ; for ( i = 0 ; i < n ; i ++ ) L [ i ] [ i ] = 1 ; for ( cl = 2 ; cl <= n ; cl ++ ) { for ( i = 0 ; i < n - cl + 1 ; i ++ ) { j = i + cl - 1 ; if ( str [ i ] == str [ j ] && cl == 2 ) L ...
cpp-python-PROGRAM_CENSOR_WORD_ASTERISKS_SENTENCE
cpp-python
string censor ( string text , string word ) { vector < string > word_list ; boost :: split ( word_list , text , boost :: is_any_of ( "\\ +" ) ) ; string result = "" ; string stars = "" ; for ( int i = 0 ; i < word . size ( ) ; i ++ ) stars += '*' ; int index = 0 ; for ( string i : word_list ) { if...
def censor ( text , word ) : word_list = text.split ( ) result = '' stars = '*' * len ( word ) count = 0 index = 0 for i in word_list : if i == word : word_list [ index ] = stars index += 1 result = ' '.join ( word_list ) return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string censor ( string text , string word ) { vector < string > word_list ; boost :: split ( word_list , text , boost :: is_any_of ( "\\ +" ) ) ; string result = "" ; string stars = "" ; for ( int i = 0 ; i < word . size ( ) ; i ++ ) stars += '*' ; int index = 0 ; for ( string i : word_list ) {...
cpp-python-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT
cpp-python
public : bool isPowerOfFour ( int n ) { if ( n == 0 ) return 0 ; while ( n != 1 ) { if ( n % 4 != 0 ) return 0 ; n = n / 4 ; } return 1 ; }
def isPowerOfFour ( n ) : if ( n == 0 ) : return False while ( n != 1 ) : if ( n % 4 != 0 ) : return False n = n // 4 return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> public : bool isPowerOfFour ( int n ) { if ( n == 0 ) return 0 ; while ( n != 1 ) { if ( n % 4 != 0 ) return 0 ; n = n / 4 ; } return 1 ; } </Code>
cpp-python-AREA_OF_THE_CIRCLE_THAT_HAS_A_SQUARE_AND_A_CIRCLE_INSCRIBED_IN_IT
cpp-python
float getArea ( int a ) { float area = ( M_PI * a * a ) / 4.0 ; return area ; }
def getArea ( a ) : area = ( math.pi * a * a ) / 4 return area
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> float getArea ( int a ) { float area = ( M_PI * a * a ) / 4.0 ; return area ; } </Code>
cpp-python-SUM_MANHATTAN_DISTANCES_PAIRS_POINTS
cpp-python
int distancesum ( int x [ ] , int y [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) sum += ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) ) ; return sum ; }
def distancesum ( x , y , n ) : sum = 0 for i in range ( n ) : for j in range ( i + 1 , n ) : sum += ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) ) return sum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int distancesum ( int x [ ] , int y [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) sum += ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) ) ; return sum ; } </Code>
cpp-python-FIND_ROW_NUMBER_BINARY_MATRIX_MAXIMUM_NUMBER_1S
cpp-python
void findMax ( int arr [ ] [ N ] ) { int row = 0 , i , j ; for ( i = 0 , j = N - 1 ; i < N ; i ++ ) { while ( arr [ i ] [ j ] == 1 && j >= 0 ) { row = i ; j -- ; } } cout << "Row number = " << row + 1 ; cout << ", MaxCount = " << N - 1 - j ; }
def findMax ( arr ) : row = 0 j = N - 1 for i in range ( 0 , N ) : while ( arr [ i ] [ j ] == 1 and j >= 0 ) : row = i j -= 1 print ( "Row number = " , row + 1 , ", MaxCount = " , N - 1 - j )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findMax ( int arr [ ] [ N ] ) { int row = 0 , i , j ; for ( i = 0 , j = N - 1 ; i < N ; i ++ ) { while ( arr [ i ] [ j ] == 1 && j >= 0 ) { row = i ; j -- ; } } cout << "Row number = " << row + 1 ; cout << ", MaxCount = " << N - 1 - j ; } </Code>