我需要功能方面的帮助
I need assistance with a function
所以我一直在努力用C#写一个函数
接收单词的字符串数组(在被函数从字符串接收到单词数组之前被拆分)。
所以我一直在尝试做的是一个函数,它检查是否在整个数组中除了words[0]
(words是单词数组的名称)之外是否有显示更多的字母超过 3 次(至少 3 次)。
比如这句话:
Why llll ddd ssssssss !!!!!!!?
因为没有一个字母在一个单词中出现超过 3 次 - 这样的单词不存在。
该函数的基础如下所示:
public bool MultipleCheck(string[] words)
{
}
到目前为止我想出了什么...我知道它有错误...我还没有修复它:
public bool AttachmentsCheck(string[] words)
{
string currentWord;
int wordCounter = 0;
for (int i=0; i < words.Length; i++)
{
currentWord = words[i];
for (int j = 0; j < currentWord.Length; j++)
{
char[] wordArr = currentWord.ToCharArray();
for (int k=0; k < wordArr.Length; k++)
{
if (wordArr[k]==wordArr[wordArr.Length-k])
{
wordCounter++;
}
}
}
if (wordCounter => 3)
{
return false;
}
}
return true;
}
我希望这能奏效
private static Tuple<bool, string> ValidateWord(string[] words)
{
bool foundResult = false;
List<string> all3CharWords = new List<string>();
string wordWith3SameChar = string.Empty;
foreach (var word in words)
{
var resultTuple = ValidateWord(word);
if (resultTuple.Item1)
{
foundResult = true;
all3CharWords.Add(resultTuple.Item2);
}
}
if (foundResult)
{
wordWith3SameChar = String.Join(";", all3CharWords.ToArray());
}
return new Tuple<bool, string>(foundResult, wordWith3SameChar);
}
private static Tuple<bool, string> ValidateWord(string words)
{
bool foundResult = false;
string wordWith3SameChar = string.Empty;
List<string> traversedChars = new List<string>();
for(int i = 0; i < words.Length; i++)
{
if (!traversedChars.Contains(words[i].ToString()))
{
string tripleChar = $"{words[i]}{words[i]}{words[i]}";
if (words.Contains(tripleChar))
{
foundResult = true;
wordWith3SameChar = words;
break;
}
}
}
return new Tuple<bool, string>(foundResult, wordWith3SameChar);
}
按如下方式调用即可得到答案
var resultTuple = ValidateWord("Why llll ddd ssssssss".Split(' ').ToArray());
if (resultTuple.Item1)
{
Console.WriteLine($"The following words have 3 similar charecters: " + resultTuple.Item2);
}
else
{
Console.WriteLine("No words has consecutive 3 similar charecters.");
}
使用扩展方法来确定一个单词是否具有 运行 个连续的 n 个字符:
public static class StringExt {
public static bool WordHasConsecutive(this string word, int n) {
if (word.Length <= 1)
return false;
if (n < 2)
return true;
if (word.Length >= n) {
var ch = word[0];
var count = 1;
for (int i = 1; i < word.Length; ++i) {
if (word[i] == ch) {
if (++count == n)
return true;
}
else {
ch = word[i];
count = 1;
}
}
}
return false;
}
}
答案很简单,只需 return 具有至少 运行 长度 3 的单词:
var ans = words.Where(w => w.WordHasConsecutive(3));
所以我一直在努力用C#写一个函数 接收单词的字符串数组(在被函数从字符串接收到单词数组之前被拆分)。
所以我一直在尝试做的是一个函数,它检查是否在整个数组中除了words[0]
(words是单词数组的名称)之外是否有显示更多的字母超过 3 次(至少 3 次)。
比如这句话:
Why llll ddd ssssssss !!!!!!!?
因为没有一个字母在一个单词中出现超过 3 次 - 这样的单词不存在。 该函数的基础如下所示:
public bool MultipleCheck(string[] words)
{
}
到目前为止我想出了什么...我知道它有错误...我还没有修复它:
public bool AttachmentsCheck(string[] words)
{
string currentWord;
int wordCounter = 0;
for (int i=0; i < words.Length; i++)
{
currentWord = words[i];
for (int j = 0; j < currentWord.Length; j++)
{
char[] wordArr = currentWord.ToCharArray();
for (int k=0; k < wordArr.Length; k++)
{
if (wordArr[k]==wordArr[wordArr.Length-k])
{
wordCounter++;
}
}
}
if (wordCounter => 3)
{
return false;
}
}
return true;
}
我希望这能奏效
private static Tuple<bool, string> ValidateWord(string[] words)
{
bool foundResult = false;
List<string> all3CharWords = new List<string>();
string wordWith3SameChar = string.Empty;
foreach (var word in words)
{
var resultTuple = ValidateWord(word);
if (resultTuple.Item1)
{
foundResult = true;
all3CharWords.Add(resultTuple.Item2);
}
}
if (foundResult)
{
wordWith3SameChar = String.Join(";", all3CharWords.ToArray());
}
return new Tuple<bool, string>(foundResult, wordWith3SameChar);
}
private static Tuple<bool, string> ValidateWord(string words)
{
bool foundResult = false;
string wordWith3SameChar = string.Empty;
List<string> traversedChars = new List<string>();
for(int i = 0; i < words.Length; i++)
{
if (!traversedChars.Contains(words[i].ToString()))
{
string tripleChar = $"{words[i]}{words[i]}{words[i]}";
if (words.Contains(tripleChar))
{
foundResult = true;
wordWith3SameChar = words;
break;
}
}
}
return new Tuple<bool, string>(foundResult, wordWith3SameChar);
}
按如下方式调用即可得到答案
var resultTuple = ValidateWord("Why llll ddd ssssssss".Split(' ').ToArray());
if (resultTuple.Item1)
{
Console.WriteLine($"The following words have 3 similar charecters: " + resultTuple.Item2);
}
else
{
Console.WriteLine("No words has consecutive 3 similar charecters.");
}
使用扩展方法来确定一个单词是否具有 运行 个连续的 n 个字符:
public static class StringExt {
public static bool WordHasConsecutive(this string word, int n) {
if (word.Length <= 1)
return false;
if (n < 2)
return true;
if (word.Length >= n) {
var ch = word[0];
var count = 1;
for (int i = 1; i < word.Length; ++i) {
if (word[i] == ch) {
if (++count == n)
return true;
}
else {
ch = word[i];
count = 1;
}
}
}
return false;
}
}
答案很简单,只需 return 具有至少 运行 长度 3 的单词:
var ans = words.Where(w => w.WordHasConsecutive(3));