-7

I need help writing my method that accepts a single parameter long i:

public static string GetWord(long i)
{
    string s = "";

    //Update s using i

    return s;
}

...for my program that saves a file of ASCII words...

public static void main(string[] args) 
{
    try
    {
        int First = int.Parse(args[0]);
        int Last = int.Parse(args[1])
        string Filename = args[2]

        for(int i = start; i <= end; i++)
             File.AppendLine(Filename, GetWord(i));

        Console.WriteLine("Process complete");

    } 
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);

    }

}

...with the following pattern.

GetWord(0) should be the result of Encoding.ASCII.GetString(new byte[]{ 0 });

GetWord(1) should be the result of Encoding.ASCII.GetString(new byte[]{ 1 });

GetWord(2) should be the result of Encoding.ASCII.GetString(new byte[]{ 2 });

...and so on until GetWord(127).

GetWord(128) should be the result of GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 0 });

GetWord(129) should be the result of GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 1 });

GetWord(130) should be the result of GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 2 });

...and so on until GetWord(255).

GetWord(256) should be the result of GetWord(1) + Encoding.ASCII.GetString(new byte[]{ 0 });

GetWord(257) should be the result of GetWord(1) + Encoding.ASCII.GetString(new byte[]{ 1 });

GetWord(258) should be the result of GetWord(1) + Encoding.ASCII.GetString(new byte[]{ 2 });

...and so on until GetWord(16383).

GetWord(16384) should be the result of GetWord(0) + GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 0 });

GetWord(16385) should be the result of GetWord(0) + GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 1 });

GetWord(16386) should be the result of GetWord(0) + GetWord(0) + Encoding.ASCII.GetString(new byte[]{ 2 });

Writing the pattern down helps me think about the code. I hope this makes sense to everyone. I think I need to use a mixture of recursion and modulus to get this to work.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
  • 1
    So you want a parameter type of long in your method, to loop until that number generating words? – Greg Nov 28 '17 at 15:21
  • 6
    As a user with 3.3k rep you should know that "I need a method" is not asking for help, but for solution :-) – Adrian Nov 28 '17 at 15:23
  • @Adriani6 Thanks, but I don't see your point. I am however listening. Please explain. – WonderWorker Nov 28 '17 at 15:31
  • 1
    I'm voting to close this question as off-topic because, as you do, I had a method that did exactly that very thing but unfortunately I left it in a taxi cab. I was much younger then. How that algorithm comes up, mixing memory and desire... – 15ee8f99-57ff-4f92-890c-b56153 Nov 28 '17 at 15:32
  • 1
    My point is, we enforce users to share what they tried so we can help them fix their issue and not spoon-feed what they want :-) No harm intended in my comment by the way. – Adrian Nov 28 '17 at 15:33
  • @Adriani6 None taken. Sorry if it seems like I'm looking to be spoon-fed. It isn't quite the case, although I will take the spoon if it's offered. I'm working on a solution too, so it isn't like I'm just waiting for someone with my mouth open lol. – WonderWorker Nov 28 '17 at 15:35
  • 1
    Note: Word[128] through Word[255] are all going to be "?". – Tom Blodget Nov 28 '17 at 16:33
  • This question does indeed meet the requirements, which are; desired behaviour, a specific problem and shortest code necessary to reproduce the bug. They may be considered subtle, but clearly met all the same. – WonderWorker Nov 30 '17 at 15:35

2 Answers2

2

I'm just gonna give you the algorithm.

Notice that every 256 is a cycle. Starting from index 256, you will be appending to Word[0], starting 512 you will be appending to Word[1] and starting 65536 you will be appending to Word[0] + Word[0] which is Word[256].

So the algorithm is to take the index/256 - 1 as the index you will be appending to and index%256 will be the byte you need to append

Steve
  • 11,696
  • 7
  • 43
  • 81
0

The solution I came up with is as follows:

public void AppendFile(string filePath, long firstWord, long lastWord)
{            
    using (StreamWriter sw = File.AppendText(filePath))
    {
        for (long i = firstWord; i < lastWord; i++)
        {
            sw.WriteLine(GetWord(i));

        }

    }

}

public void AppendFile(string filePath, long lastWord)
{
    AppendFile(filePath, 0, lastWord);

}

public void AppendFile(string filePath)
{
    AppendFile(filePath, long.MaxValue);

}

public static string GetWord(long i)
{
    string s = Encoding.ASCII.GetString(new byte[] { (byte)(i % 128) });

    if (i < 128)
        return s;

    return GetWord(i / 128) + s;

}

Use in the following way:

AppendFile("words.txt"); // Will most likely fall over or at least take a long time

AppendFile("words.txt", 1000); // This is the method requested

AppendFile("words.txt", 500, 1000); // Extended functionality

Note: I chose to NOT use the algorithm from Steve's answer. The reason I did not use Steve's algorithm is because he relies on the full array of words being resident in memory during the full procedure, which restricts the output file to the amount of free available RAM. My version doesn't have this restriction, and is restricted only to the max possible length of a string.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74