当其中任何一个变量可以是通配符时,检查 3 个变量是否相等的优雅方法是什么?
假设我有 3 个 char
变量:a
、b
和 c
。
每个都可以是'0'
,这是一种特殊情况,意味着它匹配每个字符。
因此,如果 a 是 '0'
,我只需要检查是否 b == c
。
我想检查是否 a == b == c
,但发现 C# 中的实现变得混乱且冗长。
您可以提供任何有创意或漂亮的解决方案吗?
更新
性能驱动的 ,采用 Erik A. Brandstadmoen 的方法。 为了简单起见,使用M4N的apprach,我也做了一些修改: !(query.Any() && query.Distinct().Skip(1).Any())
Say I have 3 char
variables, a
, b
and c
.
Each one can be '0'
, which is a special case and means it matches every char.
So if a is '0'
, I only need to check if b == c
.
I want to check if a == b == c
, but found the implementation in C# goes chaotic and lengthy.
Is there any creative or pretty solution you can offer?
update
for performance driven, take Erik A. Brandstadmoen's approach.
for simplicity, use M4N's apprach, also i did some modification: !(query.Any() && query.Distinct().Skip(1).Any())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
像这样的事情:
解释解决方案:
.Where(ch => ch != '0')
chars.Count() == 0
chars.Distinct().Count() == 1
更新:这是一个不同的版本,它不使用 LINQ,但仍然可读(IMO)。它作为一种方法实现,可以使用任意数量的待测试字符进行调用:
Something like this:
To explain the solution:
.Where(ch => ch != '0')
chars.Count() == 0
chars.Distinct().Count() == 1
Update: here's a different version, which does not use LINQ but is still and readable (IMO). It is implemented as a method and can be called with any number of characters to be tested:
优雅的解决方案
这需要对 LINQ 有基本的了解,并且基于 M4N 的解决方案:
当集合中最多有一个个不同的非通配符时,它返回
true
。我让它接受可变数量的参数,因此您可以调用 2、3 个或更多字符:
简单的解决方案
这是代码中逻辑的纯粹翻译,没有花哨的东西。
当参数相等或其中一个为
'0'
时,第一个IsMatch
重载返回true
。第二个重载只是调用每对的第一个重载。
(请注意,由于通配符,我们无法使用传递属性并仅比较两对。)
The Elegant Solution
This requires basic understanding of LINQ and is based on the solution by M4N:
It returns
true
when there is at most one distinct non-wildcard character in the collection.I made it accept a variable number of parameters so you may call it for 2, 3 or more chars:
The Simple Solution
This is pure translation of your logic in your code, no fancy stuff.
First
IsMatch
overload returnstrue
when its argument are equal or one of them is'0'
.The second overload simply calls the first one for each pair.
(Note that due to wildcards we cannot use the transitive property and compare just two pairs.)
像这样的东西应该适用于任意数量的 char 值:
通过以下单元测试:
Something like this should work for any number of char values:
Passes the following unit tests:
不确定我会称其为优雅,但它并不可怕(甚至可能是正确的......)(注意,这或多或少是对帕迪上面的答案的改进)。
Not sure I'd call it elegant, but it isn't horrible (and might even be correct...) (note, this is more or less a refinement of Paddy's answer above).
您可以编写一个结构“MYChar”,它实现
char
并覆盖Equals
、相等运算符和隐式转换,这样您就可以执行以下操作:编辑
事实证明您不能继承
char
因为它是密封的,但我尝试了以下代码。它可以编译,但我不确定它是否有效。我从 http://compilr.com/IDE/34853 编译它,但我没有任何东西到时候去测试一下。就这样:
You could write a struct "MYChar" that implements
char
and overridesEquals
, equality operators and implicit conversion so you could do :Edit
It turns out that you can't inherit from
char
because it is sealed, but I tried the following code. It compiles, but I'm not sure it works. I compiled it from http://compilr.com/IDE/34853, but I don't have anything to test at the time.here it goes :
这算不算混乱且冗长?
对我来说似乎没问题,前提是你只能拥有他们三个......
Does this count as chaotic and lengthy?
Seems ok for me, providing you can only ever have the three of them...
如果你将字符限制为 ASCII 而不是 unicode 那么,我喜欢:
http://ideone.com/khacx。 (编辑回应评论指出我还没有完全正确地了解规格,但我仍然喜欢基本想法。添加了额外的测试作为验证)。
对于涵盖无限数量字符的更通用的解决方案,这就是正则表达式的用途:^(.)(\1|0)*$
If you limit chars to ASCII and not unicode then, I like:
http://ideone.com/khacx. (editted in response to comment pointing out I hadn't quite got the specs right, but I still like the basic idea. Added additional test as verification).
For a more general solution that covers an unlimited number of characters, thats what regexs are for: ^(.)(\1|0)*$
怎么样:
我的逻辑错了吗?
What about:
Is my logic wrong?
这与接受的答案没有太大不同,但无论如何
That is not very different from the accepted answer but anyway