Home > Enterprise >  Best way to print variables depending on a given input order, like ABC or CAB in C#
Best way to print variables depending on a given input order, like ABC or CAB in C#

Time:03-15

I'm trying to learn C#, so I'm doing assignments on open.kattis.com. I have three variables called A, B and C and have to print them in a given order. A is the smallest variable, B is the middle variable and C is the largest variable.

I give the variables 1, 2, 5 and the order ABC, so the output print is 1 2 5 I give the variables 1, 10, 22 and the order CAB, so the output is 22 1 10

My question however is to improve my code. Right now I'm checking the input-order in a switch-case and was wondering if there is a better way.

My submission can be seen here:

using System;
using System.Linq;

class Program
{
    public static void Main(string[] args)
    {
        string[] input = Console.ReadLine().Split(' ');
        string order = Console.ReadLine();
        int[] inputArray = {0, 0, 0};
        int A = 0, B = 0, C = 0;

        for (int i = 0; i < input.Length; i  )
        {
            inputArray[i] = Int32.Parse(input[i]);
        }
        A = inputArray.Min();
        C = inputArray.Max();
        for (int i = 0; i < inputArray.Length; i  )
        {
            if (inputArray[i] != A && inputArray[i] != C)
            {
                B = inputArray[i];
            }
        }

        switch (order)
        {
            case "ABC":
                Console.WriteLine($"{A} {B} {C}");
                break;
            case "ACB":
                Console.WriteLine($"{A} {C} {B}");
                break;
            case "BAC":
                Console.WriteLine($"{B} {A} {C}");
                break;
            case "BCA":
                Console.WriteLine($"{B} {C} {A}");
                break;
            case "CBA":
                Console.WriteLine($"{C} {B} {A}");
                break;
            case "CAB":
                Console.WriteLine($"{C} {A} {B}");
                break;
        }
        
    }
}

CodePudding user response:

Let's generalize the solution. So you have data array:

int[] inputArray = new int[] {1, 2, 3}

and order:

string order = "BAC"; 

Now we can loop over order and output corresponding array's item:

for (char letter in order)
  Console.Write($"{inputArray[letter - 'A']} ");

Here we convert letters from order (B, A, C) into indexes (1, 0, 2)

Please note, that we can well use array, say, of 2 or 4 items providing that we have corresponding order

Code:

public static void Main(string[] args) {
  string[] input = Console
    .ReadLine()
    .Split(' ', StringSplitOptions.RemoveEmptyEntries);

  int[] inputArray = new int[input.Length];

  for (int i = 0; i < input.Length;   i)
    inputArray[i] = int.Parse(input[i]);

  Array.Sort(inputArray);

  string order = Console.ReadLine();

  for (char letter in order)
    Console.Write($"{inputArray[letter - 'A']} ");
}

CodePudding user response:

Your app:

class Program
{
    public static void Main(string[] args)
    {
        var input = Console.ReadLine().Split(' ').Select(int.Parse).OrderBy(x=> x).ToArray();
        var order = Console.ReadLine().Select(x=> x-'A');

        var output = string.Join(" ", order.Select(x => input[x]));
        Console.WriteLine(output);
    }
}

Work for custom number of inputs.

CodePudding user response:

One answer is super advanced and the other answer fails for any unsorted input, so here's my shot at helping you out.

You can generalise, for any number of inputs, two important parts of your task:

  • Getting min, max, middle (or min, ..., max for >3 items): this can be done by sorting the collection.
  • Translating order expressed as letters to the indexes of the elements in the now sorted array.

Min, Middle, Max

var a = new[] {22, 10, 1};
Array.Sort(a);

var min = a[0];
var middle = a[1];
var max = a[2];

Console.WriteLine($"{min}, {middle}, {max}");

This prints

1, 10, 21

From CAB to 2,0,1 (indexes)

Characters (~letters) in C# have number values and we can exploit that.

char c1 = 'A';
char c2 = 'B';

Console.WriteLine((byte)c1);
Console.WriteLine((byte)c2);
Console.WriteLine(c1 - 'A');
Console.WriteLine(c2 - 'A');

This prints

65
66
0
1

Knowing this let's try to translate "CAB":

string order = "CAB";

foreach(char c in order)
{
    int index = c - 'A';
    Console.WriteLine($"Character '{c}' is index: {index}");
}

This prints:

Character 'C' is index: 2
Character 'A' is index: 0
Character 'B' is index: 1

Solution

Equipped with these two transformations you should be able to generalise your solution.

  • Related