aalhour / aalhour/C-Sharp-Algorithms

IsAnagram bug (per element counts)

オープン 初心者向け
#153 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
C#
スター
6.2k
フォーク
1.4k
PR マージ指標
30日以内にマージされた PR はありません

説明

### Describe the bug
The `IsAnagram` function is not checking per-element counts. It is only checking if they have the same elements, but not if the count of each element matches.

A more appropriate name for the current logic is something like `ContainsNoDifferingElements` or `IntersectsMatch` rather than `IsAnagram`. I would recommend changing the name or the logic of the method.

> Note: If you aren't going to check per-element counts, then you should also get rid of this check in `IsAnagrams`:
> ```cs
> if (source.Length != other.Length)
> return false;
> ```
> because length doesn't matter if you don't also check per-element counts.

### To Reproduce
Add the following case to the `IsAnagram` unit tests:
```cs
string aab = "aab";
string abb = "abb";
Assert.False(Permutations.IsAnargram(aab, abb));
```

### Expected behavior
Spans of the same length and elements but different per-element counts should not be considered re-orders/anagrams of each other.

### Environment:
_master branch_

### Additional context
I have written my own version of this algorithm in C# _(that fixes this issue)_ if interested here...
> Source Code: https://github.com/ZacharyPatten/Towel/blob/d2660e208ad3a44ab22f192834760c5b93dc82ac/Sources/Towel/Statics-SequenceAnalysis.cs#L1321
> Examples: https://github.com/ZacharyPatten/Towel/blob/d2660e208ad3a44ab22f192834760c5b93dc82ac/Examples/BasicsAndExtensions/Program.cs#L406
> Testing: https://github.com/ZacharyPatten/Towel/blob/d2660e208ad3a44ab22f192834760c5b93dc82ac/Tools/Towel_Testing/Statics.cs#L2086
> _Note: `MapHashLinked` is my version of a `Dictionary` if you look at the source code._

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

問題は Permutations.IsAnagram メソッドにあります。現在の動作を理解するために、unit tests を確認してください。バグは、要素ごとの個数をチェックしていないことです。修正では、各文字の出現回数を数えるようにアルゴリズムを更新します。提供されている外部リンクに修正済みの実装があります。まず失敗するテストケースを追加し、その後、そのテストに合格するようにメソッドを変更してください。

索引モデルが issue の本文から書いたものです。

評価

領域
testing-qa
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
70/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。