Jump to content

Talk:Two identical strings: Difference between revisions

→‎task clarification: added example code and output
(asked for clarification about structure of the binary literals)
(→‎task clarification: added example code and output)
Line 33:
 
The two "halves" ''are still'' identical, just reversed.--[[User:Enter your username|Enter your username]] ([[User talk:Enter your username|talk]]) 10:55, 3 April 2021 (UTC)
 
Here is an example program to count the different ways:
<lang csharp>using System; using static System.Console; using System.Collections.Generic;
class Program {
static void fun(bool anylead, bool palin, bool verbose = false) {
var res = new List<int>();
string s = "01", t;
for (int a = 0; a < 2; a++)
for (int b = 0; b < 2; b++)
for (int c = 0; c < 2; c++)
for (int d = 0; d < 2; d++)
for (int e = 0; e < 2; e++) {
t = "" + s[a] + s[b] + s[c] + s[d] + s[e];
for (int i = 0; i < 5; i++) {
int x = Convert.ToInt32("" + t + t, 2);
if (t[0] == '1' || anylead) if (!res.Contains(x) && x < 1000) { if (verbose) { Write("{0} ", "" + t + t); Write("({0}) ", x); } res.Add(x); }
if (palin) {
string tt = t; for (int j = t.Length - 1; j >= 0; j--) tt += t[j];
x = Convert.ToInt32(tt, 2); if (!res.Contains(x) && x < 1000) { if (verbose) {Write("{0} ", tt); Write("({0}) ", x); } res.Add(x); }
}
t = t.Substring(1);
}
}
if (verbose) WriteLine();
Write(". {0,20}, {1,5} palindromes: ", anylead ? "any leading digit" : "only starts with '1'", palin ? "allow" : "no");
WriteLine(res.Count); res.Sort(); if (verbose) WriteLine(string.Join(" ", res));
}
 
static void Main(string[] args) {
fun(true, true);
fun(!true, true);
fun(true, !true);
fun(!true, !true);
}
}</lang>
{{out}}
<pre>. any leading digit, allow palindromes: 91
. only starts with '1', allow palindromes: 76
. any leading digit, no palindromes: 57
. only starts with '1', no palindromes: 30</pre>--[[User:Enter your username|Enter your username]] ([[User talk:Enter your username|talk]]) 11:41, 3 April 2021 (UTC)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.