Skip to content

Instantly share code, notes, and snippets.

@kissarat
Created December 3, 2020 14:38
Show Gist options
  • Save kissarat/77eb5b1dc587d3b24e0c9131b63cdbc9 to your computer and use it in GitHub Desktop.
Save kissarat/77eb5b1dc587d3b24e0c9131b63cdbc9 to your computer and use it in GitHub Desktop.
using System;
namespace Program
{
public class Program
{
public const string Impossible = "IMPOSSIBLE";
public const string StringEquals = "EQUALS";
public const string Insert = "INSERT {0}";
public const string Replace = "REPLACE {0} {1}";
public const string Swap = "SWAP {0} {1}";
public static int Main(string[] args)
{
Console.WriteLine("Taras Labiak test task (https://kissarat.github.io/). Please enter two words splitted by space");
while(true) {
var output = Console.ReadLine();
if (0 == output.Length) {
break;
}
var parts = output.Trim().Split(' ');
if (parts.Length != 2) {
Console.WriteLine("Invalid number of arguments");
continue;
}
Console.WriteLine(Compare(parts[0], parts[1]));
}
return 0;
}
public static string Compare(string first, string second) {
if (first.Equals(second)) {
return StringEquals;
}
var possiblyInsert = second.Length == first.Length + 1;
var last = first.Length - 1;
if (!(second.Length == first.Length || possiblyInsert)) {
return Impossible;
}
for(int i = 0; i < first.Length; i++) {
if (possiblyInsert) {
if (first.Substring(i).Equals(second.Substring(i+1))) {
return String.Format(Insert, second[i]);
}
}
if (first[i] == second[i]) {
continue;
}
if (first.Substring(i+1).Equals(second.Substring(i+1))) {
return String.Format(Replace, first[i], second[i]);
}
if (i < last) {
if (first[i] == second[i + 1] && first[i + 1] == second[i]) {
if (first.Substring(i+2).Equals(second.Substring(i+2))) {
return String.Format(Swap, first[i], first[i + 1]);
}
}
}
return Impossible;
}
return StringEquals;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment