जावा 7, 725 बाइट्स
f(int)( 325 बाइट्स ):
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 बाइट्स ):
int g(String s){int r=0;for(String i="10";!i.equals(s);i=f(++r));return r;}
चूंकि विधि gपद्धति fकी गणना करने के लिए विधि का उपयोग करती है, यह संभव शून्य-सूची पर लूपिंग द्वारा परिणामित होता है जब तक कि यह एक इनपुट के बराबर नहीं मिलता है, बाइट्स को fदो बार गिना जाता है (चूंकि दोनों तरीके इस चुनौती के लिए दूसरे के बिना चलने में सक्षम होना चाहिए)।
स्पष्टीकरण:
सामान्य तौर पर, विधि fकेवल पूर्णांक के सभी बाइनरी स्ट्रिंग-अभ्यावेदन पर लूप करती है, और हर बार एक वैध मिलने पर एक काउंटर बढ़ाती है। इस चुनौती के लिए मान्य बाइनरी-स्ट्रिंग्स निम्नलिखित नियमों का पालन करते हैं: वे ए से शुरू करते हैं 1, और ए के साथ समाप्त होते हैं0 ; उनके पास 1s और 0s की समान संख्या है; और हर बार जब आप पहली हटाने 1और 0और मान्य क्या फिर से छोड़ दिया है, इन दो नियम अब भी लागू होते हैं। काउंटर इनपुट के बराबर होने के बाद, यह सभी के 1साथ [और सभी के 0साथ बदलकर, बाइनरी-स्ट्रिंग को स्ट्रिंग शून्य-सूची में कनवर्ट करता है ]।
विधि के रूप में g: हम साथ शुरू करते हैं"[]" (शून्य-सूची का प्रतिनिधित्व करते हुए 0) के करते हैं , और तब fपूर्णांक को बढ़ाते समय विधि का उपयोग करना जारी रखते हैं , जब तक कि यह इनपुट-स्ट्रिंग से मेल नहीं खाता।
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`
उदाहरण इनपुट और आउटपुट मामले:
इसे यहाँ आज़माएँ। (नोट: यह पिछले कुछ परीक्षण मामलों के लिए बहुत धीमा है। इन सभी के लिए लगभग 10-15 सेकंड लगेंगे।)
0 <-> []
1 <-> [[]]
2 <-> [[][]]
3 <-> [[[]]]
4 <-> [[][][]]
5 <-> [[][[]]]
6 <-> [[[]][]]
7 <-> [[[][]]]
8 <-> [[[[]]]]
9 <-> [[][][][]]
10 <-> [[][][[]]]
11 <-> [[][[]][]]
12 <-> [[][[][]]]
13 <-> [[][[[]]]]
14 <-> [[[]][][]]
50 <-> [[[][[[]]]]]
383 <-> [[[][]][[[][]]]]