HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

For each line in a file, read two integers and output the minimum

Submitted by: @import:stackexchange-codereview··
0
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:

3
5 3
2 8
100 15




answer:

3 2 15


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

Solution

There are a few methods that will make the task easier for you:

  • File.ReadLines to read the file line-by-line (see also File.ReadAllLines)



  • Enumerable.Skip to skip the first line



  • Enumerable.Select to map the the lines to the minimum of the two numbers



  • string.Join for 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.