Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Coding Challenge - Morris Sequence

Coding Challenge - Morris Sequence

Scheduled Pinned Locked Moved The Lounge
questioncsharpcomdebuggingtutorial
98 Posts 15 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Dave Kreskowiak

    :laugh: 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. :-D

    System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
    Dave Kreskowiak

    K Offline
    K Offline
    Kenneth Haugland
    wrote on last edited by
    #10

    Then I'll have to use the harddisk and do IO handling... The pain... :sigh:

    1 Reply Last reply
    0
    • K Kenneth Haugland

      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 :D. 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 :D

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #11

      How much memory do you have? (I want to see if it's worth the try on my PC) ;)

      K 1 Reply Last reply
      0
      • L Lost User

        How much memory do you have? (I want to see if it's worth the try on my PC) ;)

        K Offline
        K Offline
        Kenneth Haugland
        wrote on last edited by
        #12

        The numbers were floating out on my desk onto my paper and now it's a real mess here :D

        D 1 Reply Last reply
        0
        • D Dave Kreskowiak

          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

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #13

          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 :thumbsup: [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

          1 Reply Last reply
          0
          • K Kenneth Haugland

            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 :D. 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 :D

            P Offline
            P Offline
            PeejayAdams
            wrote on last edited by
            #14

            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");
            
                    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);
                }
            }
            

            98.4% of statistics are made up on the spot.

            S K 3 Replies Last reply
            0
            • P PeejayAdams

              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");
              
                      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);
                  }
              }
              

              98.4% of statistics are made up on the spot.

              S Offline
              S Offline
              Sascha Lefevre
              wrote on last edited by
              #15

              PeejayAdams wrote:

              written on a whim in "do ... while" loops because someone recently said "for" loops are for dinosaurs

              :laugh:

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              1 Reply Last reply
              0
              • P PeejayAdams

                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");
                
                        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);
                    }
                }
                

                98.4% of statistics are made up on the spot.

                K Offline
                K Offline
                Kenneth Haugland
                wrote on last edited by
                #16

                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)
                {
                //If the string is above this length split it into multiple strings
                int SplitStringSize = 2;

                        //Setting the directory where files are stored relativce to the exe file
                        string projectPath = System.IO.Path.GetFullPath(@"..\\..\\..\\..\\");
                
                        //Delete files
                        File.Delete(projectPath + "output.txt");
                        File.Delete(projectPath + "input.txt");
                
                        //Add intput and and empty file 
                        System.IO.File.WriteAllLines(projectPath + "output.txt", new string\[\] { string.Empty });
                        System.IO.File.WriteAllLines(projectPath + "input.txt", new string\[\] { S.ToString() });
                
                
                        //Number of iterations you want to perform
                        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
                
                1 Reply Last reply
                0
                • P PeejayAdams

                  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");
                  
                          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);
                      }
                  }
                  

                  98.4% of statistics are made up on the spot.

                  K Offline
                  K Offline
                  Kenneth Haugland
                  wrote on last edited by
                  #17

                  Oh, the integer sequence are difined for a bit larger range than Dave gave here: A006751 - OEIS[^]

                  D 1 Reply Last reply
                  0
                  • K Kenneth Haugland

                    The numbers were floating out on my desk onto my paper and now it's a real mess here :D

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #18

                    :laugh: 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

                    1 Reply Last reply
                    0
                    • K Kenneth Haugland

                      Oh, the integer sequence are difined for a bit larger range than Dave gave here: A006751 - OEIS[^]

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #19

                      That's cheating! :-D 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

                      K 2 Replies Last reply
                      0
                      • D Dave Kreskowiak

                        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

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #20

                        Strings? Why not use a List of Tuple ? :) After more thought and before reading Richard's response... Tuple Using a byte to implement such a Tuple. Having read Richard's response... Huh, yeah, use a byte to implement a Tuple,Tuple>... :D I would also try to use many threads.

                        1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          That's cheating! :-D 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

                          K Offline
                          K Offline
                          Kenneth Haugland
                          wrote on last edited by
                          #21

                          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 :d Then it will at last contain something worthwhile :laugh:

                          1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            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

                            Richard DeemingR Offline
                            Richard DeemingR Offline
                            Richard Deeming
                            wrote on last edited by
                            #22

                            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. :-D


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                            G K 3 Replies Last reply
                            0
                            • D Dave Kreskowiak

                              That's cheating! :-D 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

                              K Offline
                              K Offline
                              Kenneth Haugland
                              wrote on last edited by
                              #23

                              PS. Do you want the text file? ... ... ... :laugh: :laugh: :laugh:

                              D 1 Reply Last reply
                              0
                              • Richard DeemingR Richard Deeming

                                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. :-D


                                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                G Offline
                                G Offline
                                GuyThiebaut
                                wrote on last edited by
                                #24

                                Here we go - breaking the no code in the lounge rule here...

                                internal void Stream(int upTo)
                                {

                                    using (StreamWriter writer = new StreamWriter("E:\\\\temp\\\\MorrisSequence\\\\" + "line1.txt"))
                                    {
                                        writer.Write("1");
                                    }
                                
                                    for (int i = 1; i <= upTo; i++)
                                    {
                                        Console.WriteLine(i);
                                
                                        using (StreamReader reader = new StreamReader("E:\\\\temp\\\\MorrisSequence\\\\" + "line" + i + ".txt"))
                                        {
                                            int count = 1;
                                            char currChar = (char)reader.Read();
                                            char lastChar = currChar;
                                            char nextChar;
                                            string writeNum = (i + 1).ToString();
                                
                                            using (StreamWriter writer = new StreamWriter("E:\\\\temp\\\\MorrisSequence\\\\" + "line" + writeNum + ".txt"))
                                            {
                                                while (reader.Peek() >= 0)
                                                {
                                                    nextChar = (char)reader.Peek();
                                                    if (nextChar != lastChar)
                                                    {
                                                        writer.Write(count.ToString() + currChar.ToString());
                                                        count = 0;
                                                    }
                                                    currChar= (char)reader.Read();
                                                    lastChar = currChar;
                                                    count++;
                                                }
                                                writer.Write(count.ToString() + currChar.ToString());
                                            }
                                        }
                                    }
                                }
                                

                                [edit] small tidy up, giving filenames proper names that relate to what they contain.

                                “That which can be asserted without evidence, can be dismissed without evidence.”

                                ― Christopher Hitchens

                                Richard DeemingR 1 Reply Last reply
                                0
                                • D Dave Kreskowiak

                                  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

                                  K Offline
                                  K Offline
                                  Kirk 10389821
                                  wrote on last edited by
                                  #25

                                  I used a new AI program I trained on twitter... It responded: 1 Really large string of numbers NOBODY cares about, just like you. Delete yourself. I added "Plz" and it simply said "Go away Troll" I am having second thoughts about it having access to: - NEST devices (including garage door, locking all doors) - Internet access to unlock and start my car - IP Phone / Router... So I can't call for hel...

                                  1 Reply Last reply
                                  0
                                  • G GuyThiebaut

                                    Here we go - breaking the no code in the lounge rule here...

                                    internal void Stream(int upTo)
                                    {

                                        using (StreamWriter writer = new StreamWriter("E:\\\\temp\\\\MorrisSequence\\\\" + "line1.txt"))
                                        {
                                            writer.Write("1");
                                        }
                                    
                                        for (int i = 1; i <= upTo; i++)
                                        {
                                            Console.WriteLine(i);
                                    
                                            using (StreamReader reader = new StreamReader("E:\\\\temp\\\\MorrisSequence\\\\" + "line" + i + ".txt"))
                                            {
                                                int count = 1;
                                                char currChar = (char)reader.Read();
                                                char lastChar = currChar;
                                                char nextChar;
                                                string writeNum = (i + 1).ToString();
                                    
                                                using (StreamWriter writer = new StreamWriter("E:\\\\temp\\\\MorrisSequence\\\\" + "line" + writeNum + ".txt"))
                                                {
                                                    while (reader.Peek() >= 0)
                                                    {
                                                        nextChar = (char)reader.Peek();
                                                        if (nextChar != lastChar)
                                                        {
                                                            writer.Write(count.ToString() + currChar.ToString());
                                                            count = 0;
                                                        }
                                                        currChar= (char)reader.Read();
                                                        lastChar = currChar;
                                                        count++;
                                                    }
                                                    writer.Write(count.ToString() + currChar.ToString());
                                                }
                                            }
                                        }
                                    }
                                    

                                    [edit] small tidy up, giving filenames proper names that relate to what they contain.

                                    “That which can be asserted without evidence, can be dismissed without evidence.”

                                    ― Christopher Hitchens

                                    Richard DeemingR Offline
                                    Richard DeemingR Offline
                                    Richard Deeming
                                    wrote on last edited by
                                    #26

                                    That's one way around it. But I hope you've got a large SSD! :-D Is there a reason you're writing strings instead of bytes?


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                    G 1 Reply Last reply
                                    0
                                    • Richard DeemingR Richard Deeming

                                      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. :-D


                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                      K Offline
                                      K Offline
                                      Kenneth Haugland
                                      wrote on last edited by
                                      #27

                                      Doesn't look like there is a know shortcut: Look-and-say sequence - Rosetta Code[^] But they do post a formula here: A005150 - OEIS[^] But it looks complicated. I used chars, and the txt file is 2 GB now :S

                                      1 Reply Last reply
                                      0
                                      • Richard DeemingR Richard Deeming

                                        That's one way around it. But I hope you've got a large SSD! :-D Is there a reason you're writing strings instead of bytes?


                                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                        G Offline
                                        G Offline
                                        GuyThiebaut
                                        wrote on last edited by
                                        #28

                                        I was tempted to leave it running this evening but as you mention I think it will fill up the disk space - file 77 is just over 1 gig in size and it's only a text file. What I may do is compress then delete files prior to the one I am currently reading(the 1 gig file compresses to 80mb largely because it is composed of 1s,2s and 3s).

                                        Richard Deeming wrote:

                                        Is there a reason you're writing strings instead of bytes?

                                        Um er yes, um err, um er because... that idea never occurred to me - thanks for the tip :thumbsup:

                                        “That which can be asserted without evidence, can be dismissed without evidence.”

                                        ― Christopher Hitchens

                                        K 1 Reply Last reply
                                        0
                                        • G GuyThiebaut

                                          I was tempted to leave it running this evening but as you mention I think it will fill up the disk space - file 77 is just over 1 gig in size and it's only a text file. What I may do is compress then delete files prior to the one I am currently reading(the 1 gig file compresses to 80mb largely because it is composed of 1s,2s and 3s).

                                          Richard Deeming wrote:

                                          Is there a reason you're writing strings instead of bytes?

                                          Um er yes, um err, um er because... that idea never occurred to me - thanks for the tip :thumbsup:

                                          “That which can be asserted without evidence, can be dismissed without evidence.”

                                          ― Christopher Hitchens

                                          K Offline
                                          K Offline
                                          Kenneth Haugland
                                          wrote on last edited by
                                          #29

                                          I have already text files over 2.2 GB so I think you'll have to delete them as you gom at least that's what I do. And I think using bytes is cheating :laugh: also I didn't know that 3 would be the highest number. I don't think is enough not if you start at 3,4,5 or any other number, at least I got some 5 then. Or my code was wrong.

                                          G Richard DeemingR 2 Replies Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups