|
Never mind Wikipedia, this is how actual charities get on sh*tlists.
|
|
|
|
|
It's also known as the Conway Sequence, Look and Say Sequence, and probably some others. It's rather simple. Start with a 1 and then describe what you see for the next iteration. So, starting at 1, the next number is one 1 (11), the next is two 1 (21), then one 2 one 1 (1211), and so on:
1
11
21
1211
111221
312211
The question to answer is what's the length in digits of the 100th number in the chain, starting with "1" as the first? The first six numbers have been given above. You could write it out by hand, but I wouldn't recommend it, and as developers, that's not what we do.
The seemingly simple challenge is to write the code to come up with the answer. The only hint you get is the 50th number is 894,810 digits long.
Oh, and don't bother Googling for code. Those examples will only get you so far and definitely won't get you to the answer.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
|
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
Now why do I want to hit you with a stick?
|
|
|
|
|
Because you're curious and have no idea why you're driven to solve the problem?
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
I think I got a working code:
string LookAndSay(int S, int n)
{
string result = S.ToString();
for (int i = 0; i < (n - 1); i++)
{
StringBuilder NewString = new StringBuilder();
for (int j = 0; j < result.Length; j++)
{
string part = RepetingItems(j, result);
j += (int)char.GetNumericValue(part[0]) - 1;
NewString.Append(part);
}
result = NewString.ToString();
}
return result;
}
string RepetingItems(int index, string array)
{
int count = 1;
for (int i = index + 1; i < array.Length; i++)
{
if (array[index] == array[i])
{ count++; }
else { break; }
}
return (count.ToString() + array[index].ToString());
}
Got your result for the length of 50 iterations, and 100, well... I get an out of memory exception . But I do see that the last digits do not change, so I bet I can shave off quite a bit of numbers that I do not need to read. This is way more interesting than reading for the exam, so I should hate you now
|
|
|
|
|
|
Oh yes
|
|
|
|
|
I ended up doing a brute force method and was surprised at what I ran into along the way and what the final answer was. That's all I'm going to say.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
modified 30-Nov-17 19:52pm.
|
|
|
|
|
Then I'll have to use the harddisk and do IO handling... The pain...
|
|
|
|
|
How much memory do you have? (I want to see if it's worth the try on my PC)
modified 19-Nov-18 21:01pm.
|
|
|
|
|
The numbers were floating out on my desk onto my paper and now it's a real mess here
|
|
|
|
|
Not so easy, is it?
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
Yes, these strings get huge, don't they? The whole thing really slows down in a very visible way once the lengths start to reach the 100,000s (40th iteration on) and gets ultra slow from there on in.
I'll leave mine running for a while and see exactly where it crashes and burns. Max chars for a StringBuilder are 2,147,483,647, I believe and it's clearly going to hit that at some point so I guess the true answer as you suggest involves hiving off the repeating elements.
My code (spectacularly Q&D C# - written on a whim in "do ... while" loops because someone recently said "for" loops are for dinosaurs) looks like this:
static void Main(string[] args)
{
string result = ("1");
<pre>
int i = 1;
do
{
Console.WriteLine(i.ToString() + "\t" + result.Length.ToString());
result = GetNextResult(result);
i += 1;
}
while (i < 101);
Console.ReadLine();
}
static string GetNextResult(string origin)
{
char character;
int occurences;
StringBuilder result = new StringBuilder();
do
{
if (origin.Length == 0)
break;
ProcessNextGroup(origin, out character, out occurences);
origin = origin.Substring(occurences);
result.Append(occurences.ToString());
result.Append(character.ToString());
GC.Collect();
}
while (true);
return result.ToString();
}
static void ProcessNextGroup(string sequence, out char character, out int occurences)
{
int index = 0;
character = sequence[0];
occurences = 1;
do
{
if (++index > (sequence.Length -1))
break;
if (sequence[index] == character)
occurences += 1;
else
break;
}
while (true);
}
}</pre>
98.4% of statistics are made up on the spot.
|
|
|
|
|
PeejayAdams wrote: written on a whim in "do ... while" loops because someone recently said "for" loops are for dinosaurs
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
No, the problem isnt that the strings are limited. You can load into memory the items you want, and perform the LookNSay from the partial string. When that is read to its end you simply load the next part into memory. Its just a little more complicated that's all:
string LookAndSayFile(int S, int n)
{
int SplitStringSize = 2;
string projectPath = System.IO.Path.GetFullPath(@"..\..\..\..\");
File.Delete(projectPath + "output.txt");
File.Delete(projectPath + "input.txt");
System.IO.File.WriteAllLines(projectPath + "output.txt", new string[] { string.Empty });
System.IO.File.WriteAllLines(projectPath + "input.txt", new string[] { S.ToString() });
for (int i = 0; i < (n - 1); i++)
{
char currentItem = '#';
int count = 1;
StringBuilder NewString = new StringBuilder();
using (StreamReader sr = new StreamReader(projectPath + "input.txt"))
{
while (sr.Peek() >= 0)
{
string res = sr.ReadLine();
foreach (char j in res)
{
if (currentItem == '#')
{
currentItem = j;
continue;
}
if (currentItem == j)
{
count++;
}
else
{
NewString.Append(count.ToString() + currentItem.ToString());
if (NewString.Length > SplitStringSize)
{
System.IO.File.AppendAllLines(projectPath + "output.txt", new string[] { NewString.ToString() });
NewString.Length = 0;
}
currentItem = j;
count = 1;
continue;
}
}
}
}
NewString.Append(count.ToString() + currentItem.ToString());
System.IO.File.AppendAllLines(projectPath + "output.txt", new string[] { NewString.ToString() });
File.Delete(projectPath + "input.txt");
System.IO.File.Copy(projectPath + "output.txt", projectPath + "input.txt");
System.IO.File.WriteAllText(projectPath + "output.txt", string.Empty);
}
return String.Join(Environment.NewLine, System.IO.File.ReadAllLines(projectPath + "input.txt"));
}
I 'think' it's working, although the string.join does not wanna play with me. Keeps returning a vector of strings.
|
|
|
|
|
Oh, the integer sequence are difined for a bit larger range than Dave gave here:
A006751 - OEIS[^]
|
|
|
|
|
That's cheating!
Oh, and what you linked to starts the sequence with a 2, not a 1.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
I have been running the code (which I posted here somewhere) in the background as I'm reading for the exam. The text file that just holds the current number is 1.6 GB, and I guess that's one way of measuring the length. So I'll definitely post the end number on the Lounge forum Then it will at last contain something worthwhile
|
|
|
|
|
PS. Do you want the text file?
...
...
...
|
|
|
|
|
Good luck posting it!
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
I have a day off, so I thought I would make use of this challenge to try TDD(test-driven development).
It's actually a rather good example for TDD and I am finding that writing the tests first is making this challenge a lot faster and even more enjoyable to develop a solution to.
Thanks for the challenge
[edit]I started off using stacks to do the calculations and soon ran out of memory.
I am now using a less processor intensive method(a secret...) and the application got to line 77 and the length of the line is 1,149,440,192 digits long - flippin' heck!
No stack overflow either, as the way I have written the application means that all that will happen is that my disk space will run out - I am not sure if I want to find that out...
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
modified 1-Dec-17 10:10am.
|
|
|
|
|
Strings? Why not use a List of Tuple<short,short> ?
After more thought and before reading Richard's response...
Tuple<nybble,nybble>
Using a byte to implement such a Tuple.
Having read Richard's response...
Huh, yeah, use a byte to implement a
Tuple<Tuple<nyblet,nyblet>,Tuple<nyblet,nyblet>> ...
I would also try to use many threads.
modified 2-Dec-17 1:07am.
|
|
|
|
|
This one's a real bugger for memory.
String-based approaches are obviously out - 16 bits to store each character is overkill when the only symbols you need to store are 1 , 2 and 3 .
List<byte> is obviously not going to work, because it would need to allocate an array big enough to hold the entire sequence.
LinkedList<byte> has to create an object for every byte in the list, so the overhead far outweighs the payload.
I settled on a custom singly-linked list of byte arrays, re-using two instances (previous and current) to reduce memory churn. But even that was eating huge amounts of memory.
Finally, realising that the only numbers in the sequence are 1 , 2 and 3 , I decided to stuff four numbers into each byte, which brings the memory usage under control. However, it still takes a damn long time to run, and I haven't left it for long enough to get to the 100th iteration yet.
Morris Sequence · GitHub[^]
Having spent far too long thinking about this, now's the time for you to tell me there's some secret trick to calculate the sequence without having to store the whole thing.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|