Skip to content

Instantly share code, notes, and snippets.

@oscarryz
Created September 25, 2012 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarryz/3782297 to your computer and use it in GitHub Desktop.
Save oscarryz/3782297 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Cuenta {
public static void main(String ... args ) {
Cuenta cuenta = new Cuenta();
List<Character> list = new ArrayList<Character>();
for( char c : "abcedario".toCharArray()) {
list.add( c );
}
System.out.println( cuenta.cuenta('a', list ));
System.out.println( cuenta.cuenta('z', list ));
}
int cuenta( char c, List<Character> list ) {
if ( list.isEmpty() ) return 0;
if ( head(list) == c ) return cuenta(c, tail(list))+1;
return cuenta(c, tail(list));
}
char head(List<Character> list ) {
return list.get(0);
}
List<Character> tail( List<Character> list ) {
return list.subList(1, list.size() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment