patterncsharpMinor
For each line in a file, read two integers and output the minimum
Viewed 0 times
filetheeachlinereadminimumoutputtwoforand
Problem
I am going through some CodingAbbey problems to learn basic programming tasks, with a focus on taking input from a file, and saving to another. I have just completed this task:
Of two numbers, please, select one with minimum value. Here are several pairs of numbers for thorough testing.
Input data will contain number of test-cases in the first line.
Following lines will contain a pair of numbers to compare each.
For Answer please enter the same amount of minimums separated by space, for example:
data:
answer:
Here is my code:
```
using System;
namespace cosnPrjo
{
class MinimumOfTwo
{
const string inputPath = @"/Users/rs/Dropbox/InputFile.txt";
const string outputPath = @"/Users/rs/Dropbox/OutputFile.txt";
const string searchAfterParam = "\n";
static void Main ()
{
string input = readFile (inputPath);
input = input.Substring (input.IndexOf (searchAfterParam) + searchAfterParam.Length);
string[] pairDelims = { "\n" };
string[] paramDelims = { " " };
string[] pairs = input.Split (pairDelims, StringSplitOptions.RemoveEmptyEntries);
string result = "";
foreach (string pair in pairs) {
int numToAppend = 0;
string[] paramas = pair.Split (paramDelims, StringSplitOptions.RemoveEmptyEntries);
int firstNum = Convert.ToInt32 (paramas [0]);
int secondNum = Convert.ToInt32 (paramas [1]);
numToAppend = firstNum < secondNum ? firstNum : secondNum;
result += numToAppend.ToString () + " ";
}
writeFile (outputPath, result.TrimEnd ());
}
static string readFile (string input)
{
return System.IO.File.ReadAllText (input);
}
static void writeFile (string path, string contents)
{
System.IO.F
Of two numbers, please, select one with minimum value. Here are several pairs of numbers for thorough testing.
Input data will contain number of test-cases in the first line.
Following lines will contain a pair of numbers to compare each.
For Answer please enter the same amount of minimums separated by space, for example:
data:
3
5 3
2 8
100 15answer:
3 2 15Here is my code:
```
using System;
namespace cosnPrjo
{
class MinimumOfTwo
{
const string inputPath = @"/Users/rs/Dropbox/InputFile.txt";
const string outputPath = @"/Users/rs/Dropbox/OutputFile.txt";
const string searchAfterParam = "\n";
static void Main ()
{
string input = readFile (inputPath);
input = input.Substring (input.IndexOf (searchAfterParam) + searchAfterParam.Length);
string[] pairDelims = { "\n" };
string[] paramDelims = { " " };
string[] pairs = input.Split (pairDelims, StringSplitOptions.RemoveEmptyEntries);
string result = "";
foreach (string pair in pairs) {
int numToAppend = 0;
string[] paramas = pair.Split (paramDelims, StringSplitOptions.RemoveEmptyEntries);
int firstNum = Convert.ToInt32 (paramas [0]);
int secondNum = Convert.ToInt32 (paramas [1]);
numToAppend = firstNum < secondNum ? firstNum : secondNum;
result += numToAppend.ToString () + " ";
}
writeFile (outputPath, result.TrimEnd ());
}
static string readFile (string input)
{
return System.IO.File.ReadAllText (input);
}
static void writeFile (string path, string contents)
{
System.IO.F
Solution
There are a few methods that will make the task easier for you:
You might also consider taking the input and output file paths from the
I've been lazy here with error checking -- it would be a good exercise to add that to the code. It's also good practice to specify the file encoding when reading and writing files, which again I haven't shown here.
File.ReadLinesto read the file line-by-line (see alsoFile.ReadAllLines)
Enumerable.Skipto skip the first line
Enumerable.Selectto map the the lines to the minimum of the two numbers
string.Joinfor formatting the output
You might also consider taking the input and output file paths from the
args parameter. Putting it all together (without error checking) could look like this:public static void Main(string[] args)
{
var minimums = File.ReadLines(args[0]).Skip(1).Select(GetMinimum);
File.WriteAllText(args[1], string.Join(" ", minimums));
}
private static int GetMinimum(string line)
{
var numbers = line.Split(' ').Select(int.Parse).ToArray();
return Math.Min(numbers[0], numbers[1]);
}I've been lazy here with error checking -- it would be a good exercise to add that to the code. It's also good practice to specify the file encoding when reading and writing files, which again I haven't shown here.
Code Snippets
public static void Main(string[] args)
{
var minimums = File.ReadLines(args[0]).Skip(1).Select(GetMinimum);
File.WriteAllText(args[1], string.Join(" ", minimums));
}
private static int GetMinimum(string line)
{
var numbers = line.Split(' ').Select(int.Parse).ToArray();
return Math.Min(numbers[0], numbers[1]);
}Context
StackExchange Code Review Q#90367, answer score: 4
Revisions (0)
No revisions yet.