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-HOSOYAS_TRIANGLE | java-python | static void printHosoya ( int n ) {
int dp [ ] [ ] = new int [ N ] [ N ] ;
dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1 ;
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 ] ;
else dp ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void printHosoya ( int n ) {
int dp [ ] [ ] = new int [ N ] [ N ] ;
dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1 ;
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... |
java-python-LARGEST_SUBARRAY_WITH_EQUAL_NUMBER_OF_0S_AND_1S | java-python | int findSubArray ( int arr [ ] , int n ) {
int sum = 0 ;
int maxsize = - 1 , startindex = 0 ;
int endindex = 0 ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
sum = ( arr [ i ] == 0 ) ? - 1 : 1 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( arr [ j ] == 0 ) sum += - 1 ;
else sum += 1 ;
... | 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 java 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 = 0 ;
int endindex = 0 ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
sum = ( arr [ i ] == 0 ) ? - 1 : 1 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( arr [ j ] == 0 ) sum += - 1 ;
else sum +... |
java-python-RECURSIVE_INSERTION_SORT | java-python | static 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static 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> |
java-python-DIFFERENCE_BETWEEN_HIGHEST_AND_LEAST_FREQUENCIES_IN_AN_ARRAY_1 | java-python | static int findDiff ( int arr [ ] , int n ) {
Map < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( mp . containsKey ( arr [ i ] ) ) {
mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ;
}
else {
mp . put ( arr [ i ] , 1 ) ;
}
}
int max_co... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int findDiff ( int arr [ ] , int n ) {
Map < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( mp . containsKey ( arr [ i ] ) ) {
mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ;
}
else {
mp . put ( arr [ i ] , 1 ) ;
}
}
int... |
java-python-GAME_REPLACING_ARRAY_ELEMENTS | java-python | public static int playGame ( int arr [ ] ) {
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i : arr ) set . add ( i ) ;
return ( set . 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static int playGame ( int arr [ ] ) {
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i : arr ) set . add ( i ) ;
return ( set . size ( ) % 2 == 0 ) ? 1 : 2 ;
}
</Code> |
java-python-MINIMUM_NUMBER_SUBSETS_DISTINCT_ELEMENTS_1 | java-python | static int subset ( int arr [ ] , int n ) {
HashMap < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ;
int res = 0 ;
for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) r... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int subset ( int arr [ ] , int n ) {
HashMap < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ;
int res = 0 ;
for ( Map . Entry < Integer , Integer > entry : mp . entrySet ... |
java-python-PAIR_WITH_GIVEN_PRODUCT_SET_1_FIND_IF_ANY_PAIR_EXISTS_1 | java-python | static boolean isProduct ( int arr [ ] , int n , int x ) {
HashSet < Integer > hset = new HashSet < > ( ) ;
if ( n < 2 ) return false ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == 0 ) {
if ( x == 0 ) return true ;
else continue ;
}
if ( x % arr [ i ] == 0 ) {
if ( hset .... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isProduct ( int arr [ ] , int n , int x ) {
HashSet < Integer > hset = new HashSet < > ( ) ;
if ( n < 2 ) return false ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == 0 ) {
if ( x == 0 ) return true ;
else continue ;
}
if ( x % arr [ i ] == 0 ) {
if (... |
java-python-MAXIMUM_AVERAGE_SUM_PARTITION_ARRAY | java-python | static double largestSumOfAverages ( int [ ] A , int K ) {
int n = A . length ;
double [ ] pre_sum = new double [ n + 1 ] ;
pre_sum [ 0 ] = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ;
double [ ] dp = new double [ n ] ;
double sum = 0 ;
for ( int i = 0 ;
i < n ;... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static double largestSumOfAverages ( int [ ] A , int K ) {
int n = A . length ;
double [ ] pre_sum = new double [ n + 1 ] ;
pre_sum [ 0 ] = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ;
double [ ] dp = new double [ n ] ;
double sum = 0 ;
for ( int i = 0 ;
... |
java-python-MINIMUM_STEPS_MINIMIZE_N_PER_GIVEN_CONDITION | java-python | static int getMinSteps ( int n ) {
int table [ ] = new int [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) table [ i ] = n - i ;
for ( int i = n ;
i >= 1 ;
i -- ) {
if ( ! ( i % 2 > 0 ) ) table [ i / 2 ] = Math . min ( table [ i ] + 1 , table [ i / 2 ] ) ;
if ( ! ( i % 3 > 0 ) ) table [ i / 3 ] = Ma... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int getMinSteps ( int n ) {
int table [ ] = new int [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) table [ i ] = n - i ;
for ( int i = n ;
i >= 1 ;
i -- ) {
if ( ! ( i % 2 > 0 ) ) table [ i / 2 ] = Math . min ( table [ i ] + 1 , table [ i / 2 ] ) ;
if ( ! ( i % 3 > 0 ) ) table [ i / 3... |
java-python-N_TH_TERM_SERIES_2_12_36_80_150 | java-python | public static int nthTerm ( int n ) {
return ( n * n ) + ( n * n * n ) ;
}
| def nthTerm ( n ) :
return ( n * n ) + ( n * n * n )
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static int nthTerm ( int n ) {
return ( n * n ) + ( n * n * n ) ;
}
</Code> |
java-python-FIND_MAXIMUM_AVERAGE_SUBARRAY_OF_K_LENGTH_1 | java-python | static 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 ++ ) {
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static 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 ++ ) {
sum = sum + arr [ i ] - arr [ i - k ] ;
if ( sum > max... |
java-python-DYNAMIC_PROGRAMMING_SET_11_EGG_DROPPING_PUZZLE | java-python | static int eggDrop ( int n , int k ) {
if ( k == 1 || k == 0 ) return k ;
if ( n == 1 ) return k ;
int min = Integer . MAX_VALUE ;
int x , res ;
for ( x = 1 ;
x <= k ;
x ++ ) {
res = Math . max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ;
if ( res < min ) min = res ;
}
return min + ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int eggDrop ( int n , int k ) {
if ( k == 1 || k == 0 ) return k ;
if ( n == 1 ) return k ;
int min = Integer . MAX_VALUE ;
int x , res ;
for ( x = 1 ;
x <= k ;
x ++ ) {
res = Math . max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ;
if ( res < min ) min = res ;
}
return... |
java-python-PROGRAM_COUNT_OCCURRENCE_GIVEN_CHARACTER_STRING | java-python | public static int count ( String s , char c ) {
int res = 0 ;
for ( int i = 0 ;
i < s . length ( ) ;
i ++ ) {
if ( s . charAt ( 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static int count ( String s , char c ) {
int res = 0 ;
for ( int i = 0 ;
i < s . length ( ) ;
i ++ ) {
if ( s . charAt ( i ) == c ) res ++ ;
}
return res ;
}
</Code> |
java-python-MINIMUM_ROTATIONS_REQUIRED_GET_STRING | java-python | static int findRotations ( String str ) {
String tmp = str + str ;
int n = str . length ( ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
String substring = tmp . substring ( i , str . length ( ) ) ;
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int findRotations ( String str ) {
String tmp = str + str ;
int n = str . length ( ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
String substring = tmp . substring ( i , str . length ( ) ) ;
if ( str == substring ) return i ;
}
return n ;
}
</Code> |
java-python-SHUFFLE_A_GIVEN_ARRAY | java-python | static void randomize ( int arr [ ] , int n ) {
Random r = new Random ( ) ;
for ( int i = n - 1 ;
i > 0 ;
i -- ) {
int j = r . nextInt ( i + 1 ) ;
int temp = arr [ i ] ;
arr [ i ] = arr [ j ] ;
arr [ j ] = temp ;
}
System . out . println ( Arrays . toString ( arr ) ) ;
}
| 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void randomize ( int arr [ ] , int n ) {
Random r = new Random ( ) ;
for ( int i = n - 1 ;
i > 0 ;
i -- ) {
int j = r . nextInt ( i + 1 ) ;
int temp = arr [ i ] ;
arr [ i ] = arr [ j ] ;
arr [ j ] = temp ;
}
System . out . println ( Arrays . toString ( arr ) ) ;
}
</Code> |
java-python-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_2 | java-python | static int findLength ( String str , int n ) {
int sum [ ] = new int [ n + 1 ] ;
sum [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum [ i ] = ( sum [ i - 1 ] + str . charAt ( i - 1 ) - ' 0 ' ) ;
int ans = 0 ;
for ( int len = 2 ;
len <= n ;
len += 2 ) {
for ( int i = 0 ;
i <= n - len ;
i +... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int findLength ( String str , int n ) {
int sum [ ] = new int [ n + 1 ] ;
sum [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum [ i ] = ( sum [ i - 1 ] + str . charAt ( i - 1 ) - ' 0 ' ) ;
int ans = 0 ;
for ( int len = 2 ;
len <= n ;
len += 2 ) {
for ( int i = 0 ;
i <= n - len ;
... |
java-python-REPLACE_OCCURRENCES_STRING_AB_C_WITHOUT_USING_EXTRA_SPACE_1 | java-python | static void translate ( char str [ ] ) {
int len = str . length ;
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 (... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void translate ( char str [ ] ) {
int len = str . length ;
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 ++ ] ;
}... |
java-python-NUMBER_WHICH_HAS_THE_MAXIMUM_NUMBER_OF_DISTINCT_PRIME_FACTORS_IN_RANGE_M_TO_N | java-python | static int maximumNumberDistinctPrimeRange ( int m , int n ) {
long factorCount [ ] = new long [ n + 1 ] ;
boolean prime [ ] = new boolean [ 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 ] == ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int maximumNumberDistinctPrimeRange ( int m , int n ) {
long factorCount [ ] = new long [ n + 1 ] ;
boolean prime [ ] = new boolean [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
factorCount [ i ] = 0 ;
prime [ i ] = true ;
}
for ( int i = 2 ;
i <= n ;
i ++ ) {
if ( prime [ ... |
java-python-PROGRAM_NTH_CATALAN_NUMBER | java-python | int catalan ( int n ) {
int res = 0 ;
if ( n <= 1 ) {
return 1 ;
}
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
int catalan ( int n ) {
int res = 0 ;
if ( n <= 1 ) {
return 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
res += catalan ( i ) * catalan ( n - i - 1 ) ;
}
return res ;
}
</Code> |
java-python-SUM_SERIES_12_32_52_2N_12 | java-python | static 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static 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> |
java-python-MINIMUM_REVOLUTIONS_MOVE_CENTER_CIRCLE_TARGET | java-python | static double minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) {
double d = Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ;
return Math . 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static double minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) {
double d = Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ;
return Math . ceil ( d / ( 2 * r ) ) ;
}
</Code> |
java-python-PRINT_STRING_SPECIFIED_CHARACTER_OCCURRED_GIVEN_NO_TIMES | java-python | static void printString ( String str , char ch , int count ) {
int occ = 0 , i ;
if ( count == 0 ) {
System . out . println ( str ) ;
return ;
}
for ( i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str . charAt ( i ) == ch ) occ ++ ;
if ( occ == count ) break ;
}
if ( i < str . length ( ) ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void printString ( String str , char ch , int count ) {
int occ = 0 , i ;
if ( count == 0 ) {
System . out . println ( str ) ;
return ;
}
for ( i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str . charAt ( i ) == ch ) occ ++ ;
if ( occ == count ) break ;
}
if ( i < str . leng... |
java-python-FINDING_THE_MAXIMUM_SQUARE_SUB_MATRIX_WITH_ALL_EQUAL_ELEMENTS | java-python | static int largestKSubmatrix ( int [ ] [ ] a ) {
int [ ] [ ] dp = new int [ Row ] [ Col ] ;
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 [ i - 1 ] [ j ] && ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int largestKSubmatrix ( int [ ] [ ] a ) {
int [ ] [ ] dp = new int [ Row ] [ Col ] ;
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 [ i - 1 ] [ ... |
java-python-PASCAL_TRIANGLE | java-python | public static void printPascal ( int n ) {
int [ ] [ ] arr = new int [ 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 ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static void printPascal ( int n ) {
int [ ] [ ] arr = new int [ 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 [ li... |
java-python-NUMBER_INDEXES_EQUAL_ELEMENTS_GIVEN_RANGE | java-python | static 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static 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> |
java-python-CASSINIS_IDENTITY | java-python | static int cassini ( int n ) {
return ( n & 1 ) != 0 ? - 1 : 1 ;
}
| def cassini ( n ) :
return - 1 if ( n & 1 ) else 1
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int cassini ( int n ) {
return ( n & 1 ) != 0 ? - 1 : 1 ;
}
</Code> |
java-python-COMPUTE_AVERAGE_TWO_NUMBERS_WITHOUT_OVERFLOW | java-python | static 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int compute_average ( int a , int b ) {
return ( a + b ) / 2 ;
}
</Code> |
java-python-NUMBER_OF_WAYS_TO_ARRANGE_N_ITEMS_UNDER_GIVEN_CONSTRAINTS | java-python | static int waysToArrange ( int N , int K , int [ ] k ) {
int [ ] [ ] C = new int [ 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 ] ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int waysToArrange ( int N , int K , int [ ] k ) {
int [ ] [ ] C = new int [ 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... |
java-python-FIND_DIMENSIONS_RIGHT_ANGLED_TRIANGLE | java-python | static void findDimen ( int H , int A ) {
if ( H * H < 4 * A ) {
System . out . println ( "Not Possible" ) ;
return ;
}
double apb = Math . sqrt ( H * H + 4 * A ) ;
double asb = Math . sqrt ( H * H - 4 * A ) ;
System . out . println ( "P = " + Math . round ( ( ( apb - asb ) / 2.0 ) * 100.0 ) / 100.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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void findDimen ( int H , int A ) {
if ( H * H < 4 * A ) {
System . out . println ( "Not Possible" ) ;
return ;
}
double apb = Math . sqrt ( H * H + 4 * A ) ;
double asb = Math . sqrt ( H * H - 4 * A ) ;
System . out . println ( "P = " + Math . round ( ( ( apb - asb ) / 2.0 ) * 100.0 ) / ... |
java-python-MAXIMUM_TRIPLET_SUM_ARRAY_1 | java-python | static int maxTripletSum ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int maxTripletSum ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
return arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] ;
}
</Code> |
java-python-FIND_MAXIMUM_DOT_PRODUCT_TWO_ARRAYS_INSERTION_0S | java-python | static int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) {
int dp [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ;
for ( int [ ] row : dp ) Arrays . fill ( row , 0 ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= m ;
j ++ ) dp [ i ] [ j ] = Math . max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) {
int dp [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ;
for ( int [ ] row : dp ) Arrays . fill ( row , 0 ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= m ;
j ++ ) dp [ i ] [ j ] = Math . max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [... |
java-python-FIND_MINIMUM_DIFFERENCE_PAIR_1 | java-python | static int findMinDiff ( int [ ] arr , int n ) {
Arrays . sort ( arr ) ;
int diff = Integer . MAX_VALUE ;
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int findMinDiff ( int [ ] arr , int n ) {
Arrays . sort ( arr ) ;
int diff = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ;
return diff ;
}
</Code> |
java-python-FIND_LAST_INDEX_CHARACTER_STRING_1 | java-python | static int findLastIndex ( String str , Character x ) {
for ( int i = str . length ( ) - 1 ;
i >= 0 ;
i -- ) if ( str . charAt ( 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int findLastIndex ( String str , Character x ) {
for ( int i = str . length ( ) - 1 ;
i >= 0 ;
i -- ) if ( str . charAt ( i ) == x ) return i ;
return - 1 ;
}
</Code> |
java-python-MAXIMUM_DISTINCT_LOWERCASE_ALPHABETS_TWO_UPPERCASE | java-python | static int maxLower ( String str ) {
int n = str . length ( ) ;
int i = 0 ;
for ( ;
i < n ;
i ++ ) {
if ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) {
i ++ ;
break ;
}
}
int maxCount = 0 ;
int count [ ] = new int [ MAX_CHAR ] ;
for ( ;
i < n ;
i ++ ) {
if ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int maxLower ( String str ) {
int n = str . length ( ) ;
int i = 0 ;
for ( ;
i < n ;
i ++ ) {
if ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) {
i ++ ;
break ;
}
}
int maxCount = 0 ;
int count [ ] = new int [ MAX_CHAR ] ;
for ( ;
i < n ;
i ++ ) {
... |
java-python-FIND_ELEMENTS_ARRAY_LEAST_TWO_GREATER_ELEMENTS | java-python | static 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 ) System . out . print ( 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static 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 ) System . out . print ( arr [ i ] + " " ) ;
}
}
</Code> |
java-python-NTH_EVEN_LENGTH_PALINDROME | java-python | static String evenlength ( String n ) {
String res = n ;
for ( int j = n . length ( ) - 1 ;
j >= 0 ;
-- j ) res += n . charAt ( 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static String evenlength ( String n ) {
String res = n ;
for ( int j = n . length ( ) - 1 ;
j >= 0 ;
-- j ) res += n . charAt ( j ) ;
return res ;
}
</Code> |
java-python-LARGEST_SUBARRAY_WITH_EQUAL_NUMBER_OF_0S_AND_1S_1 | java-python | int maxLen ( int arr [ ] , int n ) {
HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ;
int sum = 0 ;
int max_len = 0 ;
int ending_index = - 1 ;
int start_index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1 ;
}
for ( int i = 0 ;
i < n... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxLen ( int arr [ ] , int n ) {
HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ;
int sum = 0 ;
int max_len = 0 ;
int ending_index = - 1 ;
int start_index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1 ;
}
for ( int i = 0 ;
... |
java-python-COUNT_NUMBER_OF_STRINGS_MADE_OF_R_G_AND_B_USING_GIVEN_COMBINATION | java-python | static int possibleStrings ( int n , int r , int b , int g ) {
int fact [ ] = new int [ 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 <= le... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int possibleStrings ( int n , int r , int b , int g ) {
int fact [ ] = new int [ 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 ;
... |
java-python-NUMBER_WAYS_INSERT_CHARACTER_INCREASE_LCS_ONE | java-python | static int numberofways ( String A , String B , int N , int M ) {
Vector < Integer > [ ] pos = new Vector [ MAX ] ;
for ( int i = 0 ;
i < MAX ;
i ++ ) pos [ i ] = new Vector < > ( ) ;
for ( int i = 0 ;
i < M ;
i ++ ) pos [ B . charAt ( i ) ] . add ( i + 1 ) ;
int [ ] [ ] dpl = new int [ N + 2 ] [ M + 2 ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int numberofways ( String A , String B , int N , int M ) {
Vector < Integer > [ ] pos = new Vector [ MAX ] ;
for ( int i = 0 ;
i < MAX ;
i ++ ) pos [ i ] = new Vector < > ( ) ;
for ( int i = 0 ;
i < M ;
i ++ ) pos [ B . charAt ( i ) ] . add ( i + 1 ) ;
int [ ] [ ] dpl = new int [ N + 2 ] [... |
java-python-PROGRAM_FIND_CIRCUMFERENCE_CIRCLE | java-python | static double circumference ( double r ) {
double PI = 3.1415 ;
double cir = 2 * PI * r ;
return cir ;
}
| def circumference ( r ) :
return ( 2 * PI * r )
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
static double circumference ( double r ) {
double PI = 3.1415 ;
double cir = 2 * PI * r ;
return cir ;
}
</Code> |
java-python-CONVERT_SUBSTRINGS_LENGTH_K_BASE_B_DECIMAL | java-python | static void substringConversions ( String str , int k , int b ) {
for ( int i = 0 ;
i + k <= str . length ( ) ;
i ++ ) {
String sub = str . substring ( i , i + k ) ;
int sum = 0 , counter = 0 ;
for ( int j = sub . length ( ) - 1 ;
j >= 0 ;
j -- ) {
sum = ( int ) ( sum + ( ( sub . charAt ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void substringConversions ( String str , int k , int b ) {
for ( int i = 0 ;
i + k <= str . length ( ) ;
i ++ ) {
String sub = str . substring ( i , i + k ) ;
int sum = 0 , counter = 0 ;
for ( int j = sub . length ( ) - 1 ;
j >= 0 ;
j -- ) {
sum = ( int ) ( sum + ( ( sub . ... |
java-python-MAXIMUM_PATH_SUM_TRIANGLE | java-python | static int maxPathSum ( int tri [ ] [ ] , 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 ] ;
}
}
... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int maxPathSum ( int tri [ ] [ ] , 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 ] ;
... |
java-python-SUM_MINIMUM_MAXIMUM_ELEMENTS_SUBARRAYS_SIZE_K | java-python | public static int SumOfKsubArray ( int arr [ ] , int k ) {
int sum = 0 ;
Deque < Integer > S = new LinkedList < > ( ) , G = new LinkedList < > ( ) ;
int i = 0 ;
for ( i = 0 ;
i < k ;
i ++ ) {
while ( ! S . isEmpty ( ) && arr [ S . peekLast ( ) ] >= arr [ i ] ) S . removeLast ( ) ;
while ( ! G . isEm... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static int SumOfKsubArray ( int arr [ ] , int k ) {
int sum = 0 ;
Deque < Integer > S = new LinkedList < > ( ) , G = new LinkedList < > ( ) ;
int i = 0 ;
for ( i = 0 ;
i < k ;
i ++ ) {
while ( ! S . isEmpty ( ) && arr [ S . peekLast ( ) ] >= arr [ i ] ) S . removeLast ( ) ;
while ( ! G... |
java-python-TURN_OFF_THE_RIGHTMOST_SET_BIT | java-python | static int fun ( int n ) {
return n & ( n - 1 ) ;
}
| def fun ( n ) :
return n & ( n - 1 )
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int fun ( int n ) {
return n & ( n - 1 ) ;
}
</Code> |
java-python-FIND_MIRROR_IMAGE_POINT_2_D_PLANE | java-python | static pair 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 new 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static pair 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 new pair ( x , y ) ;
}
</Code> |
java-python-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY_1 | java-python | static void printDistinct ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ;
System . out . print ( 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void printDistinct ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ;
System . out . print ( arr [ i ] + " " ) ;
}
}
</Code> |
java-python-UNION_AND_INTERSECTION_OF_TWO_SORTED_ARRAYS_2_1 | java-python | static void 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 {
System . out . print ( arr2 [ j ++ ] + " " ) ;
i ++ ;
}
}
}
| 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void 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 {
System . out . print ( arr2 [ j ++ ] + " " ) ;
i ++ ;
}
}
}
</C... |
java-python-SEARCHING_ARRAY_ADJACENT_DIFFER_K | java-python | static int search ( int arr [ ] , int n , int x , int k ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + Math . max ( 1 , Math . abs ( arr [ i ] - x ) / k ) ;
}
System . out . println ( "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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int search ( int arr [ ] , int n , int x , int k ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + Math . max ( 1 , Math . abs ( arr [ i ] - x ) / k ) ;
}
System . out . println ( "number is " + "not present!" ) ;
return - 1 ;
}
</Code> |
java-python-REARRANGE_A_STRING_IN_SORTED_ORDER_FOLLOWED_BY_THE_INTEGER_SUM | java-python | static String arrangeString ( String str ) {
int char_count [ ] = new int [ MAX_CHAR ] ;
int sum = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( Character . isUpperCase ( str . charAt ( i ) ) ) char_count [ str . charAt ( i ) - ' A ' ] ++ ;
else sum = sum + ( str . charAt ( i ) - ' 0 ' ) ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static String arrangeString ( String str ) {
int char_count [ ] = new int [ MAX_CHAR ] ;
int sum = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( Character . isUpperCase ( str . charAt ( i ) ) ) char_count [ str . charAt ( i ) - ' A ' ] ++ ;
else sum = sum + ( str . charAt ( i ) - '... |
java-python-COUNT_NUMBER_BINARY_STRINGS_WITHOUT_CONSECUTIVE_1S | java-python | static int countStrings ( int n ) {
int a [ ] = new int [ n ] ;
int b [ ] = new int [ 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int countStrings ( int n ) {
int a [ ] = new int [ n ] ;
int b [ ] = new int [ 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> |
java-python-MINIMUM_SUM_SUBSEQUENCE_LEAST_ONE_EVERY_FOUR_CONSECUTIVE_ELEMENTS_PICKED | java-python | static int minSum ( int [ ] arr , int n ) {
int [ ] dp = new int [ n ] ;
if ( n == 1 ) return arr [ 0 ] ;
if ( n == 2 ) return Math . min ( arr [ 0 ] , arr [ 1 ] ) ;
if ( n == 3 ) return Math . min ( arr [ 0 ] , Math . min ( arr [ 1 ] , arr [ 2 ] ) ) ;
if ( n == 4 ) return Math . min ( Math . min ( arr [ 0 ] ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int minSum ( int [ ] arr , int n ) {
int [ ] dp = new int [ n ] ;
if ( n == 1 ) return arr [ 0 ] ;
if ( n == 2 ) return Math . min ( arr [ 0 ] , arr [ 1 ] ) ;
if ( n == 3 ) return Math . min ( arr [ 0 ] , Math . min ( arr [ 1 ] , arr [ 2 ] ) ) ;
if ( n == 4 ) return Math . min ( Math . min ( arr... |
java-python-CONVERT_SENTENCE_EQUIVALENT_MOBILE_NUMERIC_KEYPAD_SEQUENCE | java-python | static String printSequence ( String arr [ ] , String input ) {
String output = "" ;
int n = input . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( input . charAt ( i ) == ' ▁ ' ) output = output + "0" ;
else {
int position = input . charAt ( i ) - ' A ' ;
output = output + arr [ pos... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static String printSequence ( String arr [ ] , String input ) {
String output = "" ;
int n = input . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( input . charAt ( i ) == ' ▁ ' ) output = output + "0" ;
else {
int position = input . charAt ( i ) - ' A ' ;
output = output + ar... |
java-python-ROTATE_MATRIX_ELEMENTS | java-python | static void rotatematrix ( int m , int n , int mat [ ] [ ] ) {
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 [... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void rotatematrix ( int m , int n , int mat [ ] [ ] ) {
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 ] ;
... |
java-python-MAXIMUM_BINOMIAL_COEFFICIENT_TERM_VALUE | java-python | static int maxcoefficientvalue ( int n ) {
int [ ] [ ] C = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= Math . 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 ] ;... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int maxcoefficientvalue ( int n ) {
int [ ] [ ] C = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= Math . 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 ] ... |
java-python-PROGRAM_FIND_GCD_FLOATING_POINT_NUMBERS | java-python | static double gcd ( double a , double b ) {
if ( a < b ) return gcd ( b , a ) ;
if ( Math . abs ( b ) < 0.001 ) return a ;
else return ( gcd ( b , a - Math . 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static double gcd ( double a , double b ) {
if ( a < b ) return gcd ( b , a ) ;
if ( Math . abs ( b ) < 0.001 ) return a ;
else return ( gcd ( b , a - Math . floor ( a / b ) * b ) ) ;
}
</Code> |
java-python-SORT_STRING_ACCORDING_ORDER_DEFINED_ANOTHER_STRING | java-python | static void sortByPattern ( char [ ] str , char [ ] pat ) {
int count [ ] = new int [ MAX_CHAR ] ;
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void sortByPattern ( char [ ] str , char [ ] pat ) {
int count [ ] = new int [ MAX_CHAR ] ;
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 ] -... |
java-python-REARRANGE_ARRAY_MAXIMUM_MINIMUM_FORM_SET_2_O1_EXTRA_SPACE | java-python | public static 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 [... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static 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 ] += ... |
java-python-MINIMUM_NUMBER_OF_JUMPS_TO_REACH_END_OF_A_GIVEN_ARRAY_1 | java-python | private static int minJumps ( int [ ] arr , int n ) {
int jumps [ ] = new int [ n ] ;
int i , j ;
if ( n == 0 || arr [ 0 ] == 0 ) return Integer . MAX_VALUE ;
jumps [ 0 ] = 0 ;
for ( i = 1 ;
i < n ;
i ++ ) {
jumps [ i ] = Integer . MAX_VALUE ;
for ( j = 0 ;
j < i ;
j ++ ) {
if ( i <=... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
private static int minJumps ( int [ ] arr , int n ) {
int jumps [ ] = new int [ n ] ;
int i , j ;
if ( n == 0 || arr [ 0 ] == 0 ) return Integer . MAX_VALUE ;
jumps [ 0 ] = 0 ;
for ( i = 1 ;
i < n ;
i ++ ) {
jumps [ i ] = Integer . MAX_VALUE ;
for ( j = 0 ;
j < i ;
j ++ ) {
if... |
java-python-PROBABILITY_REACHING_POINT_2_3_STEPS_TIME | java-python | static float find_prob ( int N , float P ) {
double dp [ ] = new double [ 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 ( ( float ) ( 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static float find_prob ( int N , float P ) {
double dp [ ] = new double [ 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 ( ( float ) ( dp [ N ] ) ) ;
}
</Code> |
java-python-COUNT_WAYS_INCREASE_LCS_LENGTH_TWO_STRINGS_ONE | java-python | static int waysToIncreaseLCSBy1 ( String str1 , String str2 ) {
int m = str1 . length ( ) , n = str2 . length ( ) ;
Vector < Integer > [ ] position = new Vector [ M ] ;
for ( int i = 0 ;
i < M ;
i ++ ) position [ i ] = new Vector < > ( ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) position [ str2 . charAt ( i -... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int waysToIncreaseLCSBy1 ( String str1 , String str2 ) {
int m = str1 . length ( ) , n = str2 . length ( ) ;
Vector < Integer > [ ] position = new Vector [ M ] ;
for ( int i = 0 ;
i < M ;
i ++ ) position [ i ] = new Vector < > ( ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) position [ str2 . charA... |
java-python-LONGEST_INCREASING_SUBSEQUENCE_1 | java-python | static int lis ( int arr [ ] , int n ) {
int lis [ ] = new int [ n ] ;
int i , j , max = 0 ;
for ( i = 0 ;
i < n ;
i ++ ) lis [ i ] = 1 ;
for ( i = 1 ;
i < n ;
i ++ ) for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ;
for ( i = 0 ;
... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int lis ( int arr [ ] , int n ) {
int lis [ ] = new int [ n ] ;
int i , j , max = 0 ;
for ( i = 0 ;
i < n ;
i ++ ) lis [ i ] = 1 ;
for ( i = 1 ;
i < n ;
i ++ ) for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ;
for ( i =... |
java-python-STERN_BROCOT_SEQUENCE | java-python | static void SternSequenceFunc ( Vector < Integer > BrocotSequence , int n ) {
for ( int i = 1 ;
BrocotSequence . size ( ) < n ;
i ++ ) {
int considered_element = BrocotSequence . get ( i ) ;
int precedent = BrocotSequence . get ( i - 1 ) ;
BrocotSequence . add ( considered_element + precedent ) ;
... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void SternSequenceFunc ( Vector < Integer > BrocotSequence , int n ) {
for ( int i = 1 ;
BrocotSequence . size ( ) < n ;
i ++ ) {
int considered_element = BrocotSequence . get ( i ) ;
int precedent = BrocotSequence . get ( i - 1 ) ;
BrocotSequence . add ( considered_element + precedent )... |
java-python-PRUFER_CODE_TREE_CREATION | java-python | static void printTreeEdges ( int prufer [ ] , int m ) {
int vertices = m + 2 ;
int vertex_set [ ] = new int [ 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 ;
System . out . print ( "\nThe edg... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void printTreeEdges ( int prufer [ ] , int m ) {
int vertices = m + 2 ;
int vertex_set [ ] = new int [ 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 ;
System . out . print ( "\n... |
java-python-COUNT_POSSIBLE_DECODINGS_GIVEN_DIGIT_SEQUENCE_1 | java-python | static int countDecodingDP ( char digits [ ] , int n ) {
int count [ ] = new int [ 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 ( di... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int countDecodingDP ( char digits [ ] , int n ) {
int count [ ] = new int [ 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 ] ;
... |
java-python-COUNT_STRINGS_WITH_CONSECUTIVE_1S | java-python | static int countStrings ( int n ) {
int a [ ] = new int [ n ] , b [ ] = new int [ 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 ] ;
}
from 2 ^ n 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int countStrings ( int n ) {
int a [ ] = new int [ n ] , b [ ] = new int [ 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 ] ;
}
from 2 ^ n return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ;
}
</Code> |
java-python-MAXIMUM_NUMBER_SEGMENTS_LENGTHS_B_C | java-python | static int maximumSegments ( int n , int a , int b , int c ) {
int dp [ ] = new int [ n + 10 ] ;
Arrays . fill ( dp , - 1 ) ;
dp [ 0 ] = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( dp [ i ] != - 1 ) {
if ( i + a <= n ) dp [ i + a ] = Math . max ( dp [ i ] + 1 , dp [ i + a ] ) ;
if ( 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int maximumSegments ( int n , int a , int b , int c ) {
int dp [ ] = new int [ n + 10 ] ;
Arrays . fill ( dp , - 1 ) ;
dp [ 0 ] = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( dp [ i ] != - 1 ) {
if ( i + a <= n ) dp [ i + a ] = Math . max ( dp [ i ] + 1 , dp [ i + a ] ) ;
if ( i... |
java-python-PRINT_BRACKET_NUMBER | java-python | static void printBracketNumber ( String exp , int n ) {
int left_bnum = 1 ;
Stack < Integer > right_bnum = new Stack < Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( exp . charAt ( i ) == ' ( ' ) {
System . out . print ( left_bnum + " " ) ;
right_bnum . push ( left_bnum ) ;
left... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void printBracketNumber ( String exp , int n ) {
int left_bnum = 1 ;
Stack < Integer > right_bnum = new Stack < Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( exp . charAt ( i ) == ' ( ' ) {
System . out . print ( left_bnum + " " ) ;
right_bnum . push ( left_bnum ) ;
... |
java-python-COUNT_WORDS_WHOSE_TH_LETTER_EITHER_1_TH_TH_I1_TH_LETTER_GIVEN_WORD | java-python | static int countWords ( String str , int len ) {
int count = 1 ;
if ( len == 1 ) return count ;
if ( str . charAt ( 0 ) == str . charAt ( 1 ) ) count *= 1 ;
else count *= 2 ;
for ( int j = 1 ;
j < len - 1 ;
j ++ ) {
if ( str . charAt ( j ) == str . charAt ( j - 1 ) && str . charAt ( j ) == str . charA... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int countWords ( String str , int len ) {
int count = 1 ;
if ( len == 1 ) return count ;
if ( str . charAt ( 0 ) == str . charAt ( 1 ) ) count *= 1 ;
else count *= 2 ;
for ( int j = 1 ;
j < len - 1 ;
j ++ ) {
if ( str . charAt ( j ) == str . charAt ( j - 1 ) && str . charAt ( j ) == str ... |
java-python-C_PROGRAM_FIND_LARGEST_ELEMENT_ARRAY | java-python | static int largest ( ) {
int i ;
int max = arr [ 0 ] ;
for ( i = 1 ;
i < arr . length ;
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int largest ( ) {
int i ;
int max = arr [ 0 ] ;
for ( i = 1 ;
i < arr . length ;
i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ;
return max ;
}
</Code> |
java-python-FIND_MINIMUM_DIFFERENCE_PAIR | java-python | static int findMinDiff ( int [ ] arr , int n ) {
int diff = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( Math . abs ( ( arr [ i ] - arr [ j ] ) ) < diff ) diff = Math . 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int findMinDiff ( int [ ] arr , int n ) {
int diff = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( Math . abs ( ( arr [ i ] - arr [ j ] ) ) < diff ) diff = Math . abs ( ( arr [ i ] - arr [ j ] ) ) ;
return diff ;
}
</Code> |
java-python-MAXIMUM_SUM_IARRI_AMONG_ROTATIONS_GIVEN_ARRAY | java-python | static int maxSum ( int arr [ ] , int n ) {
int res = Integer . MIN_VALUE ;
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 = Math . max ( res , curr_sum ) ;
}
retur... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int maxSum ( int arr [ ] , int n ) {
int res = Integer . MIN_VALUE ;
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 = Math . max ( res , curr_sum ) ;
}
... |
java-python-PROGRAM_CALCULATE_VOLUME_ELLIPSOID | java-python | public static float volumeOfEllipsoid ( float r1 , float r2 , float r3 ) {
float pi = ( float ) 3.14 ;
return ( float ) 1.33 * pi * r1 * r2 * r3 ;
}
| def volumeOfEllipsoid ( r1 , r2 , r3 ) :
return 1.33 * math.pi * r1 * r2 * r3
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static float volumeOfEllipsoid ( float r1 , float r2 , float r3 ) {
float pi = ( float ) 3.14 ;
return ( float ) 1.33 * pi * r1 * r2 * r3 ;
}
</Code> |
java-python-LCS_LONGEST_COMMON_SUBSEQUENCE_THREE_STRINGS | java-python | static int lcsOf3 ( String X , String Y , String Z , int m , int n , int o ) {
int [ ] [ ] [ ] L = new int [ 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 ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int lcsOf3 ( String X , String Y , String Z , int m , int n , int o ) {
int [ ] [ ] [ ] L = new int [ 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 == ... |
java-python-FIND_NUMBER_ENDLESS_POINTS | java-python | static int countEndless ( boolean input [ ] [ ] , int n ) {
boolean row [ ] [ ] = new boolean [ n ] [ n ] ;
boolean col [ ] [ ] = new boolean [ n ] [ n ] ;
for ( int j = 0 ;
j < n ;
j ++ ) {
boolean isEndless = true ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
if ( input [ i ] [ j ] == fals... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int countEndless ( boolean input [ ] [ ] , int n ) {
boolean row [ ] [ ] = new boolean [ n ] [ n ] ;
boolean col [ ] [ ] = new boolean [ n ] [ n ] ;
for ( int j = 0 ;
j < n ;
j ++ ) {
boolean isEndless = true ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
if ( input [ i ] [ j ] ... |
java-python-PROGRAM_NEXT_FIT_ALGORITHM_MEMORY_MANAGEMENT | java-python | static void NextFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ ] = new int [ n ] , j = 0 ;
Arrays . fill ( allocation , - 1 ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( j < m ) {
if ( blockSize [ j ] >= processSize [ i ] ) {
allocation [ i ] = j ;
... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void NextFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ ] = new int [ n ] , j = 0 ;
Arrays . fill ( allocation , - 1 ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( j < m ) {
if ( blockSize [ j ] >= processSize [ i ] ) {
allocation [ i ] = j ;... |
java-python-LONGEST_GEOMETRIC_PROGRESSION | java-python | static int lenOfLongestGP ( int set [ ] , int n ) {
if ( n < 2 ) {
return n ;
}
if ( n == 2 ) {
return ( set [ 1 ] % set [ 0 ] == 0 ? 1 : 0 ) ;
}
Arrays . sort ( set ) ;
int L [ ] [ ] = new int [ n ] [ n ] ;
int llgp = 1 ;
for ( int i = 0 ;
i < n ;
++ i ) {
if ( set [ n - 1 ] % set [ i ]... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int lenOfLongestGP ( int set [ ] , int n ) {
if ( n < 2 ) {
return n ;
}
if ( n == 2 ) {
return ( set [ 1 ] % set [ 0 ] == 0 ? 1 : 0 ) ;
}
Arrays . sort ( set ) ;
int L [ ] [ ] = new int [ n ] [ n ] ;
int llgp = 1 ;
for ( int i = 0 ;
i < n ;
++ i ) {
if ( set [ n - 1 ] % se... |
java-python-CHECK_WHETHER_TRIANGLE_VALID_NOT_SIDES_GIVEN | java-python | public static int checkValidity ( int a , int b , int c ) {
if ( a + b <= c || a + c <= b || b + c <= a ) return 0 ;
else return 1 ;
}
| 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static int checkValidity ( int a , int b , int c ) {
if ( a + b <= c || a + c <= b || b + c <= a ) return 0 ;
else return 1 ;
}
</Code> |
java-python-DIAGONALLY_DOMINANT_MATRIX | java-python | static boolean isDDM ( int m [ ] [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) sum += Math . abs ( m [ i ] [ j ] ) ;
sum -= Math . abs ( m [ i ] [ i ] ) ;
if ( Math . 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isDDM ( int m [ ] [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) sum += Math . abs ( m [ i ] [ j ] ) ;
sum -= Math . abs ( m [ i ] [ i ] ) ;
if ( Math . abs ( m [ i ] [ i ] ) < sum ) return false ;
}
return true ;... |
java-python-PROGRAM_TO_FIND_TRANSPOSE_OF_A_MATRIX | java-python | static void transpose ( int A [ ] [ ] , int B [ ] [ ] ) {
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void transpose ( int A [ ] [ ] , int B [ ] [ ] ) {
int i , j ;
for ( i = 0 ;
i < N ;
i ++ ) for ( j = 0 ;
j < N ;
j ++ ) B [ i ] [ j ] = A [ j ] [ i ] ;
}
</Code> |
java-python-MAXIMUM_POSSIBLE_SUM_WINDOW_ARRAY_ELEMENTS_WINDOW_ARRAY_UNIQUE | java-python | static int returnMaxSum ( int A [ ] , int B [ ] , int n ) {
Set < Integer > mp = new HashSet < Integer > ( ) ;
int result = 0 ;
int curr_sum = 0 , curr_begin = 0 ;
for ( int i = 0 ;
i < n ;
++ i ) {
while ( mp . contains ( A [ i ] ) ) {
mp . remove ( A [ curr_begin ] ) ;
curr_sum -= B [ curr... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int returnMaxSum ( int A [ ] , int B [ ] , int n ) {
Set < Integer > mp = new HashSet < Integer > ( ) ;
int result = 0 ;
int curr_sum = 0 , curr_begin = 0 ;
for ( int i = 0 ;
i < n ;
++ i ) {
while ( mp . contains ( A [ i ] ) ) {
mp . remove ( A [ curr_begin ] ) ;
curr_sum -= B... |
java-python-NUMBER_OF_PAIRS_IN_AN_ARRAY_HAVING_SUM_EQUAL_TO_PRODUCT | java-python | static 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static 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> |
java-python-MULTISTAGE_GRAPH_SHORTEST_PATH | java-python | public static int shortestDist ( int [ ] [ ] graph ) {
int [ ] dist = new int [ 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 ] = Math . ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static int shortestDist ( int [ ] [ ] graph ) {
int [ ] dist = new int [ 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 ] = ... |
java-python-SUM_OF_ALL_ELEMENTS_UP_TO_NTH_ROW_IN_A_PASCALS_TRIANGLE_1 | java-python | static long calculateSum ( int n ) {
long sum = 0 ;
sum = 1 << n ;
return ( sum - 1 ) ;
}
| def calculateSum ( n ) :
sum = 0
sum = 1 << n
return ( sum - 1 )
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
static long calculateSum ( int n ) {
long sum = 0 ;
sum = 1 << n ;
return ( sum - 1 ) ;
}
</Code> |
java-python-PRIMALITY_TEST_SET_1_INTRODUCTION_AND_SCHOOL_METHOD_1 | java-python | static boolean 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static boolean 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> |
java-python-PRINT_FIBONACCI_SEQUENCE_USING_2_VARIABLES_1 | java-python | static void fib ( int n ) {
int a = 0 , b = 1 ;
if ( n >= 0 ) System . out . print ( a + " " ) ;
if ( n >= 1 ) System . out . print ( b + " " ) ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
System . out . print ( 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void fib ( int n ) {
int a = 0 , b = 1 ;
if ( n >= 0 ) System . out . print ( a + " " ) ;
if ( n >= 1 ) System . out . print ( b + " " ) ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
System . out . print ( a + b + " " ) ;
b = a + b ;
a = b - a ;
}
}
</Code> |
java-python-K_TH_DIGIT_RAISED_POWER_B | java-python | public static int kthdigit ( int a , int b , int k ) {
int p = ( int ) Math . 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static int kthdigit ( int a , int b , int k ) {
int p = ( int ) Math . 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> |
java-python-CHECK_WHETHER_GIVEN_NUMBER_EVEN_ODD_1 | java-python | public static boolean isEven ( int n ) {
if ( ( n & 1 ) == 0 ) return true ;
else return false ;
}
| def isEven ( n ) :
return ( not ( n & 1 ) )
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static boolean isEven ( int n ) {
if ( ( n & 1 ) == 0 ) return true ;
else return false ;
}
</Code> |
java-python-PRINT_SHORTEST_PATH_PRINT_STRING_SCREEN | java-python | static void printPath ( String str ) {
int i = 0 ;
int curX = 0 , curY = 0 ;
while ( i < str . length ( ) ) {
int nextX = ( str . charAt ( i ) - ' A ' ) / 5 ;
int nextY = ( str . charAt ( i ) - ' B ' + 1 ) % 5 ;
while ( curX > nextX ) {
System . out . println ( "Move Up" ) ;
curX -- ;
... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void printPath ( String str ) {
int i = 0 ;
int curX = 0 , curY = 0 ;
while ( i < str . length ( ) ) {
int nextX = ( str . charAt ( i ) - ' A ' ) / 5 ;
int nextY = ( str . charAt ( i ) - ' B ' + 1 ) % 5 ;
while ( curX > nextX ) {
System . out . println ( "Move Up" ) ;
curX --... |
java-python-CHECK_GIVEN_CIRCLE_LIES_COMPLETELY_INSIDE_RING_FORMED_TWO_CONCENTRIC_CIRCLES | java-python | public static boolean checkcircle ( int r , int R , int r1 , int x1 , int y1 ) {
int dis = ( int ) Math . 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
public static boolean checkcircle ( int r , int R , int r1 , int x1 , int y1 ) {
int dis = ( int ) Math . sqrt ( x1 * x1 + y1 * y1 ) ;
return ( dis - r1 >= R && dis + r1 <= r ) ;
}
</Code> |
java-python-GIVEN_LEVEL_ORDER_TRAVERSAL_BINARY_TREE_CHECK_TREE_MIN_HEAP | java-python | static boolean isMinHeap ( int [ ] level ) {
int n = level . length - 1 ;
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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isMinHeap ( int [ ] level ) {
int n = level . length - 1 ;
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 ;
}
</Cod... |
java-python-THIRD_LARGEST_ELEMENT_ARRAY_DISTINCT_ELEMENTS | java-python | static void thirdLargest ( int arr [ ] , int arr_size ) {
if ( arr_size < 3 ) {
System . out . 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 = Integer . MIN_VALUE ;
for ( int i = 0... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void thirdLargest ( int arr [ ] , int arr_size ) {
if ( arr_size < 3 ) {
System . out . 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 = Integer . MIN_VALUE ;
for ( in... |
java-python-FIND_A_ROTATION_WITH_MAXIMUM_HAMMING_DISTANCE | java-python | static int maxHamming ( int arr [ ] , int n ) {
int brr [ ] = new int [ 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 ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int maxHamming ( int arr [ ] , int n ) {
int brr [ ] = new int [ 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... |
java-python-FIND_SUM_MODULO_K_FIRST_N_NATURAL_NUMBER | java-python | static 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int findSum ( int N , int K ) {
int ans = 0 ;
for ( int i = 1 ;
i <= N ;
i ++ ) ans += ( i % K ) ;
return ans ;
}
</Code> |
java-python-SUM_SERIES_23_45_67_89_UPTO_N_TERMS | java-python | static double seriesSum ( int n ) {
int i = 1 ;
double res = 0.0 ;
boolean 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static double seriesSum ( int n ) {
int i = 1 ;
double res = 0.0 ;
boolean 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... |
java-python-COUNT_TOTAL_SET_BITS_IN_ALL_NUMBERS_FROM_1_TO_N | java-python | static int countSetBits ( int n ) {
int i = 0 ;
int ans = 0 ;
while ( ( 1 << i ) <= n ) {
boolean k = false ;
int change = 1 << i ;
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( k == true ) ans += 1 ;
else ans += 0 ;
if ( change == 1 ) {
k = ! k ;
change = 1 << i ;... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int countSetBits ( int n ) {
int i = 0 ;
int ans = 0 ;
while ( ( 1 << i ) <= n ) {
boolean k = false ;
int change = 1 << i ;
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( k == true ) ans += 1 ;
else ans += 0 ;
if ( change == 1 ) {
k = ! k ;
change = 1... |
java-python-COUNT_PAIRS_WITH_GIVEN_SUM_1 | java-python | static int getPairsCount ( int n , int sum ) {
HashMap < Integer , Integer > hm = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ! hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , 0 ) ;
hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ;
}
int twice_count = 0 ;
for ( int i ... | 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 java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int getPairsCount ( int n , int sum ) {
HashMap < Integer , Integer > hm = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ! hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , 0 ) ;
hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ;
}
int twice_count = 0 ;
for (... |
java-python-PROGRAM_CHECK_DIAGONAL_MATRIX_SCALAR_MATRIX | java-python | static boolean isDiagonalMatrix ( int mat [ ] [ ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < N ;
j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ;
return true ;
}
| def isDiagonalMatrix ( mat ) :
for i in range ( 0 , N ) :
for j in range ( 0 , N ) :
if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) :
return False
return True
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isDiagonalMatrix ( int mat [ ] [ ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < N ;
j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ;
return true ;
}
</Code> |
java-python-SORT_3_INTEGERS_WITHOUT_USING_CONDITION_USING_MAX_FUNCTION | java-python | static void printSorted ( int a , int b , int c ) {
int get_max = Math . max ( a , Math . max ( b , c ) ) ;
int get_min = - Math . max ( - a , Math . max ( - b , - c ) ) ;
int get_mid = ( a + b + c ) - ( get_max + get_min ) ;
System . out . print ( get_min + " " + get_mid + " " + get_max ) ;
}
| def printSorted ( a , b , c ) :
get_max = max ( a , max ( b , c ) )
get_min = - max ( - a , max ( - b , - c ) )
get_mid = ( a + b + c ) - ( get_max + get_min )
print ( get_min , " " , get_mid , " " , get_max )
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
static void printSorted ( int a , int b , int c ) {
int get_max = Math . max ( a , Math . max ( b , c ) ) ;
int get_min = - Math . max ( - a , Math . max ( - b , - c ) ) ;
int get_mid = ( a + b + c ) - ( get_max + get_min ) ;
System . out . print ( get_min + " " + get_mid + " " + get_max ) ;
}
</Code> |
java-python-NUMBER_IS_DIVISIBLE_BY_29_OR_NOT | java-python | static boolean isDivisible ( long n ) {
while ( n / 100 > 0 ) {
int last_digit = ( int ) n % 10 ;
n /= 10 ;
n += last_digit * 3 ;
}
return ( n % 29 == 0 ) ;
}
| def isDivisible ( n ) :
while ( int ( n / 100 ) ) :
last_digit = int ( n % 10 )
n = int ( n / 10 )
n += last_digit * 3
return ( n % 29 == 0 )
| Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isDivisible ( long n ) {
while ( n / 100 > 0 ) {
int last_digit = ( int ) n % 10 ;
n /= 10 ;
n += last_digit * 3 ;
}
return ( n % 29 == 0 ) ;
}
</Code> |
java-python-FIND_THE_ELEMENT_THAT_APPEARS_ONCE | java-python | static int getSingle ( int arr [ ] , int n ) {
int ones = 0 , twos = 0 ;
int common_bit_mask ;
for ( int i = 0 ;
i < n ;
i ++ ) {
twos = twos | def getSingle ( arr , n ) :
ones = 0
twos = 0
for i in range ( n ) :
twos = twos | Translate the given code from java to python. The input Code is marked with <Code> and </Code>. | <Code>
static int getSingle ( int arr [ ] , int n ) {
int ones = 0 , twos = 0 ;
int common_bit_mask ;
for ( int i = 0 ;
i < n ;
i ++ ) {
twos = twos
</Code> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.