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