读取一个字符串,一次 3x3 个字符
所以想象一下这个字符串:
_ _ _ _ _ _ _
| _| _||_||_ |_ ||_||_|
||_ _| | _||_| ||_| _|
分割这个字符串以便每个数字都可以由它自己处理的最简单/最好的方法是什么?
我正在考虑类似
public string[] SplitIntoNumbers(string input)
结果的事情,有
[" | |", " _ _||_ ", " _ _| _|", ...]
什么想法吗?
编辑
对于那些想要更多信息的人 - 问题来自 BankOCR-kata CodingDojo。我意识到有多种“只是完成工作”的方法——解决方案,但我觉得必须有一种更“奇特”的方法来解决它。类似于 clojure 的东西。
So imagine this string:
_ _ _ _ _ _ _
| _| _||_||_ |_ ||_||_|
||_ _| | _||_| ||_| _|
What would be the easiest / nicest way of splitting this string so that each number could be handled by it self?
I'm thinking of something like
public string[] SplitIntoNumbers(string input)
where the result would be like
[" | |", " _ _||_ ", " _ _| _|", ...]
Any ideas?
Edit
For thous wanting some more information - The problem comes from the BankOCR-kata over at CodingDojo. I realize that there are multiple ways of 'just get the job done'-solutions, but I felt that there had to be a more 'fancy' way of solving it. Something clojure-alike.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你问:
...我认为您可能过于从面向对象的角度来处理这个问题。你所说的实际上是一种“字体”,而不是字符的集合。坦率地说,我只是将逻辑包装到一个类中,并完全按照您在这篇文章中所做的那样定义字符数据。它易于查看、编辑和维护。
从您原来的帖子中我不明白您的最终目标是否只是渲染,或者是否是解析。无论如何,我不能只停留在数字上;)
前面的程序输出以下文本:
You asked:
... I think you may be approaching this from a little too much of an OO perspective. What your talking about is really a 'font' more than it is a collection of characters. I would frankly just wrap up the logic into a single class and define the character data exactly as you did for this post. It's easy to see, edit, and maintain.
I didn't understand from your original post if your ultimate goal was just rendering, or if it was parsing. Anyway I couldn't just stop at only numbers ;)
The preceeding program outputs the following text:
开门见山:
(编辑) 我使用的字符串是这样的:
Straight to the point:
(EDIT) The string I used was this:
我将使用正则表达式来构建具有与此类似的模式的匹配列表,
这会将输入分解为 3x1 匹配的块,并根据您拥有的匹配数量来确定数字。例如,
将生成 27 个 3x1 段的匹配,并且由于每个数字有 3 行高,因此您可以只取 27 / 3 = 9 个单独的数字。然后,您只需循环遍历正则表达式匹配并将它们组合到您想要的输出中。
I would use regex to build up a list of matches with a pattern similar to this
this would break the input into chunks of 3x1 matches, and depending on how many matches you have would determine the numbers. For example
would generate 27 matches of 3x1 segments, and since each number is 3 lines high you can just take the 27 / 3 = 9 separate numbers. Then you would just have to loop through the regex matches and combine them into the output that you want.
假设您想要保留字符串数组用于输入,我们可以非常简单地循环遍历一次拉 3 个字符的行。
Assuming you want to keep the array of strings for input, we could loop very simply through the lines pulling 3 chars at a time.
扩展方法怎么样:
假设InSetsOf()的兼容实现可用。
用法:
How about an extension method:
assuming a compatible implementation of InSetsOf() is available.
usage: