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-RECURSIVELY_BREAK_NUMBER_3_PARTS_GET_MAXIMUM_SUM
cpp-python
int breakSum ( int n ) { if ( n == 0 || n == 1 ) return n ; return max ( ( breakSum ( n / 2 ) + breakSum ( n / 3 ) + breakSum ( n / 4 ) ) , n ) ; }
def breakSum ( n ) : if ( n == 0 or n == 1 ) : return n return max ( ( breakSum ( n // 2 ) + breakSum ( n // 3 ) + breakSum ( n // 4 ) ) , n )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int breakSum ( int n ) { if ( n == 0 || n == 1 ) return n ; return max ( ( breakSum ( n / 2 ) + breakSum ( n / 3 ) + breakSum ( n / 4 ) ) , n ) ; } </Code>
cpp-python-COUNT_PAIRS_TWO_SORTED_ARRAYS_WHOSE_SUM_EQUAL_GIVEN_VALUE_X
cpp-python
int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; }
def countPairs ( arr1 , arr2 , m , n , x ) : count = 0 for i in range ( m ) : for j in range ( n ) : if arr1 [ i ] + arr2 [ j ] == x : count = 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 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; } </Code>
cpp-python-LONGEST_ALTERNATING_SUB_ARRAY_STARTING_EVERY_INDEX_BINARY_ARRAY
cpp-python
void alternateSubarray ( bool arr [ ] , int n ) { int len [ n ] ; len [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( arr [ i ] ^ arr [ i + 1 ] == 1 ) len [ i ] = len [ i + 1 ] + 1 ; else len [ i ] = 1 ; } for ( int i = 0 ; i < n ; ++ i ) cout << len [ i ] << " " ; }
def alternateSubarray ( arr , n ) : len = [ ] for i in range ( n + 1 ) : len.append ( 0 ) len [ n - 1 ] = 1 for i in range ( n - 2 , - 1 , - 1 ) : if ( arr [ i ] ^ arr [ i + 1 ] == True ) : len [ i ] = len [ i + 1 ] + 1 else : len [ i ] = 1 for i in ra...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void alternateSubarray ( bool arr [ ] , int n ) { int len [ n ] ; len [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( arr [ i ] ^ arr [ i + 1 ] == 1 ) len [ i ] = len [ i + 1 ] + 1 ; else len [ i ] = 1 ; } for ( int i = 0 ; i < n ; ++ i ) cout << len [ i ] << " " ; } </Cod...
cpp-python-LEXICOGRAPHICALLY_LARGEST_SUBSEQUENCE_EVERY_CHARACTER_OCCURS_LEAST_K_TIMES
cpp-python
void subsequence ( char s [ ] , char t [ ] , int n , int k ) { int last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( char ch = 'z' ; ch >= 'a' ; ch -- ) { cnt = 0 ; for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) cnt ++ ; } if ( cnt >= k ) { for ( int i = last ;...
def subsequence ( s , t , n , k ) : last = 0 cnt = 0 new_last = 0 size = 0 string = 'zyxwvutsrqponmlkjihgfedcba' for ch in string : cnt = 0 for i in range ( last , n ) : if s [ i ] == ch : cnt += 1 if cnt >= k : for i in range ( las...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void subsequence ( char s [ ] , char t [ ] , int n , int k ) { int last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( char ch = 'z' ; ch >= 'a' ; ch -- ) { cnt = 0 ; for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) cnt ++ ; } if ( cnt >= k ) { for ( int i =...
cpp-python-FIND_THE_LARGEST_SUBARRAY_WITH_0_SUM
cpp-python
int maxLen ( int arr [ ] , int n ) { int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_sum += arr [ j ] ; if ( curr_sum == 0 ) max_len = max ( max_len , j - i + 1 ) ; } } return max_len ; }
def maxLen ( arr ) : max_len = 0 for i in range ( len ( arr ) ) : curr_sum = 0 for j in range ( i , len ( arr ) ) : curr_sum += arr [ j ] if curr_sum == 0 : max_len = max ( max_len , j - i + 1 ) return max_len
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxLen ( int arr [ ] , int n ) { int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_sum += arr [ j ] ; if ( curr_sum == 0 ) max_len = max ( max_len , j - i + 1 ) ; } } return max_len ; } </Code>
cpp-python-DICE_THROW_PROBLEM
cpp-python
int findWays ( int m , int n , int x ) { int table [ n + 1 ] [ x + 1 ] ; memset ( table , 0 , sizeof ( table ) ) ; for ( int j = 1 ; j <= m && j <= x ; j ++ ) table [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= x ; j ++ ) for ( int k = 1 ; k <= m && k < j ; k ++ ) ta...
def findWays ( m , n , x ) : table = [ [ 0 ] * ( x + 1 ) for i in range ( n + 1 ) ] for j in range ( 1 , min ( m + 1 , x + 1 ) ) : table [ 1 ] [ j ] = 1 for i in range ( 2 , n + 1 ) : for j in range ( 1 , x + 1 ) : for k in range ( 1 , min ( m + 1 , j ) ) : table ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findWays ( int m , int n , int x ) { int table [ n + 1 ] [ x + 1 ] ; memset ( table , 0 , sizeof ( table ) ) ; for ( int j = 1 ; j <= m && j <= x ; j ++ ) table [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= x ; j ++ ) for ( int k = 1 ; k <= m && k < j ; k ...
cpp-python-CHECK_HALF_STRING_CHARACTER_FREQUENCY_CHARACTER_1
cpp-python
bool checkCorrectOrNot ( string s ) { int count [ MAX_CHAR ] = { 0 } ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s [ i ] - 'a' ] ++ ; count [ s [ j ] - 'a' ] -- ; } for ( int i = 0 ; i < MAX_CH...
def checkCorrectOrNot ( s ) : global MAX_CHAR count = [ 0 ] * MAX_CHAR n = len ( s ) if n == 1 : return true i = 0 j = n - 1 while i < j : count [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1 count [ ord ( s [ j ] ) - ord ( 'a' ) ] -= 1 i += 1 j -= 1 for i in range ( ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkCorrectOrNot ( string s ) { int count [ MAX_CHAR ] = { 0 } ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s [ i ] - 'a' ] ++ ; count [ s [ j ] - 'a' ] -- ; } for ( int i = 0 ; i <...
cpp-python-FIND_NUMBER_OF_TRIANGLES_POSSIBLE
cpp-python
int findNumberOfTriangles ( int arr [ ] , int n ) { qsort ( arr , n , sizeof ( arr [ 0 ] ) , comp ) ; int count = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { int k = i + 2 ; for ( int j = i + 1 ; j < n ; ++ j ) { while ( k < n && arr [ i ] + arr [ j ] > arr [ k ] ) ++ k ; if ( k > j...
def findnumberofTriangles ( arr ) : n = len ( arr ) arr.sort ( ) count = 0 for i in range ( 0 , n - 2 ) : k = i + 2 for j in range ( i + 1 , n ) : while ( k < n and arr [ i ] + arr [ j ] > arr [ k ] ) : k += 1 if ( k > j ) : count +...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findNumberOfTriangles ( int arr [ ] , int n ) { qsort ( arr , n , sizeof ( arr [ 0 ] ) , comp ) ; int count = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { int k = i + 2 ; for ( int j = i + 1 ; j < n ; ++ j ) { while ( k < n && arr [ i ] + arr [ j ] > arr [ k ] ) ++ k ; if ...
cpp-python-TILING_WITH_DOMINOES
cpp-python
int countWays ( int n ) { int A [ n + 1 ] , B [ n + 1 ] ; A [ 0 ] = 1 , A [ 1 ] = 0 , B [ 0 ] = 0 , B [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] ; B [ i ] = A [ i - 1 ] + B [ i - 2 ] ; } return A [ n ] ; }
def countWays ( n ) : A = [ 0 ] * ( n + 1 ) B = [ 0 ] * ( n + 1 ) A [ 0 ] = 1 A [ 1 ] = 0 B [ 0 ] = 0 B [ 1 ] = 1 for i in range ( 2 , n + 1 ) : A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] B [ i ] = A [ i - 1 ] + B [ i - 2 ] return A [ n ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countWays ( int n ) { int A [ n + 1 ] , B [ n + 1 ] ; A [ 0 ] = 1 , A [ 1 ] = 0 , B [ 0 ] = 0 , B [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] ; B [ i ] = A [ i - 1 ] + B [ i - 2 ] ; } return A [ n ] ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_4_LONGEST_COMMON_SUBSEQUENCE
cpp-python
int lcs ( char * X , char * Y , int m , int n ) { if ( m == 0 || n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; }
def lcs ( X , Y , m , n ) : if m == 0 or n == 0 : return 0 ; elif X [ m - 1 ] == Y [ n - 1 ] : return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else : return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lcs ( char * X , char * Y , int m , int n ) { if ( m == 0 || n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } </Code>
cpp-python-MINIMIZE_SUM_PRODUCT_TWO_ARRAYS_PERMUTATIONS_ALLOWED
cpp-python
int minValue ( int A [ ] , int B [ ] , int n ) { sort ( A , A + n ) ; sort ( B , B + n ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( A [ i ] * B [ n - i - 1 ] ) ; return result ; }
def minValue ( A , B , n ) : sorted ( A ) sorted ( B ) result = 0 for i in range ( n ) : result += ( A [ i ] * B [ n - i - 1 ] ) return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minValue ( int A [ ] , int B [ ] , int n ) { sort ( A , A + n ) ; sort ( B , B + n ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( A [ i ] * B [ n - i - 1 ] ) ; return result ; } </Code>
cpp-python-CHANGE_BITS_CAN_MADE_ONE_FLIP_1
cpp-python
bool isOneFlip ( string str ) { int sum = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) sum += str [ i ] - '0' ; return ( sum == n - 1 || sum == 1 ) ; }
def isOneFlip ( str ) : sum = 0 n = len ( str ) for i in range ( 0 , n ) : sum += int ( str [ i ] ) - int ( '0' ) return ( sum == n - 1 or sum == 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isOneFlip ( string str ) { int sum = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) sum += str [ i ] - '0' ; return ( sum == n - 1 || sum == 1 ) ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_8_MATRIX_CHAIN_MULTIPLICATION
cpp-python
int MatrixChainOrder ( int p [ ] , int i , int j ) { if ( i == j ) return 0 ; int k ; int min = INT_MAX ; int count ; for ( k = i ; k < j ; k ++ ) { count = MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( count < min ) min = count ; } ...
def MatrixChainOrder ( p , i , j ) : if i == j : return 0 _min = sys.maxsize for k in range ( i , j ) : count = ( MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ) if count < _min : _min = count return _min
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int MatrixChainOrder ( int p [ ] , int i , int j ) { if ( i == j ) return 0 ; int k ; int min = INT_MAX ; int count ; for ( k = i ; k < j ; k ++ ) { count = MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( count < min ) min = count...
cpp-python-MULTIPLY_TWO_NUMBERS_WITHOUT_USING_MULTIPLY_DIVISION_BITWISE_OPERATORS_AND_NO_LOOPS
cpp-python
public : int multiply ( int x , int y ) { if ( y == 0 ) return 0 ; if ( y > 0 ) return ( x + multiply ( x , y - 1 ) ) ; if ( y < 0 ) return - multiply ( x , - y ) ; }
def multiply ( x , y ) : if ( y == 0 ) : return 0 if ( y > 0 ) : return ( x + multiply ( x , y - 1 ) ) if ( y < 0 ) : return - multiply ( x , - y )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> public : int multiply ( int x , int y ) { if ( y == 0 ) return 0 ; if ( y > 0 ) return ( x + multiply ( x , y - 1 ) ) ; if ( y < 0 ) return - multiply ( x , - y ) ; } </Code>
cpp-python-COUNT_NUMBER_OF_WAYS_TO_PARTITION_A_SET_INTO_K_SUBSETS_1
cpp-python
int countP ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 0 ; i <= k ; i ++ ) dp [ 0 ] [ k ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= i ; j ++ ) if ( j == 1 || i == j ) dp [ i ] [ j ] = 1 ; else dp [ i...
def countP ( n , k ) : dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] for i in range ( n + 1 ) : dp [ i ] [ 0 ] = 0 for i in range ( k + 1 ) : dp [ 0 ] [ k ] = 0 for i in range ( 1 , n + 1 ) : for j in range ( 1 , k + 1 ) : if ( j == 1 or i == j ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countP ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 0 ; i <= k ; i ++ ) dp [ 0 ] [ k ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= i ; j ++ ) if ( j == 1 || i == j ) dp [ i ] [ j ] = 1 ; else...
cpp-python-PROGRAM_WORST_FIT_ALGORITHM_MEMORY_MANAGEMENT
cpp-python
void worstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int wstIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { ...
def worstFit ( blockSize , m , processSize , n ) : allocation = [ - 1 ] * n for i in range ( n ) : wstIdx = - 1 for j in range ( m ) : if blockSize [ j ] >= processSize [ i ] : if wstIdx == - 1 : wstIdx = j elif blockSize [ wstIdx ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void worstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int wstIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i...
cpp-python-MAXIMIZE_VOLUME_CUBOID_GIVEN_SUM_SIDES_1
cpp-python
int maxvolume ( int s ) { int length = s / 3 ; s -= length ; int breadth = s / 2 ; int height = s - breadth ; return length * breadth * height ; }
def maxvolume ( s ) : length = int ( s / 3 ) s -= length breadth = s / 2 height = s - breadth return int ( length * breadth * height )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxvolume ( int s ) { int length = s / 3 ; s -= length ; int breadth = s / 2 ; int height = s - breadth ; return length * breadth * height ; } </Code>
cpp-python-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_1
cpp-python
int findLength ( char * str ) { int n = strlen ( str ) ; int maxlen = 0 ; int sum [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) sum [ i ] [ i ] = str [ i ] - '0' ; for ( int len = 2 ; len <= n ; len ++ ) { for ( int i = 0 ; i < n - len + 1 ; i ++ ) { int j = i + len - 1 ; int ...
def findLength ( string ) : n = len ( string ) maxlen = 0 Sum = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] for i in range ( 0 , n ) : Sum [ i ] [ i ] = int ( string [ i ] ) for length in range ( 2 , n + 1 ) : for i in range ( 0 , n - length + 1 ) : j = i + length...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLength ( char * str ) { int n = strlen ( str ) ; int maxlen = 0 ; int sum [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) sum [ i ] [ i ] = str [ i ] - '0' ; for ( int len = 2 ; len <= n ; len ++ ) { for ( int i = 0 ; i < n - len + 1 ; i ++ ) { int j = i + len - 1 ; ...
cpp-python-FIND_SUBARRAY_WITH_GIVEN_SUM_1
cpp-python
int subArraySum ( int arr [ ] , int n , int sum ) { int curr_sum = arr [ 0 ] , start = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { while ( curr_sum > sum && start < i - 1 ) { curr_sum = curr_sum - arr [ start ] ; start ++ ; } if ( curr_sum == sum ) { cout << "Sum found between indexes ...
def subArraySum ( arr , n , sum ) : curr_sum = arr [ 0 ] start = 0 i = 1 while i <= n : while curr_sum > sum and start < i - 1 : curr_sum = curr_sum - arr [ start ] start += 1 if curr_sum == sum : print ( "Sum found between indexes" ) print...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int subArraySum ( int arr [ ] , int n , int sum ) { int curr_sum = arr [ 0 ] , start = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { while ( curr_sum > sum && start < i - 1 ) { curr_sum = curr_sum - arr [ start ] ; start ++ ; } if ( curr_sum == sum ) { cout << "Sum found between i...
cpp-python-COMMON_CHARACTERS_N_STRINGS
cpp-python
void commonCharacters ( string str [ ] , int n ) { bool prim [ MAX_CHAR ] ; memset ( prim , true , sizeof ( prim ) ) ; for ( int i = 0 ; i < n ; i ++ ) { bool sec [ MAX_CHAR ] = { false } ; for ( int j = 0 ; str [ i ] [ j ] ; j ++ ) { if ( prim [ str [ i ] [ j ] - 'a'...
def commonCharacters ( strings , n ) : prim = [ True ] * MAX_CHAR for i in range ( n ) : sec = [ False ] * MAX_CHAR for j in range ( len ( strings [ i ] ) ) : if ( prim [ ord ( strings [ i ] [ j ] ) - ord ( 'a' ) ] ) : sec [ ord ( strings [ i ] [ j ] ) - ord ( 'a' ) ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void commonCharacters ( string str [ ] , int n ) { bool prim [ MAX_CHAR ] ; memset ( prim , true , sizeof ( prim ) ) ; for ( int i = 0 ; i < n ; i ++ ) { bool sec [ MAX_CHAR ] = { false } ; for ( int j = 0 ; str [ i ] [ j ] ; j ++ ) { if ( prim [ str [ i ] [ j ...
cpp-python-CHECK_WHETHER_SECOND_STRING_CAN_FORMED_FIRST_STRING_USING_COUNT_ARRAY
cpp-python
bool canMakeStr2 ( string str1 , string str2 ) { int count [ MAX ] = { 0 } ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) count [ str1 [ i ] ] ++ ; for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) { if ( count [ str2 [ i ] ] == 0 ) return false ; count [ str2 [ i ] ] --...
def canMakeStr2 ( s1 , s2 ) : count = { s1 [ i ] : 0 for i in range ( len ( s1 ) ) } for i in range ( len ( s1 ) ) : count [ s1 [ i ] ] += 1 for i in range ( len ( s2 ) ) : if count [ s2 [ i ] ] == 0 : return False count [ s2 [ i ] ] -= 1 return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool canMakeStr2 ( string str1 , string str2 ) { int count [ MAX ] = { 0 } ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) count [ str1 [ i ] ] ++ ; for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) { if ( count [ str2 [ i ] ] == 0 ) return false ; count [ str2 [ i...
cpp-python-MAGIC_SQUARE
cpp-python
void generateSquare ( int n ) { int magicSquare [ n ] [ n ] ; memset ( magicSquare , 0 , sizeof ( magicSquare ) ) ; int i = n / 2 ; int j = n - 1 ; for ( int num = 1 ; num <= n * n ; ) { if ( i == - 1 && j == n ) { j = n - 2 ; i = 0 ; } else { if ( j == n ) j = 0 ; if (...
def generateSquare ( n ) : magicSquare = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] i = n / 2 j = n - 1 num = 1 while num <= ( n * n ) : if i == - 1 and j == n : j = n - 2 i = 0 else : if j == n : j = 0 if i < 0...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void generateSquare ( int n ) { int magicSquare [ n ] [ n ] ; memset ( magicSquare , 0 , sizeof ( magicSquare ) ) ; int i = n / 2 ; int j = n - 1 ; for ( int num = 1 ; num <= n * n ; ) { if ( i == - 1 && j == n ) { j = n - 2 ; i = 0 ; } else { if ( j == n ) j = 0 ; ...
cpp-python-FIND_NUMBER_TIMES_STRING_OCCURS_GIVEN_STRING
cpp-python
int count ( string a , string b , int m , int n ) { if ( ( m == 0 && n == 0 ) || n == 0 ) return 1 ; if ( m == 0 ) return 0 ; if ( a [ m - 1 ] == b [ n - 1 ] ) return count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ; else return count ( a , b , m - 1 , n ) ; }
def count ( a , b , m , n ) : if ( ( m == 0 and n == 0 ) or n == 0 ) : return 1 if ( m == 0 ) : return 0 if ( a [ m - 1 ] == b [ n - 1 ] ) : return ( count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ) else : return count ( a , b , m - 1 , n )
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 , int n ) { if ( ( m == 0 && n == 0 ) || n == 0 ) return 1 ; if ( m == 0 ) return 0 ; if ( a [ m - 1 ] == b [ n - 1 ] ) return count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ; else return count ( a , b , m - 1 , n ) ; } </Code>
cpp-python-COUNT_ARRAYS_ADJACENT_ELEMENTS_ONE_DIVIDE_ANOTHER
cpp-python
int numofArray ( int n , int m ) { int dp [ MAX ] [ MAX ] ; vector < int > di [ MAX ] ; vector < int > mu [ MAX ] ; memset ( dp , 0 , sizeof dp ) ; memset ( di , 0 , sizeof di ) ; memset ( mu , 0 , sizeof mu ) ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 2 * i ; j <= m ; j += i ) { ...
def numofArray ( n , m ) : dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] di = [ [ ] for i in range ( MAX ) ] mu = [ [ ] for i in range ( MAX ) ] for i in range ( 1 , m + 1 ) : for j in range ( 2 * i , m + 1 , i ) : di [ j ].append ( i ) mu [ i ].append ( j ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numofArray ( int n , int m ) { int dp [ MAX ] [ MAX ] ; vector < int > di [ MAX ] ; vector < int > mu [ MAX ] ; memset ( dp , 0 , sizeof dp ) ; memset ( di , 0 , sizeof di ) ; memset ( mu , 0 , sizeof mu ) ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 2 * i ; j <= m ; j +=...
cpp-python-COUNT_1S_SORTED_BINARY_ARRAY
cpp-python
int countOnes ( bool arr [ ] , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == high || arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr , low ,...
def countOnes ( arr , low , high ) : if high >= low : mid = low + ( high - low ) / 2 if ( ( mid == high or arr [ mid + 1 ] == 0 ) and ( arr [ mid ] == 1 ) ) : return mid + 1 if arr [ mid ] == 1 : return countOnes ( arr , ( mid + 1 ) , high ) return countOnes (...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countOnes ( bool arr [ ] , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == high || arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr ...
cpp-python-CEILING_IN_A_SORTED_ARRAY_1
cpp-python
int ceilSearch ( int arr [ ] , int low , int high , int x ) { int mid ; if ( x <= arr [ low ] ) return low ; if ( x > arr [ high ] ) return - 1 ; mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid + 1 ; ...
def ceilSearch ( arr , low , high , x ) : if x <= arr [ low ] : return low if x > arr [ high ] : return - 1 mid = ( low + high ) / 2 if arr [ mid ] == x : return mid elif arr [ mid ] < x : if mid + 1 <= high and x <= arr [ mid + 1 ] : return mid + 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int ceilSearch ( int arr [ ] , int low , int high , int x ) { int mid ; if ( x <= arr [ low ] ) return low ; if ( x > arr [ high ] ) return - 1 ; mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid...
cpp-python-DECIMAL_BINARY_CONVERSION_WITHOUT_USING_ARITHMETIC_OPERATORS
cpp-python
string decToBin ( int n ) { if ( n == 0 ) return "0" ; string bin = "" ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; }
def decToBin ( n ) : if ( n == 0 ) : return "0" bin = "" while ( n > 0 ) : if ( n & 1 == 0 ) : bin = '0' + bin else : bin = '1' + bin n = n >> 1 return bin
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string decToBin ( int n ) { if ( n == 0 ) return "0" ; string bin = "" ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } </Code>
cpp-python-HOW_WILL_YOU_PRINT_NUMBERS_FROM_1_TO_200_WITHOUT_USING_LOOP
cpp-python
public : void printNos ( unsigned int n ) { if ( n > 0 ) { printNos ( n - 1 ) ; cout << n << " " ; } return ; }
def printNos ( n ) : if n > 0 : printNos ( n - 1 ) print ( n , end = ' ' )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> public : void printNos ( unsigned int n ) { if ( n > 0 ) { printNos ( n - 1 ) ; cout << n << " " ; } return ; } </Code>
cpp-python-PREFIX_SUM_2D_ARRAY
cpp-python
void prefixSum2D ( int a [ ] [ C ] ) { int psa [ R ] [ C ] ; psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ] ; for ( int i = 1 ; i < C ; i ++ ) psa [ 0 ] [ i ] = psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] ; for ( int i = 0 ; i < R ; i ++ ) psa [ i ] [ 0 ] = psa [ i - 1 ] [ 0 ] + a [ i ] [ 0 ] ; for ( int i = 1 ; i < R ; ...
def prefixSum2D ( a ) : global C , R psa = [ [ 0 for x in range ( C ) ] for y in range ( R ) ] psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ] for i in range ( 1 , C ) : psa [ 0 ] [ i ] = ( psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] ) for i in range ( 0 , R ) : psa [ i ] [ 0 ] = ( psa [ i - 1 ] [ 0 ] + a [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void prefixSum2D ( int a [ ] [ C ] ) { int psa [ R ] [ C ] ; psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ] ; for ( int i = 1 ; i < C ; i ++ ) psa [ 0 ] [ i ] = psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] ; for ( int i = 0 ; i < R ; i ++ ) psa [ i ] [ 0 ] = psa [ i - 1 ] [ 0 ] + a [ i ] [ 0 ] ; for ( int i = 1 ; i ...
cpp-python-EULERIAN_NUMBER
cpp-python
int eulerian ( int n , int m ) { if ( m >= n || n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; }
def eulerian ( n , m ) : if ( m >= n or n == 0 ) : return 0 if ( m == 0 ) : return 1 return ( ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int eulerian ( int n , int m ) { if ( m >= n || n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; } </Code>
cpp-python-FIND_SUM_UNIQUE_SUB_ARRAY_SUM_GIVEN_ARRAY
cpp-python
long long int findSubarraySum ( int arr [ ] , int n ) { int res = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; m [ sum ] ++ ; } } for ( auto x : m ) if ( x . second == 1 ) res += x...
def findSubarraySum ( arr , n ) : res = 0 m = dict ( ) for i in range ( n ) : Sum = 0 for j in range ( i , n ) : Sum += arr [ j ] m [ Sum ] = m.get ( Sum , 0 ) + 1 for x in m : if m [ x ] == 1 : res += x return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int findSubarraySum ( int arr [ ] , int n ) { int res = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; m [ sum ] ++ ; } } for ( auto x : m ) if ( x . second == 1 ) r...
cpp-python-WRITE_AN_EFFICIENT_METHOD_TO_CHECK_IF_A_NUMBER_IS_MULTIPLE_OF_3
cpp-python
int isMultipleOf3 ( int n ) { int odd_count = 0 ; int even_count = 0 ; if ( n < 0 ) n = - n ; if ( n == 0 ) return 1 ; if ( n == 1 ) return 0 ; while ( n ) { if ( n & 1 ) odd_count ++ ; if ( n & 2 ) even_count ++ ; n = n >> 2 ; } return isMultipleOf3 ( abs ( odd_count - even_count ) ) ; }
def isMultipleOf3 ( n ) : odd_count = 0 even_count = 0 if ( n < 0 ) : n = - n if ( n == 0 ) : return 1 if ( n == 1 ) : return 0 while ( n ) : if ( n & 1 ) : odd_count += 1 if ( n & 2 ) : even_count += 1 n = n >> 2 return...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int isMultipleOf3 ( int n ) { int odd_count = 0 ; int even_count = 0 ; if ( n < 0 ) n = - n ; if ( n == 0 ) return 1 ; if ( n == 1 ) return 0 ; while ( n ) { if ( n & 1 ) odd_count ++ ; if ( n & 2 ) even_count ++ ; n = n >> 2 ; } return isMultipleOf3 ( abs ( odd_count - even_count ) )...
cpp-python-LCS_FORMED_CONSECUTIVE_SEGMENTS_LEAST_LENGTH_K
cpp-python
int longestSubsequenceCommonSegment ( int k , string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; int lcs [ n + 1 ] [ m + 1 ] ; int cnt [ n + 1 ] [ m + 1 ] ; memset ( lcs , 0 , sizeof ( lcs ) ) ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { fo...
def longestSubsequenceCommonSegment ( k , s1 , s2 ) : n = len ( s1 ) m = len ( s2 ) lcs = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] cnt = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] for i in range ( 1 , n + 1 ) : for j in range ( 1 , m + 1 ) : lc...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int longestSubsequenceCommonSegment ( int k , string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; int lcs [ n + 1 ] [ m + 1 ] ; int cnt [ n + 1 ] [ m + 1 ] ; memset ( lcs , 0 , sizeof ( lcs ) ) ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 1 ; i <= n ; i ++ ) {...
cpp-python-PROGRAM_AREA_SQUARE
cpp-python
int areaSquare ( int side ) { int area = side * side ; return area ; }
def areaSquare ( side ) : area = side * side return area
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int areaSquare ( int side ) { int area = side * side ; return area ; } </Code>
cpp-python-EULERIAN_PATH_UNDIRECTED_GRAPH
cpp-python
void findpath ( int graph [ ] [ 5 ] , int n ) { vector < int > numofadj ; for ( int i = 0 ; i < n ; i ++ ) numofadj . push_back ( accumulate ( graph [ i ] , graph [ i ] + 5 , 0 ) ) ; int startpoint = 0 , numofodd = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( numofadj [ i ] % 2 == 1 ) { nu...
def findpath ( graph ) : n = len ( graph ) numofadj = list ( ) for i in range ( n ) : numofadj.append ( sum ( graph [ i ] ) ) startpoint = 0 numofodd = 0 for i in range ( n - 1 , - 1 , - 1 ) : if ( numofadj [ i ] % 2 == 1 ) : numofodd += 1 startpoint = i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findpath ( int graph [ ] [ 5 ] , int n ) { vector < int > numofadj ; for ( int i = 0 ; i < n ; i ++ ) numofadj . push_back ( accumulate ( graph [ i ] , graph [ i ] + 5 , 0 ) ) ; int startpoint = 0 , numofodd = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( numofadj [ i ] % 2 == 1 ) { ...
cpp-python-LCS_LONGEST_COMMON_SUBSEQUENCE_THREE_STRINGS_1
cpp-python
int lcsOf3 ( int i , int j , int k ) { if ( i == - 1 || j == - 1 || k == - 1 ) return 0 ; if ( dp [ i ] [ j ] [ k ] != - 1 ) return dp [ i ] [ j ] [ k ] ; if ( X [ i ] == Y [ j ] && Y [ j ] == Z [ k ] ) return dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) ; else return dp [ i ] [ j ] [ k ] = max (...
def lcsOf3 ( i , j , k ) : if ( i == - 1 or j == - 1 or k == - 1 ) : return 0 if ( dp [ i ] [ j ] [ k ] != - 1 ) : return dp [ i ] [ j ] [ k ] if ( X [ i ] == Y [ j ] and Y [ j ] == Z [ k ] ) : dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) return dp [ i ] [ j ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lcsOf3 ( int i , int j , int k ) { if ( i == - 1 || j == - 1 || k == - 1 ) return 0 ; if ( dp [ i ] [ j ] [ k ] != - 1 ) return dp [ i ] [ j ] [ k ] ; if ( X [ i ] == Y [ j ] && Y [ j ] == Z [ k ] ) return dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) ; else return dp [ i ] [ j ] [ k ] ...
cpp-python-FIND_ELEMENTS_LARGER_HALF_ELEMENTS_ARRAY
cpp-python
void findLarger ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = n - 1 ; i >= n / 2 ; i -- ) cout << arr [ i ] << " " ; }
def findLarger ( arr , n ) : x = sorted ( arr ) for i in range ( n / 2 , n ) : print ( x [ i ] ) ,
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findLarger ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = n - 1 ; i >= n / 2 ; i -- ) cout << arr [ i ] << " " ; } </Code>
cpp-python-SUM_DIAGONALS_SPIRAL_ODD_ORDER_SQUARE_MATRIX
cpp-python
int spiralDiaSum ( int n ) { if ( n == 1 ) return 1 ; return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) ; }
def spiralDiaSum ( n ) : if n == 1 : return 1 return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int spiralDiaSum ( int n ) { if ( n == 1 ) return 1 ; return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) ; } </Code>
cpp-python-COUNT_NUMBER_OF_OCCURRENCES_OR_FREQUENCY_IN_A_SORTED_ARRAY
cpp-python
int countOccurrences ( int arr [ ] , int n , int x ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res ++ ; return res ; }
def countOccurrences ( arr , n , x ) : res = 0 for i in range ( n ) : if x == arr [ i ] : res += 1 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countOccurrences ( int arr [ ] , int n , int x ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res ++ ; return res ; } </Code>
cpp-python-SUM_ELEMENTS_K1TH_K2TH_SMALLEST_ELEMENTS
cpp-python
int sumBetweenTwoKth ( int arr [ ] , int n , int k1 , int k2 ) { sort ( arr , arr + n ) ; return accumulate ( arr + k1 , arr + k2 - 1 , 0 ) ; }
def sumBetweenTwoKth ( arr , n , k1 , k2 ) : arr.sort ( ) result = 0 for i in range ( k1 , k2 - 1 ) : result += arr [ i ] return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sumBetweenTwoKth ( int arr [ ] , int n , int k1 , int k2 ) { sort ( arr , arr + n ) ; return accumulate ( arr + k1 , arr + k2 - 1 , 0 ) ; } </Code>
cpp-python-FIND_NUMBER_PAIRS_ARRAY_XOR_0_1
cpp-python
int calculate ( int a [ ] , int n ) { int * maximum = max_element ( a , a + 5 ) ; int frequency [ * maximum + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { frequency [ a [ i ] ] += 1 ; } int answer = 0 ; for ( int i = 0 ; i < ( * maximum ) + 1 ; i ++ ) { answer...
def calculate ( a ) : maximum = max ( a ) frequency = [ 0 for x in range ( maximum + 1 ) ] for i in a : frequency [ i ] += 1 answer = 0 for i in frequency : answer = answer + i * ( i - 1 ) // 2 return answer
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int calculate ( int a [ ] , int n ) { int * maximum = max_element ( a , a + 5 ) ; int frequency [ * maximum + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { frequency [ a [ i ] ] += 1 ; } int answer = 0 ; for ( int i = 0 ; i < ( * maximum ) + 1 ; i ++ ) { ...
cpp-python-DOUBLE_FACTORIAL_1
cpp-python
unsigned int doublefactorial ( unsigned int n ) { int res = 1 ; for ( int i = n ; i >= 0 ; i = i - 2 ) { if ( i == 0 || i == 1 ) return res ; else res *= i ; } }
def doublefactorial ( n ) : res = 1 for i in range ( n , - 1 , - 2 ) : if ( i == 0 or i == 1 ) : return res else : res *= i
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int doublefactorial ( unsigned int n ) { int res = 1 ; for ( int i = n ; i >= 0 ; i = i - 2 ) { if ( i == 0 || i == 1 ) return res ; else res *= i ; } } </Code>
cpp-python-LONGEST_COMMON_SUBSEQUENCE
cpp-python
int lcs ( char * X , char * Y , int m , int n ) { if ( m == 0 || n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; }
def lcs ( X , Y , m , n ) : if m == 0 or n == 0 : return 0 ; elif X [ m - 1 ] == Y [ n - 1 ] : return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else : return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lcs ( char * X , char * Y , int m , int n ) { if ( m == 0 || n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } </Code>
cpp-python-MAXIMUM_PRODUCT_SUBSET_ARRAY
cpp-python
int maxProductSubset ( int a [ ] , int n ) { if ( n == 1 ) return a [ 0 ] ; int max_neg = INT_MIN ; int count_neg = 0 , count_zero = 0 ; int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 ) { count_neg ++ ; ...
def maxProductSubset ( a , n ) : if n == 1 : return a [ 0 ] max_neg = - 999999999999 count_neg = 0 count_zero = 0 prod = 1 for i in range ( n ) : if a [ i ] == 0 : count_zero += 1 continue if a [ i ] < 0 : count_neg += 1 max...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxProductSubset ( int a [ ] , int n ) { if ( n == 1 ) return a [ 0 ] ; int max_neg = INT_MIN ; int count_neg = 0 , count_zero = 0 ; int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 ) { count_neg ...
cpp-python-TEMPLE_OFFERINGS
cpp-python
int offeringNumber ( int n , int templeHeight [ ] ) { int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int left = 0 , right = 0 ; for ( int j = i - 1 ; j >= 0 ; -- j ) { if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) ++ left ; else break ; } for ( int j = i + 1 ; j < ...
def offeringNumber ( n , templeHeight ) : sum = 0 for i in range ( n ) : left = 0 right = 0 for j in range ( i - 1 , - 1 , - 1 ) : if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) : left += 1 else : break for j in range ( ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int offeringNumber ( int n , int templeHeight [ ] ) { int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int left = 0 , right = 0 ; for ( int j = i - 1 ; j >= 0 ; -- j ) { if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) ++ left ; else break ; } for ( int j = i + 1 ; ...
cpp-python-FIND_SUM_NODES_GIVEN_PERFECT_BINARY_TREE
cpp-python
int sumNodes ( int l ) { int leafNodeCount = pow ( 2 , l - 1 ) ; vector < int > vec [ l ] ; for ( int i = 1 ; i <= leafNodeCount ; i ++ ) vec [ l - 1 ] . push_back ( i ) ; for ( int i = l - 2 ; i >= 0 ; i -- ) { int k = 0 ; while ( k < vec [ i + 1 ] . size ( ) - 1 ) { vec [ i ] . push_back...
def SumNodes ( l ) : leafNodeCount = pow ( 2 , l - 1 ) vec = [ [ ] for i in range ( l ) ] for i in range ( 1 , leafNodeCount + 1 ) : vec [ l - 1 ].append ( i ) for i in range ( l - 2 , - 1 , - 1 ) : k = 0 while ( k < len ( vec [ i + 1 ] ) - 1 ) : vec [ i ].append ( ve...
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 ) ; vector < int > vec [ l ] ; for ( int i = 1 ; i <= leafNodeCount ; i ++ ) vec [ l - 1 ] . push_back ( i ) ; for ( int i = l - 2 ; i >= 0 ; i -- ) { int k = 0 ; while ( k < vec [ i + 1 ] . size ( ) - 1 ) { vec [ i ] . pu...
cpp-python-CONSTRUCT_LEXICOGRAPHICALLY_SMALLEST_PALINDROME
cpp-python
string constructPalin ( string str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != '*' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == '*' ) { str [ i ] = 'a' ; str [ j ] = 'a' ; continue ; } else...
def constructPalin ( string , l ) : string = list ( string ) i = - 1 j = l while i < j : i += 1 j -= 1 if ( string [ i ] == string [ j ] and string [ i ] != '*' ) : continue elif ( string [ i ] == string [ j ] and string [ i ] == '*' ) : string [ i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string constructPalin ( string str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != '*' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == '*' ) { str [ i ] = 'a' ; str [ j ] = 'a' ; continue ; } ...
cpp-python-COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY
cpp-python
int countPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; }
def countPairs ( arr , n ) : ans = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] == arr [ j ] ) : ans += 1 return ans
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 ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; } </Code>
cpp-python-FAST_MULTIPLICATION_METHOD_WITHOUT_USING_MULTIPLICATION_OPERATOR_RUSSIAN_PEASANTS_ALGORITHM
cpp-python
unsigned int russianPeasant ( unsigned int a , unsigned int b ) { int res = 0 ; while ( b > 0 ) { if ( b & 1 ) res = res + a ; a = a << 1 ; b = b >> 1 ; } return res ; }
def russianPeasant ( a , b ) : res = 0 while ( b > 0 ) : if ( b & 1 ) : res = res + a a = a << 1 b = b >> 1 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int russianPeasant ( unsigned int a , unsigned int b ) { int res = 0 ; while ( b > 0 ) { if ( b & 1 ) res = res + a ; a = a << 1 ; b = b >> 1 ; } return res ; } </Code>
cpp-python-CAESAR_CIPHER
cpp-python
string encrypt ( string text , int s ) { string result = "" ; for ( int i = 0 ; i < text . length ( ) ; i ++ ) { if ( isupper ( text [ i ] ) ) result += char ( int ( text [ i ] + s - 65 ) % 26 + 65 ) ; else result += char ( int ( text [ i ] + s - 97 ) % 26 + 97 ) ; } return result ; }
def encrypt ( text , s ) : result = "" for i in range ( len ( text ) ) : char = text [ i ] if ( char.isupper ( ) ) : result += chr ( ( ord ( char ) + s - 65 ) % 26 + 65 ) else : result += chr ( ( ord ( char ) + s - 97 ) % 26 + 97 ) return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string encrypt ( string text , int s ) { string result = "" ; for ( int i = 0 ; i < text . length ( ) ; i ++ ) { if ( isupper ( text [ i ] ) ) result += char ( int ( text [ i ] + s - 65 ) % 26 + 65 ) ; else result += char ( int ( text [ i ] + s - 97 ) % 26 + 97 ) ; } return result ; } </Code...
cpp-python-PROGRAM_DISTANCE_TWO_POINTS_EARTH
cpp-python
long double distance ( long double lat1 , long double long1 , long double lat2 , long double long2 ) { lat1 = toRadians ( lat1 ) ; long1 = toRadians ( long1 ) ; lat2 = toRadians ( lat2 ) ; long2 = toRadians ( long2 ) ; long double dlong = long2 - long1 ; long double dlat = lat2 - lat1 ; long double ans = ...
def distance ( lat1 , lat2 , lon1 , lon2 ) : lon1 = radians ( lon1 ) lon2 = radians ( lon2 ) lat1 = radians ( lat1 ) lat2 = radians ( lat2 ) dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin ( dlat / 2 ) ** 2 + cos ( lat1 ) * cos ( lat2 ) * sin ( dlon / 2 ) ** 2 c = 2 * asin ( sqrt ( a ) ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long double distance ( long double lat1 , long double long1 , long double lat2 , long double long2 ) { lat1 = toRadians ( lat1 ) ; long1 = toRadians ( long1 ) ; lat2 = toRadians ( lat2 ) ; long2 = toRadians ( long2 ) ; long double dlong = long2 - long1 ; long double dlat = lat2 - lat1 ; long double...
cpp-python-NTH_EVEN_FIBONACCI_NUMBER
cpp-python
long int evenFib ( int n ) { if ( n < 1 ) return n ; if ( n == 1 ) return 2 ; return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ; }
def evenFib ( n ) : if ( n < 1 ) : return n if ( n == 1 ) : return 2 return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long int evenFib ( int n ) { if ( n < 1 ) return n ; if ( n == 1 ) return 2 ; return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ; } </Code>
cpp-python-STEINS_ALGORITHM_FOR_FINDING_GCD
cpp-python
int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; int k ; for ( k = 0 ; ( ( a
def gcd ( a , b ) : if ( a == 0 ) : return b if ( b == 0 ) : return a k = 0 while ( ( ( a
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; int k ; for ( k = 0 ; ( ( a </Code>
cpp-python-WRITE_ONE_LINE_C_FUNCTION_TO_FIND_WHETHER_A_NO_IS_POWER_OF_TWO
cpp-python
bool isPowerOfTwo ( int n ) { if ( n == 0 ) return 0 ; while ( n != 1 ) { if ( n % 2 != 0 ) return 0 ; n = n / 2 ; } return 1 ; }
def isPowerOfTwo ( n ) : if ( n == 0 ) : return False while ( n != 1 ) : if ( n % 2 != 0 ) : return False n = n // 2 return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPowerOfTwo ( int n ) { if ( n == 0 ) return 0 ; while ( n != 1 ) { if ( n % 2 != 0 ) return 0 ; n = n / 2 ; } return 1 ; } </Code>
cpp-python-SUM_PAIRWISE_PRODUCTS
cpp-python
long long int findSum ( int n ) { long long int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) sum = sum + i * j ; return sum ; }
def findSum ( n ) : sm = 0 for i in range ( 1 , n + 1 ) : for j in range ( i , n + 1 ) : sm = sm + i * j return sm
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int findSum ( int n ) { long long int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) sum = sum + i * j ; return sum ; } </Code>
cpp-python-SUM_BINOMIAL_COEFFICIENTS_1
cpp-python
int binomialCoeffSum ( int n ) { return ( 1 << n ) ; }
def binomialCoeffSum ( n ) : return ( 1 << n )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int binomialCoeffSum ( int n ) { return ( 1 << n ) ; } </Code>
cpp-python-EVALUATE_AN_ARRAY_EXPRESSION_WITH_NUMBERS_AND
cpp-python
int calculateSum ( string arr [ ] , int n ) { if ( n == 0 ) return 0 ; string s = arr [ 0 ] ; int value = stoi ( s ) ; int sum = value ; for ( int i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; int value = stoi ( s ) ; char operation = arr [ i - 1 ] [ 0 ] ; if ( operation == '+' ) sum += va...
def calculateSum ( arr , n ) : if ( n == 0 ) : return 0 s = arr [ 0 ] value = int ( s ) sum = value for i in range ( 2 , n , 2 ) : s = arr [ i ] value = int ( s ) operation = arr [ i - 1 ] [ 0 ] if ( operation == '+' ) : sum += value else :...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int calculateSum ( string arr [ ] , int n ) { if ( n == 0 ) return 0 ; string s = arr [ 0 ] ; int value = stoi ( s ) ; int sum = value ; for ( int i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; int value = stoi ( s ) ; char operation = arr [ i - 1 ] [ 0 ] ; if ( operation == '+' ) su...
cpp-python-CHECK_WHETHER_POINT_EXISTS_CIRCLE_SECTOR_NOT
cpp-python
void checkPoint ( int radius , int x , int y , float percent , float startAngle ) { float endAngle = 360 / percent + startAngle ; float polarradius = sqrt ( x * x + y * y ) ; float Angle = atan ( y / x ) ; if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) printf ( "Point (%d, %d) exist in ...
def checkPoint ( radius , x , y , percent , startAngle ) : endAngle = 360 / percent + startAngle polarradius = math.sqrt ( x * x + y * y ) Angle = math.atan ( y / x ) if ( Angle >= startAngle and Angle <= endAngle and polarradius < radius ) : print ( "Point (" , x , "," , y , ") " "exist in the ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void checkPoint ( int radius , int x , int y , float percent , float startAngle ) { float endAngle = 360 / percent + startAngle ; float polarradius = sqrt ( x * x + y * y ) ; float Angle = atan ( y / x ) ; if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) printf ( "Point (%d, %d) ex...
cpp-python-PRINTING_ITEMS_01_KNAPSACK
cpp-python
void printknapSack ( int W , int wt [ ] , int val [ ] , int n ) { int i , w ; int K [ n + 1 ] [ W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [ ...
def printknapSack ( W , wt , val , n ) : K = [ [ 0 for w in range ( W + 1 ) ] for i in range ( n + 1 ) ] for i in range ( n + 1 ) : for w in range ( W + 1 ) : if i == 0 or w == 0 : K [ i ] [ w ] = 0 elif wt [ i - 1 ] <= w : K [ i ] [ w ] = max ( va...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printknapSack ( int W , int wt [ ] , int val [ ] , int n ) { int i , w ; int K [ n + 1 ] [ W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ]...
cpp-python-SEQUENCES_GIVEN_LENGTH_EVERY_ELEMENT_EQUAL_TWICE_PREVIOUS_1
cpp-python
int getTotalNumberOfSequences ( int m , int n ) { int T [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { if ( i == 0 || j == 0 ) T [ i ] [ j ] = 0 ; else if ( i < j ) T [ i ] [ j ] = 0 ; else if ( j == 1 ) T [ i ] [ j ] = i ; ...
def getTotalNumberOfSequences ( m , n ) : T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ] for i in range ( m + 1 ) : for j in range ( n + 1 ) : if i == 0 or j == 0 : T [ i ] [ j ] = 0 elif i < j : T [ i ] [ j ] = 0 elif ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getTotalNumberOfSequences ( int m , int n ) { int T [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { if ( i == 0 || j == 0 ) T [ i ] [ j ] = 0 ; else if ( i < j ) T [ i ] [ j ] = 0 ; else if ( j == 1 ) T [ i ] [ j ] = i ...
cpp-python-SUM_MATRIX_ELEMENT_ABSOLUTE_DIFFERENCE_ROW_COLUMN_NUMBERS
cpp-python
int findSum ( int n ) { int arr [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = abs ( i - j ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += arr [ i ] [ j ] ; return sum ; }
def findSum ( n ) : arr = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] for i in range ( n ) : for j in range ( n ) : arr [ i ] [ j ] = abs ( i - j ) sum = 0 for i in range ( n ) : for j in range ( n ) : sum += arr [ i ] [ j ] return sum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSum ( int n ) { int arr [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = abs ( i - j ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += arr [ i ] [ j ] ; return sum ; } </Code>
cpp-python-REPLACE_CHARACTER_C1_C2_C2_C1_STRING_S
cpp-python
string replace ( string s , char c1 , char c2 ) { int l = s . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( s [ i ] == c1 ) s [ i ] = c2 ; else if ( s [ i ] == c2 ) s [ i ] = c1 ; } return s ; }
def replace ( s , c1 , c2 ) : l = len ( s ) for i in range ( l ) : if ( s [ i ] == c1 ) : s = s [ 0 : i ] + c2 + s [ i + 1 : ] elif ( s [ i ] == c2 ) : s = s [ 0 : i ] + c1 + s [ i + 1 : ] return s
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string replace ( string s , char c1 , char c2 ) { int l = s . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( s [ i ] == c1 ) s [ i ] = c2 ; else if ( s [ i ] == c2 ) s [ i ] = c1 ; } return s ; } </Code>
cpp-python-DISCRETE_LOGARITHM_FIND_INTEGER_K_AK_CONGRUENT_MODULO_B
cpp-python
int discreteLogarithm ( int a , int b , int m ) { int n = ( int ) sqrt ( m ) + 1 ; int an = 1 ; for ( int i = 0 ; i < n ; ++ i ) an = ( an * a ) % m ; unordered_map < int , int > value ; for ( int i = 1 , cur = an ; i <= n ; ++ i ) { if ( ! value [ cur ] ) value [ cur ] = i ; cur = ( cur * an ...
def discreteLogarithm ( a , b , m ) : n = int ( math.sqrt ( m ) + 1 ) an = 1 for i in range ( n ) : an = ( an * a ) % m value = [ 0 ] * m cur = an for i in range ( 1 , n + 1 ) : if ( value [ cur ] == 0 ) : value [ cur ] = i cur = ( cur * an ) % m cur = b ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int discreteLogarithm ( int a , int b , int m ) { int n = ( int ) sqrt ( m ) + 1 ; int an = 1 ; for ( int i = 0 ; i < n ; ++ i ) an = ( an * a ) % m ; unordered_map < int , int > value ; for ( int i = 1 , cur = an ; i <= n ; ++ i ) { if ( ! value [ cur ] ) value [ cur ] = i ; cur = ( cu...
cpp-python-FIND_LARGEST_PRIME_FACTOR_NUMBER
cpp-python
long long maxPrimeFactors ( long long n ) { long long maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n >>= 1 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } } if ( n > 2 ) maxPrime = n ; return maxPrime ; }
def maxPrimeFactors ( n ) : maxPrime = - 1 while n % 2 == 0 : maxPrime = 2 n >>= 1 for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 , 2 ) : while n % i == 0 : maxPrime = i n = n / i if n > 2 : maxPrime = n return int ( maxPrime )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long maxPrimeFactors ( long long n ) { long long maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n >>= 1 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } } if ( n > 2 ) maxPrime = n ; return maxPrime ;...
cpp-python-POSSIBLE_FORM_TRIANGLE_ARRAY_VALUES
cpp-python
bool isPossibleTriangle ( int arr [ ] , int N ) { if ( N < 3 ) return false ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; }
def isPossibleTriangle ( arr , N ) : if N < 3 : return False arr.sort ( ) for i in range ( N - 2 ) : if arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] : return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPossibleTriangle ( int arr [ ] , int N ) { if ( N < 3 ) return false ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; } </Code>
cpp-python-COUNT_NUMBER_INCREASING_SUBSEQUENCES_SIZE_K
cpp-python
int numOfIncSubseqOfSizeK ( int arr [ ] , int n , int k ) { int dp [ k ] [ n ] , sum = 0 ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int l = 1 ; l < k ; l ++ ) { for ( int i = l ; i < n ; i ++ ) { dp [ l ] [ i ] = 0 ; for ( in...
def numOfIncSubseqOfSizeK ( arr , n , k ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( k ) ] for i in range ( n ) : dp [ 0 ] [ i ] = 1 for l in range ( 1 , k ) : for i in range ( l , n ) : dp [ l ] [ i ] = 0 for j in range ( l - 1 , i ) : if ( ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numOfIncSubseqOfSizeK ( int arr [ ] , int n , int k ) { int dp [ k ] [ n ] , sum = 0 ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int l = 1 ; l < k ; l ++ ) { for ( int i = l ; i < n ; i ++ ) { dp [ l ] [ i ] = 0 ; f...
cpp-python-MINIMUM_COST_MAKE_LONGEST_COMMON_SUBSEQUENCE_LENGTH_K
cpp-python
int solve ( char X [ ] , char Y [ ] , int l , int r , int k , int dp [ ] [ N ] [ N ] ) { if ( ! k ) return 0 ; if ( l < 0
def solve ( X , Y , l , r , k , dp ) : if k == 0 : return 0 if l < 0 or r < 0 : return 1000000000 if dp [ l ] [ r ] [ k ] != - 1 : return dp [ l ] [ r ] [ k ] cost = ( ( ord ( X [ l ] ) - ord ( 'a' ) ) ^ ( ord ( Y [ r ] ) - ord ( 'a' ) ) ) dp [ l ] [ r ] [ k ] = min ( [ cost ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int solve ( char X [ ] , char Y [ ] , int l , int r , int k , int dp [ ] [ N ] [ N ] ) { if ( ! k ) return 0 ; if ( l < 0 </Code>
cpp-python-REMAINDER_7_LARGE_NUMBERS
cpp-python
int remainderWith7 ( string num ) { int series [ ] = { 1 , 3 , 2 , - 1 , - 3 , - 2 } ; int series_index = 0 ; int result = 0 ; for ( int i = num . size ( ) - 1 ; i >= 0 ; i -- ) { int digit = num [ i ] - '0' ; result += digit * series [ series_index ] ; series_index = ( s...
def remainderWith7 ( num ) : series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ] series_index = 0 result = 0 for i in range ( ( len ( num ) - 1 ) , - 1 , - 1 ) : digit = ord ( num [ i ] ) - 48 result += digit * series [ series_index ] series_index = ( series_index + 1 ) % 6 result %...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int remainderWith7 ( string num ) { int series [ ] = { 1 , 3 , 2 , - 1 , - 3 , - 2 } ; int series_index = 0 ; int result = 0 ; for ( int i = num . size ( ) - 1 ; i >= 0 ; i -- ) { int digit = num [ i ] - '0' ; result += digit * series [ series_index ] ; series_inde...
cpp-python-GIVEN_TWO_STRINGS_FIND_FIRST_STRING_SUBSEQUENCE_SECOND
cpp-python
bool isSubSequence ( char str1 [ ] , char str2 [ ] , int m , int n ) { if ( m == 0 ) return true ; if ( n == 0 ) return false ; if ( str1 [ m - 1 ] == str2 [ n - 1 ] ) return isSubSequence ( str1 , str2 , m - 1 , n - 1 ) ; return isSubSequence ( str1 , str2 , m , n - 1 ) ; }
def isSubSequence ( string1 , string2 , m , n ) : if m == 0 : return True if n == 0 : return False if string1 [ m - 1 ] == string2 [ n - 1 ] : return isSubSequence ( string1 , string2 , m - 1 , n - 1 ) return isSubSequence ( string1 , string2 , m , n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isSubSequence ( char str1 [ ] , char str2 [ ] , int m , int n ) { if ( m == 0 ) return true ; if ( n == 0 ) return false ; if ( str1 [ m - 1 ] == str2 [ n - 1 ] ) return isSubSequence ( str1 , str2 , m - 1 , n - 1 ) ; return isSubSequence ( str1 , str2 , m , n - 1 ) ; } </Code>
cpp-python-PROGRAM_TO_FIND_REMAINDER_WITHOUT_USING_MODULO_OR_OPERATOR
cpp-python
int getRemainder ( int num , int divisor ) { return ( num - divisor * ( num / divisor ) ) ; }
def getRemainder ( num , divisor ) : return ( num - divisor * ( num // divisor ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getRemainder ( int num , int divisor ) { return ( num - divisor * ( num / divisor ) ) ; } </Code>
cpp-python-MINIMUM_PRODUCT_SUBSET_ARRAY
cpp-python
int minProductSubset ( int a [ ] , int n ) { if ( n == 1 ) return a [ 0 ] ; int max_neg = INT_MIN ; int min_pos = INT_MAX ; int count_neg = 0 , count_zero = 0 ; int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 )...
def minProductSubset ( a , n ) : if ( n == 1 ) : return a [ 0 ] max_neg = float ( '-inf' ) min_pos = float ( 'inf' ) count_neg = 0 count_zero = 0 prod = 1 for i in range ( 0 , n ) : if ( a [ i ] == 0 ) : count_zero = count_zero + 1 continue if ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minProductSubset ( int a [ ] , int n ) { if ( n == 1 ) return a [ 0 ] ; int max_neg = INT_MIN ; int min_pos = INT_MAX ; int count_neg = 0 , count_zero = 0 ; int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ...
cpp-python-HOW_CAN_WE_SUM_THE_DIGITS_OF_A_GIVEN_NUMBER_IN_SINGLE_STATEMENT
cpp-python
public : int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; }
def getSum ( n ) : sum = 0 while ( n != 0 ) : sum = sum + int ( n % 10 ) n = int ( n / 10 ) return sum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> public : int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } </Code>
cpp-python-COUNT_DIGITS_FACTORIAL_SET_1
cpp-python
int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += log10 ( i ) ; return floor ( digits ) + 1 ; }
def findDigits ( n ) : if ( n < 0 ) : return 0 if ( n <= 1 ) : return 1 digits = 0 for i in range ( 2 , n + 1 ) : digits += math.log10 ( i ) return math.floor ( digits ) + 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += log10 ( i ) ; return floor ( digits ) + 1 ; } </Code>
cpp-python-SORT_ARRAY_TWO_HALVES_SORTED
cpp-python
void mergeTwoHalf ( int A [ ] , int n ) { sort ( A , A + n ) ; }
def mergeTwoHalf ( A , n ) : A.sort ( )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void mergeTwoHalf ( int A [ ] , int n ) { sort ( A , A + n ) ; } </Code>
cpp-python-CREATE_A_MATRIX_WITH_ALTERNATING_RECTANGLES_OF_0_AND_X
cpp-python
void fill0X ( int m , int n ) { int i , k = 0 , l = 0 ; int r = m , c = n ; char a [ m ] [ n ] ; char x = 'X' ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) a [ k ] [ i ] = x ; k ++ ; for ( i = k ; i < m ; ++ i ) a [ i ] [ n - 1 ] = x ; n -- ; if ( k < m ) { ...
def fill0X ( m , n ) : i , k , l = 0 , 0 , 0 r = m c = n a = [ [ None ] * n for i in range ( m ) ] x = 'X' while k < m and l < n : for i in range ( l , n ) : a [ k ] [ i ] = x k += 1 for i in range ( k , m ) : a [ i ] [ n - 1 ] = x n -= 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void fill0X ( int m , int n ) { int i , k = 0 , l = 0 ; int r = m , c = n ; char a [ m ] [ n ] ; char x = 'X' ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) a [ k ] [ i ] = x ; k ++ ; for ( i = k ; i < m ; ++ i ) a [ i ] [ n - 1 ] = x ; n -- ; if ( k < m )...
cpp-python-MINIMUM_PERIMETER_N_BLOCKS
cpp-python
int minPerimeter ( int n ) { int l = sqrt ( n ) ; int sq = l * l ; if ( sq == n ) return l * 4 ; else { long long int row = n / l ; long long int perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } }
def minPerimeter ( n ) : l = math.sqrt ( n ) sq = l * l if ( sq == n ) : return l * 4 else : row = n / l perimeter = 2 * ( l + row ) if ( n % l != 0 ) : perimeter += 2 return perimeter
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minPerimeter ( int n ) { int l = sqrt ( n ) ; int sq = l * l ; if ( sq == n ) return l * 4 ; else { long long int row = n / l ; long long int perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } } </Code>
cpp-python-FIND_POSSIBLE_ROTATE_PAGE_ANGLE_NOT
cpp-python
void possibleOrNot ( long long a1 , long long a2 , long long b1 , long long b2 , long long c1 , long long c2 ) { long long dis1 = pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ; long long dis2 = pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ; if ( dis1 != dis2 ) cout << "No" ; else if ( b1 == ( ( a1 + c1 ) / 2.0 ) && b...
def possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) : dis1 = ( pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ) dis2 = ( pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ) if ( dis1 != dis2 ) : print ( "No" ) elif ( b1 == ( ( a1 + c1 ) // 2.0 ) and b2 == ( ( a2 + c2 ) // 2.0 ) ) : print ( "No" ) e...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void possibleOrNot ( long long a1 , long long a2 , long long b1 , long long b2 , long long c1 , long long c2 ) { long long dis1 = pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ; long long dis2 = pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ; if ( dis1 != dis2 ) cout << "No" ; else if ( b1 == ( ( a1 + c1 ) / 2.0...
cpp-python-PROGRAM_FIND_SLOPE_LINE
cpp-python
float slope ( float x1 , float y1 , float x2 , float y2 ) { return ( y2 - y1 ) / ( x2 - x1 ) ; }
def slope ( x1 , y1 , x2 , y2 ) : return ( float ) ( y2 - y1 ) / ( x2 - x1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> float slope ( float x1 , float y1 , float x2 , float y2 ) { return ( y2 - y1 ) / ( x2 - x1 ) ; } </Code>
cpp-python-FIND_NUMBER_PERFECT_SQUARES_TWO_GIVEN_NUMBERS_1
cpp-python
int countSquares ( int a , int b ) { return ( floor ( sqrt ( b ) ) - ceil ( sqrt ( a ) ) + 1 ) ; }
def CountSquares ( a , b ) : return ( math.floor ( math.sqrt ( b ) ) - math.ceil ( math.sqrt ( a ) ) + 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSquares ( int a , int b ) { return ( floor ( sqrt ( b ) ) - ceil ( sqrt ( a ) ) + 1 ) ; } </Code>
cpp-python-COUNT_BINARY_STRINGS_K_TIMES_APPEARING_ADJACENT_TWO_SET_BITS
cpp-python
int countStrings ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] [ 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 1 ] [ 0 ] [ 0 ] = 1 ; dp [ 1 ] [ 0 ] [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] + dp [ ...
def countStrings ( n , k ) : dp = [ [ [ 0 , 0 ] for __ in range ( k + 1 ) ] for _ in range ( n + 1 ) ] dp [ 1 ] [ 0 ] [ 0 ] = 1 dp [ 1 ] [ 0 ] [ 1 ] = 1 for i in range ( 2 , n + 1 ) : for j in range ( k + 1 ) : dp [ i ] [ j ] [ 0 ] = ( dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [ j ] [ ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countStrings ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] [ 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 1 ] [ 0 ] [ 0 ] = 1 ; dp [ 1 ] [ 0 ] [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] ...
cpp-python-FIND_SMALLEST_RANGE_CONTAINING_ELEMENTS_FROM_K_LISTS
cpp-python
void findSmallestRange ( int arr [ ] [ N ] , int n , int k ) { int i , minval , maxval , minrange , minel , maxel , flag , minind ; for ( i = 0 ; i <= k ; i ++ ) ptr [ i ] = 0 ; minrange = INT_MAX ; while ( 1 ) { minind = - 1 ; minval = INT_MAX ; maxval = INT_MIN ; flag = 0 ; for ( i = 0...
def findSmallestRange ( arr , n , k ) : i , minval , maxval , minrange , minel , maxel , flag , minind = 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 for i in range ( k + 1 ) : ptr [ i ] = 0 minrange = 10 ** 9 while ( 1 ) : minind = - 1 minval = 10 ** 9 maxval = - 10 ** 9 flag =...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findSmallestRange ( int arr [ ] [ N ] , int n , int k ) { int i , minval , maxval , minrange , minel , maxel , flag , minind ; for ( i = 0 ; i <= k ; i ++ ) ptr [ i ] = 0 ; minrange = INT_MAX ; while ( 1 ) { minind = - 1 ; minval = INT_MAX ; maxval = INT_MIN ; flag = 0 ; for ...
cpp-python-LEXICOGRAPHICAL_CONCATENATION_SUBSTRINGS_STRING
cpp-python
string lexicographicSubConcat ( string s ) { int n = s . length ( ) ; int sub_count = n * ( n + 1 ) / 2 ; string arr [ sub_count ] ; int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) arr [ index ++ ] = s . substr ( i , len ) ; sort ( arr , arr + sub_count )...
def lexicographicSubConcat ( s ) : n = len ( s ) sub_count = ( n * ( n + 1 ) ) // 2 arr = [ 0 ] * sub_count index = 0 for i in range ( n ) : for j in range ( 1 , n - i + 1 ) : arr [ index ] = s [ i : i + j ] index += 1 arr.sort ( ) res = "" for i in range ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string lexicographicSubConcat ( string s ) { int n = s . length ( ) ; int sub_count = n * ( n + 1 ) / 2 ; string arr [ sub_count ] ; int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) arr [ index ++ ] = s . substr ( i , len ) ; sort ( arr , arr + sub_...
cpp-python-FIND_MAXIMUM_ELEMENT_ROW_MATRIX
cpp-python
void maxelement ( int no_of_rows , int arr [ ] [ N ] ) { int i = 0 ; int max = 0 ; int result [ no_of_rows ] ; while ( i < no_of_rows ) { for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] [ j ] > max ) { max = arr [ i ] [ j ] ; } } result [ i ] = max ; max = 0 ; i...
def maxelement ( arr ) : no_of_rows = len ( arr ) no_of_column = len ( arr [ 0 ] ) for i in range ( no_of_rows ) : max1 = 0 for j in range ( no_of_column ) : if arr [ i ] [ j ] > max1 : max1 = arr [ i ] [ j ] print ( max1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void maxelement ( int no_of_rows , int arr [ ] [ N ] ) { int i = 0 ; int max = 0 ; int result [ no_of_rows ] ; while ( i < no_of_rows ) { for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] [ j ] > max ) { max = arr [ i ] [ j ] ; } } result [ i ] = max ; max = 0 ...
cpp-python-COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX_1
cpp-python
int countNegative ( int M [ ] [ 4 ] , int n , int m ) { int count = 0 ; int i = 0 ; int j = m - 1 ; while ( j >= 0 && i < n ) { if ( M [ i ] [ j ] < 0 ) { count += j + 1 ; i += 1 ; } else j -= 1 ; } return count ; }
def countNegative ( M , n , m ) : count = 0 i = 0 j = m - 1 while j >= 0 and i < n : if M [ i ] [ j ] < 0 : count += ( j + 1 ) i += 1 else : j -= 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countNegative ( int M [ ] [ 4 ] , int n , int m ) { int count = 0 ; int i = 0 ; int j = m - 1 ; while ( j >= 0 && i < n ) { if ( M [ i ] [ j ] < 0 ) { count += j + 1 ; i += 1 ; } else j -= 1 ; } return count ; } </Code>
cpp-python-FIND_POSITION_GIVEN_NUMBER_AMONG_NUMBERS_MADE_4_7
cpp-python
int findpos ( string n ) { int i = 0 , pos = 0 ; while ( n [ i ] != '\0' ) { switch ( n [ i ] ) { case '4' : pos = pos * 2 + 1 ; break ; case '7' : pos = pos * 2 + 2 ; break ; } i ++ ; } return pos ; }
def findpos ( n ) : i = 0 j = len ( n ) pos = 0 while ( i < j ) : if ( n [ i ] == '4' ) : pos = pos * 2 + 1 if ( n [ i ] == '7' ) : pos = pos * 2 + 2 i = i + 1 return pos
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findpos ( string n ) { int i = 0 , pos = 0 ; while ( n [ i ] != '\0' ) { switch ( n [ i ] ) { case '4' : pos = pos * 2 + 1 ; break ; case '7' : pos = pos * 2 + 2 ; break ; } i ++ ; } return pos ; } </Code>
cpp-python-FIND_VALUE_OF_Y_MOD_2_RAISED_TO_POWER_X
cpp-python
long long int yMod ( long long int y , long long int x ) { if ( log2 ( y ) < x ) return y ; if ( x > 63 ) return y ; return ( y % ( 1 << x ) ) ; }
def yMod ( y , x ) : return ( y % pow ( 2 , x ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int yMod ( long long int y , long long int x ) { if ( log2 ( y ) < x ) return y ; if ( x > 63 ) return y ; return ( y % ( 1 << x ) ) ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_14_MAXIMUM_SUM_INCREASING_SUBSEQUENCE
cpp-python
int maxSumIS ( int arr [ ] , int n ) { int i , j , max = 0 ; int msis [ n ] ; for ( i = 0 ; i < n ; i ++ ) msis [ i ] = arr [ i ] ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ] ; fo...
def maxSumIS ( arr , n ) : max = 0 msis = [ 0 for x in range ( n ) ] for i in range ( n ) : msis [ i ] = arr [ i ] for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and msis [ i ] < msis [ j ] + arr [ i ] ) : msis [ i ] = msis [ j ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSumIS ( int arr [ ] , int n ) { int i , j , max = 0 ; int msis [ n ] ; for ( i = 0 ; i < n ; i ++ ) msis [ i ] = arr [ i ] ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ]...
cpp-python-LENGTH_LONGEST_BALANCED_SUBSEQUENCE_1
cpp-python
int maxLength ( char s [ ] , int n ) { int invalidOpenBraces = 0 ; int invalidCloseBraces = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '(' ) { invalidOpenBraces ++ ; } else { if ( invalidOpenBraces == 0 ) { invalidCloseBraces ++ ; } else { invali...
def maxLength ( s , n ) : invalidOpenBraces = 0 invalidCloseBraces = 0 for i in range ( n ) : if ( s [ i ] == '(' ) : invalidOpenBraces += 1 else : if ( invalidOpenBraces == 0 ) : invalidCloseBraces += 1 else : invalidOpenBr...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxLength ( char s [ ] , int n ) { int invalidOpenBraces = 0 ; int invalidCloseBraces = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '(' ) { invalidOpenBraces ++ ; } else { if ( invalidOpenBraces == 0 ) { invalidCloseBraces ++ ; } else { ...
cpp-python-MAXIMUM_PRODUCT_INCREASING_SUBSEQUENCE
cpp-python
ll lis ( ll arr [ ] , ll n ) { ll mpis [ n ] ; for ( int i = 0 ; i < n ; i ++ ) mpis [ i ] = arr [ i ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ; return * max_elemen...
def lis ( arr , n ) : mpis = [ 0 ] * ( n ) for i in range ( n ) : mpis [ i ] = arr [ i ] for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) : mpis [ i ] = mpis [ j ] * arr [ i ] return max...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> ll lis ( ll arr [ ] , ll n ) { ll mpis [ n ] ; for ( int i = 0 ; i < n ; i ++ ) mpis [ i ] = arr [ i ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ; return * max...
cpp-python-PETERSON_GRAPH
cpp-python
bool findthepath ( char * S , int v ) { result [ 0 ] = v + '0' ; for ( int i = 1 ; S [ i ] ; i ++ ) { if ( adj [ v ] [ S [ i ] - 'A' ] || adj [ S [ i ] - 'A' ] [ v ] ) { v = S [ i ] - 'A' ; } else if ( adj [ v ] [ S [ i ] - 'A' + 5 ] || adj [ S [ i ] - 'A' + 5 ] [ v ] ) { v = S [ i ] - '...
def findthepath ( S , v ) : result [ 0 ] = v for i in range ( 1 , len ( S ) ) : if ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) ] or adj [ ord ( S [ i ] ) - ord ( 'A' ) ] [ v ] ) : v = ord ( S [ i ] ) - ord ( 'A' ) elif ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) + 5 ] or adj [ ord ( ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool findthepath ( char * S , int v ) { result [ 0 ] = v + '0' ; for ( int i = 1 ; S [ i ] ; i ++ ) { if ( adj [ v ] [ S [ i ] - 'A' ] || adj [ S [ i ] - 'A' ] [ v ] ) { v = S [ i ] - 'A' ; } else if ( adj [ v ] [ S [ i ] - 'A' + 5 ] || adj [ S [ i ] - 'A' + 5 ] [ v ] ) { v = S [ ...
cpp-python-CHECK_QUEUE_CAN_SORTED_ANOTHER_QUEUE_USING_STACK
cpp-python
bool checkSorted ( int n , queue < int > & q ) { stack < int > st ; int expected = 1 ; int fnt ; while ( ! q . empty ( ) ) { fnt = q . front ( ) ; q . pop ( ) ; if ( fnt == expected ) expected ++ ; else { if ( st . empty ( ) ) { st . push ( fnt ) ; } else if ( ! st . em...
def checkSorted ( n , q ) : st = [ ] expected = 1 fnt = None while ( not q.empty ( ) ) : fnt = q.queue [ 0 ] q.get ( ) if ( fnt == expected ) : expected += 1 else : if ( len ( st ) == 0 ) : st.append ( fnt ) elif ( len (...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkSorted ( int n , queue < int > & q ) { stack < int > st ; int expected = 1 ; int fnt ; while ( ! q . empty ( ) ) { fnt = q . front ( ) ; q . pop ( ) ; if ( fnt == expected ) expected ++ ; else { if ( st . empty ( ) ) { st . push ( fnt ) ; } else if ( ! ...
cpp-python-GENERATING_DISTINCT_SUBSEQUENCES_OF_A_GIVEN_STRING_IN_LEXICOGRAPHIC_ORDER
cpp-python
void generate ( set < string > & st , string s ) { if ( s . size ( ) == 0 ) return ; if ( st . find ( s ) == st . end ( ) ) { st . insert ( s ) ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { string t = s ; t . erase ( i , 1 ) ; generate ( st , t ) ; } } return ; }
def generate ( st , s ) : if len ( s ) == 0 : return if s not in st : st.add ( s ) for i in range ( len ( s ) ) : t = list ( s ).copy ( ) t.remove ( s [ i ] ) t = ''.join ( t ) generate ( st , t ) return
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void generate ( set < string > & st , string s ) { if ( s . size ( ) == 0 ) return ; if ( st . find ( s ) == st . end ( ) ) { st . insert ( s ) ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { string t = s ; t . erase ( i , 1 ) ; generate ( st , t ) ; } } return ; } ...
cpp-python-RECURSIVE_FUNCTIONS
cpp-python
void tower ( int n , char sourcePole , char destinationPole , char auxiliaryPole ) { if ( 0 == n ) return ; tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) ; cout << "Move the disk " << n << " from " << sourcePole << " to " << destinationPole << endl ; tower ( n - 1 , auxiliaryPole , destinationP...
def tower ( n , sourcePole , destinationPole , auxiliaryPole ) : if ( 0 == n ) : return tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) print ( "Move the disk" , sourcePole , "from" , sourcePole , "to" , destinationPole ) tower ( n - 1 , auxiliaryPole , destinationPole , sourcePol...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void tower ( int n , char sourcePole , char destinationPole , char auxiliaryPole ) { if ( 0 == n ) return ; tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) ; cout << "Move the disk " << n << " from " << sourcePole << " to " << destinationPole << endl ; tower ( n - 1 , auxiliaryPole , desti...
cpp-python-PIZZA_CUT_PROBLEM_CIRCLE_DIVISION_LINES
cpp-python
int findMaximumPieces ( int n ) { return 1 + n * ( n + 1 ) / 2 ; }
def findMaximumPieces ( n ) : return int ( 1 + n * ( n + 1 ) / 2 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMaximumPieces ( int n ) { return 1 + n * ( n + 1 ) / 2 ; } </Code>
cpp-python-LEXICOGRAPHICALLY_MINIMUM_STRING_ROTATION
cpp-python
string minLexRotation ( string str ) { int n = str . length ( ) ; string arr [ n ] ; string concat = str + str ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = concat . substr ( i , n ) ; sort ( arr , arr + n ) ; return arr [ 0 ] ; }
def minLexRotation ( str_ ) : n = len ( str_ ) arr = [ 0 ] * n concat = str_ + str_ for i in range ( n ) : arr [ i ] = concat [ i : n + i ] arr.sort ( ) return arr [ 0 ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string minLexRotation ( string str ) { int n = str . length ( ) ; string arr [ n ] ; string concat = str + str ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = concat . substr ( i , n ) ; sort ( arr , arr + n ) ; return arr [ 0 ] ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_28_MINIMUM_INSERTIONS_TO_FORM_A_PALINDROME
cpp-python
int findMinInsertions ( char str [ ] , int l , int h ) { if ( l > h ) return INT_MAX ; if ( l == h ) return 0 ; if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ; return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( min ( findMinInsertions ( str , l , h - 1 ) , findMinIns...
def findMinInsertions ( str , l , h ) : if ( l > h ) : return sys.maxsize if ( l == h ) : return 0 if ( l == h - 1 ) : return 0 if ( str [ l ] == str [ h ] ) else 1 if ( str [ l ] == str [ h ] ) : return findMinInsertions ( str , l + 1 , h - 1 ) else : return ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMinInsertions ( char str [ ] , int l , int h ) { if ( l > h ) return INT_MAX ; if ( l == h ) return 0 ; if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ; return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( min ( findMinInsertions ( str , l , h - 1 ) , fin...
cpp-python-PROGRAM_OCTAL_DECIMAL_CONVERSION
cpp-python
int octalToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base = 1 ; int temp = num ; while ( temp ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base ; base = base * 8 ; } return dec_value ; }
def octalToDecimal ( n ) : num = n dec_value = 0 base = 1 temp = num while ( temp ) : last_digit = temp % 10 temp = int ( temp / 10 ) dec_value += last_digit * base base = base * 8 return dec_value
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int octalToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base = 1 ; int temp = num ; while ( temp ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base ; base = base * 8 ; } return dec_value ; } </Code>
cpp-python-FIND_THE_MISSING_NUMBER
cpp-python
int getMissingNo ( int a [ ] , int n ) { int total = ( n + 1 ) * ( n + 2 ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) total -= a [ i ] ; return total ; }
def getMissingNo ( A ) : n = len ( A ) total = ( n + 1 ) * ( n + 2 ) / 2 sum_of_A = sum ( A ) return total - sum_of_A
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getMissingNo ( int a [ ] , int n ) { int total = ( n + 1 ) * ( n + 2 ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) total -= a [ i ] ; return total ; } </Code>
cpp-python-PRINT_SQUARES_FIRST_N_NATURAL_NUMBERS_WITHOUT_USING
cpp-python
void printSquares ( int n ) { int square = 0 , prev_x = 0 ; for ( int x = 0 ; x < n ; x ++ ) { square = ( square + x + prev_x ) ; cout << square << " " ; prev_x = x ; } }
def printSquares ( n ) : square = 0 ; prev_x = 0 ; for x in range ( 0 , n ) : square = ( square + x + prev_x ) print ( square , end = " " ) prev_x = x
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 , prev_x = 0 ; for ( int x = 0 ; x < n ; x ++ ) { square = ( square + x + prev_x ) ; cout << square << " " ; prev_x = x ; } } </Code>
cpp-python-PERMUTE_TWO_ARRAYS_SUM_EVERY_PAIR_GREATER_EQUAL_K
cpp-python
bool isPossible ( int a [ ] , int b [ ] , int n , int k ) { sort ( a , a + n ) ; sort ( b , b + n , greater < int > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] + b [ i ] < k ) return false ; return true ; }
def isPossible ( a , b , n , k ) : a.sort ( reverse = True ) b.sort ( ) for i in range ( n ) : if ( a [ i ] + b [ i ] < k ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPossible ( int a [ ] , int b [ ] , int n , int k ) { sort ( a , a + n ) ; sort ( b , b + n , greater < int > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] + b [ i ] < k ) return false ; return true ; } </Code>