0

Here is my code:

private static String recString(final int i) {

return (i>0 ?   i + "." + recString(i-1) : i<0? "." + recString(i+1) : ""  ) ;
}

The method should return i dots and the number of dots at begin (example recString(4) returns "4....") when i>0 and just dots when i<=0 (example recString(-4) returns "...."). The condition is that I use just one return line any other modification is not allowed. All I get "4.3.2.1." when I call recString(4). I see where is the problem but cant figure out how to take variable just at the beginning and not change it ? Thanks in advance

4
  • 1
    Why do you think you need recursion? The iterative solution is far more straightforward. Commented Apr 22, 2017 at 19:20
  • @JoeC I know, but I have test that I have to pass and it won't pass unless it is done using recursion Commented Apr 22, 2017 at 19:22
  • @vucko95 please add this information to your question by editing your question. Commented Apr 22, 2017 at 19:31
  • @Turing85 I said in Title that that that has to be done using recursion and wrote that "The condition is that I use just one return line any other modification is not allowed" Commented Apr 22, 2017 at 19:36

3 Answers 3

4

As for a positive number you have to output the initial number only once, and you can only output that number in the 'positive' branch, you have to leave that 'positive' branch after one iteration.

A possible solution is to negate your number after you output it in your 'positive' branch and then allow the 'negative' branch to finish the job (output all the dots):

return (i > 0 ? i + recString(-i) : i < 0 ? "." + recString(i + 1) : "");

The initial code of the question author worked for non-negative numbers. The suggested sulution makes it work for positive ones too.

Sign up to request clarification or add additional context in comments.

2 Comments

Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem.
Thanks for pointing this out, I've updated the answer with textual explanation.
1

If non-recursive solutions are allowed, you can use

public static String recString(final int length) {
    return String.format("%d%" + length + "s", length, "").replace(' ', '.');
}

Which returns "1." for length = 1, "2.." for length = 2, and so on, but fails for length < 1. If you also need the non-positive numbers, you can use:

public static String recString(final int length) {
    return length < 1
           ? length + ""
           : String.format("%d%" + length + "s", length, "").replace(' ', '.');
}

By the way: »Only one return line« could also mean that your you are allowed to write functions like the following.

public static String recString(final int length) {
    String result;

    // do something

    return result;
}

Comments

1

if i value is >0 i is appended to a StringBuilder and then i dots follow otherwise i dots are appended to this StringBuilder, finally a String is returned from the our StringBuilder using toString method.

private static String recString(final int i) {

            StringBuilder recString= new StringBuilder();

            if (i > 0) {
                recString.append(i);
                for (int x = 0; x < i; x++) {
                    recString.append(".");
                }
            } else {

                for (int x = 0; x > i; x--) 
                    recString.append(".");

            }
            return recString.toString();
        }

1 Comment

An explanation was added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.