- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要一种有效的方法来测试给定字符 (System.Char) 是否为辅音。然后我需要同样的元音。更一般地说,我需要以下接口(interface)的有效实现,因为目标字符集大约有 20 个字符。请指教。谢谢!
using System;
public interface ITester
{
Boolean IsInCategory(Char something);
}
更新
好的,我已经进行了一些测试。这就是我得到的。最好的方法是使用预先计算的映射,其中每个字符都映射到一个 bool 值。 (请参阅下面代码中的 IndexBased)。事实证明,HashSet 并不是最好的。如果有人有更多想法,请告诉我。
[TestClass]
public class Runner
{
public const Int32 NumberOfRuns = 10000;
public const String TextSample = @"The Letter It was November. Although it was not yet late, the sky was dark when I turned into Laundress Passage. Father had finished for the day, switched off the shop lights and closed the shutters; but so I would not come home to darkness he had left on the light over the stairs to the flat. Through the glass in the door it cast a foolscap rectangle of paleness onto the wet pavement, and it was while I was standing in that rectangle, about to turn my key in the door, that I first saw the letter. Another white rectangle, it was on the fifth step from the bottom, where I couldn't miss it. I closed the door and put the shop key in its usual place behind Bailey's Advanced Principles of Geometry. Poor Bailey. No one has wanted his fat gray book for thirty years. Sometimes I wonder what he makes of his role as guardian of the bookshop keys. I don't suppose it's the destiny he had in mind for the masterwork that he spent two decades writing. A letter. For me. That was something of an event. The crisp-cornered envelope, puffed up with its thickly folded contents, was addressed in a hand that must have given the postman a certain amount of trouble. Although the style of the writing was old-fashioned, with its heavily embellished capitals and curly flourishes, my first impression was that it had been written by a child. The letters seemed untrained. Their uneven strokes either faded into nothing or were heavily etched into the paper. There was no sense of flow in the letters that spelled out my name.";
private interface ITester
{
Boolean IsConsonant(Char something);
}
// results in millisecs: 14807, 16411, 15050,
private class HashSetBasedTester : ITester
{
private HashSet<Char> hash;
public HashSetBasedTester()
{
this.hash = new HashSet<Char>("bcdfghjklmnpqrstvwxz");
}
public Boolean IsConsonant(Char something)
{
return this.hash.Contains(Char.ToLower(something));
}
}
// results in millisecs: 12270, 12495, 12853,
private class HardcodedTester : ITester
{
public Boolean IsConsonant(Char something)
{
var lower = Char.ToLower(something);
return lower == 'b' || lower == 'c' || lower == 'd' || lower == 'f' || lower == 'g' || lower == 'h' || lower == 'j' || lower == 'k' || lower == 'l' || lower == 'm' || lower == 'n' || lower == 'p' || lower == 'q' || lower == 'r' || lower == 's' || lower == 't' || lower == 'v' || lower == 'w' || lower == 'x' || lower == 'z';
}
}
// WORST RESULTS
// results in millisecs: 32140, 31549, 31856
private class ListBasedTester : ITester
{
private List<Char> list;
public ListBasedTester()
{
this.list = new List<Char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z' };
}
public Boolean IsConsonant(Char something)
{
return this.list.Contains(Char.ToLower(something));
}
}
// WINNER! (fastest and most consistent)
// results in millisecs: 11335, 11349, 11386,
private class IndexBased : ITester
{
private Boolean[] map;
private char min;
private char max;
public IndexBased()
{
var chars = "bcdfghjklmnpqrstvwxz".ToArray();
this.min = chars.Min();
this.max = chars.Max();
var length = this.max - this.min + 1;
this.map = new Boolean[length];
foreach (var at in chars)
{
map[at - min] = true;
}
}
public Boolean IsConsonant(Char something)
{
something = Char.ToLower(something);
return something <= this.max && something >= this.min && this.map[something - this.min];
}
}
[TestMethod]
public void RunTest()
{
var tester = new IndexBased(); // new HashSetBasedTester(); // new HardcodedTester(); // new ListBasedTester(); //
var stopwatch = Stopwatch.StartNew();
for (var i = 0; i < NumberOfRuns; i++)
{
foreach (var at in TextSample)
{
var tested = tester.IsConsonant(at);
}
}
Trace.WriteLine(stopwatch.ElapsedMilliseconds.ToString());
}
}
更新 2:
这组测试没有转换为下/上,我们得到了更好的结果!无论如何,热门/失败者都是一样的。检查一下:
[TestClass]
public class Runner
{
public const Int32 NumberOfRuns = 10000;
public const String TextSample = @"The Letter It was November. Although it was not yet late, the sky was dark when I turned into Laundress Passage. Father had finished for the day, switched off the shop lights and closed the shutters; but so I would not come home to darkness he had left on the light over the stairs to the flat. Through the glass in the door it cast a foolscap rectangle of paleness onto the wet pavement, and it was while I was standing in that rectangle, about to turn my key in the door, that I first saw the letter. Another white rectangle, it was on the fifth step from the bottom, where I couldn't miss it. I closed the door and put the shop key in its usual place behind Bailey's Advanced Principles of Geometry. Poor Bailey. No one has wanted his fat gray book for thirty years. Sometimes I wonder what he makes of his role as guardian of the bookshop keys. I don't suppose it's the destiny he had in mind for the masterwork that he spent two decades writing. A letter. For me. That was something of an event. The crisp-cornered envelope, puffed up with its thickly folded contents, was addressed in a hand that must have given the postman a certain amount of trouble. Although the style of the writing was old-fashioned, with its heavily embellished capitals and curly flourishes, my first impression was that it had been written by a child. The letters seemed untrained. Their uneven strokes either faded into nothing or were heavily etched into the paper. There was no sense of flow in the letters that spelled out my name.";
private interface ITester
{
Boolean IsConsonant(Char something);
}
// results in millisecs: 8378, 7980, 7533, 7752
private class HashSetBasedTester : ITester
{
private HashSet<Char> hash;
public HashSetBasedTester()
{
this.hash = new HashSet<Char>("bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ");
}
public Boolean IsConsonant(Char something)
{
return this.hash.Contains(something);
}
}
// results in millisecs: 6406, 6667, 6500, 6708
private class HardcodedTester : ITester
{
public Boolean IsConsonant(Char something)
{
return something == 'b' || something == 'c' || something == 'd' || something == 'f' || something == 'g' || something == 'h' || something == 'j' || something == 'k' || something == 'l' || something == 'm' || something == 'n' || something == 'p' || something == 'q' || something == 'r' || something == 's' || something == 't' || something == 'v' || something == 'w' || something == 'x' || something == 'z' ||
something == 'B' || something == 'C' || something == 'D' || something == 'F' || something == 'G' || something == 'H' || something == 'J' || something == 'K' || something == 'L' || something == 'M' || something == 'N' || something == 'P' || something == 'Q' || something == 'R' || something == 'S' || something == 'T' || something == 'V' || something == 'W' || something == 'X' || something == 'Z';
}
}
// WORST RESULTS
// results in millisecs: 36585, 37702, ...
private class ListBasedTester : ITester
{
private List<Char> list;
public ListBasedTester()
{
this.list = new List<Char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z',
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Z' };
}
public Boolean IsConsonant(Char something)
{
return this.list.Contains(something);
}
}
// WINNER!
// results in millisecs: 4716, 4846, 4756, 4550
private class IndexBased : ITester
{
private Boolean[] map;
private char min;
private char max;
public IndexBased()
{
var chars = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ".ToArray();
this.min = chars.Min();
this.max = chars.Max();
var length = this.max - this.min + 1;
this.map = new Boolean[length];
foreach (var at in chars)
{
map[at - min] = true;
}
}
public Boolean IsConsonant(Char something)
{
return something <= this.max && something >= this.min && this.map[something - this.min];
}
}
[TestMethod]
public void RunTest()
{
var tester = new IndexBased();//new IndexBased(); // new HashSetBasedTester(); // new HardcodedTester(); // new ListBasedTester(); //
var stopwatch = Stopwatch.StartNew();
for (var i = 0; i < NumberOfRuns; i++)
{
foreach (var at in TextSample)
{
var tested = tester.IsConsonant(at);
}
}
Trace.WriteLine(stopwatch.ElapsedMilliseconds.ToString());
}
}
最佳答案
HashSet<char>
会成功的。为辅音、元音等创建单独的实例,并使用它们来测试成员资格。
ISet<char> vowels = new HashSet<char>("auoie");
if (vowels.Contains('a')) {
// ...
}
更新:对于英文字符子集,您可以构建一个 bool
数组 - 类似于您在更新中使用的数组,但没有 min
的偏移量, 以及大写/小写重复:它会更快 - 几乎和它得到的一样快。
private class IndexBased : ITester {
private readonly bool[] map = new bool[128];
public IndexBased() {
foreach (var ch in "bcdfghjklmnpqrstvwxz") {
map[ch] = map[Char.ToUpper(ch)] = true;
}
}
public bool IsConsonant(Char ch) {
return ch < map.Length && map[ch];
}
}
关于c# - 测试角色是否属于 .net 中某个类别的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10607374/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!