- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在接受以下编程练习的培训:"Without repeats" 。该声明解释如下:
Given a String of words, return a String with all repeated words replaced with the appropriate numbers of underscores. For example, if the given String was: Here here here we go! Go w'e!?.
The expected output would be: Here __ __ we go! __ __
Notice how punctuation (commas, periods, etc.) and case are ignored for determining whether a word has been used, but they still show up in the final String.
我在伪代码中想到了以下内容:
Remove all special characters and get just alphabetic ones
Split words by space
For each word
if word is not in result
add the word into result
else
add underscores as needed
return result
在 Java 中我编写了以下代码:
import java.util.*;
public class RepeatDelete
{
public static String withoutRepeats /*🚫🔁*/(String s)
{
//System.out.println("s: "+s);
List<String> uniques = new ArrayList<String>();
List<String> result = new ArrayList<String>();
String[] words = s.split(" ");
//System.out.println("words: "+Arrays.toString(words));
for(int i = 0, j = 0; i < words.length; i++){
String word = words[i];
String alphabetic = word.replaceAll("[^A-Za-z]","");
//System.out.println("alphabetic: "+alphabetic);
for(; j < uniques.size(); j++){
String unique = uniques.get(j);
if(unique.toLowerCase().equals(alphabetic.toLowerCase())){
result.add("_".repeat(word.length()));
break;
}
}
if(j == uniques.size()){
result.add(word);
uniques.add(alphabetic.toLowerCase());
}
}
System.out.println("uniques: "+Arrays.toString(uniques.toArray()));
return String.join(" ",result.toArray(new String[0]));
}
}
以及测试用例(取自练习):
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExampleTest
{
@Test public void FixedTest1()
{
String lyrics="Here here here we go!\n"+
"\n"+
"So they're finally here, performing for you\n"+
"If you know the words, you can join in too\n"+
"Put your hands together, if you want to clap\n"+
"As we take you through this monkey rap!\n"+
"Huh!\n"+
"\n"+
"\n"+
"DK\n"+
"Donkey Kong!\n"+
"\n"+
"\n"+
"He's the leader of the bunch, you know him well\n"+
"He's finally back to kick some tail\n"+
"His coconut gun can fire in spurts\n"+
"If he shoots ya, it's gonna hurt!\n"+
"He's bigger, faster, and stronger too\n"+
"He's the first member of the DK crew!\n"+
"Huh!\n"+
"\n"+
"\n"+
"DK\n"+
"Donkey Kong!\n"+
"DK\n"+
"Donkey Kong is here!\n"+
"\n"+
"\n"+
"This Kong's got style, so listen up dudes\n"+
"She can shrink in size, to suit her mood\n"+
"She's quick and nimble when she needs to be\n"+
"She can float through the air and climb up trees!\n"+
"If you choose her, you'll not choose wrong\n"+
"With a skip and a hop, she's one cool Kong!\n"+
"Huh!\n"+
"\n"+
"\n"+
"DK\n"+
"Donkey Kong!\n"+
"\n"+
"\n"+
"He has no style, he has no grace\n"+
"Th-this Kong has a funny face\n"+
"He can handstand when he needs to\n"+
"And stretch his arms out, just for you\n"+
"Inflate himself just like a balloon\n"+
"This crazy Kong just digs this tune!\n"+
"Huh!\n"+
"\n"+
"\n"+
"DK\n"+
"Donkey Kong!\n"+
"DK\n"+
"Donkey Kong is here!\n"+
"\n"+
"\n"+
"He's back again and about time too\n"+
"And this time he's in the mood\n"+
"He can fly real high with his jetpack on\n"+
"With his pistols out, he's one tough Kong!\n"+
"He'll make you smile when he plays his tune\n"+
"But Kremlings beware 'cause he's after you!\n"+
"Huh!\n"+
"\n"+
"\n"+
"DK\n"+
"Donkey Kong!\n"+
"Huh!\n"+
"\n"+
"\n"+
"Finally, he's here for you\n"+
"It's the last member of the DK crew!\n"+
"This Kong's so strong, it isn't funny\n"+
"Can make a Kremling cry out for mummy\n"+
"Can pick up a boulder with relative ease\n"+
"Makes crushing rocks seem such a breeze\n"+
"He may move slow, he can't jump high\n"+
"But this Kong's one hell of a guy!\n"+
"\n"+
"\n"+
"C'mon Cranky, take it to the fridge!\n"+
"\n"+
"\n"+
"Walnuts, peanuts, pineapple smells\n"+
"Grapes, melons, oranges and coconut shells\n"+
"Aww yeah!\n"+
"Walnuts, peanuts, pineapple smells\n"+
"Grapes, melons, oranges and coconut shells\n"+
"Aww yeah!";
String without="Here ____ ____ we go!\n"+
"\n"+
"So they're finally _____ performing for you\n"+
"If ___ know the words, ___ can join in too\n"+
"Put your hands together, __ ___ want to clap\n"+
"As __ take ___ through this monkey rap!\n"+
"Huh!\n"+
"\n"+
"\n"+
"DK\n"+
"Donkey Kong!\n"+
"\n"+
"\n"+
"He's ___ leader of ___ bunch, ___ ____ him well\n"+
"____ _______ back __ kick some tail\n"+
"His coconut gun ___ fire __ spurts\n"+
"__ he shoots ya, it's gonna hurt!\n"+
"____ bigger, faster, and stronger ___\n"+
"____ ___ first member __ ___ __ crew!\n"+
"____\n"+
"\n"+
"\n"+
"__\n"+
"______ _____\n"+
"__\n"+
"______ ____ is _____\n"+
"\n"+
"\n"+
"____ Kong's got style, __ listen up dudes\n"+
"She ___ shrink __ size, __ suit her mood\n"+
"She's quick ___ nimble when ___ needs __ be\n"+
"___ ___ float _______ ___ air ___ climb __ trees!\n"+
"__ ___ choose ____ you'll not ______ wrong\n"+
"With a skip ___ _ hop, _____ one cool _____\n"+
"____\n"+
"\n"+
"\n"+
"__\n"+
"______ _____\n"+
"\n"+
"\n"+
"__ has no ______ __ ___ __ grace\n"+
"Th-this ____ ___ _ funny face\n"+
"__ ___ handstand ____ __ _____ __\n"+
"___ stretch ___ arms out, just ___ ___\n"+
"Inflate himself ____ like _ balloon\n"+
"____ crazy ____ ____ digs ____ tune!\n"+
"____\n"+
"\n"+
"\n"+
"__\n"+
"______ _____\n"+
"__\n"+
"______ ____ __ _____\n"+
"\n"+
"\n"+
"____ ____ again ___ about time ___\n"+
"___ ____ ____ ____ __ ___ ____\n"+
"__ ___ fly real high ____ ___ jetpack on\n"+
"____ ___ pistols ____ ____ ___ tough _____\n"+
"He'll make ___ smile ____ __ plays ___ ____\n"+
"But Kremlings beware 'cause ____ after ____\n"+
"____\n"+
"\n"+
"\n"+
"__\n"+
"______ _____\n"+
"____\n"+
"\n"+
"\n"+
"________ ____ ____ ___ ___\n"+
"____ ___ last ______ __ ___ __ _____\n"+
"____ ______ __ strong, it isn't _____\n"+
"___ ____ _ Kremling cry ___ ___ mummy\n"+
"___ pick __ _ boulder ____ relative ease\n"+
"Makes crushing rocks seem such _ breeze\n"+
"__ may move slow, __ can't jump ____\n"+
"___ ____ ______ ___ ____ __ _ guy!\n"+
"\n"+
"\n"+
"C'mon Cranky, ____ __ __ ___ fridge!\n"+
"\n"+
"\n"+
"Walnuts, peanuts, pineapple smells\n"+
"Grapes, melons, oranges ___ _______ shells\n"+
"Aww yeah!\n"+
"________ ________ _________ ______\n"+
"_______ _______ _______ ___ _______ ______\n"+
"___ _____";
assertEquals(without, RepeatDelete.withoutRepeats(lyrics));
}
我们看到测试预期:
expected:<...
So they're finally [_____ performing for you
If ___ know the words, ___ can join in too
Put your hands together, __ ___ want to clap
As __ take ___ through this monkey rap!
Huh!
DK
Donkey Kong!
He's ___ leader of ___ bunch, ___ ____ him well
____ _______ back __ kick some tail
His coconut gun ___ fire __ spurts
__ he shoots ya, it's gonna hurt!
____ bigger, faster, and stronger ___
____ ___ first member __ ___ __ crew!
____
__
______ _____
__
______ ____ is _____
____ Kong's got style, __ listen up dudes
She ___ shrink __ size, __ suit her mood
She's quick ___ nimble when ___ needs __ be
___ ___ float _______ ___ air ___ climb __ trees!
__ ___ choose ____ you'll not ______ wrong
With a skip ___ _ hop, _____ one cool _____
____
__
______ _____
__ has no ______ __ ___ __ grace
Th-this ____ ___ _ funny face
__ ___ handstand ____ __ _____ __
___ stretch ___ arms out, just ___ ___
Inflate himself ____ like _ balloon
____ crazy ____ ____ digs ____ tune!
____
__
______ _____
__
______ ____ __ _____
____ ____ again ___ about time ___
___ ____ ____ ____ __ ___ ____
__ ___ fly real high ____ ___ jetpack on
____ ___ pistols ____ ____ ___ tough _____
He'll make ___ smile ____ __ plays ___ ____
But Kremlings beware 'cause ____ after ____
____
__
______ _____
____
________ ____ ____ ___ ___
____ ___ last ______ __ ___ __ _____
____ ______ __ strong, it isn't _____
___ ____ _ Kremling cry ___ ___ mummy
___ pick __ _ boulder ____ relative ease
Makes crushing rocks seem such _ breeze
__ may move slow, __ can't jump ____
___ ____ ______ ___ ____ __ _ guy!
C'mon Cranky, ____ __ __ ___ fridge!
Walnuts, peanuts, pineapple smells
Grapes, melons, oranges ___ _______ shells
Aww yeah!
________ ________ _________ ______
_______ _______ _______ ___ _______ ______
___ _____]>
但是,代码输出:
but was:<...
So they're finally [here, performing for you
If you know the words, you can join in too
Put your hands together, if you want to clap
As we take you through this monkey rap!
Huh!
DK
Donkey Kong!
He's the leader of the bunch, you know him well
He's finally back to kick some tail
His coconut gun can fire in spurts
If he shoots ya, it's gonna hurt!
He's bigger, faster, and stronger too
He's the first member of the DK crew!
Huh!
DK
Donkey Kong!
DK
Donkey Kong is here!
This Kong's got style, so listen up dudes
She can shrink in size, to suit her mood
She's quick and nimble when she needs to be
She can float through the air and climb up trees!
If you choose her, you'll not choose wrong
With a skip and a hop, she's one cool Kong!
Huh!
DK
Donkey Kong!
He has no style, he has no grace
Th-this Kong has a funny face
He can handstand when he needs to
And stretch his arms out, just for you
Inflate himself just like a balloon
This crazy Kong just digs this tune!
Huh!
DK
Donkey Kong!
DK
Donkey Kong is here!
He's back again and about time too
And this time he's in the mood
He can fly real high with his jetpack on
With his pistols out, he's one tough Kong!
He'll make you smile when he plays his tune
But Kremlings beware 'cause he's after you!
Huh!
DK
Donkey Kong!
Huh!
Finally, he's here for you
It's the last member of the DK crew!
This Kong's so strong, it isn't funny
Can make a Kremling cry out for mummy
Can pick up a boulder with relative ease
Makes crushing rocks seem such a breeze
He may move slow, he can't jump high
But this Kong's one hell of a guy!
C'mon Cranky, take it to the fridge!
Walnuts, peanuts, pineapple smells
Grapes, melons, oranges and coconut shells
Aww yeah!
Walnuts, peanuts, pineapple smells
Grapes, melons, oranges and coconut shells
Aww yeah!]>
我知道代码会在结果中插入重复的“此处”,但为什么呢?
正如您所注意到的,“here”已经添加到唯一值中,因此不应将其添加到结果中!
你能帮我一下吗?
我还读过:
您将如何调试这段代码,甚至提出一个更简单的方法?
如何用下划线替换重复的单词,Java‽
编辑:在尝试理解@kevin ternet答案并且不复制它之后,我编码了:
import java.util.*;
public class RepeatDelete
{
public static String withoutRepeats(String s)
{
String[] words = s.split(" ");
List<String> result = new ArrayList<String>(Arrays.asList(words));
return String.join(" ",replaceRepeatedWords(result, 0));
}
public static List<String> replaceRepeatedWords(List<String> result, int i){
String originalWord = result.get(i);
Collections.replaceAll(result, result.get(i), "_".repeat(result.get(i).length()));
result.set(i, originalWord);
if(i == result.size() - 1){
return result;
}
return replaceRepeatedWords(result, i+1);
}
}
它不是用下划线替换所有重复的单词,而是替换最后一个。
给定:
“我们开始吧!\n”
它输出:
这里,我们开始吧!
而不是:
这里______我们出发!
最佳答案
如果不运行您的代码,我看不到任何明显的突出内容,但是我建议您以更简单的方式编写它,以便您更容易调试:
JDK 提供了一些类,可以让您的生活更轻松,例如,唯一单词列表最好实现为 Set<String>
,那么您不需要第二个嵌套循环,因为您可以简单地询问它 if (uniques.contains(alphabetic))
。 (作为奖励,以这种方式测试它可以提供更好的性能,尽管性能现在不应该是您关心的问题)。您还可以使用 for-each 循环而不是使用数字索引:
for (String word : words.split(" ") {
String alphabetic = word.replaceAll("...");
// ...
}
大多数人都会同意,这对于人类来说“阅读”得更好,并且可读的代码更容易调试和使用。
我还建议使用StringBuilder
构建您的输出字符串,而不是另一个列表。这几乎就是它设计的用例。
StringBuilder output = new StringBuilder();
output.append(word);
// etc...
System.out.println(output.toString());
祝你好运,我认为你有正确的方法,但是有更干净(更简单)的方法来实现它。请务必使用允许您设置断点并逐行单步执行正在运行的代码的 IDE。
关于java - 我们如何用下划线替换重复的单词,Java‽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59309109/
执行此查询 INSERT INTO classes( '_fkUserID', 'date', 'time' ) VALUES ( '1', '2017-07-04', '8:15' ) 给
不知道它是否重复(无法找到要搜索的词,例如 “允许使用 java 字符”)。我在测试面试中遇到了这个问题: 考虑以下类: class _ {_ f; _(){}_(_ f){_ t = f; f =
我需要验证用户的屏幕名称以确保它不能有 多个连字符或下划线 我不希望人们的网名全是标点符号。 这是我到目前为止的验证: public boolean validateScreenName(String
我正在尝试检查我收到的新数据是否针对我持有的对象,我想知道的是,我正在发送的对象的键是否与我当前拥有的对象中的任何键匹配。 所以我捕获了一个像 myObj = [{"one": 1}, {"two":
这是我第一次使用下划线...我有这个简单的 json... "categories" : [ { "tag" : "cat1", "active" : true
一个很简单的问题: 为什么在WPF内容中看不到_(下划线)? 例如内容 显示为"testt"(未显示下划线)。 最佳答案 标签支持助记符(即您可以使用ctrl +(key)赋予它们焦点)。您可以使用
下面是我正在处理的简化逻辑,我想在数组中查找具有匹配位置(文件夹)的文件。 我能够使用普通的 JS 循环来实现此功能,您能建议更好/更简单/类似下划线的方法来实现此类功能吗? // source va
我正在尝试在对象的函数中查找和替换值 我的对象看起来像这样: var testStates = [{ "module": "module1", "customUrl": [
尝试让 _.uniq() 在以下结构上工作: [ {'x' : 1, 'y': 2}, {'x' : 1, 'y': 2}, {'x' : 2, 'y': 3}, {'
明白了: [{ "title": "qq", "dateValuePair": { "date": "2016-04-29T22:00:00.000Z", "va
我不知道这是否可能,但我试图做的是“清理”一个对象。基本想法是我有一个对象的表格(以 Angular ),然后单击我想添加一个新行(控制对象中的一个新项目,但我希望它没有值。我有下划线尝试一下。一些考
所以我有一大堆对象需要将其变成一个小对象。 它有 50-60 个对象,我需要过滤到一个新的对象数组中,其中只有 3 个。 所以看起来像 myOb = {{"ob1": 1},{"ob2": 1},{"
我有一个像这样的对象 - {"house" : red, "car" : green, "apple" : blue}; 并且正在发送另一个带有单个键/值的对象,如下所示 {"apple" : gre
我有一个包含多个对象的数组,例如 var val = [ _id: ["5412fc1bd123cf7016674a92", "5412cf270e9ca9b517b43ca3"],
所以我有一个对象列表,例如 var options = [{"car" : "red"},{"house": "green"},{"dog":"bark"}] 我正在尝试将其转换为一个值数组,
我正在尝试将此数组转换为对象。使用下划线,我想转换这个数组: [ { "id": "parentA", "children": [ { "nam
我正在尝试使用这样的链检索嵌套项目值。我正在遍历的对象如下所示: var testStates = [{ "module": "module1", "customUrl
我有一些内容可编辑的段落,我希望能够在双击时使某些单词加粗下划线。当我双击一个单词时,它会被选中,并显示一个包含 3 个选项的工具提示。但是,单击工具提示选项后,选择就会消失,并且文本不会发生任何更改
要在 CSS 中给文本加下划线,我们可以这样做: h3 {text-decoration:underline;} 然而,这只会强调包含在 h3 标签中的文本。如果想让下划线穿过页面怎么办? 谢谢 最佳
我正在尝试解决我的最后一个问题,但我仍然不知道如何解决它。我的任务是编写一个对数字进行排序的程序,但是:我们的导师给了我们一些处理数字的程序的额外要点,例如:000054667(实际上是 54667)
我是一名优秀的程序员,十分优秀!