In: Computer Science
I need this Java code transform to Python Code
PROGRAM:
import java.util.Scanner;
public class Main
{
static int count=0;
int calculate(int row, int column)
{
count++;
if(row==1&&column==1)
{
return 0;
}
else if(column==1)
{
return ((200+calculate(row-1,column))/2);
}
else if(column==row)
{
return (200+calculate(row-1,column-1))/2;
}
else
{
return
((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2);
}
}
public static void main(String[] args)
{
int row,column,weight;
Main m=new Main();
System.out.println("Welcome to the Human pyramid. Select a row
column combination and i will tell you how much weight the person
is supporting");
System.out.println("Please type your selection for Row and
Column");
Scanner scan=new Scanner(System.in);
row=scan.nextInt();
column=scan.nextInt();
if(row==0 && column==0)
{
System.out.println("Thanks for playing Human Pyramid. Don't let me
down.");
System.exit(0);
}
if(row==0 || column==0)
{
System.out.println("Wrong option");
System.exit(0);
}
weight=m.calculate(row,column);
System.out.println("Person at ("+row+","+column+") is supporting
"+weight+" pounds");
System.out.println("Recursive function is called "+count+"
times");
}
}
Here is code:
count = 0
def calculate(row, column) :
global count
count += 1
if (row == 1 and column == 1) :
return 0
elif (column == 1) :
return ((200 + calculate(row - 1, column)) / 2)
elif (column == row) :
return (200 + calculate(row - 1, column - 1)) / 2
else :
return ((200 + calculate(row - 1, column - 1)) / 2) + ((200 + calculate(row - 1, column)) / 2)
print("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the person is supporting")
print("Please type your selection for Row and Column")
row = int(input())
column = int(input())
if (row == 0 and column == 0) :
print("Thanks for playing Human Pyramid. Don't let me down.")
quit()
if (row == 0 or column == 0) :
print("Wrong option")
quit()
weight = calculate(row, column)
print("Person at (" , row , "," , column , ") is supporting " , weight , " pounds")
print("Recursive function is called " , count , " times")
Output: