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-LAST_NON_ZERO_DIGIT_FACTORIAL
cpp-python
int lastNon0Digit ( int n ) { if ( n < 10 ) return dig [ n ] ; if ( ( ( n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ; else return ( 4 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ; }
def lastNon0Digit ( n ) : if ( n < 10 ) : return dig [ n ] if ( ( ( n // 10 ) % 10 ) % 2 == 0 ) : return ( 6 * lastNon0Digit ( n // 5 ) * dig [ n % 10 ] ) % 10 else : return ( 4 * lastNon0Digit ( n // 5 ) * dig [ n % 10 ] ) % 10 return 0
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lastNon0Digit ( int n ) { if ( n < 10 ) return dig [ n ] ; if ( ( ( n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ; else return ( 4 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ; } </Code>
cpp-python-EVEN_FIBONACCI_NUMBERS_SUM
cpp-python
int evenFibSum ( int limit ) { if ( limit < 2 ) return 0 ; long long int ef1 = 0 , ef2 = 2 ; long long int sum = ef1 + ef2 ; while ( ef2 <= limit ) { long long int ef3 = 4 * ef2 + ef1 ; if ( ef3 > limit ) break ; ef1 = ef2 ; ef2 = ef3 ; sum += ef2 ; } return sum ; }
def evenFibSum ( limit ) : if ( limit < 2 ) : return 0 ef1 = 0 ef2 = 2 sm = ef1 + ef2 while ( ef2 <= limit ) : ef3 = 4 * ef2 + ef1 if ( ef3 > limit ) : break ef1 = ef2 ef2 = ef3 sm = sm + ef2 return sm
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int evenFibSum ( int limit ) { if ( limit < 2 ) return 0 ; long long int ef1 = 0 , ef2 = 2 ; long long int sum = ef1 + ef2 ; while ( ef2 <= limit ) { long long int ef3 = 4 * ef2 + ef1 ; if ( ef3 > limit ) break ; ef1 = ef2 ; ef2 = ef3 ; sum += ef2 ; } return sum ; } </Code>
cpp-python-SEARCH_AN_ELEMENT_IN_AN_ARRAY_WHERE_DIFFERENCE_BETWEEN_ADJACENT_ELEMENTS_IS_1
cpp-python
int search ( int arr [ ] , int n , int x ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + abs ( arr [ i ] - x ) ; } cout << "number is not present!" ; return - 1 ; }
def search ( arr , n , x ) : i = 0 while ( i < n ) : if ( arr [ i ] == x ) : return i i = i + abs ( arr [ i ] - x ) print ( "number is not present!" ) return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int search ( int arr [ ] , int n , int x ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + abs ( arr [ i ] - x ) ; } cout << "number is not present!" ; return - 1 ; } </Code>
cpp-python-CHECK_STRING_CAN_OBTAINED_ROTATING_ANOTHER_STRING_2_PLACES
cpp-python
bool isRotated ( string str1 , string str2 ) { if ( str1 . length ( ) != str2 . length ( ) ) return false ; string clock_rot = "" ; string anticlock_rot = "" ; int len = str2 . length ( ) ; anticlock_rot = anticlock_rot + str2 . substr ( len - 2 , 2 ) + str2 . substr ( 0 , len - 2 ) ; clock_rot = clock_rot ...
def isRotated ( str1 , str2 ) : if ( len ( str1 ) != len ( str2 ) ) : return False clock_rot = "" anticlock_rot = "" l = len ( str2 ) anticlock_rot = ( anticlock_rot + str2 [ l - 2 : ] + str2 [ 0 : l - 2 ] ) clock_rot = clock_rot + str2 [ 2 : ] + str2 [ 0 : 2 ] return ( str1 == clock...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isRotated ( string str1 , string str2 ) { if ( str1 . length ( ) != str2 . length ( ) ) return false ; string clock_rot = "" ; string anticlock_rot = "" ; int len = str2 . length ( ) ; anticlock_rot = anticlock_rot + str2 . substr ( len - 2 , 2 ) + str2 . substr ( 0 , len - 2 ) ; clock_rot = clo...
cpp-python-HOSOYAS_TRIANGLE
cpp-python
void printHosoya ( int n ) { int dp [ N ] [ N ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ; e...
def printHosoya ( n ) : dp = [ [ 0 for i in range ( N ) ] for i in range ( N ) ] dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1 for i in range ( 2 , n ) : for j in range ( n ) : if ( i > j ) : dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printHosoya ( int n ) { int dp [ N ] [ N ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ; ...
cpp-python-LARGEST_SUBARRAY_WITH_EQUAL_NUMBER_OF_0S_AND_1S
cpp-python
int findSubArray ( int arr [ ] , int n ) { int sum = 0 ; int maxsize = - 1 , startindex ; for ( int i = 0 ; i < n - 1 ; i ++ ) { sum = ( arr [ i ] == 0 ) ? - 1 : 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { ( arr [ j ] == 0 ) ? ( sum += - 1 ) : ( sum += 1 ) ; if ( sum == 0 && maxsize...
def findSubArray ( arr , n ) : sum = 0 maxsize = - 1 for i in range ( 0 , n - 1 ) : sum = - 1 if ( arr [ i ] == 0 ) else 1 for j in range ( i + 1 , n ) : sum = sum + ( - 1 ) if ( arr [ j ] == 0 ) else sum + 1 if ( sum == 0 and maxsize < j - i + 1 ) : m...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSubArray ( int arr [ ] , int n ) { int sum = 0 ; int maxsize = - 1 , startindex ; for ( int i = 0 ; i < n - 1 ; i ++ ) { sum = ( arr [ i ] == 0 ) ? - 1 : 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { ( arr [ j ] == 0 ) ? ( sum += - 1 ) : ( sum += 1 ) ; if ( sum == 0 && ...
cpp-python-RECURSIVE_INSERTION_SORT
cpp-python
void insertionSortRecursive ( int arr [ ] , int n ) { if ( n <= 1 ) return ; insertionSortRecursive ( arr , n - 1 ) ; int last = arr [ n - 1 ] ; int j = n - 2 ; while ( j >= 0 && arr [ j ] > last ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = last ; }
def insertionSortRecursive ( arr , n ) : if n <= 1 : return insertionSortRecursive ( arr , n - 1 ) last = arr [ n - 1 ] j = n - 2 while ( j >= 0 and arr [ j ] > last ) : arr [ j + 1 ] = arr [ j ] j = j - 1 arr [ j + 1 ] = last
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void insertionSortRecursive ( int arr [ ] , int n ) { if ( n <= 1 ) return ; insertionSortRecursive ( arr , n - 1 ) ; int last = arr [ n - 1 ] ; int j = n - 2 ; while ( j >= 0 && arr [ j ] > last ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = last ; } </Code>
cpp-python-DIFFERENCE_BETWEEN_HIGHEST_AND_LEAST_FREQUENCIES_IN_AN_ARRAY_1
cpp-python
int findDiff ( int arr [ ] , int n ) { unordered_map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ arr [ i ] ] ++ ; int max_count = 0 , min_count = n ; for ( auto x : hm ) { max_count = max ( max_count , x . second ) ; min_count = min ( min_count , x . second ) ; } return ( max_count ...
def findDiff ( arr , n ) : mp = defaultdict ( lambda : 0 ) for i in range ( n ) : mp [ arr [ i ] ] += 1 max_count = 0 min_count = n for key , values in mp.items ( ) : max_count = max ( max_count , values ) min_count = min ( min_count , values ) return max_count - min_count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findDiff ( int arr [ ] , int n ) { unordered_map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ arr [ i ] ] ++ ; int max_count = 0 , min_count = n ; for ( auto x : hm ) { max_count = max ( max_count , x . second ) ; min_count = min ( min_count , x . second ) ; } return ( max...
cpp-python-GAME_REPLACING_ARRAY_ELEMENTS
cpp-python
int playGame ( int arr [ ] , int n ) { unordered_set < int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash . insert ( arr [ i ] ) ; return ( hash . size ( ) % 2 == 0 ? 1 : 2 ) ; }
def playGame ( arr , n ) : s = set ( ) for i in range ( n ) : s.add ( arr [ i ] ) return 1 if len ( s ) % 2 == 0 else 2
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int playGame ( int arr [ ] , int n ) { unordered_set < int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash . insert ( arr [ i ] ) ; return ( hash . size ( ) % 2 == 0 ? 1 : 2 ) ; } </Code>
cpp-python-MINIMUM_NUMBER_SUBSETS_DISTINCT_ELEMENTS_1
cpp-python
int subset ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; int res = 0 ; for ( auto x : mp ) res = max ( res , x . second ) ; return res ; }
def subset ( arr , n ) : mp = { i : 0 for i in range ( 10 ) } for i in range ( n ) : mp [ arr [ i ] ] += 1 res = 0 for key , value in mp.items ( ) : res = max ( res , value ) return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int subset ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; int res = 0 ; for ( auto x : mp ) res = max ( res , x . second ) ; return res ; } </Code>
cpp-python-PAIR_WITH_GIVEN_PRODUCT_SET_1_FIND_IF_ANY_PAIR_EXISTS_1
cpp-python
bool isProduct ( int arr [ ] , int n , int x ) { if ( n < 2 ) return false ; unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) return true ; else continue ; } if ( x % arr [ i ] == 0 ) { if ( s . find ( x / arr [ i ] ) != s . end (...
def isProduct ( arr , n , x ) : if n < 2 : return False s = set ( ) for i in range ( 0 , n ) : if arr [ i ] == 0 : if x == 0 : return True else : continue if x % arr [ i ] == 0 : if x // arr [ i ] in s : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isProduct ( int arr [ ] , int n , int x ) { if ( n < 2 ) return false ; unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) return true ; else continue ; } if ( x % arr [ i ] == 0 ) { if ( s . find ( x / arr [ i ] ) != s ...
cpp-python-MAXIMUM_AVERAGE_SUM_PARTITION_ARRAY
cpp-python
double largestSumOfAverages ( vector < int > & A , int K ) { int n = A . size ( ) ; double pre_sum [ n + 1 ] ; pre_sum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ; double dp [ n ] = { 0 } ; double sum = 0 ; for ( int i = 0 ; i < n ; i +...
def largestSumOfAverages ( A , K ) : n = len ( A ) pre_sum = [ 0 ] * ( n + 1 ) pre_sum [ 0 ] = 0 for i in range ( n ) : pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] dp = [ 0 ] * n sum = 0 for i in range ( n ) : dp [ i ] = ( pre_sum [ n ] - pre_sum [ i ] ) / ( n - i ) for k...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double largestSumOfAverages ( vector < int > & A , int K ) { int n = A . size ( ) ; double pre_sum [ n + 1 ] ; pre_sum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ; double dp [ n ] = { 0 } ; double sum = 0 ; for ( int i = 0 ; i < n ; ...
cpp-python-MINIMUM_STEPS_MINIMIZE_N_PER_GIVEN_CONDITION
cpp-python
int getMinSteps ( int n ) { int table [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) table [ i ] = n - i ; for ( int i = n ; i >= 1 ; i -- ) { if ( ! ( i % 2 ) ) table [ i / 2 ] = min ( table [ i ] + 1 , table [ i / 2 ] ) ; if ( ! ( i % 3 ) ) table [ i / 3 ] = min ( table [ i ] + 1 , table [ i / 3 ...
def getMinSteps ( n ) : table = [ 0 ] * ( n + 1 ) for i in range ( n + 1 ) : table [ i ] = n - i for i in range ( n , 0 , - 1 ) : if ( not ( i % 2 ) ) : table [ i // 2 ] = min ( table [ i ] + 1 , table [ i // 2 ] ) if ( not ( i % 3 ) ) : table [ i // 3 ] = min...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getMinSteps ( int n ) { int table [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) table [ i ] = n - i ; for ( int i = n ; i >= 1 ; i -- ) { if ( ! ( i % 2 ) ) table [ i / 2 ] = min ( table [ i ] + 1 , table [ i / 2 ] ) ; if ( ! ( i % 3 ) ) table [ i / 3 ] = min ( table [ i ] + 1 , table [...
cpp-python-N_TH_TERM_SERIES_2_12_36_80_150
cpp-python
int nthTerm ( int n ) { return ( n * n ) + ( n * n * n ) ; }
def nthTerm ( n ) : return ( n * n ) + ( n * n * n )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int nthTerm ( int n ) { return ( n * n ) + ( n * n * n ) ; } </Code>
cpp-python-FIND_MAXIMUM_AVERAGE_SUBARRAY_OF_K_LENGTH_1
cpp-python
int findMaxAverage ( int arr [ ] , int n , int k ) { if ( k > n ) return - 1 ; int sum = arr [ 0 ] ; for ( int i = 1 ; i < k ; i ++ ) sum += arr [ i ] ; int max_sum = sum , max_end = k - 1 ; for ( int i = k ; i < n ; i ++ ) { int sum = sum + arr [ i ] - arr [ i - k ] ; if ( sum > max_sum ) { ...
def findMaxAverage ( arr , n , k ) : if ( k > n ) : return - 1 sum = arr [ 0 ] for i in range ( 1 , k ) : sum += arr [ i ] max_sum = sum max_end = k - 1 for i in range ( k , n ) : sum = sum + arr [ i ] - arr [ i - k ] if ( sum > max_sum ) : max_sum = s...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMaxAverage ( int arr [ ] , int n , int k ) { if ( k > n ) return - 1 ; int sum = arr [ 0 ] ; for ( int i = 1 ; i < k ; i ++ ) sum += arr [ i ] ; int max_sum = sum , max_end = k - 1 ; for ( int i = k ; i < n ; i ++ ) { int sum = sum + arr [ i ] - arr [ i - k ] ; if ( sum > max_su...
cpp-python-DYNAMIC_PROGRAMMING_SET_11_EGG_DROPPING_PUZZLE
cpp-python
int eggDrop ( int n , int k ) { if ( k == 1 || k == 0 ) return k ; if ( n == 1 ) return k ; int min = INT_MAX , x , res ; for ( x = 1 ; x <= k ; x ++ ) { res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ; if ( res < min ) min = res ; } return min + 1 ; }
def eggDrop ( n , k ) : if ( k == 1 or k == 0 ) : return k if ( n == 1 ) : return k min = sys.maxsize for x in range ( 1 , k + 1 ) : res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) if ( res < min ) : min = res return min + 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int eggDrop ( int n , int k ) { if ( k == 1 || k == 0 ) return k ; if ( n == 1 ) return k ; int min = INT_MAX , x , res ; for ( x = 1 ; x <= k ; x ++ ) { res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ; if ( res < min ) min = res ; } return min + 1 ; } </Code>
cpp-python-PROGRAM_COUNT_OCCURRENCE_GIVEN_CHARACTER_STRING
cpp-python
int count ( string s , char c ) { int res = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s [ i ] == c ) res ++ ; return res ; }
def count ( s , c ) : res = 0 for i in range ( len ( s ) ) : if ( s [ i ] == c ) : res = res + 1 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int count ( string s , char c ) { int res = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s [ i ] == c ) res ++ ; return res ; } </Code>
cpp-python-MINIMUM_ROTATIONS_REQUIRED_GET_STRING
cpp-python
int findRotations ( string str ) { string tmp = str + str ; int n = str . length ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { string substring = tmp . substr ( i , str . size ( ) ) ; if ( str == substring ) return i ; } return n ; }
def findRotations ( str ) : tmp = str + str n = len ( str ) for i in range ( 1 , n + 1 ) : substring = tmp [ i : n ] if ( str == substring ) : return i return n
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findRotations ( string str ) { string tmp = str + str ; int n = str . length ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { string substring = tmp . substr ( i , str . size ( ) ) ; if ( str == substring ) return i ; } return n ; } </Code>
cpp-python-SHUFFLE_A_GIVEN_ARRAY
cpp-python
void randomize ( int arr [ ] , int n ) { srand ( time ( NULL ) ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { int j = rand ( ) % ( i + 1 ) ; swap ( & arr [ i ] , & arr [ j ] ) ; } }
def randomize ( arr , n ) : for i in range ( n - 1 , 0 , - 1 ) : j = random.randint ( 0 , i + 1 ) arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] return arr
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void randomize ( int arr [ ] , int n ) { srand ( time ( NULL ) ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { int j = rand ( ) % ( i + 1 ) ; swap ( & arr [ i ] , & arr [ j ] ) ; } } </Code>
cpp-python-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_2
cpp-python
int findLength ( string str , int n ) { int sum [ n + 1 ] ; sum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum [ i ] = ( sum [ i - 1 ] + str [ i - 1 ] - '0' ) ; int ans = 0 ; for ( int len = 2 ; len <= n ; len += 2 ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ...
def findLength ( string , n ) : Sum = [ 0 ] * ( n + 1 ) for i in range ( 1 , n + 1 ) : Sum [ i ] = ( Sum [ i - 1 ] + int ( string [ i - 1 ] ) ) ans = 0 for length in range ( 2 , n + 1 , 2 ) : for i in range ( 0 , n - length + 1 ) : j = i + length - 1 if ( Sum [ i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLength ( string str , int n ) { int sum [ n + 1 ] ; sum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum [ i ] = ( sum [ i - 1 ] + str [ i - 1 ] - '0' ) ; int ans = 0 ; for ( int len = 2 ; len <= n ; len += 2 ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + l...
cpp-python-REPLACE_OCCURRENCES_STRING_AB_C_WITHOUT_USING_EXTRA_SPACE_1
cpp-python
void translate ( char * str ) { int len = strlen ( str ) ; if ( len < 2 ) return ; int i = 0 ; int j = 0 ; while ( j < len - 1 ) { if ( str [ j ] == 'A' && str [ j + 1 ] == 'B' ) { j = j + 2 ; str [ i ++ ] = 'C' ; continue ; } str [ i ++ ] = str [ j ++ ] ; } if ( j == len - 1...
def translate ( st ) : l = len ( st ) if ( l < 2 ) : return i = 0 j = 0 while ( j < l - 1 ) : if ( st [ j ] == 'A' and st [ j + 1 ] == 'B' ) : j += 2 st [ i ] = 'C' i += 1 continue st [ i ] = st [ j ] i += 1 j +=...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void translate ( char * str ) { int len = strlen ( str ) ; if ( len < 2 ) return ; int i = 0 ; int j = 0 ; while ( j < len - 1 ) { if ( str [ j ] == 'A' && str [ j + 1 ] == 'B' ) { j = j + 2 ; str [ i ++ ] = 'C' ; continue ; } str [ i ++ ] = str [ j ++ ] ; } if ( j == ...
cpp-python-NUMBER_WHICH_HAS_THE_MAXIMUM_NUMBER_OF_DISTINCT_PRIME_FACTORS_IN_RANGE_M_TO_N
cpp-python
int maximumNumberDistinctPrimeRange ( int m , int n ) { long long factorCount [ n + 1 ] ; bool prime [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { factorCount [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] == true ) { factorCount [ i ] = 1 ;...
def maximumNumberDistinctPrimeRange ( m , n ) : factorCount = [ 0 ] * ( n + 1 ) prime = [ False ] * ( n + 1 ) for i in range ( n + 1 ) : factorCount [ i ] = 0 prime [ i ] = True for i in range ( 2 , n + 1 ) : if ( prime [ i ] == True ) : factorCount [ i ] = 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maximumNumberDistinctPrimeRange ( int m , int n ) { long long factorCount [ n + 1 ] ; bool prime [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { factorCount [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] == true ) { factorCount [ i ...
cpp-python-PROGRAM_NTH_CATALAN_NUMBER
cpp-python
unsigned long int catalan ( unsigned int n ) { if ( n <= 1 ) return 1 ; unsigned long int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res += catalan ( i ) * catalan ( n - i - 1 ) ; return res ; }
def catalan ( n ) : if n <= 1 : return 1 res = 0 for i in range ( n ) : res += catalan ( i ) * catalan ( n - i - 1 ) return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned long int catalan ( unsigned int n ) { if ( n <= 1 ) return 1 ; unsigned long int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res += catalan ( i ) * catalan ( n - i - 1 ) ; return res ; } </Code>
cpp-python-SUM_SERIES_12_32_52_2N_12
cpp-python
int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ; return sum ; }
def sumOfSeries ( n ) : sum = 0 for i in range ( 1 , n + 1 ) : sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) return sum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ; return sum ; } </Code>
cpp-python-MINIMUM_REVOLUTIONS_MOVE_CENTER_CIRCLE_TARGET
cpp-python
int minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) { double d = sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; return ceil ( d / ( 2 * r ) ) ; }
def minRevolutions ( r , x1 , y1 , x2 , y2 ) : d = math.sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) return math.ceil ( d // ( 2 * r ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) { double d = sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; return ceil ( d / ( 2 * r ) ) ; } </Code>
cpp-python-PRINT_STRING_SPECIFIED_CHARACTER_OCCURRED_GIVEN_NO_TIMES
cpp-python
void printString ( string str , char ch , int count ) { int occ = 0 , i ; if ( count == 0 ) { cout << str ; return ; } for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ch ) occ ++ ; if ( occ == count ) break ; } if ( i < str . length ( ) - 1 ) cout << str . substr ( i + 1 ...
def printString ( str , ch , count ) : occ , i = 0 , 0 if ( count == 0 ) : print ( str ) for i in range ( len ( str ) ) : if ( str [ i ] == ch ) : occ += 1 if ( occ == count ) : break if ( i < len ( str ) - 1 ) : print ( str [ i + 1 : len ( str ) -...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printString ( string str , char ch , int count ) { int occ = 0 , i ; if ( count == 0 ) { cout << str ; return ; } for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ch ) occ ++ ; if ( occ == count ) break ; } if ( i < str . length ( ) - 1 ) cout << str . substr (...
cpp-python-FINDING_THE_MAXIMUM_SQUARE_SUB_MATRIX_WITH_ALL_EQUAL_ELEMENTS
cpp-python
int largestKSubmatrix ( int a [ ] [ Col ] ) { int dp [ Row ] [ Col ] ; memset ( dp , sizeof ( dp ) , 0 ) ; int result = 0 ; for ( int i = 0 ; i < Row ; i ++ ) { for ( int j = 0 ; j < Col ; j ++ ) { if ( i == 0 || j == 0 ) dp [ i ] [ j ] = 1 ; else { if ( a [ i ] [ j ] == a [ ...
def largestKSubmatrix ( a ) : dp = [ [ 0 for x in range ( Row ) ] for y in range ( Col ) ] result = 0 for i in range ( Row ) : for j in range ( Col ) : if ( i == 0 or j == 0 ) : dp [ i ] [ j ] = 1 else : if ( a [ i ] [ j ] == a [ i - 1 ] [ j ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int largestKSubmatrix ( int a [ ] [ Col ] ) { int dp [ Row ] [ Col ] ; memset ( dp , sizeof ( dp ) , 0 ) ; int result = 0 ; for ( int i = 0 ; i < Row ; i ++ ) { for ( int j = 0 ; j < Col ; j ++ ) { if ( i == 0 || j == 0 ) dp [ i ] [ j ] = 1 ; else { if ( a [ i ] [ j ] ...
cpp-python-PASCAL_TRIANGLE
cpp-python
void printPascal ( int n ) { int arr [ n ] [ n ] ; for ( int line = 0 ; line < n ; line ++ ) { for ( int i = 0 ; i <= line ; i ++ ) { if ( line == i || i == 0 ) arr [ line ] [ i ] = 1 ; else arr [ line ] [ i ] = arr [ line - 1 ] [ i - 1 ] + arr [ line - 1 ] [ i ] ; cout << arr [ li...
def printPascal ( n : int ) : arr = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] for line in range ( 0 , n ) : for i in range ( 0 , line + 1 ) : if ( i is 0 or i is line ) : arr [ line ] [ i ] = 1 print ( arr [ line ] [ i ] , end = "" ) else...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printPascal ( int n ) { int arr [ n ] [ n ] ; for ( int line = 0 ; line < n ; line ++ ) { for ( int i = 0 ; i <= line ; i ++ ) { if ( line == i || i == 0 ) arr [ line ] [ i ] = 1 ; else arr [ line ] [ i ] = arr [ line - 1 ] [ i - 1 ] + arr [ line - 1 ] [ i ] ; cout << a...
cpp-python-NUMBER_INDEXES_EQUAL_ELEMENTS_GIVEN_RANGE
cpp-python
int answer_query ( int a [ ] , int n , int l , int r ) { int count = 0 ; for ( int i = l ; i < r ; i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ; return count ; }
def answer_query ( a , n , l , r ) : count = 0 for i in range ( l , r ) : if ( a [ i ] == a [ i + 1 ] ) : count += 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int answer_query ( int a [ ] , int n , int l , int r ) { int count = 0 ; for ( int i = l ; i < r ; i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ; return count ; } </Code>
cpp-python-CASSINIS_IDENTITY
cpp-python
int cassini ( int n ) { return ( n & 1 ) ? - 1 : 1 ; }
def cassini ( n ) : return - 1 if ( n & 1 ) else 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int cassini ( int n ) { return ( n & 1 ) ? - 1 : 1 ; } </Code>
cpp-python-COMPUTE_AVERAGE_TWO_NUMBERS_WITHOUT_OVERFLOW
cpp-python
int compute_average ( int a , int b ) { return ( a + b ) / 2 ; }
def compute_average ( a , b ) : return floor ( ( a + b ) / 2 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int compute_average ( int a , int b ) { return ( a + b ) / 2 ; } </Code>
cpp-python-NUMBER_OF_WAYS_TO_ARRANGE_N_ITEMS_UNDER_GIVEN_CONSTRAINTS
cpp-python
int waysToArrange ( int N , int K , int k [ ] ) { int C [ N + 1 ] [ N + 1 ] ; int i , j ; for ( i = 0 ; i <= N ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) ; } } int dp ...
def waysToArrange ( N , K , k ) : C = np.zeros ( ( N + 1 , N + 1 ) ) for i in range ( N + 1 ) : for j in range ( i + 1 ) : if ( j == 0 or j == i ) : C [ i ] [ j ] = 1 else : C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) dp = np....
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int waysToArrange ( int N , int K , int k [ ] ) { int C [ N + 1 ] [ N + 1 ] ; int i , j ; for ( i = 0 ; i <= N ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) ; } } ...
cpp-python-FIND_DIMENSIONS_RIGHT_ANGLED_TRIANGLE
cpp-python
void findDimen ( int H , int A ) { if ( H * H < 4 * A ) { cout << "Not Possible\n" ; return ; } double apb = sqrt ( H * H + 4 * A ) ; double asb = sqrt ( H * H - 4 * A ) ; cout . precision ( 2 ) ; cout << "P = " << fixed << ( apb - asb ) / 2.0 << "\n" ; cout << "B = " << ( apb + asb ) / 2.0 ; }
def findDimen ( H , A ) : if H * H < 4 * A : print ( "Not Possible" ) return apb = sqrt ( H * H + 4 * A ) asb = sqrt ( H * H - 4 * A ) print ( "P = " , "%.2f" % ( ( apb - asb ) / 2.0 ) ) print ( "B = " , "%.2f" % ( ( apb + asb ) / 2.0 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findDimen ( int H , int A ) { if ( H * H < 4 * A ) { cout << "Not Possible\n" ; return ; } double apb = sqrt ( H * H + 4 * A ) ; double asb = sqrt ( H * H - 4 * A ) ; cout . precision ( 2 ) ; cout << "P = " << fixed << ( apb - asb ) / 2.0 << "\n" ; cout << "B = " << ( apb + asb ) / 2.0...
cpp-python-MAXIMUM_TRIPLET_SUM_ARRAY_1
cpp-python
int maxTripletSum ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; return arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] ; }
def maxTripletSum ( arr , n ) : arr.sort ( ) return ( arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxTripletSum ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; return arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] ; } </Code>
cpp-python-FIND_MAXIMUM_DOT_PRODUCT_TWO_ARRAYS_INSERTION_0S
cpp-python
long long int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) { long long int dp [ n + 1 ] [ m + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= m ; j ++ ) dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ...
def MaxDotProduct ( A , B , m , n ) : dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ] for i in range ( 1 , n + 1 , 1 ) : for j in range ( i , m + 1 , 1 ) : dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ] [ j - 1 ] ) return dp ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) { long long int dp [ n + 1 ] [ m + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= m ; j ++ ) dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , ...
cpp-python-FIND_MINIMUM_DIFFERENCE_PAIR_1
cpp-python
int findMinDiff ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int diff = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; return diff ; }
def findMinDiff ( arr , n ) : arr = sorted ( arr ) diff = 10 ** 20 for i in range ( n - 1 ) : if arr [ i + 1 ] - arr [ i ] < diff : diff = arr [ i + 1 ] - arr [ i ] return diff
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMinDiff ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int diff = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; return diff ; } </Code>
cpp-python-FIND_LAST_INDEX_CHARACTER_STRING_1
cpp-python
int findLastIndex ( string & str , char x ) { for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) if ( str [ i ] == x ) return i ; return - 1 ; }
def findLastIndex ( str , x ) : for i in range ( len ( str ) - 1 , - 1 , - 1 ) : if ( str [ i ] == x ) : return i return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLastIndex ( string & str , char x ) { for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) if ( str [ i ] == x ) return i ; return - 1 ; } </Code>
cpp-python-MAXIMUM_DISTINCT_LOWERCASE_ALPHABETS_TWO_UPPERCASE
cpp-python
int maxLower ( string str ) { int n = str . length ( ) ; int i = 0 ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) { i ++ ; break ; } } int maxCount = 0 ; int count [ MAX_CHAR ] = { 0 } ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= 'A' ...
def maxLower ( str ) : n = len ( str ) i = 0 for i in range ( n ) : if str [ i ] >= 'A' and str [ i ] <= 'Z' : i += 1 break maxCount = 0 count = [ ] for j in range ( MAX_CHAR ) : count.append ( 0 ) for j in range ( i , n ) : if str [ j ] >= 'A'...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxLower ( string str ) { int n = str . length ( ) ; int i = 0 ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) { i ++ ; break ; } } int maxCount = 0 ; int count [ MAX_CHAR ] = { 0 } ; for ( ; i < n ; i ++ ) { if ( str [ i ] ...
cpp-python-FIND_ELEMENTS_ARRAY_LEAST_TWO_GREATER_ELEMENTS
cpp-python
void findElements ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; if ( count >= 2 ) cout << arr [ i ] << " " ; } }
def findElements ( arr , n ) : for i in range ( n ) : count = 0 for j in range ( 0 , n ) : if arr [ j ] > arr [ i ] : count = count + 1 if count >= 2 : print ( arr [ i ] , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findElements ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; if ( count >= 2 ) cout << arr [ i ] << " " ; } } </Code>
cpp-python-NTH_EVEN_LENGTH_PALINDROME
cpp-python
string evenlength ( string n ) { string res = n ; for ( int j = n . length ( ) - 1 ; j >= 0 ; -- j ) res += n [ j ] ; return res ; }
def evenlength ( n ) : res = n for j in range ( len ( n ) - 1 , - 1 , - 1 ) : res += n [ j ] return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string evenlength ( string n ) { string res = n ; for ( int j = n . length ( ) - 1 ; j >= 0 ; -- j ) res += n [ j ] ; return res ; } </Code>
cpp-python-LARGEST_SUBARRAY_WITH_EQUAL_NUMBER_OF_0S_AND_1S_1
cpp-python
int maxLen ( int arr [ ] , int n ) { unordered_map < int , int > hM ; int sum = 0 ; int max_len = 0 ; int ending_index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) { max_len = i...
def maxLen ( arr , n ) : hash_map = { } curr_sum = 0 max_len = 0 ending_index = - 1 for i in range ( 0 , n ) : if ( arr [ i ] == 0 ) : arr [ i ] = - 1 else : arr [ i ] = 1 for i in range ( 0 , n ) : curr_sum = curr_sum + arr [ i ] if ( curr...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxLen ( int arr [ ] , int n ) { unordered_map < int , int > hM ; int sum = 0 ; int max_len = 0 ; int ending_index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) { max_...
cpp-python-COUNT_NUMBER_OF_STRINGS_MADE_OF_R_G_AND_B_USING_GIVEN_COMBINATION
cpp-python
int possibleStrings ( int n , int r , int b , int g ) { int fact [ n + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; int left = n - ( r + g + b ) ; int sum = 0 ; for ( int i = 0 ; i <= left ; i ++ ) { for ( int j = 0 ; j <= left - i ; j ++ ) {...
def possibleStrings ( n , r , b , g ) : fact = [ 0 for i in range ( n + 1 ) ] fact [ 0 ] = 1 for i in range ( 1 , n + 1 , 1 ) : fact [ i ] = fact [ i - 1 ] * i left = n - ( r + g + b ) sum = 0 for i in range ( 0 , left + 1 , 1 ) : for j in range ( 0 , left - i + 1 , 1 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int possibleStrings ( int n , int r , int b , int g ) { int fact [ n + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; int left = n - ( r + g + b ) ; int sum = 0 ; for ( int i = 0 ; i <= left ; i ++ ) { for ( int j = 0 ; j <= left - i ; j...
cpp-python-NUMBER_WAYS_INSERT_CHARACTER_INCREASE_LCS_ONE
cpp-python
int numberofways ( string A , string B , int N , int M ) { vector < int > pos [ MAX ] ; for ( int i = 0 ; i < M ; i ++ ) pos [ B [ i ] ] . push_back ( i + 1 ) ; int dpl [ N + 2 ] [ M + 2 ] ; memset ( dpl , 0 , sizeof ( dpl ) ) ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; ...
def numberofways ( A , B , N , M ) : pos = [ [ ] for _ in range ( MAX ) ] for i in range ( M ) : pos [ ord ( B [ i ] ) ].append ( i + 1 ) dpl = [ [ 0 ] * ( M + 2 ) for _ in range ( N + 2 ) ] for i in range ( 1 , N + 1 ) : for j in range ( 1 , M + 1 ) : if A [ i - 1 ] == B [ j...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numberofways ( string A , string B , int N , int M ) { vector < int > pos [ MAX ] ; for ( int i = 0 ; i < M ; i ++ ) pos [ B [ i ] ] . push_back ( i + 1 ) ; int dpl [ N + 2 ] [ M + 2 ] ; memset ( dpl , 0 , sizeof ( dpl ) ) ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <=...
cpp-python-PROGRAM_FIND_CIRCUMFERENCE_CIRCLE
cpp-python
double circumference ( double r ) { double cir = 2 * PI * r ; return cir ; }
def circumference ( r ) : return ( 2 * PI * r )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double circumference ( double r ) { double cir = 2 * PI * r ; return cir ; } </Code>
cpp-python-CONVERT_SUBSTRINGS_LENGTH_K_BASE_B_DECIMAL
cpp-python
int substringConversions ( string str , int k , int b ) { for ( int i = 0 ; i + k <= str . size ( ) ; i ++ ) { string sub = str . substr ( i , k ) ; int sum = 0 , counter = 0 ; for ( int i = sub . size ( ) - 1 ; i >= 0 ; i -- ) { sum = sum + ( ( sub . at ( i ) - '0' ) * pow ( b , counter...
def substringConversions ( s , k , b ) : l = len ( s ) for i in range ( l ) : if ( ( i + k ) < l + 1 ) : sub = s [ i : i + k ] sum , counter = 0 , 0 for i in range ( len ( sub ) - 1 , - 1 , - 1 ) : sum = sum + ( ( ord ( sub [ i ] ) - ord ( '0' ) ) * po...
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 ) { for ( int i = 0 ; i + k <= str . size ( ) ; i ++ ) { string sub = str . substr ( i , k ) ; int sum = 0 , counter = 0 ; for ( int i = sub . size ( ) - 1 ; i >= 0 ; i -- ) { sum = sum + ( ( sub . at ( i ) - '0' ) * pow ( b , ...
cpp-python-MAXIMUM_PATH_SUM_TRIANGLE
cpp-python
int maxPathSum ( int tri [ ] [ N ] , int m , int n ) { for ( int i = m - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ; } } ret...
def maxPathSum ( tri , m , n ) : for i in range ( m - 1 , - 1 , - 1 ) : for j in range ( i + 1 ) : if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) : tri [ i ] [ j ] += tri [ i + 1 ] [ j ] else : tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] retur...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxPathSum ( int tri [ ] [ N ] , int m , int n ) { for ( int i = m - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ; } ...
cpp-python-SUM_MINIMUM_MAXIMUM_ELEMENTS_SUBARRAYS_SIZE_K
cpp-python
int SumOfKsubArray ( int arr [ ] , int n , int k ) { int sum = 0 ; deque < int > S ( k ) , G ( k ) ; int i = 0 ; for ( i = 0 ; i < k ; i ++ ) { while ( ( ! S . empty ( ) ) && arr [ S . back ( ) ] >= arr [ i ] ) S . pop_back ( ) ; while ( ( ! G . empty ( ) ) && arr [ G . back ( ) ] <= arr [ i ] ) G ....
def SumOfKsubArray ( arr , n , k ) : Sum = 0 S = deque ( ) G = deque ( ) for i in range ( k ) : while ( len ( S ) > 0 and arr [ S [ - 1 ] ] >= arr [ i ] ) : S.pop ( ) while ( len ( G ) > 0 and arr [ G [ - 1 ] ] <= arr [ i ] ) : G.pop ( ) G.append ( i ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int SumOfKsubArray ( int arr [ ] , int n , int k ) { int sum = 0 ; deque < int > S ( k ) , G ( k ) ; int i = 0 ; for ( i = 0 ; i < k ; i ++ ) { while ( ( ! S . empty ( ) ) && arr [ S . back ( ) ] >= arr [ i ] ) S . pop_back ( ) ; while ( ( ! G . empty ( ) ) && arr [ G . back ( ) ] <= arr [ i ...
cpp-python-TURN_OFF_THE_RIGHTMOST_SET_BIT
cpp-python
int fun ( unsigned int n ) { return n & ( n - 1 ) ; }
def fun ( n ) : return n & ( n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int fun ( unsigned int n ) { return n & ( n - 1 ) ; } </Code>
cpp-python-FIND_MIRROR_IMAGE_POINT_2_D_PLANE
cpp-python
pair < double , double > mirrorImage ( double a , double b , double c , double x1 , double y1 ) { double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ; double x = temp * a + x1 ; double y = temp * b + y1 ; return make_pair ( x , y ) ; }
def mirrorImage ( a , b , c , x1 , y1 ) : temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) x = temp * a + x1 y = temp * b + y1 return ( x , y )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> pair < double , double > mirrorImage ( double a , double b , double c , double x1 , double y1 ) { double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ; double x = temp * a + x1 ; double y = temp * b + y1 ; return make_pair ( x , y ) ; } </Code>
cpp-python-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY_1
cpp-python
void printDistinct ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ; cout << arr [ i ] << " " ; } }
def printDistinct ( arr , n ) : arr.sort ( ) for i in range ( n ) : if ( i < n - 1 and arr [ i ] == arr [ i + 1 ] ) : while ( i < n - 1 and ( arr [ i ] == arr [ i + 1 ] ) ) : i += 1 else : print ( arr [ i ] , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printDistinct ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ; cout << arr [ i ] << " " ; } } </Code>
cpp-python-UNION_AND_INTERSECTION_OF_TWO_SORTED_ARRAYS_2_1
cpp-python
int printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) i ++ ; else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ; else { cout << arr2 [ j ] << " " ; i ++ ; j ++ ; } } }
def printIntersection ( arr1 , arr2 , m , n ) : i , j = 0 , 0 while i < m and j < n : if arr1 [ i ] < arr2 [ j ] : i += 1 elif arr2 [ j ] < arr1 [ i ] : j += 1 else : print ( arr2 [ j ] ) j += 1 i += 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) i ++ ; else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ; else { cout << arr2 [ j ] << " " ; i ++ ; j ++ ; } } } </Code>
cpp-python-SEARCHING_ARRAY_ADJACENT_DIFFER_K
cpp-python
int search ( int arr [ ] , int n , int x , int k ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + max ( 1 , abs ( arr [ i ] - x ) / k ) ; } cout << "number is not present!" ; return - 1 ; }
def search ( arr , n , x , k ) : i = 0 while ( i < n ) : if ( arr [ i ] == x ) : return i i = i + max ( 1 , int ( abs ( arr [ i ] - x ) / k ) ) print ( "number is not present!" ) return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int search ( int arr [ ] , int n , int x , int k ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + max ( 1 , abs ( arr [ i ] - x ) / k ) ; } cout << "number is not present!" ; return - 1 ; } </Code>
cpp-python-REARRANGE_A_STRING_IN_SORTED_ORDER_FOLLOWED_BY_THE_INTEGER_SUM
cpp-python
string arrangeString ( string str ) { int char_count [ MAX_CHAR ] = { 0 } ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) char_count [ str [ i ] - 'A' ] ++ ; else sum = sum + ( str [ i ] - '0' ) ; } string res = ...
def arrangeString ( string ) : char_count = [ 0 ] * MAX_CHAR s = 0 for i in range ( len ( string ) ) : if string [ i ] >= "A" and string [ i ] <= "Z" : char_count [ ord ( string [ i ] ) - ord ( "A" ) ] += 1 else : s += ord ( string [ i ] ) - ord ( "0" ) res = "" ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string arrangeString ( string str ) { int char_count [ MAX_CHAR ] = { 0 } ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) char_count [ str [ i ] - 'A' ] ++ ; else sum = sum + ( str [ i ] - '0' ) ; } string...
cpp-python-COUNT_NUMBER_BINARY_STRINGS_WITHOUT_CONSECUTIVE_1S
cpp-python
int countStrings ( int n ) { int a [ n ] , b [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return a [ n - 1 ] + b [ n - 1 ] ; }
def countStrings ( n ) : a = [ 0 for i in range ( n ) ] b = [ 0 for i in range ( n ) ] a [ 0 ] = b [ 0 ] = 1 for i in range ( 1 , n ) : a [ i ] = a [ i - 1 ] + b [ i - 1 ] b [ i ] = a [ i - 1 ] return a [ n - 1 ] + b [ n - 1 ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countStrings ( int n ) { int a [ n ] , b [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return a [ n - 1 ] + b [ n - 1 ] ; } </Code>
cpp-python-MINIMUM_SUM_SUBSEQUENCE_LEAST_ONE_EVERY_FOUR_CONSECUTIVE_ELEMENTS_PICKED
cpp-python
int minSum ( int arr [ ] , int n ) { int dp [ n ] ; if ( n == 1 ) return arr [ 0 ] ; if ( n == 2 ) return min ( arr [ 0 ] , arr [ 1 ] ) ; if ( n == 3 ) return min ( arr [ 0 ] , min ( arr [ 1 ] , arr [ 2 ] ) ) ; if ( n == 4 ) return min ( min ( arr [ 0 ] , arr [ 1 ] ) , min ( arr [ 2 ] , arr [ 3 ] ) ) ; dp [...
def minSum ( arr , n ) : dp = [ 0 ] * n if ( n == 1 ) : return arr [ 0 ] if ( n == 2 ) : return min ( arr [ 0 ] , arr [ 1 ] ) if ( n == 3 ) : return min ( arr [ 0 ] , min ( arr [ 1 ] , arr [ 2 ] ) ) if ( n == 4 ) : return min ( min ( arr [ 0 ] , arr [ 1 ] ) , min ( ar...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minSum ( int arr [ ] , int n ) { int dp [ n ] ; if ( n == 1 ) return arr [ 0 ] ; if ( n == 2 ) return min ( arr [ 0 ] , arr [ 1 ] ) ; if ( n == 3 ) return min ( arr [ 0 ] , min ( arr [ 1 ] , arr [ 2 ] ) ) ; if ( n == 4 ) return min ( min ( arr [ 0 ] , arr [ 1 ] ) , min ( arr [ 2 ] , arr [ 3 ] ) ) ;...
cpp-python-CONVERT_SENTENCE_EQUIVALENT_MOBILE_NUMERIC_KEYPAD_SEQUENCE
cpp-python
string printSequence ( string arr [ ] , string input ) { string output = "" ; int n = input . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( input [ i ] == ' ' ) output = output + "0" ; else { int position = input [ i ] - 'A' ; output = output + arr [ position ] ; } } return ...
def printSequence ( arr , input ) : n = len ( input ) output = "" for i in range ( n ) : if ( input [ i ] == ' ' ) : output = output + "0" else : position = ord ( input [ i ] ) - ord ( 'A' ) output = output + arr [ position ] return output
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string printSequence ( string arr [ ] , string input ) { string output = "" ; int n = input . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( input [ i ] == ' ' ) output = output + "0" ; else { int position = input [ i ] - 'A' ; output = output + arr [ position ] ; } } ...
cpp-python-ROTATE_MATRIX_ELEMENTS
cpp-python
void rotatematrix ( int m , int n , int mat [ R ] [ C ] ) { int row = 0 , col = 0 ; int prev , curr ; while ( row < m && col < n ) { if ( row + 1 == m || col + 1 == n ) break ; prev = mat [ row + 1 ] [ col ] ; for ( int i = col ; i < n ; i ++ ) { curr = mat [ row ] [ i ] ; mat [ ro...
def rotateMatrix ( mat ) : if not len ( mat ) : return top = 0 bottom = len ( mat ) - 1 left = 0 right = len ( mat [ 0 ] ) - 1 while left < right and top < bottom : prev = mat [ top + 1 ] [ left ] for i in range ( left , right + 1 ) : curr = mat [ top ] [ i ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void rotatematrix ( int m , int n , int mat [ R ] [ C ] ) { int row = 0 , col = 0 ; int prev , curr ; while ( row < m && col < n ) { if ( row + 1 == m || col + 1 == n ) break ; prev = mat [ row + 1 ] [ col ] ; for ( int i = col ; i < n ; i ++ ) { curr = mat [ row ] [ i ] ; m...
cpp-python-MAXIMUM_BINOMIAL_COEFFICIENT_TERM_VALUE
cpp-python
int maxcoefficientvalue ( int n ) { int C [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } int maxvalue = 0 ; ...
def maxcoefficientvalue ( n ) : C = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] for i in range ( n + 1 ) : for j in range ( min ( i , n ) + 1 ) : if ( j == 0 or j == i ) : C [ i ] [ j ] = 1 else : C [ i ] [ j ] = ( C [ i - 1 ] [ j -...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxcoefficientvalue ( int n ) { int C [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } int maxvalue...
cpp-python-PROGRAM_FIND_GCD_FLOATING_POINT_NUMBERS
cpp-python
double gcd ( double a , double b ) { if ( a < b ) return gcd ( b , a ) ; if ( fabs ( b ) < 0.001 ) return a ; else return ( gcd ( b , a - floor ( a / b ) * b ) ) ; }
def gcd ( a , b ) : if ( a < b ) : return gcd ( b , a ) if ( abs ( b ) < 0.001 ) : return a else : return ( gcd ( b , a - math.floor ( a / b ) * b ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double gcd ( double a , double b ) { if ( a < b ) return gcd ( b , a ) ; if ( fabs ( b ) < 0.001 ) return a ; else return ( gcd ( b , a - floor ( a / b ) * b ) ) ; } </Code>
cpp-python-SORT_STRING_ACCORDING_ORDER_DEFINED_ANOTHER_STRING
cpp-python
void sortByPattern ( string & str , string pat ) { int count [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ str [ i ] - 'a' ] ++ ; int index = 0 ; for ( int i = 0 ; i < pat . length ( ) ; i ++ ) for ( int j = 0 ; j < count [ pat [ i ] - 'a' ] ; ...
def sortByPattern ( str , pat ) : global MAX_CHAR count = [ 0 ] * MAX_CHAR for i in range ( 0 , len ( str ) ) : count [ ord ( str [ i ] ) - 97 ] += 1 index = 0 str = "" for i in range ( 0 , len ( pat ) ) : j = 0 while ( j < count [ ord ( pat [ i ] ) - ord ( 'a' ) ] ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void sortByPattern ( string & str , string pat ) { int count [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ str [ i ] - 'a' ] ++ ; int index = 0 ; for ( int i = 0 ; i < pat . length ( ) ; i ++ ) for ( int j = 0 ; j < count [ pat [ i ] - 'a...
cpp-python-REARRANGE_ARRAY_MAXIMUM_MINIMUM_FORM_SET_2_O1_EXTRA_SPACE
cpp-python
void rearrange ( int arr [ ] , int n ) { int max_idx = n - 1 , min_idx = 0 ; int max_elem = arr [ n - 1 ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] += ( arr [ max_idx ] % max_elem ) * max_elem ; max_idx -- ; } else { arr [ i ] += ( arr [ min_idx ] % m...
def rearrange ( arr , n ) : max_idx = n - 1 min_idx = 0 max_elem = arr [ n - 1 ] + 1 for i in range ( 0 , n ) : if i % 2 == 0 : arr [ i ] += ( arr [ max_idx ] % max_elem ) * max_elem max_idx -= 1 else : arr [ i ] += ( arr [ min_idx ] % max_elem ) * max...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void rearrange ( int arr [ ] , int n ) { int max_idx = n - 1 , min_idx = 0 ; int max_elem = arr [ n - 1 ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] += ( arr [ max_idx ] % max_elem ) * max_elem ; max_idx -- ; } else { arr [ i ] += ( arr [ min_id...
cpp-python-MINIMUM_NUMBER_OF_JUMPS_TO_REACH_END_OF_A_GIVEN_ARRAY_1
cpp-python
int minJumps ( int arr [ ] , int n ) { int * jumps = new int [ n ] ; int i , j ; if ( n == 0 || arr [ 0 ] == 0 ) return INT_MAX ; jumps [ 0 ] = 0 ; for ( i = 1 ; i < n ; i ++ ) { jumps [ i ] = INT_MAX ; for ( j = 0 ; j < i ; j ++ ) { if ( i <= j + arr [ j ] && jumps [ j ] != INT_MAX ...
def minJumps ( arr , n ) : jumps = [ 0 for i in range ( n ) ] if ( n == 0 ) or ( arr [ 0 ] == 0 ) : return float ( 'inf' ) jumps [ 0 ] = 0 for i in range ( 1 , n ) : jumps [ i ] = float ( 'inf' ) for j in range ( i ) : if ( i <= j + arr [ j ] ) and ( jumps [ j ] != fl...
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 i , j ; if ( n == 0 || arr [ 0 ] == 0 ) return INT_MAX ; jumps [ 0 ] = 0 ; for ( i = 1 ; i < n ; i ++ ) { jumps [ i ] = INT_MAX ; for ( j = 0 ; j < i ; j ++ ) { if ( i <= j + arr [ j ] && jumps [ j ] != I...
cpp-python-PROBABILITY_REACHING_POINT_2_3_STEPS_TIME
cpp-python
float find_prob ( int N , float P ) { double dp [ N + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 0 ; dp [ 2 ] = P ; dp [ 3 ] = 1 - P ; for ( int i = 4 ; i <= N ; ++ i ) dp [ i ] = ( P ) * dp [ i - 2 ] + ( 1 - P ) * dp [ i - 3 ] ; return dp [ N ] ; }
def find_prob ( N , P ) : dp = [ 0 ] * ( n + 1 ) dp [ 0 ] = 1 dp [ 1 ] = 0 dp [ 2 ] = P dp [ 3 ] = 1 - P for i in range ( 4 , N + 1 ) : dp [ i ] = ( P ) * dp [ i - 2 ] + ( 1 - P ) * dp [ i - 3 ] return dp [ N ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> float find_prob ( int N , float P ) { double dp [ N + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 0 ; dp [ 2 ] = P ; dp [ 3 ] = 1 - P ; for ( int i = 4 ; i <= N ; ++ i ) dp [ i ] = ( P ) * dp [ i - 2 ] + ( 1 - P ) * dp [ i - 3 ] ; return dp [ N ] ; } </Code>
cpp-python-COUNT_WAYS_INCREASE_LCS_LENGTH_TWO_STRINGS_ONE
cpp-python
int waysToIncreaseLCSBy1 ( string str1 , string str2 ) { int m = str1 . length ( ) , n = str2 . length ( ) ; vector < int > position [ M ] ; for ( int i = 1 ; i <= n ; i ++ ) position [ toInt ( str2 [ i - 1 ] ) ] . push_back ( i ) ; int lcsl [ m + 2 ] [ n + 2 ] ; int lcsr [ m + 2 ] [ n + 2 ] ; for ( int...
def waysToIncreaseLCSBy1 ( str1 , str2 ) : m = len ( str1 ) n = len ( str2 ) position = [ [ ] for i in range ( M ) ] for i in range ( 1 , n + 1 , 1 ) : position [ ord ( str2 [ i - 1 ] ) - 97 ].append ( i ) lcsl = [ [ 0 for i in range ( n + 2 ) ] for j in range ( m + 2 ) ] lcsr = [ [ 0 fo...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int waysToIncreaseLCSBy1 ( string str1 , string str2 ) { int m = str1 . length ( ) , n = str2 . length ( ) ; vector < int > position [ M ] ; for ( int i = 1 ; i <= n ; i ++ ) position [ toInt ( str2 [ i - 1 ] ) ] . push_back ( i ) ; int lcsl [ m + 2 ] [ n + 2 ] ; int lcsr [ m + 2 ] [ n + 2 ] ; fo...
cpp-python-LONGEST_INCREASING_SUBSEQUENCE_1
cpp-python
int lis ( int arr [ ] , int n ) { int lis [ n ] ; lis [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { lis [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } return * max_element ( lis , lis + n ) ; }
def lis ( arr ) : n = len ( arr ) lis = [ 1 ] * n for i in range ( 1 , n ) : for j in range ( 0 , i ) : if arr [ i ] > arr [ j ] and lis [ i ] < lis [ j ] + 1 : lis [ i ] = lis [ j ] + 1 maximum = 0 for i in range ( n ) : maximum = max ( maximum , lis [ i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lis ( int arr [ ] , int n ) { int lis [ n ] ; lis [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { lis [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } return * max_element ( lis , lis + n ) ; } <...
cpp-python-STERN_BROCOT_SEQUENCE
cpp-python
void SternSequenceFunc ( vector < int > & BrocotSequence , int n ) { for ( int i = 1 ; BrocotSequence . size ( ) < n ; i ++ ) { int considered_element = BrocotSequence [ i ] ; int precedent = BrocotSequence [ i - 1 ] ; BrocotSequence . push_back ( considered_element + precedent ) ; BrocotSequence ...
def SternSequenceFunc ( BrocotSequence , n ) : for i in range ( 1 , n ) : considered_element = BrocotSequence [ i ] precedent = BrocotSequence [ i - 1 ] BrocotSequence.append ( considered_element + precedent ) BrocotSequence.append ( considered_element ) for i in range ( 0 , 15 )...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void SternSequenceFunc ( vector < int > & BrocotSequence , int n ) { for ( int i = 1 ; BrocotSequence . size ( ) < n ; i ++ ) { int considered_element = BrocotSequence [ i ] ; int precedent = BrocotSequence [ i - 1 ] ; BrocotSequence . push_back ( considered_element + precedent ) ; BrocotSe...
cpp-python-PRUFER_CODE_TREE_CREATION
cpp-python
void printTreeEdges ( int prufer [ ] , int m ) { int vertices = m + 2 ; int vertex_set [ vertices ] ; for ( int i = 0 ; i < vertices ; i ++ ) vertex_set [ i ] = 0 ; for ( int i = 0 ; i < vertices - 2 ; i ++ ) vertex_set [ prufer [ i ] - 1 ] += 1 ; cout << "\nThe edge set E(G) is :\n" ; int j = 0 ; ...
def printTreeEdges ( prufer , m ) : vertices = m + 2 vertex_set = [ 0 ] * vertices for i in range ( vertices - 2 ) : vertex_set [ prufer [ i ] - 1 ] += 1 print ( "The edge set E(G) is :" ) j = 0 for i in range ( vertices - 2 ) : for j in range ( vertices ) : if ( vert...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printTreeEdges ( int prufer [ ] , int m ) { int vertices = m + 2 ; int vertex_set [ vertices ] ; for ( int i = 0 ; i < vertices ; i ++ ) vertex_set [ i ] = 0 ; for ( int i = 0 ; i < vertices - 2 ; i ++ ) vertex_set [ prufer [ i ] - 1 ] += 1 ; cout << "\nThe edge set E(G) is :\n" ; int j ...
cpp-python-COUNT_POSSIBLE_DECODINGS_GIVEN_DIGIT_SEQUENCE_1
cpp-python
int countDecodingDP ( char * digits , int n ) { int count [ n + 1 ] ; count [ 0 ] = 1 ; count [ 1 ] = 1 ; if ( digits [ 0 ] == '0' ) return 0 ; for ( int i = 2 ; i <= n ; i ++ ) { count [ i ] = 0 ; if ( digits [ i - 1 ] > '0' ) count [ i ] = count [ i - 1 ] ; if ( digits [ i - 2 ] == '1' || ( ...
def countDecodingDP ( digits , n ) : count = [ 0 ] * ( n + 1 ) count [ 0 ] = 1 count [ 1 ] = 1 for i in range ( 2 , n + 1 ) : count [ i ] = 0 if ( digits [ i - 1 ] > '0' ) : count [ i ] = count [ i - 1 ] if ( digits [ i - 2 ] == '1' or ( digits [ i - 2 ] == '2' and di...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countDecodingDP ( char * digits , int n ) { int count [ n + 1 ] ; count [ 0 ] = 1 ; count [ 1 ] = 1 ; if ( digits [ 0 ] == '0' ) return 0 ; for ( int i = 2 ; i <= n ; i ++ ) { count [ i ] = 0 ; if ( digits [ i - 1 ] > '0' ) count [ i ] = count [ i - 1 ] ; if ( digits [ i - 2 ] == '1...
cpp-python-COUNT_STRINGS_WITH_CONSECUTIVE_1S
cpp-python
int countStrings ( int n ) { int a [ n ] , b [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ; }
def countStrings ( n ) : a = [ 0 ] * n b = [ 0 ] * n a [ 0 ] = b [ 0 ] = 1 for i in range ( 1 , n ) : a [ i ] = a [ i - 1 ] + b [ i - 1 ] b [ i ] = a [ i - 1 ] return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countStrings ( int n ) { int a [ n ] , b [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ; } </Code>
cpp-python-MAXIMUM_NUMBER_SEGMENTS_LENGTHS_B_C
cpp-python
int maximumSegments ( int n , int a , int b , int c ) { int dp [ n + 1 ] ; memset ( dp , - 1 , sizeof ( dp ) ) ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ i ] != - 1 ) { if ( i + a <= n ) dp [ i + a ] = max ( dp [ i ] + 1 , dp [ i + a ] ) ; if ( i + b <= n ) dp [ i + b ] = ...
def maximumSegments ( n , a , b , c ) : dp = [ - 1 ] * ( n + 10 ) dp [ 0 ] = 0 for i in range ( 0 , n ) : if ( dp [ i ] != - 1 ) : if ( i + a <= n ) : dp [ i + a ] = max ( dp [ i ] + 1 , dp [ i + a ] ) if ( i + b <= n ) : dp [ i + b ] = max ( d...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maximumSegments ( int n , int a , int b , int c ) { int dp [ n + 1 ] ; memset ( dp , - 1 , sizeof ( dp ) ) ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ i ] != - 1 ) { if ( i + a <= n ) dp [ i + a ] = max ( dp [ i ] + 1 , dp [ i + a ] ) ; if ( i + b <= n ) dp [ i +...
cpp-python-PRINT_BRACKET_NUMBER
cpp-python
void printBracketNumber ( string exp , int n ) { int left_bnum = 1 ; stack < int > right_bnum ; for ( int i = 0 ; i < n ; i ++ ) { if ( exp [ i ] == '(' ) { cout << left_bnum << " " ; right_bnum . push ( left_bnum ) ; left_bnum ++ ; } else if ( exp [ i ] == ')' ) { cout << ...
def printBracketNumber ( exp , n ) : left_bnum = 1 right_bnum = list ( ) for i in range ( n ) : if exp [ i ] == '(' : print ( left_bnum , end = " " ) right_bnum.append ( left_bnum ) left_bnum += 1 elif exp [ i ] == ')' : print ( right_bnum [ - ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printBracketNumber ( string exp , int n ) { int left_bnum = 1 ; stack < int > right_bnum ; for ( int i = 0 ; i < n ; i ++ ) { if ( exp [ i ] == '(' ) { cout << left_bnum << " " ; right_bnum . push ( left_bnum ) ; left_bnum ++ ; } else if ( exp [ i ] == ')' ) { c...
cpp-python-COUNT_WORDS_WHOSE_TH_LETTER_EITHER_1_TH_TH_I1_TH_LETTER_GIVEN_WORD
cpp-python
int countWords ( char str [ ] , int len ) { int count = 1 ; if ( len == 1 ) return count ; if ( str [ 0 ] == str [ 1 ] ) count *= 1 ; else count *= 2 ; for ( int j = 1 ; j < len - 1 ; j ++ ) { if ( str [ j ] == str [ j - 1 ] && str [ j ] == str [ j + 1 ] ) count *= 1 ; else if ( str [ j ] == str [...
def countWords ( str , l ) : count = 1 ; if ( l == 1 ) : return count if ( str [ 0 ] == str [ 1 ] ) : count *= 1 else : count *= 2 for j in range ( 1 , l - 1 ) : if ( str [ j ] == str [ j - 1 ] and str [ j ] == str [ j + 1 ] ) : count *= 1 elif ( s...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countWords ( char str [ ] , int len ) { int count = 1 ; if ( len == 1 ) return count ; if ( str [ 0 ] == str [ 1 ] ) count *= 1 ; else count *= 2 ; for ( int j = 1 ; j < len - 1 ; j ++ ) { if ( str [ j ] == str [ j - 1 ] && str [ j ] == str [ j + 1 ] ) count *= 1 ; else if ( str [ j ] =...
cpp-python-C_PROGRAM_FIND_LARGEST_ELEMENT_ARRAY
cpp-python
int largest ( int arr [ ] , int n ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ; return max ; }
def largest ( arr , n ) : max = arr [ 0 ] for i in range ( 1 , n ) : if arr [ i ] > max : max = arr [ i ] return max
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int largest ( int arr [ ] , int n ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ; return max ; } </Code>
cpp-python-FIND_MINIMUM_DIFFERENCE_PAIR
cpp-python
int findMinDiff ( int arr [ ] , int n ) { int diff = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( arr [ i ] - arr [ j ] ) < diff ) diff = abs ( arr [ i ] - arr [ j ] ) ; return diff ; }
def findMinDiff ( arr , n ) : diff = 10 ** 20 for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : if abs ( arr [ i ] - arr [ j ] ) < diff : diff = abs ( arr [ i ] - arr [ j ] ) return diff
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMinDiff ( int arr [ ] , int n ) { int diff = INT_MAX ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( arr [ i ] - arr [ j ] ) < diff ) diff = abs ( arr [ i ] - arr [ j ] ) ; return diff ; } </Code>
cpp-python-MAXIMUM_SUM_IARRI_AMONG_ROTATIONS_GIVEN_ARRAY
cpp-python
int maxSum ( int arr [ ] , int n ) { int res = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = max ( res , curr_sum ) ; } return res ; }
def maxSum ( arr , n ) : res = - sys.maxsize for i in range ( 0 , n ) : curr_sum = 0 for j in range ( 0 , n ) : index = int ( ( i + j ) % n ) curr_sum += j * arr [ index ] res = max ( res , curr_sum ) return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSum ( int arr [ ] , int n ) { int res = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = max ( res , curr_sum ) ; } return res ; } </Code>
cpp-python-PROGRAM_CALCULATE_VOLUME_ELLIPSOID
cpp-python
float volumeOfEllipsoid ( float r1 , float r2 , float r3 ) { float pi = 3.14 ; return 1.33 * pi * r1 * r2 * r3 ; }
def volumeOfEllipsoid ( r1 , r2 , r3 ) : return 1.33 * math.pi * r1 * r2 * r3
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> float volumeOfEllipsoid ( float r1 , float r2 , float r3 ) { float pi = 3.14 ; return 1.33 * pi * r1 * r2 * r3 ; } </Code>
cpp-python-LCS_LONGEST_COMMON_SUBSEQUENCE_THREE_STRINGS
cpp-python
int lcsOf3 ( string X , string Y , string Z , int m , int n , int o ) { int L [ m + 1 ] [ n + 1 ] [ o + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { for ( int k = 0 ; k <= o ; k ++ ) { if ( i == 0 || j == 0 || k == 0 ) L [ i ] [ j ] [ k ] = ...
def lcsOf3 ( X , Y , Z , m , n , o ) : L = [ [ [ 0 for i in range ( o + 1 ) ] for j in range ( n + 1 ) ] for k in range ( m + 1 ) ] for i in range ( m + 1 ) : for j in range ( n + 1 ) : for k in range ( o + 1 ) : if ( i == 0 or j == 0 or k == 0 ) : L [ i ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lcsOf3 ( string X , string Y , string Z , int m , int n , int o ) { int L [ m + 1 ] [ n + 1 ] [ o + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { for ( int k = 0 ; k <= o ; k ++ ) { if ( i == 0 || j == 0 || k == 0 ) L [ i ] [ j ] [...
cpp-python-FIND_NUMBER_ENDLESS_POINTS
cpp-python
int countEndless ( bool input [ ] [ MAX ] , int n ) { bool row [ n ] [ n ] , col [ n ] [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { bool isEndless = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( input [ i ] [ j ] == 0 ) isEndless = 0 ; col [ i ] [ j ] = isEndless ; } } for (...
def countEndless ( input_mat , n ) : row = np.zeros ( ( n , n ) ) col = np.zeros ( ( n , n ) ) for j in range ( n ) : isEndless = 1 for i in range ( n - 1 , - 1 , - 1 ) : if ( input_mat [ i ] [ j ] == 0 ) : isEndless = 0 col [ i ] [ j ] = isEndless ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countEndless ( bool input [ ] [ MAX ] , int n ) { bool row [ n ] [ n ] , col [ n ] [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { bool isEndless = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( input [ i ] [ j ] == 0 ) isEndless = 0 ; col [ i ] [ j ] = isEndless ; } } ...
cpp-python-PROGRAM_NEXT_FIT_ALGORITHM_MEMORY_MANAGEMENT
cpp-python
void NextFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] , j = 0 ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; bl...
def NextFit ( blockSize , m , processSize , n ) : allocation = [ - 1 ] * n j = 0 for i in range ( n ) : while j < m : if blockSize [ j ] >= processSize [ i ] : allocation [ i ] = j blockSize [ j ] -= processSize [ i ] break j = ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void NextFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] , j = 0 ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; ...
cpp-python-LONGEST_GEOMETRIC_PROGRESSION
cpp-python
int lenOfLongestGP ( int set [ ] , int n ) { if ( n < 2 ) return n ; if ( n == 2 ) return ( set [ 1 ] % set [ 0 ] == 0 ) ; sort ( set , set + n ) ; int L [ n ] [ n ] ; int llgp = 1 ; for ( int i = 0 ; i < n ; ++ i ) if ( set [ n - 1 ] % set [ i ] == 0 ) L [ i ] [ n - 1 ] = 2 ; else L [ i ] [ n - 1 ] =...
def lenOfLongestGP ( sett , n ) : if n < 2 : return n if n == 2 : return ( sett [ 1 ] % sett [ 0 ] == 0 ) sett.sort ( ) L = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] llgp = 1 for i in range ( 0 , n ) : if sett [ n - 1 ] % sett [ i ] == 0 : L [ i ] [ ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lenOfLongestGP ( int set [ ] , int n ) { if ( n < 2 ) return n ; if ( n == 2 ) return ( set [ 1 ] % set [ 0 ] == 0 ) ; sort ( set , set + n ) ; int L [ n ] [ n ] ; int llgp = 1 ; for ( int i = 0 ; i < n ; ++ i ) if ( set [ n - 1 ] % set [ i ] == 0 ) L [ i ] [ n - 1 ] = 2 ; else L [ i ] [ n ...
cpp-python-CHECK_WHETHER_TRIANGLE_VALID_NOT_SIDES_GIVEN
cpp-python
bool checkValidity ( int a , int b , int c ) { if ( a + b <= c || a + c <= b || b + c <= a ) return false ; else return true ; }
def checkValidity ( a , b , c ) : if ( a + b <= c ) or ( a + c <= b ) or ( b + c <= a ) : return False else : return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkValidity ( int a , int b , int c ) { if ( a + b <= c || a + c <= b || b + c <= a ) return false ; else return true ; } </Code>
cpp-python-DIAGONALLY_DOMINANT_MATRIX
cpp-python
bool isDDM ( int m [ N ] [ N ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum += abs ( m [ i ] [ j ] ) ; sum -= abs ( m [ i ] [ i ] ) ; if ( abs ( m [ i ] [ i ] ) < sum ) return false ; } return true ; }
def isDDM ( m , n ) : for i in range ( 0 , n ) : sum = 0 for j in range ( 0 , n ) : sum = sum + abs ( m [ i ] [ j ] ) sum = sum - abs ( m [ i ] [ i ] ) if ( abs ( m [ i ] [ i ] ) < sum ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isDDM ( int m [ N ] [ N ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum += abs ( m [ i ] [ j ] ) ; sum -= abs ( m [ i ] [ i ] ) ; if ( abs ( m [ i ] [ i ] ) < sum ) return false ; } return true ; } </Code>
cpp-python-PROGRAM_TO_FIND_TRANSPOSE_OF_A_MATRIX
cpp-python
void transpose ( int A [ ] [ N ] , int B [ ] [ N ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) B [ i ] [ j ] = A [ j ] [ i ] ; }
def transpose ( A , B ) : for i in range ( N ) : for j in range ( N ) : B [ i ] [ j ] = A [ j ] [ i ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void transpose ( int A [ ] [ N ] , int B [ ] [ N ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) B [ i ] [ j ] = A [ j ] [ i ] ; } </Code>
cpp-python-MAXIMUM_POSSIBLE_SUM_WINDOW_ARRAY_ELEMENTS_WINDOW_ARRAY_UNIQUE
cpp-python
int returnMaxSum ( int A [ ] , int B [ ] , int n ) { unordered_set < int > mp ; int result = 0 ; int curr_sum = 0 , curr_begin = 0 ; for ( int i = 0 ; i < n ; ++ i ) { while ( mp . find ( A [ i ] ) != mp . end ( ) ) { mp . erase ( A [ curr_begin ] ) ; curr_sum -= B [ curr_begin ] ; cur...
def returnMaxSum ( A , B , n ) : mp = set ( ) result = 0 curr_sum = curr_begin = 0 for i in range ( 0 , n ) : while A [ i ] in mp : mp.remove ( A [ curr_begin ] ) curr_sum -= B [ curr_begin ] curr_begin += 1 mp.add ( A [ i ] ) curr_sum += B [ i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int returnMaxSum ( int A [ ] , int B [ ] , int n ) { unordered_set < int > mp ; int result = 0 ; int curr_sum = 0 , curr_begin = 0 ; for ( int i = 0 ; i < n ; ++ i ) { while ( mp . find ( A [ i ] ) != mp . end ( ) ) { mp . erase ( A [ curr_begin ] ) ; curr_sum -= B [ curr_begin ] ; ...
cpp-python-NUMBER_OF_PAIRS_IN_AN_ARRAY_HAVING_SUM_EQUAL_TO_PRODUCT
cpp-python
int sumEqualProduct ( int a [ ] , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; }
def sumEqualProduct ( a , n ) : zero = 0 two = 0 for i in range ( n ) : if a [ i ] == 0 : zero += 1 if a [ i ] == 2 : two += 1 cnt = ( zero * ( zero - 1 ) ) // 2 + \ ( two * ( two - 1 ) ) // 2 return cnt
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sumEqualProduct ( int a [ ] , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } </Code>
cpp-python-MULTISTAGE_GRAPH_SHORTEST_PATH
cpp-python
INT_MAX int shortestDist ( int graph [ N ] [ N ] ) { int dist [ N ] ; dist [ N - 1 ] = 0 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { dist [ i ] = INF ; for ( int j = i ; j < N ; j ++ ) { if ( graph [ i ] [ j ] == INF ) continue ; dist [ i ] = min ( dist [ i ] , graph [ i ] [ j ] + di...
def shortestDist ( graph ) : global INF dist = [ 0 ] * N dist [ N - 1 ] = 0 for i in range ( N - 2 , - 1 , - 1 ) : dist [ i ] = INF for j in range ( N ) : if graph [ i ] [ j ] == INF : continue dist [ i ] = min ( dist [ i ] , graph [ i ] [ j ] + di...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> INT_MAX int shortestDist ( int graph [ N ] [ N ] ) { int dist [ N ] ; dist [ N - 1 ] = 0 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { dist [ i ] = INF ; for ( int j = i ; j < N ; j ++ ) { if ( graph [ i ] [ j ] == INF ) continue ; dist [ i ] = min ( dist [ i ] , graph [ i ] [ j...
cpp-python-SUM_OF_ALL_ELEMENTS_UP_TO_NTH_ROW_IN_A_PASCALS_TRIANGLE_1
cpp-python
long long int calculateSum ( int n ) { long long int sum = 0 ; sum = 1 << n ; return ( sum - 1 ) ; }
def calculateSum ( n ) : sum = 0 sum = 1 << n return ( sum - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int calculateSum ( int n ) { long long int sum = 0 ; sum = 1 << n ; return ( sum - 1 ) ; } </Code>
cpp-python-PRIMALITY_TEST_SET_1_INTRODUCTION_AND_SCHOOL_METHOD_1
cpp-python
bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; }
def isPrime ( n ) : if ( n <= 1 ) : return False if ( n <= 3 ) : return True if ( n % 2 == 0 or n % 3 == 0 ) : return False i = 5 while ( i * i <= n ) : if ( n % i == 0 or n % ( i + 2 ) == 0 ) : return False i = i + 6 return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } </Code>
cpp-python-PRINT_FIBONACCI_SEQUENCE_USING_2_VARIABLES_1
cpp-python
void fib ( int n ) { int a = 0 , b = 1 ; if ( n >= 0 ) cout << a << " " ; if ( n >= 1 ) cout << b << " " ; for ( int i = 2 ; i <= n ; i ++ ) { cout << a + b << " " ; b = a + b ; a = b - a ; } }
def fib ( n ) : a = 0 b = 1 if ( n >= 0 ) : print ( a , end = ' ' ) if ( n >= 1 ) : print ( b , end = ' ' ) for i in range ( 2 , n + 1 ) : print ( a + b , end = ' ' ) b = a + b a = b - a
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void fib ( int n ) { int a = 0 , b = 1 ; if ( n >= 0 ) cout << a << " " ; if ( n >= 1 ) cout << b << " " ; for ( int i = 2 ; i <= n ; i ++ ) { cout << a + b << " " ; b = a + b ; a = b - a ; } } </Code>
cpp-python-K_TH_DIGIT_RAISED_POWER_B
cpp-python
int kthdigit ( int a , int b , int k ) { int p = pow ( a , b ) ; int count = 0 ; while ( p > 0 && count < k ) { int rem = p % 10 ; count ++ ; if ( count == k ) return rem ; p = p / 10 ; } return 0 ; }
def kthdigit ( a , b , k ) : p = a ** b count = 0 while ( p > 0 and count < k ) : rem = p % 10 count = count + 1 if ( count == k ) : return rem p = p / 10
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int kthdigit ( int a , int b , int k ) { int p = pow ( a , b ) ; int count = 0 ; while ( p > 0 && count < k ) { int rem = p % 10 ; count ++ ; if ( count == k ) return rem ; p = p / 10 ; } return 0 ; } </Code>
cpp-python-CHECK_WHETHER_GIVEN_NUMBER_EVEN_ODD_1
cpp-python
bool isEven ( int n ) { return ( ! ( n & 1 ) ) ; }
def isEven ( n ) : return ( not ( n & 1 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isEven ( int n ) { return ( ! ( n & 1 ) ) ; } </Code>
cpp-python-PRINT_SHORTEST_PATH_PRINT_STRING_SCREEN
cpp-python
void printPath ( string str ) { int i = 0 ; int curX = 0 , curY = 0 ; while ( i < str . length ( ) ) { int nextX = ( str [ i ] - 'A' ) / 5 ; int nextY = ( str [ i ] - 'B' + 1 ) % 5 ; while ( curX > nextX ) { cout << "Move Up" << endl ; curX -- ; } while ( curY > nextY ) { cou...
def printPath ( str ) : i = 0 curX = 0 curY = 0 while ( i < len ( str ) ) : nextX = int ( ( ord ( str [ i ] ) - ord ( 'A' ) ) / 5 ) nextY = ( ord ( str [ i ] ) - ord ( 'B' ) + 1 ) % 5 while ( curX > nextX ) : print ( "Move Up" ) curX -= 1 while ( c...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printPath ( string str ) { int i = 0 ; int curX = 0 , curY = 0 ; while ( i < str . length ( ) ) { int nextX = ( str [ i ] - 'A' ) / 5 ; int nextY = ( str [ i ] - 'B' + 1 ) % 5 ; while ( curX > nextX ) { cout << "Move Up" << endl ; curX -- ; } while ( curY > nextY ) { ...
cpp-python-CHECK_GIVEN_CIRCLE_LIES_COMPLETELY_INSIDE_RING_FORMED_TWO_CONCENTRIC_CIRCLES
cpp-python
bool checkcircle ( int r , int R , int r1 , int x1 , int y1 ) { int dis = sqrt ( x1 * x1 + y1 * y1 ) ; return ( dis - r1 >= R && dis + r1 <= r ) ; }
def checkcircle ( r , R , r1 , x1 , y1 ) : dis = int ( math.sqrt ( x1 * x1 + y1 * y1 ) ) return ( dis - r1 >= R and dis + r1 <= r )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkcircle ( int r , int R , int r1 , int x1 , int y1 ) { int dis = sqrt ( x1 * x1 + y1 * y1 ) ; return ( dis - r1 >= R && dis + r1 <= r ) ; } </Code>
cpp-python-GIVEN_LEVEL_ORDER_TRAVERSAL_BINARY_TREE_CHECK_TREE_MIN_HEAP
cpp-python
bool isMinHeap ( int level [ ] , int n ) { for ( int i = ( n / 2 - 1 ) ; i >= 0 ; i -- ) { if ( level [ i ] > level [ 2 * i + 1 ] ) return false ; if ( 2 * i + 2 < n ) { if ( level [ i ] > level [ 2 * i + 2 ] ) return false ; } } return true ; }
def isMinHeap ( level , n ) : for i in range ( int ( n / 2 ) - 1 , - 1 , - 1 ) : if level [ i ] > level [ 2 * i + 1 ] : return False if 2 * i + 2 < n : if level [ i ] > level [ 2 * i + 2 ] : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isMinHeap ( int level [ ] , int n ) { for ( int i = ( n / 2 - 1 ) ; i >= 0 ; i -- ) { if ( level [ i ] > level [ 2 * i + 1 ] ) return false ; if ( 2 * i + 2 < n ) { if ( level [ i ] > level [ 2 * i + 2 ] ) return false ; } } return true ; } </Code>
cpp-python-THIRD_LARGEST_ELEMENT_ARRAY_DISTINCT_ELEMENTS
cpp-python
void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { printf ( " Invalid Input " ) ; return ; } int first = arr [ 0 ] ; for ( int i = 1 ; i < arr_size ; i ++ ) if ( arr [ i ] > first ) first = arr [ i ] ; int second = INT_MIN ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( ...
def thirdLargest ( arr , arr_size ) : if ( arr_size < 3 ) : print ( " Invalid Input " ) return first = arr [ 0 ] for i in range ( 1 , arr_size ) : if ( arr [ i ] > first ) : first = arr [ i ] second = - sys.maxsize for i in range ( 0 , arr_size ) : if ( ar...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { printf ( " Invalid Input " ) ; return ; } int first = arr [ 0 ] ; for ( int i = 1 ; i < arr_size ; i ++ ) if ( arr [ i ] > first ) first = arr [ i ] ; int second = INT_MIN ; for ( int i = 0 ; i < arr_size ; i ++ ...
cpp-python-FIND_A_ROTATION_WITH_MAXIMUM_HAMMING_DISTANCE
cpp-python
int maxHamming ( int arr [ ] , int n ) { int brr [ 2 * n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ n + i ] = arr [ i ] ; int maxHam = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int currHam = 0 ; for ( int j = i , k = 0 ; j < ( i...
def maxHamming ( arr , n ) : brr = [ 0 ] * ( 2 * n + 1 ) for i in range ( n ) : brr [ i ] = arr [ i ] for i in range ( n ) : brr [ n + i ] = arr [ i ] maxHam = 0 for i in range ( 1 , n ) : currHam = 0 k = 0 for j in range ( i , i + n ) : if brr [ j...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxHamming ( int arr [ ] , int n ) { int brr [ 2 * n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ n + i ] = arr [ i ] ; int maxHam = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int currHam = 0 ; for ( int j = i , k = 0 ; ...
cpp-python-FIND_SUM_MODULO_K_FIRST_N_NATURAL_NUMBER
cpp-python
int findSum ( int N , int K ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) ans += ( i % K ) ; return ans ; }
def findSum ( N , K ) : ans = 0 for i in range ( 1 , N + 1 ) : ans += ( i % K ) return ans
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSum ( int N , int K ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) ans += ( i % K ) ; return ans ; } </Code>
cpp-python-SUM_SERIES_23_45_67_89_UPTO_N_TERMS
cpp-python
double seriesSum ( int n ) { int i = 1 ; double res = 0.0 ; bool sign = true ; while ( n > 0 ) { n -- ; if ( sign ) { sign = ! sign ; res = res + ( double ) ++ i / ++ i ; } else { sign = ! sign ; res = res - ( double ) ++ i / ++ i ; } } return res ; }
def seriesSum ( n ) : i = 1 res = 0.0 sign = True while ( n > 0 ) : n = n - 1 if ( sign ) : sign = False res = res + ( i + 1 ) / ( i + 2 ) i = i + 2 else : sign = True res = res - ( i + 1 ) / ( i + 2 ) i = i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double seriesSum ( int n ) { int i = 1 ; double res = 0.0 ; bool sign = true ; while ( n > 0 ) { n -- ; if ( sign ) { sign = ! sign ; res = res + ( double ) ++ i / ++ i ; } else { sign = ! sign ; res = res - ( double ) ++ i / ++ i ; } } return res ; } </Co...
cpp-python-COUNT_TOTAL_SET_BITS_IN_ALL_NUMBERS_FROM_1_TO_N
cpp-python
int countSetBits ( int n ) { int i = 0 ; int ans = 0 ; while ( ( 1 << i ) <= n ) { bool k = 0 ; int change = 1 << i ; for ( int j = 0 ; j <= n ; j ++ ) { ans += k ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; } ...
def countSetBits ( n ) : i = 0 ans = 0 while ( ( 1 << i ) <= n ) : k = 0 change = 1 << i for j in range ( 0 , n + 1 ) : ans += k if change == 1 : k = not k change = 1 << i else : change -= 1 i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSetBits ( int n ) { int i = 0 ; int ans = 0 ; while ( ( 1 << i ) <= n ) { bool k = 0 ; int change = 1 << i ; for ( int j = 0 ; j <= n ; j ++ ) { ans += k ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; ...
cpp-python-COUNT_PAIRS_WITH_GIVEN_SUM_1
cpp-python
int getPairsCount ( int arr [ ] , int n , int sum ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; int twice_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { twice_count += m [ sum - arr [ i ] ] ; if ( sum - arr [ i ] == arr [ i ] ) twice_count -- ; } r...
def getPairsCount ( arr , n , sum ) : m = [ 0 ] * 1000 for i in range ( 0 , n ) : m [ arr [ i ] ] m [ arr [ i ] ] += 1 twice_count = 0 for i in range ( 0 , n ) : twice_count += m [ sum - arr [ i ] ] if ( sum - arr [ i ] == arr [ i ] ) : twice_count -= 1 re...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getPairsCount ( int arr [ ] , int n , int sum ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; int twice_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { twice_count += m [ sum - arr [ i ] ] ; if ( sum - arr [ i ] == arr [ i ] ) twice_count -- ; ...