Reordering arguments using recursion (pro, cons, alternatives)

I find that I often make a recursive call only to reorder the arguments.

For example, here is my solution for endOtherfrom codingbat.com :

Given two lines, return trueif one of the lines appears at the very end of the other line, ignoring the upper / lower case difference (in other words, the calculation should not be case sensitive). Note: str.toLowerCase()returns a string version of a string.

public boolean endOther(String a, String b) {
  return a.length() < b.length() ? endOther(b, a)
    : a.toLowerCase().endsWith(b.toLowerCase());
}

I really like recursion, but I can, of course, understand why some may object to this.

There are two obvious alternatives to this recursion technique:

Traditionally aandb

public boolean endOther(String a, String b) {
  if (a.length() < b.length()) {
    String t = a;
    a = b;
    b = t;
  }
  return a.toLowerCase().endsWith(b.toLowerCase());
}
  • Not convenient in Java, which does not follow the link
  • if ""

public boolean endOther(String a, String b) {
  return (a.length() < b.length())
    ? b.toLowerCase().endsWith(a.toLowerCase())
    : a.toLowerCase().endsWith(b.toLowerCase());
}
  • ( ?)
  • ,
    • ... ||

, :

  • ? ( ?)
    • ? (, " ", ?)
  • ()?
  • , , , ?

, , , , , if-else, .

// sorts 3 values and return as array
static int[] sort3(int a, int b, int c) {
    return
      (a > b) ? sort3(b, a, c) :
      (b > c) ? sort3(a, c, b) :
      new int[] { a, b, c };
}

, ; , - Java-, . .

+3
4

, . :

public boolean endOther(String a, String b){
    String alower=a.toLowerCase();
    String blower=b.toLowerCase();
    if ( a.length() < b.length() ){
        return blower.endsWith(alower);
    } else {
        return alower.endsWith(blower);
    }
} 

, if , . , if, , ( , ). , , Java, , .

"" "" / "" . - , .

, , . , , .

... , , "" , . . , , ... , , , .


... ( Verilog , ( HDL), ). , ; , if... else, , , .

+1

, .

, , , , , .

:

  • ( if, ).
  • swap - Java , .
  • ( @Has), , - , .

, . , , (1) , . Euklids GCD .

swap (2), , . . , , (1) (3), . -.

(3) , , , . , , , "Impl".

, (1) , (3) .

+1

+1 " , , . , , ".

, :

// sorts 3 values and return as array
static int[] sort3(int a, int b, int c) {
    return
      (a > b) ? sort3(b, a, c) :
      (b > c) ? sort3(a, c, b) :
      new int[] { a, b, c };
}

, , " Java", ... , , , ...

, ( - ...). , , javadoc unit... , , ... , , , , , ...

, , , , ... , .

, , , , - :

// sorts int values
public static int[] sort(Integer... intValues) {
    ArrayList list = new ArrayList(
    for ( Integer i : intValues ) {
      list.add(i);
    }
    Collections.sort(list);
    return list.toArray();
}

, java >= 1.5, 1 n ... , , ++ asm:)

+1

public boolean endOther(String a, String b) {
    return a.length() < b.length() ? endOtherImpl(b,a):endOtherImpl(a,b);
}

protected boolean endOtherImpl(String longStr,String shortStr)
{
    return longStr.toLowerCase().endsWith(shortStr.toLowerCase());
}
0

Source: https://habr.com/ru/post/1739742/


All Articles