জাভা 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; তাদের 1 এবং 0 এর সমান সংখ্যক রয়েছে; এবং প্রত্যেক সময় আপনি প্রথম অপসারণ 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`
ইনপুট এবং আউটপুট কেসগুলির উদাহরণ:
এখানে চেষ্টা করুন। (দ্রষ্টব্য: গত কয়েকটি পরীক্ষার ক্ষেত্রে এটি বেশ ধীর।
0 <-> []
1 <-> [[]]
2 <-> [[][]]
3 <-> [[[]]]
4 <-> [[][][]]
5 <-> [[][[]]]
6 <-> [[[]][]]
7 <-> [[[][]]]
8 <-> [[[[]]]]
9 <-> [[][][][]]
10 <-> [[][][[]]]
11 <-> [[][[]][]]
12 <-> [[][[][]]]
13 <-> [[][[[]]]]
14 <-> [[[]][][]]
50 <-> [[[][[[]]]]]
383 <-> [[[][]][[[][]]]]