0

I wrote a cantor function in java and I'm trying to translate it to Python. Can anyone give me some pointers ? Should I compare the fractions as one variable each or treat the top and the bottom as separate variables?

java:

    import java.util.Scanner;
    public class asdf {
     public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("ENter the nth ratio you want: ");
    int end = keyboard.nextInt();
    int count = 0;
    int top = 0;
    int bottom = 0;
    boolean flip = true;
    for(int i=0; i<end; i++){
        if(flip){
            top = i+1;
            bottom = 1;
            for(int j = 0; j<i+1; j++){
                if(count == end){
                    break;
                }
                System.out.println(top + "/" + bottom);
                top--;
                bottom++;
                count++;
            }
            flip = false;
        }
        else{
            top = 1;
            bottom = i+1;
            for(int j = 0; j<i+1; j++){
                if(count == end){
                    break;
                }
                System.out.println(top + "/" + bottom);
                top++;
                bottom--;
                count++;
                flip = true;
            }
        }
    }
}
    }

Python:

    def cantor(dat):
    n=len(dat)
    count, top, bottom = 0,0,0
    boolean flip = true
    for i in range (0,x):
            if (flip):
                    top = i+1
                    bottom = 1
                    for j in range(0,top):
                            if count == 21: break
                            boolean found = false
                            double temp = top*1.0/bottom
                            for k in range(0,n)):
                                    if temp == n[k]:
                                            found = true
                                            break
                    if !
         top = top-1
         bottom = bottom+1
         count = count+1
strah
  • 6,702
  • 4
  • 33
  • 45
jayMur
  • 1
  • 2
    I'm guessing you don't have much experience with Python, because `boolean flip = true` and `double temp = top*1.0/bottom` is not valid syntax. The [Python tutorial](http://docs.python.org/3/tutorial/index.html) might be a good place to start. – Kevin Mar 18 '14 at 13:07
  • 1
    @user3433147 I'll also add to what @Kevin said, pointing out that your indentation is wrong under the `def`. All control structures in Python require proper indentation. – wheaties Mar 18 '14 at 13:13
  • I am new to Python and I do know about white spaces, I just copied what I had already and pasted it. – jayMur Mar 18 '14 at 14:12

1 Answers1

0

The java2py3 option of AgileUML should be able to translate this. Correctly-formatted Python3 is produced.

https://github.com/eclipse/agileuml/blob/master/translators.zip

Kevin Lano
  • 81
  • 2