Jump to content

Java to Python

Can somebody convert this into python somehow?

 

package slotmachinerunner;
import java.util.Scanner;

/**
 *
 * @author
 */
public class SlotMachineRunner {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Runtime rt = Runtime.getRuntime();
     System.out.println("Used Memory: " + (rt.totalMemory() - rt.freeMemory())/1024 + " KBytes");
     SlotMachine stuff = new SlotMachine();
    
        stuff.changeNumber();
        System.out.println(stuff.getRandomNumber());
        stuff.changeNumber();
        System.out.println(stuff.getRandomNumber());
        stuff.changeNumber();
        System.out.println(stuff.getRandomNumber());
        

     System.out.println("Used Memory: " + (rt.totalMemory() - rt.freeMemory())/1024 + " KBytes");
    }
   
}

package slotmachinerunner;
import java.util.Random;
/**
 *
 * @author
 */
public class SlotMachine {
    private int randomNum;
   
    public SlotMachine(){
       randomNum = 0;
    }
   
    public void changeNumber(){
        Random rn = new Random();
    int n = 4;
    int i = rn.nextInt() % n;
    randomNum =  4 + i;
    }
    public int getRandomNumber(){
        return randomNum;
    }   
}

Link to comment
Share on other sites

Link to post
Share on other sites

Why don't you convert it into Python? If you know Java, then this should be pretty straight forward as Python is far less complicated.

Link to comment
Share on other sites

Link to post
Share on other sites

Why don't you convert it into Python? If you know Java, then this should be pretty straight forward as Python is far less complicated.

That really isn't helpful...perhaps he is just learning programming which would mean not being able to easily transfer over languages.

 

Anyways @electronuke2 I have never used Python before, but I think the following is roughly correct.

 

class SlotMachine:	def __init__(self):		self.randomNumber = 0	def changeNumber(self):		self.randomNumber = (int(random.Random()) % 4) + 4SlotMachine sm = SlotMachine()sm.changeNumber()print sm.randomNumbersm.changeNumber()print sm.randomNumbersm.changeNumber()print sm.randomNumber

Anyways, I have always been too lazy to learn Python (or even install it)....so this might not even work at all.  I think it is roughly similar to your original code (minus the memory portion)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Or perhaps this is an assignment? In which case they learn nothing. I'd be happy to give help where effort had been put into finding a solution.

Link to comment
Share on other sites

Link to post
Share on other sites

There still isn't really any reason to just say convert it yourself...that is less helpful than just remaining silent.  I remember being a student and not having a clue where to begin in researching.  At the very least point people in the general direction (documents for them to read, to help them convert)

 

As a note...given that the TC has been asking about this type of assignment for a bit, it is quite clear it isn't about just converting to other languages...my guess is their teacher is asking about memory performances in languages.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

That really isn't helpful...perhaps he is just learning programming which would mean not being able to easily transfer over languages.

 

Anyways @electronuke2 I have never used Python before, but I think the following is roughly correct.

 

class SlotMachine:	def __init__(self):		self.randomNumber = 0	def changeNumber(self):		self.randomNumber = (int(random.Random()) % 4) + 4SlotMachine sm = SlotMachine()sm.changeNumber()print sm.randomNumbersm.changeNumber()print sm.randomNumbersm.changeNumber()print sm.randomNumber

Anyways, I have always been too lazy to learn Python (or even install it)....so this might not even work at all.  I think it is roughly similar to your original code (minus the memory portion)

I found a nice way to get the answer through hard research, but now I need to find how much memory it used.

so basically, I converted everything except for

Runtime rt = Runtime.getRuntime();

     System.out.println("Used Memory: " + (rt.totalMemory() - rt.freeMemory())/1024 + " KBytes");

 

Please just convert that one.

Link to comment
Share on other sites

Link to post
Share on other sites

I found a nice way to get the answer through hard research, but now I need to find how much memory it used.

so basically, I converted everything except for

Runtime rt = Runtime.getRuntime();

     System.out.println("Used Memory: " + (rt.totalMemory() - rt.freeMemory())/1024 + " KBytes");

 

Please just convert that one.

Well this might be able to help

resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

I got this from http://pythonforbiologists.com/index.php/measuring-memory-usage-in-python/

and reading through the documentation http://docs.python.org/2/library/resource.html#resource.getrlimit

it appears that it might work

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Well this might be able to help

resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

I got this from http://pythonforbiologists.com/index.php/measuring-memory-usage-in-python/

and reading through the documentation http://docs.python.org/2/library/resource.html#resource.getrlimit

it appears that it might work

 

I keep getting the same error everytime I import resources, No module named resource. How do I fix that?

Link to comment
Share on other sites

Link to post
Share on other sites

I keep getting the same error everytime I import resources, No module named resource. How do I fix that?

 

Uhm, again I haven't ever bothered with Python, but given my first link I would assume putting

import resource

at the top of your code would work.

 

When in doubt of how to get something to work the error messages can be very helpful.  Like in your case, "No module named resource" so looking through the code on the first link I looked for "resource".  And found the import statement.  So it might work, might not...anyways good luck

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

I found a nice way to get the answer through hard research, but now I need to find how much memory it used.

so basically, I converted everything except for

Runtime rt = Runtime.getRuntime();

     System.out.println("Used Memory: " + (rt.totalMemory() - rt.freeMemory())/1024 + " KBytes");

 

Please just convert that one.

This link should help with that:

http://stackoverflow.com/questions/938733/total-memory-used-by-python-process

Intel 3570K - MSI GTX 660Ti 3GB OC Edition - 16GB Corsair LP RAM - ASRock Extreme4 Motherboard - Corsair HX850 - Adata Premier Pro SP900 120GB SSD with Windows 7 - Seagate Barracuda 1TD HDD - Seagate Barracuda 500GB HDD - Thermaltake Frio CPU Cooler - CM Storm Enforcer Case - Macbook Pro Early 2011 Laptop

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×