Example 1: python to java converter
try jython, Refer: https://www.jython.org
Example 2: convert python to java online
import check50
import check50_java
@check50.check()
def book_exists():
"""Book.java exists."""
check50.exists("Book.java")
@check50.check(book_exists)
def book_compiles():
"""Book.java compiles."""
check50_java.compile("Book.java")
@check50.check()
def press_exists():
"""Press.java exists."""
check50.exists("Press.java")
@check50.check(press_exists)
def press_compiles():
"""Press.java compiles."""
check50_java.compile("Press.java")
@check50.check()
def vm_exists():
"""VendingMachine.java exists."""
check50.exists("VendingMachine.java")
@check50.check(vm_exists)
def vm_compiles():
"""VendingMachine.java compiles."""
check50_java.compile("VendingMachine.java")
Example 3: convert python to java online
#include <bits/stdc++.h>
using namespace std;
int main()
{
int m,ans=INT_MAX,I=-1,J=-1;
cin>>m;
vector<vector<int>> mat(m,vector<int>());
cin.ignore();
for(int i=0;i<m;i++)
{
string s;
getline(cin,s);
istringstream ss(s);
while(ss)
{
string w;
ss>>w;
if(w=="") break;
mat[i].push_back(stoi(w));
}
}
int n=mat[0].size();
vector<vector<int>> Ans1(m,vector<int>(n,1));
vector<vector<int>> Ans2(m,vector<int>(n,1));
vector<vector<int>> Ans3(m,vector<int>(n,1));
vector<vector<int>> Ans4(m,vector<int>(n,1));
for(int i=0;i<m-1;i++)
for(int j=0;j<mat[i].size();j++)
if(mat[i][j]==mat[i+1][j])
Ans1[i+1][j]=Ans1[i][j]+1;
for(int i=0;i<m;i++)
for(int j=0;j<mat[i].size() -1;j++)
if(mat[i][j]==mat[i][j+1])
Ans2[i][j+1]=Ans2[i][j]+1;
for(int i=0;i<m-1;i++)
for(int j=0;j<mat[i].size()-1;j++)
if(mat[i][j]==mat[i+1][j+1])
Ans3[i+1][j+1]=Ans3[i][j]+1;
for(int i=0;i<m-1;i++)
for(int j=1;j<mat[i].size();j++)
if(mat[i][j]==mat[i+1][j-1])
Ans4[i+1][j-1]=Ans4[i][j]+1;
for(int i=0;i<m;i++)
for(int j=0;j<mat[i].size();j++)
{
if(Ans1[i][j]>=4 && ans>mat[i][j])
{ans=mat[i][j];I=i;J=j;}
if(Ans2[i][j]>=4 && ans>mat[i][j])
{ans=mat[i][j];I=i;J=j;}
if(Ans3[i][j]>=4 && ans>mat[i][j])
{ans=mat[i][j];I=i;J=j;}
if(Ans3[i][j]>=4 && ans>mat[i][j])
{ans=mat[i][j];I=i;J=j;}
}
if(I==-1)cout<<-1;
else cout<<ans<<endl;
}
Example 4: convert python to java online
#include <bits/stdc++.h>
using namespace std;
int main()
{
int m,ans=INT_MAX,I=-1,J=-1;
cin>>m;
vector<vector<int>> mat(m,vector<int>());
cin.ignore();
for(int i=0;i<m;i++)
{
string s;
getline(cin,s);
istringstream ss(s);
while(ss)
{
string w;
ss>>w;
if(w=="") break;
mat[i].push_back(stoi(w));
}
}
int n=mat[0].size();
vector<vector<int>> Ans1(m,vector<int>(n,1));
vector<vector<int>> Ans2(m,vector<int>(n,1));
vector<vector<int>> Ans3(m,vector<int>(n,1));
vector<vector<int>> Ans4(m,vector<int>(n,1));
for(int i=0;i<m-1;i++)
for(int j=0;j<mat[i].size();j++)
if(mat[i][j]==mat[i+1][j])
Ans1[i+1][j]=Ans1[i][j]+1;
for(int i=0;i<m;i++)
for(int j=0;j<mat[i].size() -1;j++)
if(mat[i][j]==mat[i][j+1])
Ans2[i][j+1]=Ans2[i][j]+1;
for(int i=0;i<m-1;i++)
for(int j=0;j<mat[i].size()-1;j++)
if(mat[i][j]==mat[i+1][j+1])
Ans3[i+1][j+1]=Ans3[i][j]+1;
for(int i=0;i<m-1;i++)
for(int j=1;j<mat[i].size();j++)
if(mat[i][j]==mat[i+1][j-1])
Ans4[i+1][j-1]=Ans4[i][j]+1;
for(int i=0;i<m;i++)
for(int j=0;j<mat[i].size();j++)
{
if(Ans1[i][j]>=4 && ans>mat[i][j])
{ans=mat[i][j];I=i;J=j;}
if(Ans2[i][j]>=4 && ans>mat[i][j])
{ans=mat[i][j];I=i;J=j;}
if(Ans3[i][j]>=4 && ans>mat[i][j])
{ans=mat[i][j];I=i;J=j;}
if(Ans3[i][j]>=4 && ans>mat[i][j])
{ans=mat[i][j];I=i;J=j;}
}
if(I==-1)cout<<-1;
else cout<<ans<<endl;
}
Example 5: python to java converter
def SeatingStudents(arr):
K = arr[0]
occupied = arr[1:]
rows = int(K/2)
seats = []
x = 0
for i in range(rows):
seats.append([])
for j in range(2):
if((x+1) in occupied):
full_seat = True
else:
full_seat = False
seats[i].append(str(full_seat))
x+=1
seating = 0
for i in range(rows-1):
if((seats[i][0] == str(False)) and (seats[i][1] == str(False))):
seating+=1
if((seats[i][0] == str(False)) and (seats[i+1][0] == str(False))):
seating+=1
if((seats[i][1] == str(False)) and (seats[i + 1][1] == str(False))):
seating+=1
if((seats[rows - 1][0] == str(False)) and (seats[rows - 1][1] == str(False))):
seating+=1
return seating
print(SeatingStudents([12, 2, 6, 7, 11]))
Comments
Post a Comment