Is there a way to set custom cost for different operations? Like:
Replace = 1
Insert = 1.5
Delete = 1.2
The closest I found is https://github.com/glienard/StringSimilarity.NET with Weighted Levenshtein, but I could not find out how to actually use it.
My (simplified) version is something like this.
Help classes:
[Flags]
public enum EditOperationKind {
None = 0,
Insert = 1,
Delete = 2,
Edit = Insert | Delete
}
public sealed class EditOperation<T>
: IEquatable<EditOperation<T>> {
internal string ToReport() {
switch (Kind) {
case EditOperationKind.None: return $"Keep '{Before}', ({Cost})";
case EditOperationKind.Insert: return $"Insert '{After}', ({Cost})";
case EditOperationKind.Delete: return $"Delete '{Before}', ({Cost})";
case EditOperationKind.Edit: return $"Edit '{Before}' into '{After}', ({Cost})";
default: return $"??? '{Before}' into '{After}', ({Cost})";
};
}
internal EditOperation(EditOperationKind kind, T before, T after, double cost)
: base() {
Kind = kind;
Before = before;
After = after;
Cost = cost;
}
public EditOperationKind Kind { get; }
public T Before { get; }
public T After { get; }
public double Cost { get; private set; }
public override string ToString() {
switch (Kind)
{
case EditOperationKind.None: return $"Keep '{Before}'";
case EditOperationKind.Insert: return $"Insert '{After}'";
case EditOperationKind.Delete: return $"Delete '{Before}'";
case EditOperationKind.Edit: return $"Edit '{Before}' into '{After}'";
default: return $"Unknown '{Before}' into '{After}'";
};
}
public static bool operator ==(EditOperation<T> left, EditOperation<T> right) {
if (ReferenceEquals(left, right))
return true;
else if (null == left || null == right)
return false;
return left.Equals(right);
}
public static bool operator !=(EditOperation<T> left, EditOperation<T> right) {
if (ReferenceEquals(left, right))
return false;
else if (null == left || null == right)
return true;
return !left.Equals(right);
}
public bool Equals(EditOperation<T> other) {
if (ReferenceEquals(this, other))
return true;
else if (null == other)
return false;
return object.Equals(this.Before, other.Before) &&
object.Equals(this.After, other.After) &&
this.Kind == other.Kind;
}
public override bool Equals(object obj) => Equals(obj as EditOperation<T>);
public override int GetHashCode() {
return (Before == null ? 0 : Before.GetHashCode()) ^
(After == null ? 0 : After.GetHashCode()) ^
(int)Kind;
}
}
Main class:
public sealed class EditProcedure<T> : IReadOnlyList<EditOperation<T>> {
private List<EditOperation<T>> m_Sequence;
private void CorePerform(T[] source,
T[] target,
Func<T, double> insertCost,
Func<T, double> deleteCost,
Func<T, T, double> editCost) {
// Best operation (among insert, update, delete) to perform
EditOperationKind[][] M = Enumerable
.Range(0, source.Length + 1)
.Select(line => new EditOperationKind[target.Length + 1])
.ToArray();
// Minimum cost so far
double[][] D = Enumerable
.Range(0, source.Length + 1)
.Select(line => new double[target.Length + 1])
.ToArray();
// Edge: all removes
double sum = 0.0;
for (int i = 1; i <= source.Length; ++i) {
M[i][0] = EditOperationKind.Delete;
D[i][0] = (sum += deleteCost(source[i - 1]));
}
// Edge: all inserts
sum = 0.0;
for (int i = 1; i <= target.Length; ++i) {
M[0][i] = EditOperationKind.Insert;
D[0][i] = (sum += insertCost(target[i - 1]));
}
// Having fit N - 1, K - 1 characters let's fit N, K
for (int i = 1; i <= source.Length; ++i)
for (int j = 1; j <= target.Length; ++j) {
// here we choose the operation with the least cost
double insert = D[i][j - 1] + insertCost(target[j - 1]);
double delete = D[i - 1][j] + deleteCost(source[i - 1]);
double edit = D[i - 1][j - 1] + editCost(source[i - 1], target[j - 1]);
double min = Math.Min(Math.Min(insert, delete), edit);
if (min == insert)
M[i][j] = EditOperationKind.Insert;
else if (min == delete)
M[i][j] = EditOperationKind.Delete;
else if (min == edit)
M[i][j] = object.Equals(source[i - 1], target[j - 1])
? EditOperationKind.None
: EditOperationKind.Edit;
D[i][j] = min;
}
EditDistance = D[source.Length][target.Length];
// Backward: knowing scores (D) and actions (M) let's building edit sequence
m_Sequence =
new List<EditOperation<T>>(source.Length + target.Length);
for (int x = target.Length, y = source.Length; (x > 0) || (y > 0);) {
EditOperationKind op = M[y][x];
if (op == EditOperationKind.Insert) {
x -= 1;
m_Sequence.Add(new EditOperation<T>(op, default, target[x], D[y][x + 1] - D[y][x]));
}
else if (op == EditOperationKind.Delete) {
y -= 1;
m_Sequence.Add(new EditOperation<T>(op, source[y], default, D[y + 1][x] - D[y][x]));
}
else if (op == EditOperationKind.Edit || op == EditOperationKind.None) {
x -= 1;
y -= 1;
m_Sequence.Add(new EditOperation<T>(op, source[y], target[x], D[y + 1][x + 1] - D[y][x]));
}
else // Start of the matching (EditOperationKind.None)
break;
}
m_Sequence.Reverse();
}
public EditProcedure(IEnumerable<T> source,
IEnumerable<T> target,
Func<T, double> insertCost,
Func<T, double> deleteCost,
Func<T, T, double> editCost) {
if (null == source)
throw new ArgumentNullException(nameof(source));
else if (null == target)
throw new ArgumentNullException(nameof(target));
else if (null == insertCost)
throw new ArgumentNullException(nameof(insertCost));
else if (null == deleteCost)
throw new ArgumentNullException(nameof(deleteCost));
else if (null == editCost)
throw new ArgumentNullException(nameof(editCost));
CorePerform(source.ToArray(),
target.ToArray(),
insertCost,
deleteCost,
editCost);
}
public double EditDistance { get; private set; }
public IReadOnlyList<EditOperation<T>> EditSequence => m_Sequence;
public override string ToString() {
return string.Join(Environment.NewLine,
$"Distance: {EditDistance}",
$"Sequence ({m_Sequence.Count} steps):",
string.Join(Environment.NewLine, m_Sequence
.Select(item => $" {item.ToReport()}")));
}
public int Count => m_Sequence.Count;
public EditOperation<T> this[int index] => m_Sequence[index];
public IEnumerator<EditOperation<T>> GetEnumerator() => m_Sequence.GetEnumerator();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => m_Sequence.GetEnumerator();
}
Demo:
string source = "abracadabra";
string target = "alakazam";
var edit = new EditProcedure<char>(
source,
target,
insertChar => 1.5,
deleteChar => 1.2,
(fromChar, toChar) => fromChar == toChar ? 0 : 1.0);
Console.WriteLine(edit.EditDistance);
Console.WriteLine();
Console.WriteLine(edit.ToString());
Outcome:
7.6
Distance: 7.6
Sequence (11 steps):
Keep 'a', (0)
Edit 'b' into 'l', (1)
Delete 'r', (1.2)
Keep 'a', (0)
Edit 'c' into 'k', (1)
Keep 'a', (0)
Edit 'd' into 'z', (1)
Keep 'a', (0)
Edit 'b' into 'm', (1)
Delete 'r', (1.2)
Delete 'a', (1.2)
Somebody had posted an answer just to remove it 1 hour later. I modified it a bit and here is the result:
public class Levenshtein
{
public double ReplaceCost { get; set; } = 1;
public double DeleteCost { get; set; } = 1;
public double InsertCost { get; set; } = 1;
public double Distance(string source, string target, bool caseSensitive = true)
{
if (!caseSensitive)
{
source = source.ToUpperInvariant();
target = target.ToUpperInvariant();
}
int xLength = source.Length;
int yLength = target.Length;
//short circuit
if (xLength == 0) return yLength;
if (yLength == 0) return xLength;
//create path matrix
var d = new double[xLength + 1, yLength + 1];
for (int i = 0; i <= xLength; i++)
{
d[i, 0] = i;
}
for (int j = 0; j <= yLength; j++)
{
d[0, j] = j;
}
//navigate best route
for (int i = 1; i <= xLength; i++)
{
for (int j = 1; j <= yLength; j++)
{
double diagCost = (target[j - 1] == source[i - 1]) ? 0 : ReplaceCost;
double diagMovement = d[i - 1, j - 1] + diagCost; //replace
double horzMovement = d[i - 1, j] + DeleteCost; //delete
double vertMovement = d[i, j - 1] + InsertCost; //insert (does not appear in source)
d[i, j] = Math.Min(Math.Min(horzMovement, vertMovement), diagMovement);
}
}
return d[xLength, yLength];
}
}
Related
I'm trying to validate a password string in a .NET for sequential patterns (forward or reverse) with numbers or letters of 5 or more.
Examples of patterns that will not be accepted:
"ABCDE",
"12345",
"54321",
"edcba"
I cannot find a decent regex pattern that handles finding the characters in order, currently just returning any sequence of 5 letters or numbers:
public bool CheckForSequence(string input)
{
return Regex.IsMatch(input.ToUpper(), #"([A-Z])(?!\1)([A-Z])(?!\1|\2)([A-Z])(?!\1|\2|\3)([A-Z])(?!\1|\2|\3|\4)([A-Z])") ||
Regex.IsMatch(input, #"([1-9])(?!\1)([1-9])(?!\1|\2)([1-9])(?!\1|\2|\3)([1-9])(?!\1|\2|\3|\4)([1-9])");
}
There are probably way better ways to do this, but, just for fun, I've made a simple brute-force algorithm:
bool CheckForSequence(string inp) {
bool InRange(int c) {
const int minLower = (int)'a';
const int maxLower = (int)'z';
const int minUpper = (int)'A';
const int maxUpper = (int)'Z';
const int minNumber = (int)'0';
const int maxNumber = (int)'9';
return (c >= minLower && c <= maxLower) || (c >= minUpper && c <= maxUpper) || (c >= minNumber && c <= maxNumber);
}
if(inp.Length < 5) return false;
for(var i = 0; i < inp.Length - 4; i++)
{
var c = (int)inp[i];
if(InRange(c))
{
var vM = c;
int x;
for(x = i+1; x < i + 5; x++)
{
if(inp[x] != vM+1 || !InRange(inp[x])) break;
vM++;
}
if(x == i+5) return true;
for(x = i+1; x < i + 5; x++)
{
if(inp[x] != vM-1 || !InRange(inp[x])) break;
vM--;
}
if(x == i+5) return true;
}
}
return false;
}
You can see it in action in this fiddle
Wiktor is correct - regex is the wrong tool for this.
Here's one possible implementation:
public static class SequenceChecker
{
private static char MapChar(char c) => c switch
{
>= '0' and <= '9' => c,
>= 'A' and <= 'Z' => c,
>= 'a' and <= 'z' => (char)(c - 'a' + 'A'),
_ => default,
};
private static bool IsSequence(ReadOnlySpan<char> input)
{
char x = MapChar(input[0]);
if (x == default) return false;
char y = MapChar(input[1]);
if (y == default) return false;
int direction = y - x;
if (Math.Abs(direction) != 1) return false;
for (int index = 2; index < input.Length; index++)
{
x = y;
y = MapChar(input[index]);
if (y == default) return false;
int nextDirection = y - x;
if (nextDirection != direction) return false;
}
return true;
}
public static bool ContainsSequence(string input, int sequenceLength = 5)
{
if (sequenceLength < 2) throw new ArgumentOutOfRangeException(nameof(sequenceLength));
if (input is null) return false;
if (input.Length < sequenceLength) return false;
for (int startIndex = 0; startIndex < 1 + input.Length - sequenceLength; startIndex++)
{
if (IsSequence(input.AsSpan(startIndex, sequenceLength)))
{
return true;
}
}
return false;
}
}
Just to add to the plethora of solutions posted so far:
public static int LongestAscendingOrDescendingRun(string s)
{
if (s.Length <= 1)
return 0;
int longest = 0;
int current = 0;
bool ascending = false;
for (int i = 1; i < s.Length; i++)
{
bool isAscending () => s[i]-s[i-1] == +1;
bool isDescending() => s[i]-s[i-1] == -1;
if (current > 0)
{
if (ascending)
{
if (isAscending())
{
longest = Math.Max(longest, ++current);
}
else // No longer ascending.
{
current = 0;
}
}
else // Descending.
{
if (isDescending())
{
longest = Math.Max(longest, ++current);
}
else // No longer descending.
{
current = 0;
}
}
}
else // No current.
{
if (isAscending())
{
ascending = true;
current = 2;
longest = Math.Max(longest, current);
}
else if (isDescending())
{
ascending = false;
current = 2;
longest = Math.Max(longest, current);
}
}
}
return longest;
}
Like Wiktor has already said, regex isn't a good way to do this. You could find the difference between consecutive characters of the string, and complain if you find a sequence of four or more ones (or -1s).
public bool CheckForSequence(string pass)
{
int curr_diff = 0; // The difference between the i-1th and i-2th character
int consec_diff = 0; // The number of consecutive pairs having the same difference
for (int i = 1; i < pass.Length; i++)
{
int diff = pass[i] - pass[i - 1]; // The difference between the ith and i-1th character
if (Math.Abs(diff) == 1 && curr_diff == diff)
{
// If the difference is the same, increment consec_diff
// And check if the password is invalid
consec_diff++;
if (consec_diff >= 4)
return false;
}
else
{
// New diff. reset curr_diff and consec_diff
curr_diff = diff;
consec_diff = Math.Abs(diff)==1 ? 1 : 0;
// If the difference is 1, set consec_diff to 1 else 0
}
}
return consec_diff < 4;
}
I'm trying to get the job done MOHIBPIZ - PIZZA (https://www.spoj.com/problems/MOHIBPIZ/).
I'm already sitting on it the second day, I've tried everything I can and found on the internet. The last chance before giving up is to ask you guys
For recudces time I'm using InputOutput class created by davidsekar (https://github.com/davidsekar/C-sharp-Programming-IO/blob/master/ConsoleInOut/InputOutput.cs)
but still I have time "time limit exceeded". :(
I tried with two loops, but the method with the function seems more optimal to me. Thanks in advance for all the hints, suggestions and answers.
This is code (link on ideone: https://ideone.com/):
using System;
using System.IO;
public class Test
{
public static void Main()
{
InputOutput reader = new InputOutput();
StreamWriter _output = new StreamWriter(Console.OpenStandardOutput());
int T = reader.ReadInt();
for (int i = 0; i < T; i++)
{
_output.WriteLine(Recursion(reader.ReadInt()));
}
_output.Flush();
}
private static int Recursion(int x)
{
if(x <= 1)
{
return 2;
}
else
{
return Recursion(x - 1) + x;
}
}
#region Input Output Helper
public class InputOutput : System.IDisposable
{
private System.IO.Stream _readStream, _writeStream;
private int _readIdx, _bytesRead, _writeIdx, _inBuffSize, _outBuffSize;
private readonly byte[] _inBuff, _outBuff;
private readonly bool _bThrowErrorOnEof;
public void SetBuffSize(int n)
{
_inBuffSize = _outBuffSize = n;
}
public InputOutput(bool throwEndOfInputsError = false)
{
_readStream = System.Console.OpenStandardInput();
_writeStream = System.Console.OpenStandardOutput();
_readIdx = _bytesRead = _writeIdx = 0;
_inBuffSize = _outBuffSize = 1 << 22;
_inBuff = new byte[_inBuffSize];
_outBuff = new byte[_outBuffSize];
_bThrowErrorOnEof = throwEndOfInputsError;
}
public void SetFilePath(string strPath)
{
strPath = System.IO.Path.GetFullPath(strPath);
_readStream = System.IO.File.Open(strPath, System.IO.FileMode.Open);
}
public T ReadNumber<T>()
{
byte rb;
while ((rb = GetByte()) < '-')
;
var neg = false;
if (rb == '-')
{
neg = true;
rb = GetByte();
}
dynamic m = (T)Convert.ChangeType(rb - '0', typeof(T));
while (true)
{
rb = GetByte();
if (rb < '0')
break;
m = m * 10 + (rb - '0');
}
return neg ? -m : m;
}
public int ReadInt()
{
byte readByte;
while ((readByte = GetByte()) < '-')
;
var neg = false;
if (readByte == '-')
{
neg = true;
readByte = GetByte();
}
var m = readByte - '0';
while (true)
{
readByte = GetByte();
if (readByte < '0')
break;
m = m * 10 + (readByte - '0');
}
return neg ? -m : m;
}
public string ReadString()
{
return ReadString(' ');
}
public string ReadString(string delimiter)
{
return ReadString(delimiter[0]);
}
public string ReadString(char delimiter)
{
byte readByte;
while ((readByte = GetByte()) <= delimiter)
;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
do
{
sb.Append((char)readByte);
} while ((readByte = GetByte()) > delimiter);
return sb.ToString();
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private byte GetByte()
{
if (_readIdx >= _bytesRead)
{
_readIdx = 0;
_bytesRead = _readStream.Read(_inBuff, 0, _inBuffSize);
if (_bytesRead >= 1)
return _inBuff[_readIdx++];
if (_bThrowErrorOnEof)
throw new System.Exception("End Of Input");
_inBuff[_bytesRead++] = 0;
}
return _inBuff[_readIdx++];
}
public void WriteToBuffer(string s)
{
foreach (var b in System.Text.Encoding.ASCII.GetBytes(s))
{
if (_writeIdx == _outBuffSize)
Flush();
_outBuff[_writeIdx++] = b;
}
}
public void WriteLineToBuffer(string s)
{
WriteToBuffer(s);
if (_writeIdx == _outBuffSize)
Flush();
_outBuff[_writeIdx++] = 10;
}
public void WriteToBuffer(int c)
{
byte[] temp = new byte[10];
int tempidx = 0;
if (c < 0)
{
if (_writeIdx == _outBuffSize)
Flush();
_outBuff[_writeIdx++] = (byte)'-';
c = -c;
}
do
{
temp[tempidx++] = (byte)((c % 10) + '0');
c /= 10;
} while (c > 0);
for (int i = tempidx - 1; i >= 0; i--)
{
if (_writeIdx == _outBuffSize)
Flush();
_outBuff[_writeIdx++] = temp[i];
}
}
public void WriteLineToBuffer(int c)
{
WriteToBuffer(c);
if (_writeIdx == _outBuffSize)
Flush();
_outBuff[_writeIdx++] = 10;
}
private void Flush()
{
_writeStream.Write(_outBuff, 0, _writeIdx);
_writeStream.Flush();
_writeIdx = 0;
}
public void Dispose()
{
Flush();
_writeStream.Close();
_readStream.Close();
}
}
#endregion Input Output Helper
}
As far as I can see, you have a well known Circle Division problem; see also A000124 sequence:
number of pieces after n cuts are (n * n + n + 2) / 2
That's why we can put O(1) time and space complexity
Code:
private static int Solution(int n) => (int)(((long)n * n + n + 2) / 2);
Here I've put (long) n in case n * n exceeds int.MaxValue, when (n * n + n + 2) / 2 doesn't.
Edit: I've implemented int Solution(int n) method which is based on current code int Recursion(int x) signature; but if there're tests for large n we are going to have integer overflow.
In this case
private static long Solution(long n) =>
1 + (n % 2 == 0 ? n / 2 * (n + 1) : (n + 1) / 2 * n);
In case of arbitrary n we have to use BigInteger:
using System.Numerics;
...
private static BigInteger Solution(BigInteger n) =>
1 + (n * n + n) / 2;
Im trying to write a quicksort algorithm in C#, and I've been getting System.StackOverflowExceptions for the last while and can't figure out why.
Here is my Class:
class quicksort : sortalgorithm
{
int pivotIndex = -1;
int pivotSwapped = 0;
Random rand = new Random();
public quicksort(int[] arr)
{
if (arr.Length < 5)
{
throw new Exception("Array has too few Entries");
}
toSort = arr;
}
protected override int sort()
{
if (pivotIndex == -1) getPivot();
int indexL = getIndexLeft();
int indexR = getIndexRight();
if (indexR != -1 && indexL != -1 && indexL != toSort.Length-1)
{
swap(indexL, indexR);
}
if (indexL > indexR)
{
Console.WriteLine("Index thingy");
swap(toSort.Length - 1, indexL);
pivotSwapped++;
if (pivotSwapped == toSort.Length - 1)
{
return 1;
}
else
{
Console.WriteLine("new piv");
pivotSwapped++;
getPivot();
sort();
return -1;
}
}
else
{
sort();
return -1;
}
}
private void swap(int i, int j)
{
int temp = toSort[i];
toSort[i] = toSort[j];
toSort[j] = temp;
}
private void getPivot()
{
pivotIndex = rand.Next(0, toSort.Length - 1);
swap(toSort.Length - 1, pivotIndex);
}
private int getIndexLeft() // Larger then Pivot Starting: Left
{ //Error Here
int itemFromLeft = -1;
for (int i = 0; i < toSort.Length - 1; i++)
{
if (toSort[i] >= toSort[toSort.Length - 1])
{
itemFromLeft = i;
i = toSort.Length + 1;
}
}
//Console.WriteLine(itemFromLeft);
return itemFromLeft;
}
private int getIndexRight()// Smaller then Pivot Starting: Right
{
int itemFromRight = -1;
for (int i = toSort.Length - 1; i >= 0; i--)
{
if (toSort[i] < toSort[toSort.Length - 1])
{
itemFromRight = i;
i = -1;
}
}
//Console.WriteLine(itemFromRight);
return itemFromRight;
}
}
I hope that someone can help me.
P.S. the class sortalgorithm in this case only has the the array toSort.
My Console output always looks a bit like this:
[4, 28, 62, 33, 11] // The unsorted array
Index thingy
new piv
Index thingy
new piv
Index thingy
new piv
Process is terminated due to `StackOverflowException`.
a stack overflow error usually happens when you have a error in your recursion , ie a function keeps calling itself until there is no room left in the stack to hold all the references,
the only obvious candidate is
**sort();**
return -1;
}
}
else
{
**sort();**
return -1;
}
so either one or the other recursive calls is probably incorrect and needs removing and i suspect the issue will be resolved
EDIT:
as you are unable to debug your code
this is what a quick sort should look like
public int sort()
{
qsort(0, toSort.Length - 1);
return 0;
}
private void qsort( int left, int right)
{
if (left < right)
{
int pivot = Partition( left, right);
if (pivot > 1)
{
qsort( left, pivot - 1);
}
if (pivot + 1 < right)
{
qsort( pivot + 1, right);
}
}
}
private int Partition( int left, int right)
{
int pivot = toSort[left];
while (true)
{
while (toSort[left] < pivot)
{
left++;
}
while (toSort[right] > pivot)
{
right--;
}
if (left < right)
{
if (toSort[left] == toSort[right]) return right;
int temp = toSort[left];
toSort[left] = toSort[right];
toSort[right] = temp;
}
else
{
return right;
}
}
}
Note that if left >= right do nothing
This simple implementation of Hoare partition scheme demonstrates how to avoid stack overflow by recursing on the smaller partition, and looping back for the larger partition, an idea used in the original quicksort. Stack space complexity is limited to O(log2(n)), but worst case time complexity is still O(n^2).
static public void Quicksort(int [] a, int lo, int hi)
{
while(lo < hi){ // while partition size > 1
int p = a[(lo + hi) / 2];
int i = lo, j = hi;
i--; // Hoare partition
j++;
while (true)
{
while (a[++i] < p) ;
while (a[--j] > p) ;
if (i >= j)
break;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
i = j++; // i = last left, j = first right
if((i - lo) <= (hi - j)){ // if size(left) <= size(right)
Quicksort(a, lo, i); // recurse on smaller partition
lo = j; // and loop on larger partition
} else {
Quicksort(a, j, hi); // recurse on smaller partition
hi = i; // and loop on larger partition
}
}
}
I am working on solving a puzzle online and stumbled upon this problem where given a 2D matrix and a number k, I need to return the kth smallest element in the matrix.
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
],
k = 8,
return 13.
I am able to solve this problem with my own implementation of a Binary Heap. Since I am preparing for interviews, I am not sure if implementing my own Heap is an acceptable solution in all cases. So I tried to solve this with SortedList/SortedSet.
I am basically creating a Point object that takes index i, j and the value at i, j. I have implemented IComparable and IEquatable. But for some reason, with the above example, the Point object for index 1,2(value 13) and the one for index 2,1(value 13) are being considered equal when they shouldn't be. I get an ArgumentException when using a SortedList, a SortedSet meanwhile just overwrites the existing object. Is my implementation of IEquatable, IComparable wrong? I have double checked that they generate different hashcodes.
P.S. This is not a homework problem. I am solving questions from an online interview prep platform.
public class Solution {
public int KthSmallest(int[,] matrix, int k) {
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
SortedSet<Point> pq = new SortedSet<Point>();
bool[,] visited = new bool[rows, cols];
int count = 1;
int i=0, j=0;
var start = new Point(i, j, matrix[i, j]);
pq.Add(start);
visited[0, 0] = true;
while(true) {
k--;
var curr = pq.Min;
pq.Remove(pq.First());
if(k == 0)
return curr.val;
i = curr.x + 1;
j = curr.y;
if(i < rows && j < cols && !visited[i, j]) {
var next = new Point(i, j, matrix[i, j]);
Console.WriteLine(next.GetHashCode());
Console.WriteLine(i+", "+j+", "+next.val);
pq.Add(next);
visited[i, j] = true;
}
i = curr.x;
j = curr.y + 1;
if(i < rows && j < cols && !visited[i, j]) {
var next = new Point(i, j, matrix[i, j]);
Console.WriteLine(next.GetHashCode());
Console.WriteLine(i+", "+j+", "+next.val);
pq.Add(next);
visited[i, j] = true;
}
}
}
}
public class Point : IComparable<Point>, IEquatable<Point>
{
public int x { get; private set; }
public int y { get; private set; }
public int val { get; private set; }
public Point(int x, int y, int val)
{
this.x = x;
this.y = y;
this.val = val;
}
public int CompareTo(Point that)
{
if(this.val == that.val) {
if(this.x == that.x) {
return this.y.CompareTo(that.y);
}
else {
this.x.CompareTo(that.x);
}
}
return val.CompareTo(that.val);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Point)obj);
}
public bool Equals(Point p1) {
return x == p1.x && y == p1.y && val == p1.val;
}
public override int GetHashCode() {
long hashCode = ((x * 31 + y) * 31 + val);
return hashCode.GetHashCode();
}
}
You're missing a return statement in your CompareTo. I commented your original below, along with a corrected version. In the case of comparing [2,1] and [1,2], the x values don't match, but when you hit the this.x.CompareTo, you never actually return that comparison, so instead your value comparison returns.
You have:
public int CompareTo(Point that)
{
if(this.val == that.val) {
if(this.x == that.x) {
return this.y.CompareTo(that.y);
}
else {
//****MISSING RETURN STATEMENT -
//will return the val.ComapreTo statement after
//it leaves this block*****
this.x.CompareTo(that.x);
}
}
return val.CompareTo(that.val);
}
You need:
public int CompareTo(Point that)
{
if(this.val == that.val) {
if(this.x == that.x) {
return this.y.CompareTo(that.y);
}
else {
return this.x.CompareTo(that.x);
}
}
return val.CompareTo(that.val);
}
Is it possible to generate all permutations of a collection in c#?
char[] inputSet = { 'A','B','C' };
Permutations<char> permutations = new Permutations<char>(inputSet);
foreach (IList<char> p in permutations)
{
Console.WriteLine(String.Format("{{{0} {1} {2}}}", p[0], p[1], p[2]));
}
I've already faced the problem and I wrote these simple methods:
public static IList<T[]> GeneratePermutations<T>(T[] objs, long? limit)
{
var result = new List<T[]>();
long n = Factorial(objs.Length);
n = (!limit.HasValue || limit.Value > n) ? n : (limit.Value);
for (long k = 0; k < n; k++)
{
T[] kperm = GenerateKthPermutation<T>(k, objs);
result.Add(kperm);
}
return result;
}
public static T[] GenerateKthPermutation<T>(long k, T[] objs)
{
T[] permutedObjs = new T[objs.Length];
for (int i = 0; i < objs.Length; i++)
{
permutedObjs[i] = objs[i];
}
for (int j = 2; j < objs.Length + 1; j++)
{
k = k / (j - 1); // integer division cuts off the remainder
long i1 = (k % j);
long i2 = j - 1;
if (i1 != i2)
{
T tmpObj1 = permutedObjs[i1];
T tmpObj2 = permutedObjs[i2];
permutedObjs[i1] = tmpObj2;
permutedObjs[i2] = tmpObj1;
}
}
return permutedObjs;
}
public static long Factorial(int n)
{
if (n < 0) { throw new Exception("Unaccepted input for factorial"); } //error result - undefined
if (n > 256) { throw new Exception("Input too big for factorial"); } //error result - input is too big
if (n == 0) { return 1; }
// Calculate the factorial iteratively rather than recursively:
long tempResult = 1;
for (int i = 1; i <= n; i++)
{
tempResult *= i;
}
return tempResult;
}
Usage:
var perms = Utilities.GeneratePermutations<char>(new char[]{'A','B','C'}, null);
There's nothing built in.
I've found a a couple of Code Project articles here and here which might help you implement your own class.
public static void Recursion(char[] charList, int loBound, int upBound )
{
for (int i = loBound; i <= upBound; i++)
{
swap(ref charList[loBound], ref charList[i]);
if (loBound == upBound)
{
Console.Write(charList);
Console.WriteLine("");
}
Recursion(charList, loBound + 1, upBound);
swap(ref charList[loBound], ref charList[i]);
}
}
public static void swap(ref char a, ref char b)
{
if (a == b) return;
a ^= b;
b ^= a;
a ^= b;
}
public static void Main(string[] args)
{
string c = "123";
char[] c2 = c.ToCharArray();
Recursion(c2, 0, c2.Length-1);
Console.ReadKey();
}
I built these Extensions Methods for Enumerable<T>
The following Generics Methods can find Permutations as well as Combinations of an IEnumerable of any type.
Visit http://github.com/MathewSachin/Equamatics for more
using System;
using System.Collections.Generic;
using System.Linq;
namespace Equamatics
{
public static class Combinatorics
{
#region Permute
public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> Input, int r = -1)
{
int n = Input.Count();
if (r == -1) foreach (var item in new Permutor<T>(Input).Recursion(0))
yield return item;
if (r > n) throw new ArgumentOutOfRangeException("r cannot be greater than no of elements");
foreach (var list in Input.Combinations(r))
foreach (var item in new Permutor<T>(list).Recursion(0))
yield return item;
}
class Permutor<T>
{
int ElementLevel = -1;
int[] PermutationValue;
T[] Elements;
public Permutor(IEnumerable<T> Input)
{
Elements = Input.ToArray();
PermutationValue = new int[Input.Count()];
}
public IEnumerable<IEnumerable<T>> Recursion(int k)
{
ElementLevel++;
PermutationValue[k] = ElementLevel;
if (ElementLevel == Elements.Length)
{
List<T> t = new List<T>();
foreach (int i in PermutationValue) t.Add(Elements[i - 1]);
yield return t;
}
else
for (int i = 0; i < Elements.Length; i++)
if (PermutationValue[i] == 0)
foreach (IEnumerable<T> e in Recursion(i))
yield return e;
ElementLevel--;
PermutationValue[k] = 0;
}
}
public static double P(int n, int r)
{
if (r < 0 | n < 0 | n < r) return Double.NaN;
else if (r == 0) return 1;
else if (n == r) return Factorial(n);
else
{
double Product = 1;
for (int i = n - r + 1; i <= n; ++i) Product *= i;
return Product;
}
}
#endregion
#region Combinations
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> Input, int r = -1)
{
if (r == -1)
{
yield return Input;
yield break;
}
int n = Input.Count();
if (r > n) throw new ArgumentOutOfRangeException("r cannot be greater than no of elements");
int[] Indices = Enumerable.Range(0, r).ToArray();
yield return Indices.Select(k => Input.ElementAt(k));
while (true)
{
int i;
for (i = r - 1; i >= 0; --i)
if (Indices[i] != i + n - r)
break;
if (i < 0) break;
Indices[i] += 1;
for (int j = i + 1; j < r; ++j)
Indices[j] = Indices[j - 1] + 1;
yield return Indices.Select(k => Input.ElementAt(k));
}
}
public static double C(int n, int r)
{
if (r < 0 | n < 0 | n < r) return Double.NaN;
else if (n - r == 1 | r == 1) return n;
else if (n == r | r == 0) return 1;
else if (n - r > r) return (P(n, n - r) / Factorial(n - r));
else return (P(n, r) / Factorial(r));
}
#endregion
public static double Factorial(int n)
{
if (n < 0) return Double.NaN;
else if (n == 0) return 1;
else
{
double Product = 1;
for (int i = 1; i <= n; ++i) Product *= i;
return Product;
}
}
public static int Derangement(int n)
{
double x = 0;
for (int i = 2; i <= n; ++i)
{
if (i % 2 == 0) x += (1 / Factorial(i));
else x -= (1 / Factorial(i));
}
return (int)(Factorial(n) * x);
}
public static int Catalan(int n) { return (int)C(2 * n, n) / (n + 1); }
}
}
`
A simple implementation using recursion
static void Main(string[] args)
{
char[] inputSet = { 'A', 'B', 'C' };
var permutations = GetPermutations(new string(inputSet));
foreach (var p in permutations)
{
Console.WriteLine(String.Format("{{{0} {1} {2}}}", p[0], p[1], p[2]));
}
}
public static List<string> GetPermutations(string str)
{
List<string> result = new List<string>();
if (str == null)
return null;
if (str.Length == 0)
{
result.Add("");
return result;
}
char c = str.ElementAt(0);
var perms = GetPermutations(str.Substring(1));
foreach (var word in perms)
{
for (int i = 0; i <= word.Length; i++)
{
result.Add(word.Substring(0, i) + c + word.Substring(i));
}
}
return result;
}