- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
您好,我正在做以下编程练习:The Hunger Games - Foxes and Chickens 。声明如下:
Story
Old MacDingle had a farm.
To be more precise, he had a free-range chicken farm.
But Old MacDingle also had a fox problem.
Foxes F eat chickens C
At night the only guaranteed "safe" chickens are in their cages [] (unless a fox has got into the cage with them!)
Kata Task
Given the initial configuration of foxes and chickens what will the farm look like the next morning after the hungry foxes have been feasting?
Examples
Ex1 Before
CCC[CCC]FCC[CCCCC]CFFFF[CCC]FFFF
After
...[CCC]F..[CCCCC].FFFF[CCC]FFFF
Ex2 Before
...[CCC]...[CCCFC].....[CCC]....
After
...[CCC]...[...F.].....[CCC]....
Ex3 Before
CCC[CCC]FCC[CCCFC]CFFFF[CCC]FFFF
After
...[CCC]F..[...F.].FFFF[CCC]FFFF
Notes
Anything not a fox, a chicken, or a cage is just dirt .
All cages are intact (not open-ended), and there are no cages inside other cages
我编写了以下代码:
public class Dinglemouse {
public static String hungryFoxes /*🦊🦊*/ (String farm) {
System.out.println("farm: "+farm);
int posCageStarted = -1;
int posCageEnded = -1;
//If there are not cages, and there are foxes, all chickens are cleared; otherwise we keep the farm as it is.
if(!farm.contains("[") && !farm.contains("]")){
if(farm.contains("F")){
return farm.replace("C",".");
}else{
return farm;
}
}
for(int i = 0; i < farm.length(); i++){
System.out.println("i: "+i);
char c = farm.charAt(i);
if(c=='['){
posCageStarted=i;
}else if(c==']'){
posCageEnded=i;
}
//If we are between cages ]...[
if(posCageEnded>posCageStarted){
System.out.println("posCageEnded: "+posCageEnded);
System.out.println("posCageStarted: "+posCageStarted);
String betweenCages = "";
String ending = "";
String starting = "";
int nextCageStart;
if(farm.substring(posCageEnded).contains("[")){
nextCageStart = posCageEnded+farm.substring(posCageEnded).indexOf('[');
betweenCages = farm.substring(posCageEnded+1,nextCageStart);
}else{
nextCageStart = farm.indexOf('[');
ending = farm.substring(posCageEnded+1);
starting = farm.substring(0,nextCageStart);
betweenCages = ending+starting;
}
System.out.println("nextCageStart: "+nextCageStart);
System.out.println("betweenCages: "+betweenCages);
if(betweenCages.contains("F")){
betweenCages = betweenCages.replace('C','.');
System.out.println("betweenCages: "+betweenCages);
if(nextCageStart>posCageEnded){
farm = farm.substring(0,posCageEnded+1) + betweenCages + farm.substring(nextCageStart);
}else{
farm = betweenCages.substring(ending.length()) + farm.substring(farm.indexOf('['),posCageEnded+1)
+ betweenCages.substring(0,ending.length());
}
System.out.println("new farm: "+farm);
}
i+=betweenCages.length();
//If we are inside a cage [...]
}else if(posCageStarted>posCageEnded){
System.out.println("inside cage: ");
System.out.println("posCageStarted: "+posCageStarted);
System.out.println("posCageEnded: "+posCageEnded);
int cageEnd = posCageStarted+1 + farm.substring(posCageStarted+1).indexOf(']');
System.out.println("cageEnd: "+cageEnd);
String insideCage = farm.substring(posCageStarted+1,cageEnd);
System.out.println("insideCage: "+insideCage);
if(insideCage.contains("F")){
insideCage = insideCage.replace("C",".");
farm = farm.substring(0,posCageStarted+1) + insideCage + farm.substring(cageEnd);
System.out.println("new farm: "+farm);
}
i+=insideCage.length();
}
}
System.out.println("\n\nWe return farm: "+farm);
return farm;
}
}
我很好奇为什么随机测试失败:
import org.junit.*;
import static org.junit.Assert.assertEquals;
public class ExampleTests {
@Test
public void ex1() {
final String before = "CCC[CCC]FCC[CCCCC]CFFFF[CCC]FFFF";
final String after = "...[CCC]F..[CCCCC].FFFF[CCC]FFFF";
assertEquals(after, Dinglemouse.hungryFoxes(before));
}
@Test
public void ex2() {
final String before = "...[CCC]...[CCCFC].....[CCC]....";
final String after = "...[CCC]...[...F.].....[CCC]....";
assertEquals(after, Dinglemouse.hungryFoxes(before));
}
@Test
public void ex3() {
final String before = "CCC[CCC]FCC[CCCFC]CFFFF[CCC]FFFF";
final String after = "...[CCC]F..[...F.].FFFF[CCC]FFFF";
assertEquals(after, Dinglemouse.hungryFoxes(before));
}
@Test
public void noCagesTest(){
final String before = "CCCCCF.CC..C..CC.CF.";
final String after = ".....F............F.";
assertEquals(after, Dinglemouse.hungryFoxes(before));
}
@Test
public void randomTest3(){
final String before = ".C......C.F.[...F.]C..CC.....CC.CC[..CFCCC..CCCCCFC....CC...]C[.....C.....C...]";
final String after = "..........F.[...F.]...............[...F..........F..........].[.....C.....C...]";
assertEquals(after, Dinglemouse.hungryFoxes(before));
}
@Test
public void randomTest1(){
final String before = "..CCCF....[.CC.CCC.CFFC...C.........C..C.CF..CC]CCCC....[.]";
final String after = ".....F....[.........FF....................F....]........[.]";
assertEquals(after, Dinglemouse.hungryFoxes(before));
}
}
如果我们密切关注测试 random1,它会期望我们替换结尾的 Chickens。我的意思是之前的代码输出:
.....F....[.........FF....................F....]CCCC....[.]
它应该输出:
.....F....[.........FF....................F....]........[.]
此外,当我们执行 randomTest3 时,我们的代码输出:
..........F.[...F.]C..CC.....CC.CC[...F..........F..........]C[.....C.....C...]
它应该输出:
..........F.[...F.]...............[...F..........F..........].[.....C.....C...]
这怎么可能?
最佳答案
At night the only guaranteed "safe" chickens are in their cages [] (unless a fox has got into the cage with them!)
据我所知,如果没有笼子,那么所有的鸡都会死,无论狐狸与否,所以
//If there are not cages, and there are foxes, all chickens are cleared; otherwise we keep the farm as it is.
if(!farm.contains("[") && !farm.contains("]")){
if(farm.contains("F")){
return farm.replace("C",".");
}else{
return farm;
}
}
应替换为
//If there are not cages all chickens are cleared
if(!farm.contains("[") && !farm.contains("]")){
return farm.replace("C",".");
}
您的代码的另一个问题是当我们在笼子内和不在笼子内时无法检测到情况。维护一个 boolean 变量 isInsideCage
会更容易,每次传递 '[' 时将其设置为 true,每次传递 ']' 时将其设置为 false。
而不是 someIndex + farm.substring(someIndex).indexOf(']')
您可以使用farm.indexOf(']', someIndex)
.
但是,您的代码不起作用的原因是您正在检查笼子之间是否存在狐狸以消灭鸡,而笼子外的鸡无论如何都应该死了。具体来说,您在 if-branch //If we are between cages ]...[
中有这一行:
if(betweenCages.contains("F")){
删除狐狸的此项检查。
现在你的代码正确地删除了第一个笼子之前的所有鸡,因为然后 posCageEnded
和posCageStarted
都是 -1,所以 posCageStarted>posCageEnded
都不是也不posCageEnded>posCageStarted
,但在第一个笼子之后,它会停止将鸡移出笼子,除非狐狸也在那里。
关于java - 检查是否存在由 [cages] 包围的某些字母并替换它们的算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60398371/
我想在页面中放置这样一个栏: [ Angular -l][变量][装饰][变量][ Angular -r] “可变”间隔部分应该可以调整大小,但最终,周围的容器(包含所有这些)应该规定最大宽度,并且上
我想写一个正则表达式,这样我只匹配没有用方括号括起来的第一个数字。 例如asdadsas,*&(*&(*2asdasd*U(*&*()&(*3应该匹配 2(没有方括号) 和 asdadsas,*&(*
我尝试尝试这些 iostream 奇怪的行为。 int value = 2; cout > 2) > 的性质。在该语言从 C 发展的早期,这些运算符专门用作所谓的“位移位”运算符,其中 > 会将位向右
这个问题在这里已经有了答案: How to use Objective-C classes with names which are keywords in Swift (1 个回答) 关闭 7 年
我看不到问题,我没有对参数进行编码,我只是将变量从一个页面传递到另一个页面。 这是页面 A: if (isset($_SESSION['loggedIn'])==1){ ?> '">Edit 这
我有一个静态工厂类,如果在调用之前没有进行初始化,它会抛出异常。我不明白为什么 Eclipse 对我大喊大叫,因为我在工厂中抛出了异常(它要求我在 try/catch 中包围抛出异常),但在另一个类中
我有一些代码: public static void a() throws NumberFormatException { return; } public stati
我看不到问题,我没有对参数进行编码,我只是将变量从一个页面传递到另一个页面。 这是页面 A: if (isset($_SESSION['loggedIn'])==1){ ?> '">Edit 这
This question already has answers here: Using explicitly numbered repetition instead of question mar
包围
在我的网站上,我有一些图像与其余内容存储在相同的 SQL 单元格中。当我加载包含图像的页面并且 PHP 回显“文章”单元格时,图像会自动包含在段落标签中,这不是我想要的。有什么办法可以防止这种情况发生
我想弄清楚为什么我的 JSON 响应被方括号括起来。我正在使用 ASP.NET Web API 和 Angular。我在想这就是我的 Angular 代码没有打印到 HTML 的原因。 namespa
在eclipse中我们可以用try/catch包围一段代码。我想用 if 语句包围一段代码。有没有快捷键。选择代码块后按 Ctrl + 1 没有提示用 If 包围。 最佳答案 突出显示代码块,按 Al
我有一个用 Go 实现的 Web 服务,它从外部服务返回一个 JSON 结构。返回对象后,它看起来像这样: {"otherServiceInfoList":[],"action... 我的 Go 网络
我在一个网站上工作,客户想要类似这样的东西:http://www.csszengarden.com/?cssfile=202/202.css有几个叠加层附加到屏幕的边缘,而中心的文本以原始浏览器滚动条
我想圈出 react-native-vector 图标。我在样式中添加了一个边框半径,但它对所有设备都没有帮助,而且每个图标的行为都不同。 react 原生矢量图标的链接: https://
使用文本节点的以下值... MatcH one MatcHer two MarcH three 如何使用 java matcher.find() 创建以下输出? MatcH one MatcHer t
我有一个读取文件的非常简单的程序。 eclipse 要求我添加 throws 声明或用 try catch 包围。哪一个是“合适的”。以及如果我已经在方法级别 try catch ,为什么还必须拥有其
在我们从“psd2html”服务获得的代码中,我看到围绕 div 标签的内容有很多跨度。 我知道 span 和 div 之间的区别,但我不明白为什么代码看起来像这样: Forgot pass
我有一个 HTML 代码,有 3 个输入字段:
我正在尝试替换字符串中单词 true 的所有实例,例如在 "true && 0 || 1" 中. 这些字符串可能包含变量值,标记为#{varname} .我不想替换被 #{ 包围的实例和 } . 例子
我是一名优秀的程序员,十分优秀!