Java 7, 725 bajtów
f(int)( 325 bajtów ):
String f(int i){String s="";for(int j=0,e=0;e<i;e+=v(s))s=Integer.toBinaryString(j++);return"["+s.replace("1","[").replace("0","]")+"]";}int v(String s){for(;!s.isEmpty();s=s.replaceFirst("1","").replaceFirst("0",""))if(s.replace("1","").length()!=s.replace("0","").length()|s.charAt(0)<49|s.endsWith("1"))return 0;return 1;}
g(String)( 75 + 325 bajtów ):
int g(String s){int r=0;for(String i="10";!i.equals(s);i=f(++r));return r;}
Ponieważ metoda gużywa metody fdo obliczenia wyniku, zapętlając nad możliwą listą pustek, aż znajdzie jeden równy wprowadzonemu, bajty z fsą liczone dwukrotnie (ponieważ obie metody powinny być w stanie działać bez drugiej dla tego wyzwania).
Wyjaśnienie:
Ogólnie metoda fpo prostu zapętla wszystkie binarne reprezentacje ciągu liczb całkowitych i zwiększa licznik za każdym razem, gdy zostanie znaleziona poprawna. Prawidłowe ciągi binarne dla tego wyzwania są zgodne z następującymi zasadami: zaczynają się na 1, a kończą na 0; mają taką samą liczbę 1 i 0; i za każdym razem, gdy usuniesz pierwszy 1i 0ponownie zweryfikujesz to, co zostało, te dwie reguły nadal obowiązują. Po licznik jest równy wkład, to zamienia to binarny string na ciąg pustych liście, zastępując wszystko 1z [a wszystko 0z ].
Jeśli chodzi o metodę g: zaczynamy od "[]"(reprezentuje listę pustek 0), a następnie kontynuujemy używanie metody f, zwiększając liczbę całkowitą, aż do dopasowania ciągu wejściowego.
String f(int i){ // Method `f` with integer parameter and String return-type
String s=""; // Start with an empty String
for(int j=0,e=0;e<i; // Loop as long as `e` does not equal the input
e+=v(s)) // And append increase integer `e` if String `s` is valid
s=Integer.toBinaryString(j++);
// Change `s` to the next byte-String of integer `j`
// End of loop (implicit / single-line body)
return"["+ // Return the result String encapsulated in "[" and "]"
s.replace("1","[").replace("0","]")+"]";
// after we've replaced all 1s with "[" and all 0s with "]"
} // End of method `f`
int v(String s){ // Separate method with String parameter and integer return-type
for(;!s.isEmpty(); // Loop as long as String `s` isn't empty
s=s.replaceFirst("1","").replaceFirst("0",""))
// After each iteration: Remove the first "1" and "0"
if(s.replace("1","").length()!=s.replace("0","").length()
// If there isn't an equal amount of 1s and 0s
|s.charAt(0)<49 // or the String doesn't start with a 1
|s.endsWith("1")) // or the String doesn't end with a 0
return 0; // Return 0 (String is not valid)
// End of loop (implicit / single-line body)
return 1; // Return 1 (String is valid)
} // End of separate method
int g(String s){ // Method `g` with String parameter and integer return-type
int r=0; // Result integer
for(String i="[]";!i.equals(s);
// Loop as long as `i` does not equal the input String
i=f(++r)); // After each iteration: Set `i` to the next String in line
return r; // Return the result integer
} // End of method `g`
Przykładowe przypadki wejścia i wyjścia:
Wypróbuj tutaj. (UWAGA: w ostatnich kilku przypadkach testowych jest dość wolny. Dla wszystkich zajmie to około 10-15 sekund.)
0 <-> []
1 <-> [[]]
2 <-> [[][]]
3 <-> [[[]]]
4 <-> [[][][]]
5 <-> [[][[]]]
6 <-> [[[]][]]
7 <-> [[[][]]]
8 <-> [[[[]]]]
9 <-> [[][][][]]
10 <-> [[][][[]]]
11 <-> [[][[]][]]
12 <-> [[][[][]]]
13 <-> [[][[[]]]]
14 <-> [[[]][][]]
50 <-> [[[][[[]]]]]
383 <-> [[[][]][[[][]]]]