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
java-python-MINIMUM_CELLS_REQUIRED_REACH_DESTINATION_JUMPS_EQUAL_CELL_VALUES
java-python
static int minCells ( int mat [ ] [ ] , int m , int n ) { int dp [ ] [ ] = new int [ m ] [ n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = Integer . MAX_VALUE ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++...
def minCells ( mat , m , n ) : dp = [ [ MAX for i in range ( n ) ] for i in range ( m ) ] dp [ 0 ] [ 0 ] = 1 for i in range ( m ) : for j in range ( n ) : if ( dp [ i ] [ j ] != MAX and ( j + mat [ i ] [ j ] ) < n and ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int minCells ( int mat [ ] [ ] , int m , int n ) { int dp [ ] [ ] = new int [ m ] [ n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = Integer . MAX_VALUE ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; ...
java-python-REARRANGE_BINARY_STRING_ALTERNATE_X_Y_OCCURRENCES
java-python
static void arrangeString ( String str , int x , int y ) { int count_0 = 0 ; int count_1 = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == ' 0 ' ) count_0 ++ ; else count_1 ++ ; } while ( count_0 > 0 || count_1 > 0 ) { for ( int j = 0 ; j ...
def arrangeString ( str1 , x , y ) : count_0 = 0 count_1 = 0 n = len ( str1 ) for i in range ( n ) : if str1 [ i ] == '0' : count_0 += 1 else : count_1 += 1 while count_0 > 0 or count_1 > 0 : for i in range ( 0 , x ) : if count_0 > 0 : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void arrangeString ( String str , int x , int y ) { int count_0 = 0 ; int count_1 = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == ' 0 ' ) count_0 ++ ; else count_1 ++ ; } while ( count_0 > 0 || count_1 > 0 ) { for ( int j = 0 ;...
java-python-MINIMUM_NUMBER_DELETIONS_MAKE_STRING_PALINDROME_SET_2
java-python
static int getLevenstein ( StringBuilder input ) { StringBuilder revInput = new StringBuilder ( input ) ; revInput = revInput . reverse ( ) ; int n = input . length ( ) ; int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; ++ i ) { dp [ 0 ] [ i ] = i ; dp [ i ] [ 0 ] = i ; ...
def getLevenstein ( inpt ) : revInput = inpt [ : : - 1 ] n = len ( inpt ) dp = [ [ - 1 for _ in range ( n + 1 ) ] for __ in range ( n + 1 ) ] for i in range ( n + 1 ) : dp [ 0 ] [ i ] = i dp [ i ] [ 0 ] = i for i in range ( 1 , n + 1 ) : for j in range ( 1 , n + 1 ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int getLevenstein ( StringBuilder input ) { StringBuilder revInput = new StringBuilder ( input ) ; revInput = revInput . reverse ( ) ; int n = input . length ( ) ; int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; ++ i ) { dp [ 0 ] [ i ] = i ; dp [ i ] [ 0 ] =...
java-python-DYNAMIC_PROGRAMMING_SET_34_ASSEMBLY_LINE_SCHEDULING
java-python
static int carAssembly ( int a [ ] [ ] , int t [ ] [ ] , int e [ ] , int x [ ] ) { int T1 [ ] = new int [ NUM_STATION ] ; int T2 [ ] = new int [ NUM_STATION ] ; int i ; T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ; T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ; for ( i = 1 ; i < NUM_STATION ; ++ i ) { T1 [ i ] = min (...
def carAssembly ( a , t , e , x ) : NUM_STATION = len ( a [ 0 ] ) T1 = [ 0 for i in range ( NUM_STATION ) ] T2 = [ 0 for i in range ( NUM_STATION ) ] T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] for i in range ( 1 , NUM_STATION ) : T1 [ i ] = min ( T1 [ i - 1 ] +...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int carAssembly ( int a [ ] [ ] , int t [ ] [ ] , int e [ ] , int x [ ] ) { int T1 [ ] = new int [ NUM_STATION ] ; int T2 [ ] = new int [ NUM_STATION ] ; int i ; T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ; T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ; for ( i = 1 ; i < NUM_STATION ; ++ i ) { T1 [ i ] ...
java-python-BASIC_AND_EXTENDED_EUCLIDEAN_ALGORITHMS
java-python
public static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; }
def gcd ( a , b ) : if a == 0 : return b return gcd ( b % a , a )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } </Code>
java-python-FIND_TRIPLETS_ARRAY_WHOSE_SUM_EQUAL_ZERO_2
java-python
static void findTriplets ( int arr [ ] , int n ) { boolean found = false ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int l = i + 1 ; int r = n - 1 ; int x = arr [ i ] ; while ( l < r ) { if ( x + arr [ l ] + arr [ r ] == 0 ) { System . out . print ( x + " " )...
def findTriplets ( arr , n ) : found = False arr.sort ( ) for i in range ( 0 , n - 1 ) : l = i + 1 r = n - 1 x = arr [ i ] while ( l < r ) : if ( x + arr [ l ] + arr [ r ] == 0 ) : print ( x , arr [ l ] , arr [ r ] ) l += 1 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void findTriplets ( int arr [ ] , int n ) { boolean found = false ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int l = i + 1 ; int r = n - 1 ; int x = arr [ i ] ; while ( l < r ) { if ( x + arr [ l ] + arr [ r ] == 0 ) { System . out . print ( x ...
java-python-FIND_THE_NUMBER_OCCURRING_ODD_NUMBER_OF_TIMES_2
java-python
int getOddOccurrence ( int ar [ ] , int ar_size ) { int i ; int res = 0 ; for ( i = 0 ; i < ar_size ; i ++ ) { res = res ^ ar [ i ] ; } return res ; }
def getOddOccurrence ( arr ) : res = 0 for element in arr : res = res ^ element return res
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> int getOddOccurrence ( int ar [ ] , int ar_size ) { int i ; int res = 0 ; for ( i = 0 ; i < ar_size ; i ++ ) { res = res ^ ar [ i ] ; } return res ; } </Code>
java-python-DYNAMIC_PROGRAMMING_HIGH_EFFORT_VS_LOW_EFFORT_TASKS_PROBLEM
java-python
static int maxTasks ( int high [ ] , int low [ ] , int n ) { if ( n <= 0 ) return 0 ; return Math . max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ; }
def maxTasks ( high , low , n ) : if ( n <= 0 ) : return 0 return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxTasks ( int high [ ] , int low [ ] , int n ) { if ( n <= 0 ) return 0 ; return Math . max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ; } </Code>
java-python-FIND_TWO_SIDES_RIGHT_ANGLE_TRIANGLE
java-python
static void printOtherSides ( int n ) { if ( n % 2 != 0 ) { if ( n == 1 ) System . out . println ( "-1" ) ; else { int b = ( n * n - 1 ) / 2 ; int c = ( n * n + 1 ) / 2 ; System . out . println ( "b = " + b + ", c = " + c ) ; } } else { if ( n == 2 ) System . out . println ( "-1"...
def printOtherSides ( n ) : if ( n & 1 ) : if ( n == 1 ) : print ( - 1 ) else : b = ( n * n - 1 ) // 2 c = ( n * n + 1 ) // 2 print ( "b =" , b , ", c =" , c ) else : if ( n == 2 ) : print ( - 1 ) else : b = ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printOtherSides ( int n ) { if ( n % 2 != 0 ) { if ( n == 1 ) System . out . println ( "-1" ) ; else { int b = ( n * n - 1 ) / 2 ; int c = ( n * n + 1 ) / 2 ; System . out . println ( "b = " + b + ", c = " + c ) ; } } else { if ( n == 2 ) System . out . println...
java-python-MINIMUM_LENGTH_UNSORTED_SUBARRAY_SORTING_WHICH_MAKES_THE_COMPLETE_ARRAY_SORTED
java-python
static void printUnsorted ( int arr [ ] , int n ) { int s = 0 , e = n - 1 , i , max , min ; for ( s = 0 ; s < n - 1 ; s ++ ) { if ( arr [ s ] > arr [ s + 1 ] ) break ; } if ( s == n - 1 ) { System . out . println ( "The complete array is sorted" ) ; return ; } for ( e = n - 1 ; e > 0 ; e...
def printUnsorted ( arr , n ) : e = n - 1 for s in range ( 0 , n - 1 ) : if arr [ s ] > arr [ s + 1 ] : break if s == n - 1 : print ( "The complete array is sorted" ) exit ( ) e = n - 1 while e > 0 : if arr [ e ] < arr [ e - 1 ] : break ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printUnsorted ( int arr [ ] , int n ) { int s = 0 , e = n - 1 , i , max , min ; for ( s = 0 ; s < n - 1 ; s ++ ) { if ( arr [ s ] > arr [ s + 1 ] ) break ; } if ( s == n - 1 ) { System . out . println ( "The complete array is sorted" ) ; return ; } for ( e = n - 1 ; e > ...
java-python-CHECK_TWO_GIVEN_CIRCLES_TOUCH_INTERSECT
java-python
static int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return - 1 ; else return 0 ; }
def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) : distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) radSumSq = ( r1 + r2 ) * ( r1 + r2 ) if ( distSq == radSumSq ) : return 1 elif ( distSq > radSumSq ) : return - 1 else : return 0
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return - 1 ; else return 0 ; } </Code>
java-python-COUNT_DISTINCT_ELEMENTS_IN_EVERY_WINDOW_OF_SIZE_K
java-python
static void countDistinct ( int arr [ ] , int k ) { HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ; int dist_count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( hM . get ( arr [ i ] ) == null ) { hM . put ( arr [ i ] , 1 ) ; dist_count ++ ; } else { int...
def countDistinct ( arr , k , n ) : mp = defaultdict ( lambda : 0 ) dist_count = 0 for i in range ( k ) : if mp [ arr [ i ] ] == 0 : dist_count += 1 mp [ arr [ i ] ] += 1 print ( dist_count ) for i in range ( k , n ) : if mp [ arr [ i - k ] ] == 1 : di...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void countDistinct ( int arr [ ] , int k ) { HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ; int dist_count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( hM . get ( arr [ i ] ) == null ) { hM . put ( arr [ i ] , 1 ) ; dist_count ++ ; } else { ...
java-python-PROGRAM_TO_FIND_THE_AREA_OF_PENTAGON
java-python
static float findArea ( float a ) { float area ; area = ( float ) ( Math . sqrt ( 5 * ( 5 + 2 * ( Math . sqrt ( 5 ) ) ) ) * a * a ) / 4 ; return area ; }
def findArea ( a ) : area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4 return area
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static float findArea ( float a ) { float area ; area = ( float ) ( Math . sqrt ( 5 * ( 5 + 2 * ( Math . sqrt ( 5 ) ) ) ) * a * a ) / 4 ; return area ; } </Code>
java-python-COUNT_ROTATIONS_DIVISIBLE_8
java-python
static int countRotationsDivBy8 ( String n ) { int len = n . length ( ) ; int count = 0 ; if ( len == 1 ) { int oneDigit = n . charAt ( 0 ) - ' 0 ' ; if ( oneDigit % 8 == 0 ) return 1 ; return 0 ; } if ( len == 2 ) { int first = ( n . charAt ( 0 ) - ' 0 ' ) * 10 + ( n . charAt ( 1 ) - ' 0 ' ) ...
def countRotationsDivBy8 ( n ) : l = len ( n ) count = 0 if ( l == 1 ) : oneDigit = int ( n [ 0 ] ) if ( oneDigit % 8 == 0 ) : return 1 return 0 if ( l == 2 ) : first = int ( n [ 0 ] ) * 10 + int ( n [ 1 ] ) second = int ( n [ 1 ] ) * 10 + int ( n [ 0 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countRotationsDivBy8 ( String n ) { int len = n . length ( ) ; int count = 0 ; if ( len == 1 ) { int oneDigit = n . charAt ( 0 ) - ' 0 ' ; if ( oneDigit % 8 == 0 ) return 1 ; return 0 ; } if ( len == 2 ) { int first = ( n . charAt ( 0 ) - ' 0 ' ) * 10 + ( n . charAt ( 1 ) - '...
java-python-LONGEST_COMMON_SUBSEQUENCE_WITH_AT_MOST_K_CHANGES_ALLOWED
java-python
static int lcs ( int [ ] [ ] [ ] dp , int [ ] arr1 , int n , int [ ] arr2 , int m , int k ) { if ( k < 0 ) return - 10000000 ; if ( n < 0 || m < 0 ) return 0 ; int ans = dp [ n ] [ m ] [ k ] ; if ( ans != - 1 ) return ans ; try { ans = Math . max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr...
def lcs ( dp , arr1 , n , arr2 , m , k ) : if k < 0 : return - ( 10 ** 7 ) if n < 0 or m < 0 : return 0 ans = dp [ n ] [ m ] [ k ] if ans != - 1 : return ans ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) ) if arr1 [ n - 1 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int lcs ( int [ ] [ ] [ ] dp , int [ ] arr1 , int n , int [ ] arr2 , int m , int k ) { if ( k < 0 ) return - 10000000 ; if ( n < 0 || m < 0 ) return 0 ; int ans = dp [ n ] [ m ] [ k ] ; if ( ans != - 1 ) return ans ; try { ans = Math . max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( d...
java-python-DYNAMIC_PROGRAMMING_SET_5_EDIT_DISTANCE_1
java-python
static int editDistDP ( String str1 , String str2 , int m , int n ) { int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( str1 . c...
def editDistDP ( str1 , str2 , m , n ) : dp = [ [ 0 for x in range ( n + 1 ) ] for x in range ( m + 1 ) ] for i in range ( m + 1 ) : for j in range ( n + 1 ) : if i == 0 : dp [ i ] [ j ] = j elif j == 0 : dp [ i ] [ j ] = i elif str1 [ ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int editDistDP ( String str1 , String str2 , int m , int n ) { int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( s...
java-python-FIND_NUMBER_TRANSFORMATION_MAKE_TWO_MATRIX_EQUAL
java-python
static int countOps ( int A [ ] [ ] , int B [ ] [ ] , int m , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 1 ; j < m ; j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [ ...
def countOps ( A , B , m , n ) : for i in range ( n ) : for j in range ( m ) : A [ i ] [ j ] -= B [ i ] [ j ] for i in range ( 1 , n ) : for j in range ( 1 , n ) : if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [ 0 ] != 0 ) : return - 1 r...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countOps ( int A [ ] [ ] , int B [ ] [ ] , int m , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 1 ; j < m ; j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [...
java-python-LONGEST_ALTERNATING_POSITIVE_NEGATIVE_SUBARRAY_STARTING_EVERY_INDEX
java-python
public static void longestAlternating ( int arr [ ] , int n ) { int [ ] count = new int [ n ] ; count [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ; else count [ i ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) Syste...
def longestAlternating ( arr , n ) : count = [ None ] * n count [ n - 1 ] = 1 i = n - 2 while i >= 0 : if ( arr [ i ] * arr [ i + 1 ] < 0 ) : count [ i ] = count [ i + 1 ] + 1 else : count [ i ] = 1 i = i - 1 i = 0 while i < n : print ( cou...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static void longestAlternating ( int arr [ ] , int n ) { int [ ] count = new int [ n ] ; count [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ; else count [ i ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ...
java-python-FIND_ALL_DIVISORS_OF_A_NATURAL_NUMBER_SET_2
java-python
static void printDivisors ( int n ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) System . out . printf ( "%d " , i ) ; else { System . out . printf ( "%d " , i ) ; v . add ( n / i ) ; ...
def printDivisors ( n ) : list = [ ] for i in range ( 1 , int ( math.sqrt ( n ) + 1 ) ) : if ( n % i == 0 ) : if ( n / i == i ) : print ( i , end = " " ) else : print ( i , end = " " ) list.append ( int ( n / i ) ) for i in list...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printDivisors ( int n ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) System . out . printf ( "%d " , i ) ; else { System . out . printf ( "%d " , i ) ; v . add ( n / i ) ...
java-python-CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER
java-python
public static boolean isPower ( int x , int y ) { if ( x == 1 ) return ( y == 1 ) ; int pow = 1 ; while ( pow < y ) pow = pow * x ; return ( pow == y ) ; }
def isPower ( x , y ) : if ( x == 1 ) : return ( y == 1 ) pow = 1 while ( pow < y ) : pow = pow * x return ( pow == y )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static boolean isPower ( int x , int y ) { if ( x == 1 ) return ( y == 1 ) ; int pow = 1 ; while ( pow < y ) pow = pow * x ; return ( pow == y ) ; } </Code>
java-python-MAXIMUM_SUM_SUBSEQUENCE_LEAST_K_DISTANT_ELEMENTS
java-python
static int maxSum ( int arr [ ] , int N , int k ) { int MS [ ] = new int [ N ] ; MS [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( i + k + 1 >= N ) MS [ i ] = Math . max ( arr [ i ] , MS [ i + 1 ] ) ; else MS [ i ] = Math . max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ...
def maxSum ( arr , N , k ) : MS = [ 0 for i in range ( N ) ] MS [ N - 1 ] = arr [ N - 1 ] for i in range ( N - 2 , - 1 , - 1 ) : if ( i + k + 1 >= N ) : MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] ) else : MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxSum ( int arr [ ] , int N , int k ) { int MS [ ] = new int [ N ] ; MS [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( i + k + 1 >= N ) MS [ i ] = Math . max ( arr [ i ] , MS [ i + 1 ] ) ; else MS [ i ] = Math . max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i +...
java-python-GROUP_OCCURRENCES_CHARACTERS_ACCORDING_FIRST_APPEARANCE
java-python
static void printGrouped ( String str ) { int n = str . length ( ) ; int count [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { while ( count [ str . charAt ( i ) - ' a ' ] != 0 ) { System . out . print (...
def printGrouped ( string ) : n = len ( string ) count = [ 0 ] * MAX_CHAR for i in range ( n ) : count [ ord ( string [ i ] ) - ord ( "a" ) ] += 1 for i in range ( n ) : while count [ ord ( string [ i ] ) - ord ( "a" ) ] : print ( string [ i ] , end = "" ) count [...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printGrouped ( String str ) { int n = str . length ( ) ; int count [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { while ( count [ str . charAt ( i ) - ' a ' ] != 0 ) { System . out . ...
java-python-COUNT_SORTED_ROWS_MATRIX
java-python
static int sortedCount ( int mat [ ] [ ] , int r , int c ) { int result = 0 ; for ( int i = 0 ; i < r ; i ++ ) { int j ; for ( j = 0 ; j < c - 1 ; j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ; if ( j == c - 1 ) result ++ ; } for ( int i = 0 ; i < r ; i ++ ) { int j...
def sortedCount ( mat , r , c ) : result = 0 for i in range ( r ) : j = 0 for j in range ( c - 1 ) : if mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] : break if j == c - 2 : result += 1 for i in range ( 0 , r ) : j = 0 for j in range (...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int sortedCount ( int mat [ ] [ ] , int r , int c ) { int result = 0 ; for ( int i = 0 ; i < r ; i ++ ) { int j ; for ( j = 0 ; j < c - 1 ; j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ; if ( j == c - 1 ) result ++ ; } for ( int i = 0 ; i < r ; i ++ ) { ...
java-python-COUNT_DISTINCT_SUBSEQUENCES
java-python
static int countSub ( String str ) { int [ ] last = new int [ MAX_CHAR ] ; Arrays . fill ( last , - 1 ) ; int n = str . length ( ) ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] = 2 * dp [ i - 1 ] ; if ( last [ ( int ) str . charAt ( i - 1 ) ] != -...
def countSub ( ss ) : last = [ - 1 for i in range ( MAX_CHAR + 1 ) ] n = len ( ss ) dp = [ - 2 for i in range ( n + 1 ) ] dp [ 0 ] = 1 for i in range ( 1 , n + 1 ) : dp [ i ] = 2 * dp [ i - 1 ] if last [ ord ( ss [ i - 1 ] ) ] != - 1 : dp [ i ] = dp [ i ] - dp [ last [ or...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countSub ( String str ) { int [ ] last = new int [ MAX_CHAR ] ; Arrays . fill ( last , - 1 ) ; int n = str . length ( ) ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] = 2 * dp [ i - 1 ] ; if ( last [ ( int ) str . charAt ( i - 1 )...
java-python-LARGEST_INCREASING_SUBSEQUENCE_OF_CONSECUTIVE_INTEGERS
java-python
static void findLIS ( int A [ ] , int n ) { Map < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; int LIS_size = 1 ; int LIS_index = 0 ; hash . put ( A [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { hash . put ( A [ i ] , hash . get ( A [ i ] - 1 ) == null ? 1 : hash . get ( A [ i...
def findLIS ( A , n ) : hash = dict ( ) LIS_size , LIS_index = 1 , 0 hash [ A [ 0 ] ] = 1 for i in range ( 1 , n ) : if A [ i ] - 1 not in hash : hash [ A [ i ] - 1 ] = 0 hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1 if LIS_size < hash [ A [ i ] ] : LIS_size...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void findLIS ( int A [ ] , int n ) { Map < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; int LIS_size = 1 ; int LIS_index = 0 ; hash . put ( A [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { hash . put ( A [ i ] , hash . get ( A [ i ] - 1 ) == null ? 1 : hash . get ...
java-python-FIND_THE_MAXIMUM_OF_MINIMUMS_FOR_EVERY_WINDOW_SIZE_IN_A_GIVEN_ARRAY
java-python
static void printMaxOfMin ( int n ) { for ( int k = 1 ; k <= n ; k ++ ) { int maxOfMin = Integer . MIN_VALUE ; for ( int i = 0 ; i <= n - k ; i ++ ) { int min = arr [ i ] ; for ( int j = 1 ; j < k ; j ++ ) { if ( arr [ i + j ] < min ) min = arr [ i + j ] ; } ...
def printMaxOfMin ( arr , n ) : for k in range ( 1 , n + 1 ) : maxOfMin = INT_MIN for i in range ( n - k + 1 ) : min = arr [ i ] for j in range ( k ) : if ( arr [ i + j ] < min ) : min = arr [ i + j ] if ( min > maxOfMin ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printMaxOfMin ( int n ) { for ( int k = 1 ; k <= n ; k ++ ) { int maxOfMin = Integer . MIN_VALUE ; for ( int i = 0 ; i <= n - k ; i ++ ) { int min = arr [ i ] ; for ( int j = 1 ; j < k ; j ++ ) { if ( arr [ i + j ] < min ) min = arr [ i + j ] ; ...
java-python-MAXIMUM_NUMBER_OF_TRAILING_ZEROS_IN_THE_PRODUCT_OF_THE_SUBSETS_OF_SIZE_K
java-python
static int maximumZeros ( int arr [ ] , int n , int k ) { int subset [ ] [ ] = new int [ k + 1 ] [ MAX5 + 5 ] ; for ( int [ ] row : subset ) { Arrays . fill ( row , - 1 ) ; } subset [ 0 ] [ 0 ] = 0 ; for ( int p = 0 ; p < n ; p ++ ) { int pw2 = 0 , pw5 = 0 ; while ( arr [ p ] % 2 == 0 ) { ...
def maximumZeros ( arr , n , k ) : global MAX5 subset = [ [ - 1 ] * ( MAX5 + 5 ) for _ in range ( k + 1 ) ] subset [ 0 ] [ 0 ] = 0 for p in arr : pw2 , pw5 = 0 , 0 while not p % 2 : pw2 += 1 p //= 2 while not p % 5 : pw5 += 1 p //= ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maximumZeros ( int arr [ ] , int n , int k ) { int subset [ ] [ ] = new int [ k + 1 ] [ MAX5 + 5 ] ; for ( int [ ] row : subset ) { Arrays . fill ( row , - 1 ) ; } subset [ 0 ] [ 0 ] = 0 ; for ( int p = 0 ; p < n ; p ++ ) { int pw2 = 0 , pw5 = 0 ; while ( arr [ p ] % 2 == 0 )...
java-python-LARGEST_DIVISIBLE_PAIRS_SUBSET
java-python
static int largestSubset ( int [ ] a , int n ) { Arrays . sort ( a ) ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int mxm = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] % a [ i ] == 0 ) { mxm = Math . max ( mxm , dp [ j ] ...
def largestSubset ( a , n ) : a.sort ( ) dp = [ 0 for i in range ( n ) ] dp [ n - 1 ] = 1 for i in range ( n - 2 , - 1 , - 1 ) : mxm = 0 for j in range ( i + 1 , n ) : if a [ j ] % a [ i ] == 0 : mxm = max ( mxm , dp [ j ] ) dp [ i ] = 1 + mxm retu...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int largestSubset ( int [ ] a , int n ) { Arrays . sort ( a ) ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int mxm = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] % a [ i ] == 0 ) { mxm = Math . max ( mxm , dp...
java-python-FIND_KTH_CHARACTER_OF_DECRYPTED_STRING
java-python
static char encodedChar ( String str , int k ) { String expand = "" ; String temp = "" ; int freq = 0 ; for ( int i = 0 ; i < str . length ( ) ; ) { temp = "" ; freq = 0 ; while ( i < str . length ( ) && str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { temp += str . charAt ( ...
def encodedChar ( str , k ) : expand = "" freq = 0 i = 0 while ( i < len ( str ) ) : temp = "" freq = 0 while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( 'a' ) and ord ( str [ i ] ) <= ord ( 'z' ) ) : temp += str [ i ] i += 1 while ( i < len (...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static char encodedChar ( String str , int k ) { String expand = "" ; String temp = "" ; int freq = 0 ; for ( int i = 0 ; i < str . length ( ) ; ) { temp = "" ; freq = 0 ; while ( i < str . length ( ) && str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { temp += str . ch...
java-python-ELEMENTS_TO_BE_ADDED_SO_THAT_ALL_ELEMENTS_OF_A_RANGE_ARE_PRESENT_IN_ARRAY_1
java-python
static int countNum ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; int count = 0 , maxm = Integer . MIN_VALUE , minm = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { s . add ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm ) maxm =...
def countNum ( arr , n ) : s = dict ( ) count , maxm , minm = 0 , - 10 ** 9 , 10 ** 9 for i in range ( n ) : s [ arr [ i ] ] = 1 if ( arr [ i ] < minm ) : minm = arr [ i ] if ( arr [ i ] > maxm ) : maxm = arr [ i ] for i in range ( minm , maxm + 1 ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countNum ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; int count = 0 , maxm = Integer . MIN_VALUE , minm = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { s . add ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm )...
java-python-COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY_1
java-python
public static int countPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; else hm . put ( arr [ i ] , 1 ) ; } int ans = 0 ; for ( Map...
def countPairs ( arr , n ) : mp = dict ( ) for i in range ( n ) : if arr [ i ] in mp.keys ( ) : mp [ arr [ i ] ] += 1 else : mp [ arr [ i ] ] = 1 ans = 0 for it in mp : count = mp [ it ] ans += ( count * ( count - 1 ) ) // 2 return ans
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int countPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; else hm . put ( arr [ i ] , 1 ) ; } int ans = 0 ; fo...
java-python-MAXIMUM_POSSIBLE_DIFFERENCE_TWO_SUBSETS_ARRAY_1
java-python
static int maxDiff ( int [ ] arr , int n ) { int result = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += Math . abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . abs ( arr [ n - 1 ] ) ; return ...
def maxDiff ( arr , n ) : result = 0 arr.sort ( ) for i in range ( n - 1 ) : if ( abs ( arr [ i ] ) != abs ( arr [ i + 1 ] ) ) : result += abs ( arr [ i ] ) else : pass if ( arr [ n - 2 ] != arr [ n - 1 ] ) : result += abs ( arr [ n - 1 ] ) return resu...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxDiff ( int [ ] arr , int n ) { int result = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += Math . abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . abs ( arr [ n - 1 ] ) ; ...
java-python-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS
java-python
public static int middleOfThree ( int a , int b , int c ) { if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ; else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ; else return c ; }
def middleOfThree ( a , b , c ) : if ( ( a < b and b < c ) or ( c < b and b < a ) ) : return b if ( ( b < a and a < c ) or ( c < a and a < b ) ) : return a else : return c
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int middleOfThree ( int a , int b , int c ) { if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ; else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ; else return c ; } </Code>
java-python-COUNT_ARRAYS_CONSECUTIVE_ELEMENT_DIFFERENT_VALUES
java-python
public static int countarray ( int n , int k , int x ) { int [ ] dp = new int [ 109 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ; }
def countarray ( n , k , x ) : dp = list ( ) dp.append ( 0 ) dp.append ( 1 ) i = 2 while i < n : dp.append ( ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ) i = i + 1 return ( ( k - 1 ) * dp [ n - 2 ] if x == 1 else dp [ n - 1 ] )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int countarray ( int n , int k , int x ) { int [ ] dp = new int [ 109 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ; } </Code>
java-python-NEWMAN_SHANKS_WILLIAMS_PRIME
java-python
static int nswp ( int n ) { if ( n == 0 || n == 1 ) return 1 ; return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ; }
def nswp ( n ) : if n == 0 or n == 1 : return 1 return 2 * nswp ( n - 1 ) + nswp ( n - 2 )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int nswp ( int n ) { if ( n == 0 || n == 1 ) return 1 ; return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ; } </Code>
java-python-DYNAMIC_PROGRAMMING_SET_1
java-python
int fib ( int n ) { if ( lookup [ n ] == NIL ) { if ( n <= 1 ) lookup [ n ] = n ; else lookup [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ; } return lookup [ n ] ; }
def fib ( n , lookup ) : if n == 0 or n == 1 : lookup [ n ] = n if lookup [ n ] is None : lookup [ n ] = fib ( n - 1 , lookup ) + fib ( n - 2 , lookup ) return lookup [ n ]
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> int fib ( int n ) { if ( lookup [ n ] == NIL ) { if ( n <= 1 ) lookup [ n ] = n ; else lookup [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ; } return lookup [ n ] ; } </Code>
java-python-COUNT_NUMBERS_CAN_CONSTRUCTED_USING_TWO_NUMBERS
java-python
static int countNums ( int n , int x , int y ) { boolean [ ] arr = new boolean [ n + 1 ] ; if ( x <= n ) arr [ x ] = true ; if ( y <= n ) arr [ y ] = true ; int result = 0 ; for ( int i = Math . min ( x , y ) ; i <= n ; i ++ ) { if ( arr [ i ] ) { if ( i + x <= n ) arr [ i + x ] = true ; i...
def countNums ( n , x , y ) : arr = [ False for i in range ( n + 2 ) ] if ( x <= n ) : arr [ x ] = True if ( y <= n ) : arr [ y ] = True result = 0 for i in range ( min ( x , y ) , n + 1 ) : if ( arr [ i ] ) : if ( i + x <= n ) : arr [ i + x ] = Tr...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countNums ( int n , int x , int y ) { boolean [ ] arr = new boolean [ n + 1 ] ; if ( x <= n ) arr [ x ] = true ; if ( y <= n ) arr [ y ] = true ; int result = 0 ; for ( int i = Math . min ( x , y ) ; i <= n ; i ++ ) { if ( arr [ i ] ) { if ( i + x <= n ) arr [ i + x ] = true ; ...
java-python-COUNT_PALINDROMIC_SUBSEQUENCE_GIVEN_STRING
java-python
static int countPS ( String str ) { int N = str . length ( ) ; int [ ] [ ] cps = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) cps [ i ] [ i ] = 1 ; for ( int L = 2 ; L <= N ; L ++ ) { for ( int i = 0 ; i < N ; i ++ ) { int k = L + i - 1 ; if ( k < N ) { ...
def countPS ( str ) : N = len ( str ) cps = [ [ 0 for i in range ( N + 2 ) ] for j in range ( N + 2 ) ] for i in range ( N ) : cps [ i ] [ i ] = 1 for L in range ( 2 , N + 1 ) : for i in range ( N ) : k = L + i - 1 if ( k < N ) : if ( str [ i ] == ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countPS ( String str ) { int N = str . length ( ) ; int [ ] [ ] cps = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) cps [ i ] [ i ] = 1 ; for ( int L = 2 ; L <= N ; L ++ ) { for ( int i = 0 ; i < N ; i ++ ) { int k = L + i - 1 ; if ( k < N ) { ...
java-python-CONVERT_DECIMAL_FRACTION_BINARY_NUMBER
java-python
static String decimalToBinary ( double num , int k_prec ) { String binary = "" ; int Integral = ( int ) num ; double fractional = num - Integral ; while ( Integral > 0 ) { int rem = Integral % 2 ; binary += ( ( char ) ( rem + ' 0 ' ) ) ; Integral /= 2 ; } binary = reverse ( binary ) ; binary +...
def decimalToBinary ( num , k_prec ) : binary = "" Integral = int ( num ) fractional = num - Integral while ( Integral ) : rem = Integral % 2 binary += str ( rem ) Integral //= 2 binary = binary [ : : - 1 ] binary += '.' while ( k_prec ) : fractional *= 2 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static String decimalToBinary ( double num , int k_prec ) { String binary = "" ; int Integral = ( int ) num ; double fractional = num - Integral ; while ( Integral > 0 ) { int rem = Integral % 2 ; binary += ( ( char ) ( rem + ' 0 ' ) ) ; Integral /= 2 ; } binary = reverse ( binary ) ; b...
java-python-DYNAMIC_PROGRAMMING_SET_37_BOOLEAN_PARENTHESIZATION_PROBLEM
java-python
static int countParenth ( char symb [ ] , char oper [ ] , int n ) { int F [ ] [ ] = new int [ n ] [ n ] ; int T [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { F [ i ] [ i ] = ( symb [ i ] == ' F ' ) ? 1 : 0 ; T [ i ] [ i ] = ( symb [ i ] == ' T ' ) ? 1 : 0 ; } for ( int gap = 1...
def countParenth ( symb , oper , n ) : F = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ] T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ] for i in range ( n ) : if symb [ i ] == 'F' : F [ i ] [ i ] = 1 else : F [ i ] [ i ] = 0 if s...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countParenth ( char symb [ ] , char oper [ ] , int n ) { int F [ ] [ ] = new int [ n ] [ n ] ; int T [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { F [ i ] [ i ] = ( symb [ i ] == ' F ' ) ? 1 : 0 ; T [ i ] [ i ] = ( symb [ i ] == ' T ' ) ? 1 : 0 ; } for ( int ...
java-python-COUNT_SET_BITS_IN_AN_INTEGER_1
java-python
public static int countSetBits ( int n ) { if ( n == 0 ) return 0 ; else return ( n & 1 ) + countSetBits ( n >> 1 ) ; }
def countSetBits ( n ) : if ( n == 0 ) : return 0 else : return ( n & 1 ) + countSetBits ( n >> 1 )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int countSetBits ( int n ) { if ( n == 0 ) return 0 ; else return ( n & 1 ) + countSetBits ( n >> 1 ) ; } </Code>
java-python-LONGEST_SPAN_SUM_TWO_BINARY_ARRAYS
java-python
static int longestCommonSum ( int n ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum1 = 0 , sum2 = 0 ; for ( int j = i ; j < n ; j ++ ) { sum1 += arr1 [ j ] ; sum2 += arr2 [ j ] ; if ( sum1 == sum2 ) { int len = j - i + 1 ; if ( len > maxLen ) max...
def longestCommonSum ( arr1 , arr2 , n ) : maxLen = 0 for i in range ( 0 , n ) : sum1 = 0 sum2 = 0 for j in range ( i , n ) : sum1 += arr1 [ j ] sum2 += arr2 [ j ] if ( sum1 == sum2 ) : len = j - i + 1 if ( len > maxLen ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int longestCommonSum ( int n ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum1 = 0 , sum2 = 0 ; for ( int j = i ; j < n ; j ++ ) { sum1 += arr1 [ j ] ; sum2 += arr2 [ j ] ; if ( sum1 == sum2 ) { int len = j - i + 1 ; if ( len > maxLe...
java-python-MAXIMUM_TRIPLET_SUM_ARRAY
java-python
static int maxTripletSum ( int arr [ ] , int n ) { int sum = - 1000000 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ; return sum ; }
def maxTripletSum ( arr , n ) : sm = - 1000000 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : for k in range ( j + 1 , n ) : if ( sm < ( arr [ i ] + arr [ j ] + arr [ k ] ) ) : sm = arr [ i ] + arr [ j ] + arr [ k ] return sm
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxTripletSum ( int arr [ ] , int n ) { int sum = - 1000000 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ; return sum ; } </Code>
java-python-EVALUATE_A_BOOLEAN_EXPRESSION_REPRESENTED_AS_STRING
java-python
static int evaluateBoolExpr ( StringBuffer s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i += 2 ) { if ( i + 1 < n && i + 2 < n ) { if ( s . charAt ( i + 1 ) == ' A ' ) { if ( s . charAt ( i + 2 ) == ' 0 ' || s . charAt ( i ) == 0 ) s . setCharAt ( i + 2 , ' 0 ' ) ; else s ....
def evaluateBoolExpr ( s ) : n = len ( s ) for i in range ( 0 , n - 2 , 2 ) : if ( s [ i + 1 ] == "A" ) : if ( s [ i + 2 ] == "0" or s [ i ] == "0" ) : s [ i + 2 ] = "0" else : s [ i + 2 ] = "1" elif ( s [ i + 1 ] == "B" ) : if ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int evaluateBoolExpr ( StringBuffer s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i += 2 ) { if ( i + 1 < n && i + 2 < n ) { if ( s . charAt ( i + 1 ) == ' A ' ) { if ( s . charAt ( i + 2 ) == ' 0 ' || s . charAt ( i ) == 0 ) s . setCharAt ( i + 2 , ' 0 ' ) ; e...
java-python-N_TH_NUMBER_WHOSE_SUM_OF_DIGITS_IS_TEN_2
java-python
public static int findNth ( int n ) { int nthElement = 19 + ( n - 1 ) * 9 ; int outliersCount = ( int ) Math . log10 ( nthElement ) - 1 ; nthElement += 9 * outliersCount ; return nthElement ; }
def findNth ( n ) : nthElement = 19 + ( n - 1 ) * 9 outliersCount = int ( math.log10 ( nthElement ) ) - 1 nthElement += 9 * outliersCount return nthElement
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int findNth ( int n ) { int nthElement = 19 + ( n - 1 ) * 9 ; int outliersCount = ( int ) Math . log10 ( nthElement ) - 1 ; nthElement += 9 * outliersCount ; return nthElement ; } </Code>
java-python-COUNT_ARITHMETIC_PROGRESSION_SUBSEQUENCES_ARRAY
java-python
static int numofAP ( int a [ ] , int n ) { int minarr = + 2147483647 ; int maxarr = - 2147483648 ; for ( int i = 0 ; i < n ; i ++ ) { minarr = Math . min ( minarr , a [ i ] ) ; maxarr = Math . max ( maxarr , a [ i ] ) ; } int dp [ ] = new int [ n ] ; int sum [ ] = new int [ MAX ] ; int ans = n...
def numofAP ( a , n ) : minarr = + 2147483647 maxarr = - 2147483648 for i in range ( n ) : minarr = min ( minarr , a [ i ] ) maxarr = max ( maxarr , a [ i ] ) dp = [ 0 for i in range ( n + 1 ) ] ans = n + 1 for d in range ( ( minarr - maxarr ) , ( maxarr - minarr ) + 1 ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int numofAP ( int a [ ] , int n ) { int minarr = + 2147483647 ; int maxarr = - 2147483648 ; for ( int i = 0 ; i < n ; i ++ ) { minarr = Math . min ( minarr , a [ i ] ) ; maxarr = Math . max ( maxarr , a [ i ] ) ; } int dp [ ] = new int [ n ] ; int sum [ ] = new int [ MAX ] ; int ...
java-python-FIND_NUMBER_OF_SOLUTIONS_OF_A_LINEAR_EQUATION_OF_N_VARIABLES
java-python
static int countSol ( int coeff [ ] , int start , int end , int rhs ) { if ( rhs == 0 ) return 1 ; int result = 0 ; for ( int i = start ; i <= end ; i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ; return result ; }
def countSol ( coeff , start , end , rhs ) : if ( rhs == 0 ) : return 1 result = 0 for i in range ( start , end + 1 ) : if ( coeff [ i ] <= rhs ) : result += countSol ( coeff , i , end , rhs - coeff [ i ] ) return result
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countSol ( int coeff [ ] , int start , int end , int rhs ) { if ( rhs == 0 ) return 1 ; int result = 0 ; for ( int i = start ; i <= end ; i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ; return result ; } </Code>
java-python-MINIMUM_NUMBER_SWAPS_REQUIRED_SORT_ARRAY
java-python
public static int minSwaps ( int [ ] arr ) { int n = arr . length ; ArrayList < Pair < Integer , Integer >> arrpos = new ArrayList < Pair < Integer , Integer >> ( ) ; for ( int i = 0 ; i < n ; i ++ ) arrpos . add ( new Pair < Integer , Integer > ( arr [ i ] , i ) ) ; arrpos . sort ( new Comparator < Pair < ...
def minSwaps ( arr ) : n = len ( arr ) arrpos = [ * enumerate ( arr ) ] arrpos.sort ( key = lambda it : it [ 1 ] ) vis = { k : False for k in range ( n ) } ans = 0 for i in range ( n ) : if vis [ i ] or arrpos [ i ] [ 0 ] == i : continue cycle_size = 0 j = i ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int minSwaps ( int [ ] arr ) { int n = arr . length ; ArrayList < Pair < Integer , Integer >> arrpos = new ArrayList < Pair < Integer , Integer >> ( ) ; for ( int i = 0 ; i < n ; i ++ ) arrpos . add ( new Pair < Integer , Integer > ( arr [ i ] , i ) ) ; arrpos . sort ( new Comparator < ...
java-python-CIRCULAR_MATRIX_CONSTRUCT_A_MATRIX_WITH_NUMBERS_1_TO_MN_IN_SPIRAL_WAY
java-python
static void spiralFill ( int m , int n , int a [ ] [ ] ) { int val = 1 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k ] [ i ] = val ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i ] [ n - 1 ] = val ++ ; } n -- ; ...
def spiralFill ( m , n , a ) : val = 1 k , l = 0 , 0 while ( k < m and l < n ) : for i in range ( l , n ) : a [ k ] [ i ] = val val += 1 k += 1 for i in range ( k , m ) : a [ i ] [ n - 1 ] = val val += 1 n -= 1 if ( k < ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void spiralFill ( int m , int n , int a [ ] [ ] ) { int val = 1 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k ] [ i ] = val ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i ] [ n - 1 ] = val ++ ; } ...
java-python-GIVEN_TWO_SORTED_ARRAYS_NUMBER_X_FIND_PAIR_WHOSE_SUM_CLOSEST_X
java-python
void printClosest ( int ar1 [ ] , int ar2 [ ] , int m , int n , int x ) { int diff = Integer . MAX_VALUE ; int res_l = 0 , res_r = 0 ; int l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( Math . abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . abs ( ar...
def printClosest ( ar1 , ar2 , m , n , x ) : diff = sys.maxsize l = 0 r = n - 1 while ( l < m and r >= 0 ) : if abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff : res_l = l res_r = r diff = abs ( ar1 [ l ] + ar2 [ r ] - x ) if ar1 [ l ] + ar2 [ r ] > x : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> void printClosest ( int ar1 [ ] , int ar2 [ ] , int m , int n , int x ) { int diff = Integer . MAX_VALUE ; int res_l = 0 , res_r = 0 ; int l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( Math . abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . a...
java-python-NUMBER_VISIBLE_BOXES_PUTTING_ONE_INSIDE_ANOTHER
java-python
static int minimumBox ( int [ ] arr , int n ) { Queue < Integer > q = new LinkedList < > ( ) ; Arrays . sort ( arr ) ; q . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int now = q . element ( ) ; if ( arr [ i ] >= 2 * now ) q . remove ( ) ; q . add ( arr [ i ] ) ; } return q . size...
def minimumBox ( arr , n ) : q = collections.deque ( [ ] ) arr.sort ( ) q.append ( arr [ 0 ] ) for i in range ( 1 , n ) : now = q [ 0 ] if ( arr [ i ] >= 2 * now ) : q.popleft ( ) q.append ( arr [ i ] ) return len ( q )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int minimumBox ( int [ ] arr , int n ) { Queue < Integer > q = new LinkedList < > ( ) ; Arrays . sort ( arr ) ; q . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int now = q . element ( ) ; if ( arr [ i ] >= 2 * now ) q . remove ( ) ; q . add ( arr [ i ] ) ; } return q...
java-python-INTEGER_POSITIVE_VALUE_POSITIVE_NEGATIVE_VALUE_ARRAY
java-python
static int findInteger ( int arr [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < > ( ) ; int maximum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) hash . put ( Math . abs ( arr [ i ] ) , ( hash . get ( Math . abs ( arr [ i ] ) ) == null ? 0 : hash . get ( Math . abs ( ar...
def findInteger ( arr , n ) : hash = dict ( ) maximum = 0 for i in arr : if ( i < 0 ) : if abs ( i ) not in hash.keys ( ) : hash [ abs ( i ) ] = - 1 else : hash [ abs ( i ) ] -= 1 else : hash [ i ] = hash.get ( i , 0 ) + 1 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findInteger ( int arr [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < > ( ) ; int maximum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) hash . put ( Math . abs ( arr [ i ] ) , ( hash . get ( Math . abs ( arr [ i ] ) ) == null ? 0 : hash . get ( Math . a...
java-python-CONVERT_ALTERNATE_CHARACTERS_STRING_UPPER_CASE
java-python
static void convertOpposite ( StringBuffer str ) { int ln = str . length ( ) ; for ( int i = 0 ; i < ln ; i ++ ) { Character c = str . charAt ( i ) ; if ( Character . isLowerCase ( c ) ) str . replace ( i , i + 1 , Character . toUpperCase ( c ) + "" ) ; else str . replace ( i , i + 1 , Character . t...
def convertOpposite ( str ) : ln = len ( str ) for i in range ( ln ) : if str [ i ] >= 'a' and str [ i ] <= 'z' : str [ i ] = chr ( ord ( str [ i ] ) - 32 ) elif str [ i ] >= 'A' and str [ i ] <= 'Z' : str [ i ] = chr ( ord ( str [ i ] ) + 32 )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void convertOpposite ( StringBuffer str ) { int ln = str . length ( ) ; for ( int i = 0 ; i < ln ; i ++ ) { Character c = str . charAt ( i ) ; if ( Character . isLowerCase ( c ) ) str . replace ( i , i + 1 , Character . toUpperCase ( c ) + "" ) ; else str . replace ( i , i + 1 , Charac...
java-python-PROGRAM_DECIMAL_BINARY_CONVERSION_2
java-python
static int decimalToBinary ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; double c = Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } return B_Number ; }
def decimalToBinary ( N ) : B_Number = 0 cnt = 0 while ( N != 0 ) : rem = N % 2 c = pow ( 10 , cnt ) B_Number += rem * c N //= 2 cnt += 1 return B_Number
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int decimalToBinary ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; double c = Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } return B_Number ; } </Code>
java-python-FIND_REPETITIVE_ELEMENT_1_N_1_1
java-python
static int findRepeating ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) return arr [ i ] ; s . add ( arr [ i ] ) ; } return - 1 ; }
def findRepeating ( arr , n ) : s = set ( ) for i in range ( n ) : if arr [ i ] in s : return arr [ i ] s.add ( arr [ i ] ) rteurn - 1
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findRepeating ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) return arr [ i ] ; s . add ( arr [ i ] ) ; } return - 1 ; } </Code>
java-python-PROGRAM_TO_FIND_TRANSPOSE_OF_A_MATRIX_2
java-python
static void transpose ( int A [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { int temp = A [ i ] [ j ] ; A [ i ] [ j ] = A [ j ] [ i ] ; A [ j ] [ i ] = temp ; } }
def transpose ( A ) : for i in range ( N ) : for j in range ( i + 1 , N ) : A [ i ] [ j ] , A [ j ] [ i ] = A [ j ] [ i ] , A [ i ] [ j ]
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void transpose ( int A [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { int temp = A [ i ] [ j ] ; A [ i ] [ j ] = A [ j ] [ i ] ; A [ j ] [ i ] = temp ; } } </Code>
java-python-PRINT_POSSIBLE_SUMS_CONSECUTIVE_NUMBERS_SUM_N
java-python
static void findConsecutive ( int N ) { int start = 1 ; int end = ( N + 1 ) / 2 ; while ( start < end ) { int sum = 0 ; for ( int i = start ; i <= end ; i ++ ) { sum = sum + i ; if ( sum == N ) { for ( int j = start ; j <= i ; j ++ ) System . out . print ( j + "...
def findConsecutive ( N ) : start = 1 end = ( N + 1 ) // 2 while ( start < end ) : Sum = 0 for i in range ( start , end + 1 ) : Sum = Sum + i if ( Sum == N ) : for j in range ( start , i + 1 ) : print ( j , end = " " ) ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void findConsecutive ( int N ) { int start = 1 ; int end = ( N + 1 ) / 2 ; while ( start < end ) { int sum = 0 ; for ( int i = start ; i <= end ; i ++ ) { sum = sum + i ; if ( sum == N ) { for ( int j = start ; j <= i ; j ++ ) System . out . print ...
java-python-PRINT_POSSIBLE_STRINGS_CAN_MADE_PLACING_SPACES_2
java-python
static void printSubsequences ( String s ) { char [ ] str = s . toCharArray ( ) ; int n = str . length ; int opsize = ( int ) ( Math . pow ( 2 , n - 1 ) ) ; for ( int counter = 0 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( str [ j ] ) ; ...
def printSubsequences ( str ) : n = len ( str ) opsize = int ( pow ( 2 , n - 1 ) ) for counter in range ( opsize ) : for j in range ( n ) : print ( str [ j ] , end = "" ) if ( counter & ( 1 << j ) ) : print ( "" , end = "" ) print ( "\n" , end = "" )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printSubsequences ( String s ) { char [ ] str = s . toCharArray ( ) ; int n = str . length ; int opsize = ( int ) ( Math . pow ( 2 , n - 1 ) ) ; for ( int counter = 0 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( str [ j ] ...
java-python-MAXIMUM_AREA_QUADRILATERAL
java-python
static double maxArea ( double a , double b , double c , double d ) { double semiperimeter = ( a + b + c + d ) / 2 ; return Math . sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) ; }
def maxArea ( a , b , c , d ) : semiperimeter = ( a + b + c + d ) / 2 return math.sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static double maxArea ( double a , double b , double c , double d ) { double semiperimeter = ( a + b + c + d ) / 2 ; return Math . sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) ; } </Code>
java-python-POSITION_OF_RIGHTMOST_SET_BIT_2
java-python
static int Right_most_setbit ( int num ) { int pos = 1 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { if ( ( num & ( 1 << i ) ) == 0 ) pos ++ ; else break ; } return pos ; }
def Right_most_setbit ( num ) : pos = 1 for i in range ( INT_SIZE ) : if not ( num & ( 1 << i ) ) : pos += 1 else : break return pos
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int Right_most_setbit ( int num ) { int pos = 1 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { if ( ( num & ( 1 << i ) ) == 0 ) pos ++ ; else break ; } return pos ; } </Code>
java-python-CONVERTING_DECIMAL_NUMBER_LYING_BETWEEN_1_TO_3999_TO_ROMAN_NUMERALS
java-python
static void printRoman ( int number ) { char c [ ] = new char [ 10001 ] ; int i = 0 ; if ( number <= 0 ) { System . out . printf ( "Invalid number" ) ; return ; } while ( number != 0 ) { if ( number >= 1000 ) { i = digit ( ' M ' , number / 1000 , i , c ) ; number = number % 1000 ; ...
def printRoman ( number ) : num = [ 1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 ] sym = [ "I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" ] i = 12 while number : div = number // num [ i ] number %= num [ i ] while div : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printRoman ( int number ) { char c [ ] = new char [ 10001 ] ; int i = 0 ; if ( number <= 0 ) { System . out . printf ( "Invalid number" ) ; return ; } while ( number != 0 ) { if ( number >= 1000 ) { i = digit ( ' M ' , number / 1000 , i , c ) ; number = number % 1000...
java-python-DYNAMIC_PROGRAMMING_SUBSET_SUM_PROBLEM
java-python
static boolean isSubsetSum ( int set [ ] , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 && sum != 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; }
def isSubsetSum ( set , n , sum ) : if ( sum == 0 ) : return True if ( n == 0 and sum != 0 ) : return False if ( set [ n - 1 ] > sum ) : return isSubsetSum ( set , n - 1 , sum ) return isSubsetSum ( set , n - 1 , sum ) or isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isSubsetSum ( int set [ ] , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 && sum != 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; } </...
java-python-NUMBER_DIGITS_PRODUCT_TWO_NUMBERS_1
java-python
public static int countDigits ( int a , int b ) { if ( a == 0 || b == 0 ) return 1 ; return ( int ) Math . floor ( Math . log10 ( Math . abs ( a ) ) + Math . log10 ( Math . abs ( b ) ) ) + 1 ; }
def countDigits ( a , b ) : if ( a == 0 or b == 0 ) : return 1 return math.floor ( math.log10 ( abs ( a ) ) + math.log10 ( abs ( b ) ) ) + 1
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int countDigits ( int a , int b ) { if ( a == 0 || b == 0 ) return 1 ; return ( int ) Math . floor ( Math . log10 ( Math . abs ( a ) ) + Math . log10 ( Math . abs ( b ) ) ) + 1 ; } </Code>
java-python-WAYS_TO_WRITE_N_AS_SUM_OF_TWO_OR_MORE_POSITIVE_INTEGERS
java-python
static int countWays ( int n ) { int table [ ] = new int [ n + 1 ] ; Arrays . fill ( table , 0 ) ; table [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) table [ j ] += table [ j - i ] ; return table [ n ] ; }
def CountWays ( n ) : table = [ 0 ] * ( n + 1 ) table [ 0 ] = 1 for i in range ( 1 , n ) : for j in range ( i , n + 1 ) : table [ j ] += table [ j - i ] return table [ n ]
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countWays ( int n ) { int table [ ] = new int [ n + 1 ] ; Arrays . fill ( table , 0 ) ; table [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) table [ j ] += table [ j - i ] ; return table [ n ] ; } </Code>
java-python-SORT_EVEN_NUMBERS_ASCENDING_ORDER_SORT_ODD_NUMBERS_DESCENDING_ORDER_1
java-python
static void twoWaySort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; }
def twoWaySort ( arr , n ) : for i in range ( 0 , n ) : if ( arr [ i ] & 1 ) : arr [ i ] *= - 1 arr.sort ( ) for i in range ( 0 , n ) : if ( arr [ i ] & 1 ) : arr [ i ] *= - 1
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void twoWaySort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; } </Code>
java-python-FINDING_POWER_PRIME_NUMBER_P_N_1
java-python
static int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; }
def PowerOFPINnfactorial ( n , p ) : ans = 0 temp = p while ( temp <= n ) : ans += n / temp temp = temp * p return int ( ans )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } </Code>
java-python-FIND_A_SPECIFIC_PAIR_IN_MATRIX
java-python
static int findMaxValue ( int N , int mat [ ] [ ] ) { int maxValue = Integer . MIN_VALUE ; for ( int a = 0 ; a < N - 1 ; a ++ ) for ( int b = 0 ; b < N - 1 ; b ++ ) for ( int d = a + 1 ; d < N ; d ++ ) for ( int e = b + 1 ; e < N ; e ++ ) if ( maxValue < ( mat [ d ] [ e ] - mat [ a ] [ b ] ) ) maxVa...
def findMaxValue ( mat ) : maxValue = 0 for a in range ( N - 1 ) : for b in range ( N - 1 ) : for d in range ( a + 1 , N ) : for e in range ( b + 1 , N ) : if maxValue < int ( mat [ d ] [ e ] - mat [ a ] [ b ] ) : maxValue = int ( m...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findMaxValue ( int N , int mat [ ] [ ] ) { int maxValue = Integer . MIN_VALUE ; for ( int a = 0 ; a < N - 1 ; a ++ ) for ( int b = 0 ; b < N - 1 ; b ++ ) for ( int d = a + 1 ; d < N ; d ++ ) for ( int e = b + 1 ; e < N ; e ++ ) if ( maxValue < ( mat [ d ] [ e ] - mat [ a ] [ b ] ) ...
java-python-FIND_THREE_ELEMENT_FROM_DIFFERENT_THREE_ARRAYS_SUCH_THAT_THAT_A_B_C_K_1
java-python
static boolean findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) { s . add ( a1 [ i ] ) ; } ArrayList < Integer > al = new ArrayList < > ( s ) ; for ( int i = 0 ; i < n2 ...
def findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) : s = set ( ) for i in range ( n1 ) : s.add ( a1 [ i ] ) for i in range ( n2 ) : for j in range ( n3 ) : if sum - a2 [ i ] - a3 [ j ] in s : return True return False
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) { s . add ( a1 [ i ] ) ; } ArrayList < Integer > al = new ArrayList < > ( s ) ; for ( int i = 0 ; ...
java-python-FIND_THE_NUMBER_OCCURRING_ODD_NUMBER_OF_TIMES
java-python
static int getOddOccurrence ( int arr [ ] , int arr_size ) { int i ; for ( i = 0 ; i < arr_size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr_size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count % 2 != 0 ) return arr [ i ] ; } return - 1 ; }
def getOddOccurrence ( arr , arr_size ) : for i in range ( 0 , arr_size ) : count = 0 for j in range ( 0 , arr_size ) : if arr [ i ] == arr [ j ] : count += 1 if ( count % 2 != 0 ) : return arr [ i ] return - 1
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int getOddOccurrence ( int arr [ ] , int arr_size ) { int i ; for ( i = 0 ; i < arr_size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr_size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count % 2 != 0 ) return arr [ i ] ; } return - 1 ; } </Co...
java-python-PRIME_FACTORS_BIG_NUMBER
java-python
static void factorize ( long n ) { int count = 0 ; while ( ! ( n % 2 > 0 ) ) { n >>= 1 ; count ++ ; } if ( count > 0 ) { System . out . println ( "2" + " " + count ) ; } for ( long i = 3 ; i <= ( long ) Math . sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ...
def factorize ( n ) : count = 0 while ( ( n % 2 > 0 ) == False ) : n >>= 1 count += 1 if ( count > 0 ) : print ( 2 , count ) for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 ) : count = 0 while ( n % i == 0 ) : count += 1 n = int ( n / i ) ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void factorize ( long n ) { int count = 0 ; while ( ! ( n % 2 > 0 ) ) { n >>= 1 ; count ++ ; } if ( count > 0 ) { System . out . println ( "2" + " " + count ) ; } for ( long i = 3 ; i <= ( long ) Math . sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { co...
java-python-SMALLEST_DIFFERENCE_PAIR_VALUES_TWO_UNSORTED_ARRAYS
java-python
static int findSmallestDifference ( int A [ ] , int B [ ] , int m , int n ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; int a = 0 , b = 0 ; int result = Integer . MAX_VALUE ; while ( a < m && b < n ) { if ( Math . abs ( A [ a ] - B [ b ] ) < result ) result = Math . abs ( A [ a ] - B [ b ] ) ; if ( ...
def findSmallestDifference ( A , B , m , n ) : A.sort ( ) B.sort ( ) a = 0 b = 0 result = sys.maxsize while ( a < m and b < n ) : if ( abs ( A [ a ] - B [ b ] ) < result ) : result = abs ( A [ a ] - B [ b ] ) if ( A [ a ] < B [ b ] ) : a += 1 else ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findSmallestDifference ( int A [ ] , int B [ ] , int m , int n ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; int a = 0 , b = 0 ; int result = Integer . MAX_VALUE ; while ( a < m && b < n ) { if ( Math . abs ( A [ a ] - B [ b ] ) < result ) result = Math . abs ( A [ a ] - B [ b ] ) ; ...
java-python-CHECK_GIVEN_STRING_CAN_SPLIT_FOUR_DISTINCT_STRINGS
java-python
public static boolean check ( String s ) { if ( s . length ( ) >= 10 ) return true ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { for ( int k = j + 1 ; k < s . length ( ) ; k ++ ) { String s1 = "" , s2 = "" , s3 = "" , ...
def check ( s ) : if ( len ( s ) >= 10 ) : return True for i in range ( 1 , len ( s ) ) : for j in range ( i + 1 , len ( s ) ) : for k in range ( j + 1 , len ( s ) ) : s1 = s [ 0 : i ] s2 = s [ i : j - i ] s3 = s [ j : k - j ] ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static boolean check ( String s ) { if ( s . length ( ) >= 10 ) return true ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { for ( int k = j + 1 ; k < s . length ( ) ; k ++ ) { String s1 = "" , s2 = "" , s3 ...
java-python-CHECK_POSSIBLE_PATH_2D_MATRIX
java-python
static boolean isPath ( int arr [ ] [ ] ) { arr [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < 5 ; i ++ ) if ( arr [ 0 ] [ i ] != - 1 ) arr [ 0 ] [ i ] = arr [ 0 ] [ i - 1 ] ; for ( int j = 1 ; j < 5 ; j ++ ) if ( arr [ j ] [ 0 ] != - 1 ) arr [ j ] [ 0 ] = arr [ j - 1 ] [ 0 ] ; for ( int i = 1 ; i < 5 ; i ...
def isPath ( arr ) : arr [ 0 ] [ 0 ] = 1 for i in range ( 1 , row ) : if ( arr [ i ] [ 0 ] != - 1 ) : arr [ i ] [ 0 ] = arr [ i - 1 ] [ 0 ] for j in range ( 1 , col ) : if ( arr [ 0 ] [ j ] != - 1 ) : arr [ 0 ] [ j ] = arr [ 0 ] [ j - 1 ] for i in range ( 1 , row ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isPath ( int arr [ ] [ ] ) { arr [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < 5 ; i ++ ) if ( arr [ 0 ] [ i ] != - 1 ) arr [ 0 ] [ i ] = arr [ 0 ] [ i - 1 ] ; for ( int j = 1 ; j < 5 ; j ++ ) if ( arr [ j ] [ 0 ] != - 1 ) arr [ j ] [ 0 ] = arr [ j - 1 ] [ 0 ] ; for ( int i = 1 ; i < 5...
java-python-SORT_EVEN_PLACED_ELEMENTS_INCREASING_ODD_PLACED_DECREASING_ORDER_1
java-python
static void bitonicGenerator ( int arr [ ] , int n ) { int i = 1 ; int j = n - 1 ; if ( j % 2 != 0 ) j -- ; while ( i < j ) { arr = swap ( arr , i , j ) ; i += 2 ; j -= 2 ; } Arrays . sort ( arr , 0 , ( n + 1 ) / 2 ) ; Arrays . sort ( arr , ( n + 1 ) / 2 , n ) ; int low = ( n + 1 ) / 2 , hig...
def bitonicGenerator ( arr , n ) : i = 1 j = n - 1 if ( j % 2 != 0 ) : j = j - 1 while ( i < j ) : arr [ j ] , arr [ i ] = arr [ i ] , arr [ j ] i = i + 2 j = j - 2 arr_f = [ ] arr_s = [ ] for i in range ( int ( ( n + 1 ) / 2 ) ) : arr_f.append ( arr [...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void bitonicGenerator ( int arr [ ] , int n ) { int i = 1 ; int j = n - 1 ; if ( j % 2 != 0 ) j -- ; while ( i < j ) { arr = swap ( arr , i , j ) ; i += 2 ; j -= 2 ; } Arrays . sort ( arr , 0 , ( n + 1 ) / 2 ) ; Arrays . sort ( arr , ( n + 1 ) / 2 , n ) ; int low = ( n + 1 ) / ...
java-python-REMOVE_ARRAY_END_ELEMENT_MAXIMIZE_SUM_PRODUCT
java-python
static int solve ( int dp [ ] [ ] , int a [ ] , int low , int high , int turn ) { if ( low == high ) { return a [ low ] * turn ; } if ( dp [ low ] [ high ] != 0 ) { return dp [ low ] [ high ] ; } dp [ low ] [ high ] = Math . max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ h...
def solve ( dp , a , low , high , turn ) : if ( low == high ) : return a [ low ] * turn if ( dp [ low ] [ high ] != 0 ) : return dp [ low ] [ high ] dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low , high -...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int solve ( int dp [ ] [ ] , int a [ ] , int low , int high , int turn ) { if ( low == high ) { return a [ low ] * turn ; } if ( dp [ low ] [ high ] != 0 ) { return dp [ low ] [ high ] ; } dp [ low ] [ high ] = Math . max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) ...
java-python-DIRECTION_LAST_SQUARE_BLOCK
java-python
static void direction ( int R , int C ) { if ( R != C && R % 2 == 0 && C % 2 != 0 && R < C ) { System . out . println ( "Left" ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R > C ) { System . out . println ( "Up" ) ; return ; } if ( R == C && R % 2 != 0 && C % 2 != 0 ) { System...
def direction ( R , C ) : if ( R != C and R % 2 == 0 and C % 2 != 0 and R < C ) : print ( "Left" ) return if ( R != C and R % 2 == 0 and C % 2 == 0 and R > C ) : print ( "Up" ) return if R == C and R % 2 != 0 and C % 2 != 0 : print ( "Right" ) return if R ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void direction ( int R , int C ) { if ( R != C && R % 2 == 0 && C % 2 != 0 && R < C ) { System . out . println ( "Left" ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R > C ) { System . out . println ( "Up" ) ; return ; } if ( R == C && R % 2 != 0 && C % 2 != 0 ) { ...
java-python-SPLIT_NUMERIC_ALPHABETIC_AND_SPECIAL_SYMBOLS_FROM_A_STRING
java-python
static void splitString ( String str ) { StringBuffer alpha = new StringBuffer ( ) , num = new StringBuffer ( ) , special = new StringBuffer ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isDigit ( str . charAt ( i ) ) ) num . append ( str . charAt ( i ) ) ; else if ( Character...
def splitString ( str ) : alpha = "" num = "" special = "" for i in range ( len ( str ) ) : if ( str [ i ].isdigit ( ) ) : num = num + str [ i ] elif ( ( str [ i ] >= 'A' and str [ i ] <= 'Z' ) or ( str [ i ] >= 'a' and str [ i ] <= 'z' ) ) : alpha += str [ i ] ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void splitString ( String str ) { StringBuffer alpha = new StringBuffer ( ) , num = new StringBuffer ( ) , special = new StringBuffer ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isDigit ( str . charAt ( i ) ) ) num . append ( str . charAt ( i ) ) ; else if ( Ch...
java-python-FIND_MEDIAN_ROW_WISE_SORTED_MATRIX
java-python
static int binaryMedian ( int m [ ] [ ] , int r , int c ) { int max = Integer . MIN_VALUE ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < r ; i ++ ) { if ( m [ i ] [ 0 ] < min ) min = m [ i ] [ 0 ] ; if ( m [ i ] [ c - 1 ] > max ) max = m [ i ] [ c - 1 ] ; } int desired = ( r * c + 1 ) / 2...
def binaryMedian ( m , r , d ) : mi = m [ 0 ] [ 0 ] mx = 0 for i in range ( r ) : if m [ i ] [ 0 ] < mi : mi = m [ i ] [ 0 ] if m [ i ] [ d - 1 ] > mx : mx = m [ i ] [ d - 1 ] desired = ( r * d + 1 ) // 2 while ( mi < mx ) : mid = mi + ( mx - mi ) // 2...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int binaryMedian ( int m [ ] [ ] , int r , int c ) { int max = Integer . MIN_VALUE ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < r ; i ++ ) { if ( m [ i ] [ 0 ] < min ) min = m [ i ] [ 0 ] ; if ( m [ i ] [ c - 1 ] > max ) max = m [ i ] [ c - 1 ] ; } int desired = ( r * c + ...
java-python-FIND_DUPLICATES_GIVEN_ARRAY_ELEMENTS_NOT_LIMITED_RANGE
java-python
private static void printDuplicates ( int [ ] arr , int n ) { Map < Integer , Integer > map = new HashMap < > ( ) ; int count = 0 ; boolean dup = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( arr [ i ] ) ) { count = map . get ( arr [ i ] ) ; map . put ( arr [ i ] , coun...
def printDuplicates ( arr ) : dict = { } for ele in arr : try : dict [ ele ] += 1 except : dict [ ele ] = 1 for item in dict : if ( dict [ item ] > 1 ) : print ( item , end = " " ) print ( "\n" )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> private static void printDuplicates ( int [ ] arr , int n ) { Map < Integer , Integer > map = new HashMap < > ( ) ; int count = 0 ; boolean dup = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( arr [ i ] ) ) { count = map . get ( arr [ i ] ) ; map . put ( arr [ i ]...
java-python-PRINT_FIBONACCI_SEQUENCE_USING_2_VARIABLES
java-python
static void fib ( int n ) { int a = 0 , b = 1 , c ; if ( n >= 0 ) System . out . print ( a + " " ) ; if ( n >= 1 ) System . out . print ( b + " " ) ; for ( int i = 2 ; i <= n ; i ++ ) { c = a + b ; System . out . print ( c + " " ) ; a = b ; b = c ; } }
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 ) : c = a + b print ( c , end = ' ' ) a = b b = c
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void fib ( int n ) { int a = 0 , b = 1 , c ; if ( n >= 0 ) System . out . print ( a + " " ) ; if ( n >= 1 ) System . out . print ( b + " " ) ; for ( int i = 2 ; i <= n ; i ++ ) { c = a + b ; System . out . print ( c + " " ) ; a = b ; b = c ; } } </Code>
java-python-SWAP_TWO_NIBBLES_BYTE
java-python
static int swapNibbles ( int x ) { return ( ( x & 0x0F ) << 4
def swapNibbles ( x ) : return ( ( x & 0x0F ) << 4
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int swapNibbles ( int x ) { return ( ( x & 0x0F ) << 4 </Code>
java-python-COUNT_PALINDROMIC_SUBSEQUENCE_GIVEN_STRING_1
java-python
static int countPS ( int i , int j ) { if ( i >= n || j < 0 ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; if ( ( i - j == 1 ) || ( i - j == - 1 ) ) { if ( str . charAt ( i ) == str . charAt ( j ) ) return dp [ i ] [ j ] = 3 ; else return dp [ i ] [ j ] = 2 ; } if ( i == j ) return ...
def countPS ( i , j ) : if ( i >= n or j < 0 ) : return 0 if ( dp [ i ] [ j ] != - 1 ) : return dp [ i ] [ j ] if ( abs ( i - j ) == 1 ) : if ( str [ i ] == str [ j ] ) : dp [ i ] [ j ] = 3 return dp [ i ] [ j ] else : dp [ i ] [ j ] = 2 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countPS ( int i , int j ) { if ( i >= n || j < 0 ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; if ( ( i - j == 1 ) || ( i - j == - 1 ) ) { if ( str . charAt ( i ) == str . charAt ( j ) ) return dp [ i ] [ j ] = 3 ; else return dp [ i ] [ j ] = 2 ; } if ( i == j ) ...
java-python-SUM_DIVISORS_1_N_1
java-python
static int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) sum += ( n / i ) * i ; return sum ; }
def divisorSum ( n ) : sum = 0 for i in range ( 1 , n + 1 ) : sum += int ( n / i ) * i return int ( sum )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) sum += ( n / i ) * i ; return sum ; } </Code>
java-python-ADD_1_TO_A_GIVEN_NUMBER
java-python
static int addOne ( int x ) { int m = 1 ; while ( ( int ) ( x & m ) >= 1 ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; }
def addOne ( x ) : m = 1 while ( x & m ) : x = x ^ m m <<= 1 x = x ^ m return x
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int addOne ( int x ) { int m = 1 ; while ( ( int ) ( x & m ) >= 1 ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; } </Code>
java-python-CHECK_LARGE_NUMBER_DIVISIBLE_9_NOT
java-python
static boolean check ( String str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - ' 0 ' ) ; return ( digitSum % 9 == 0 ) ; }
def check ( st ) : n = len ( st ) digitSum = 0 for i in range ( 0 , n ) : digitSum = digitSum + ( int ) ( st [ i ] ) return ( digitSum % 9 == 0 )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean check ( String str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - ' 0 ' ) ; return ( digitSum % 9 == 0 ) ; } </Code>
java-python-FIND_PAIR_MAXIMUM_GCD_ARRAY
java-python
public static int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . max ( high , arr [ i ] ) ; int divisors [ ] = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( ...
def findMaxGCD ( arr , n ) : high = 0 i = 0 while i < n : high = max ( high , arr [ i ] ) i = i + 1 divisors = [ 0 ] * ( high + 1 ) i = 0 while i < n : j = 1 while j <= math.sqrt ( arr [ i ] ) : if ( arr [ i ] % j == 0 ) : divisors [ j ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . max ( high , arr [ i ] ) ; int divisors [ ] = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= Math . sqrt ( arr [ i ] ) ; j ++ ) { ...
java-python-CONVERT_STRICTLY_INCREASING_ARRAY_MINIMUM_CHANGES
java-python
static int minRemove ( int arr [ ] , int n ) { int LIS [ ] = new int [ n ] ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) LIS [...
def minRemove ( arr , n ) : LIS = [ 0 for i in range ( n ) ] len = 0 for i in range ( n ) : LIS [ i ] = 1 for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) : LIS [ i ] = max ( LIS [ i ] , ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int minRemove ( int arr [ ] , int n ) { int LIS [ ] = new int [ n ] ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ...
java-python-SEARCHING_FOR_PATTERNS_SET_2_KMP_ALGORITHM
java-python
void computeLPSArray ( String pat , int M , int lps [ ] ) { int len = 0 ; int i = 1 ; lps [ 0 ] = 0 ; while ( i < M ) { if ( pat . charAt ( i ) == pat . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } ...
def computeLPSArray ( pat , M , lps ) : len = 0 lps [ 0 ] i = 1 while i < M : if pat [ i ] == pat [ len ] : len += 1 lps [ i ] = len i += 1 else : if len != 0 : len = lps [ len - 1 ] else : lps [ ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> void computeLPSArray ( String pat , int M , int lps [ ] ) { int len = 0 ; int i = 1 ; lps [ 0 ] = 0 ; while ( i < M ) { if ( pat . charAt ( i ) == pat . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; ...
java-python-COUNT_MINIMUM_NUMBER_SUBSETS_SUBSEQUENCES_CONSECUTIVE_NUMBERS
java-python
static int numofsubset ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; } return count ; }
def numofsubset ( arr , n ) : x = sorted ( arr ) count = 1 for i in range ( 0 , n - 1 ) : if ( x [ i ] + 1 != x [ i + 1 ] ) : count = count + 1 return count
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int numofsubset ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; } return count ; } </Code>
java-python-STOOGE_SORT
java-python
static void stoogesort ( int arr [ ] , int l , int h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { int t = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = t ; } if ( h - l + 1 > 2 ) { int t = ( h - l + 1 ) / 3 ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ...
def stoogesort ( arr , l , h ) : if l >= h : return if arr [ l ] > arr [ h ] : t = arr [ l ] arr [ l ] = arr [ h ] arr [ h ] = t if h - l + 1 > 2 : t = ( int ) ( ( h - l + 1 ) / 3 ) stoogesort ( arr , l , ( h - t ) ) stoogesort ( arr , l + t , ( h ) ) ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void stoogesort ( int arr [ ] , int l , int h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { int t = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = t ; } if ( h - l + 1 > 2 ) { int t = ( h - l + 1 ) / 3 ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t...
java-python-FIND_ARRANGEMENT_QUEUE_GIVEN_TIME
java-python
static void solve ( int n , int t , char s [ ] ) { for ( int i = 0 ; i < t ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) if ( s [ j ] == ' B ' && s [ j + 1 ] == ' G ' ) { char temp = s [ j ] ; s [ j ] = s [ j + 1 ] ; s [ j + 1 ] = temp ; j ++ ; } System . out . print ( s ) ; }
def solve ( n , t , p ) : s = list ( p ) for i in range ( 0 , t ) : for j in range ( 0 , n - 1 ) : if ( s [ j ] == 'B' and s [ j + 1 ] == 'G' ) : temp = s [ j ] s [ j ] = s [ j + 1 ] s [ j + 1 ] = temp j = j + 1 print ( ''.j...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void solve ( int n , int t , char s [ ] ) { for ( int i = 0 ; i < t ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) if ( s [ j ] == ' B ' && s [ j + 1 ] == ' G ' ) { char temp = s [ j ] ; s [ j ] = s [ j + 1 ] ; s [ j + 1 ] = temp ; j ++ ; } System . out . print ( s ) ; } </Co...
java-python-HOW_TO_TURN_OFF_A_PARTICULAR_BIT_IN_A_NUMBER
java-python
static int turnOffK ( int n , int k ) { if ( k <= 0 ) return n ; return ( n & ~ ( 1 << ( k - 1 ) ) ) ; }
def turnOffK ( n , k ) : if ( k <= 0 ) : return n return ( n & ~ ( 1 << ( k - 1 ) ) )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int turnOffK ( int n , int k ) { if ( k <= 0 ) return n ; return ( n & ~ ( 1 << ( k - 1 ) ) ) ; } </Code>
java-python-FINDING_K_MODULUS_ARRAY_ELEMENT
java-python
static void printEqualModNumbers ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int d = arr [ n - 1 ] - arr [ 0 ] ; Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i * i <= d ; i ++ ) { if ( d % i == 0 ) { v . add ( i ) ; if ( i != d / i ) v . add ( d / i ) ; } } for...
def printEqualModNumbers ( arr , n ) : arr.sort ( ) d = arr [ n - 1 ] - arr [ 0 ] v = [ ] i = 1 while ( i * i <= d ) : if ( d % i == 0 ) : v.append ( i ) if ( i != d / i ) : v.append ( d / i ) i += 1 for i in range ( len ( v ) ) : t...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printEqualModNumbers ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int d = arr [ n - 1 ] - arr [ 0 ] ; Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i * i <= d ; i ++ ) { if ( d % i == 0 ) { v . add ( i ) ; if ( i != d / i ) v . add ( d / i ) ; } ...
java-python-FIND_THE_MAXIMUM_ELEMENT_IN_AN_ARRAY_WHICH_IS_FIRST_INCREASING_AND_THEN_DECREASING_1
java-python
static int findMaximum ( int arr [ ] , int low , int high ) { if ( low == high ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] > ...
def findMaximum ( arr , low , high ) : if low == high : return arr [ low ] if high == low + 1 and arr [ low ] >= arr [ high ] : return arr [ low ] if high == low + 1 and arr [ low ] < arr [ high ] : return arr [ high ] mid = ( low + high ) // 2 if arr [ mid ] > arr [ mid + 1 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findMaximum ( int arr [ ] , int low , int high ) { if ( low == high ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ; int mid = ( low + high ) / 2 ; if ( arr [ m...
java-python-SMALLEST_LENGTH_STRING_WITH_REPEATED_REPLACEMENT_OF_TWO_DISTINCT_ADJACENT
java-python
static int stringReduction ( String str ) { int n = str . length ( ) ; int count [ ] = new int [ 3 ] ; for ( int i = 0 ; i < n ; ++ i ) { count [ str . charAt ( i ) - ' a ' ] ++ ; } if ( count [ 0 ] == n || count [ 1 ] == n || count [ 2 ] == n ) { return n ; } if ( ( count [ 0 ] % 2 ) == ( cou...
def stringReduction ( str ) : n = len ( str ) count = [ 0 ] * 3 for i in range ( n ) : count [ ord ( str [ i ] ) - ord ( 'a' ) ] += 1 if ( count [ 0 ] == n or count [ 1 ] == n or count [ 2 ] == n ) : return n if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) and ( count [ 1 ] % 2 ) == ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int stringReduction ( String str ) { int n = str . length ( ) ; int count [ ] = new int [ 3 ] ; for ( int i = 0 ; i < n ; ++ i ) { count [ str . charAt ( i ) - ' a ' ] ++ ; } if ( count [ 0 ] == n || count [ 1 ] == n || count [ 2 ] == n ) { return n ; } if ( ( count [ 0 ] % 2 ) =...
java-python-SPACE_OPTIMIZED_DP_SOLUTION_0_1_KNAPSACK_PROBLEM_1
java-python
static int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int [ ] dp = new int [ W + 1 ] ; Arrays . fill ( dp , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = W ; j >= wt [ i ] ; j -- ) dp [ j ] = Math . max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; return dp [ W ] ; }
def KnapSack ( val , wt , n , W ) : dp = [ 0 ] * ( W + 1 ) for i in range ( n ) : for j in range ( W , wt [ i ] , - 1 ) : dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) return dp [ W ]
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int [ ] dp = new int [ W + 1 ] ; Arrays . fill ( dp , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = W ; j >= wt [ i ] ; j -- ) dp [ j ] = Math . max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; return dp [ W ] ; } </Co...
java-python-SUM_PAIRWISE_PRODUCTS_1
java-python
static int findSum ( int n ) { int multiTerms = n * ( n + 1 ) / 2 ; int sum = multiTerms ; for ( int i = 2 ; i <= n ; i ++ ) { multiTerms = multiTerms - ( i - 1 ) ; sum = sum + multiTerms * i ; } return sum ; }
def findSum ( n ) : multiTerms = n * ( n + 1 ) // 2 sm = multiTerms for i in range ( 2 , n + 1 ) : multiTerms = multiTerms - ( i - 1 ) sm = sm + multiTerms * i return sm
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findSum ( int n ) { int multiTerms = n * ( n + 1 ) / 2 ; int sum = multiTerms ; for ( int i = 2 ; i <= n ; i ++ ) { multiTerms = multiTerms - ( i - 1 ) ; sum = sum + multiTerms * i ; } return sum ; } </Code>
java-python-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE
java-python
public static String recursiveReverse ( char [ ] str ) { Stack < Character > st = new Stack < > ( ) ; for ( int i = 0 ; i < str . length ; i ++ ) st . push ( str [ i ] ) ; for ( int i = 0 ; i < str . length ; i ++ ) { str [ i ] = st . peek ( ) ; st . pop ( ) ; } return String . valueOf ( str )...
def recursiveReverse ( str ) : stack = [ ] for i in range ( len ( str ) ) : stack.append ( str [ i ] ) for i in range ( len ( str ) ) : str [ i ] = stack.pop ( )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static String recursiveReverse ( char [ ] str ) { Stack < Character > st = new Stack < > ( ) ; for ( int i = 0 ; i < str . length ; i ++ ) st . push ( str [ i ] ) ; for ( int i = 0 ; i < str . length ; i ++ ) { str [ i ] = st . peek ( ) ; st . pop ( ) ; } return String . valueOf ...
java-python-FIND_SUBARRAY_WITH_GIVEN_SUM
java-python
int subArraySum ( int arr [ ] , int n , int sum ) { int curr_sum , i , j ; for ( i = 0 ; i < n ; i ++ ) { curr_sum = arr [ i ] ; for ( j = i + 1 ; j <= n ; j ++ ) { if ( curr_sum == sum ) { int p = j - 1 ; System . out . println ( "Sum found between indexes " + i + " and " ...
def subArraySum ( arr , n , sum ) : for i in range ( n ) : curr_sum = arr [ i ] j = i + 1 while j <= n : if curr_sum == sum : print ( "Sum found between" ) print ( "indexes %d and %d" % ( i , j - 1 ) ) return 1 if curr_s...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> int subArraySum ( int arr [ ] , int n , int sum ) { int curr_sum , i , j ; for ( i = 0 ; i < n ; i ++ ) { curr_sum = arr [ i ] ; for ( j = i + 1 ; j <= n ; j ++ ) { if ( curr_sum == sum ) { int p = j - 1 ; System . out . println ( "Sum found between indexes " + i + "...
java-python-FIND_THE_ELEMENT_THAT_APPEARS_ONCE_1
java-python
static int getSingle ( int arr [ ] , int n ) { int result = 0 ; int x , sum ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & x ) == 0 ) sum ++ ; } if ( ( sum % 3 ) == 0 ) result |= x ; } return re...
def getSingle ( arr , n ) : result = 0 for i in range ( 0 , INT_SIZE ) : sm = 0 x = ( 1 << i ) for j in range ( 0 , n ) : if ( arr [ j ] & x ) : sm = sm + 1 if ( sm % 3 ) : result = result
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int getSingle ( int arr [ ] , int n ) { int result = 0 ; int x , sum ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & x ) == 0 ) sum ++ ; } if ( ( sum % 3 ) == 0 ) result |= x ; } re...