【问题标题】:I need assistance with a function我需要有关功能的帮助
【发布时间】:2019-07-26 07:25:52
【问题描述】:

所以我一直在努力用 C# 编写一个函数 接收单词的字符串数组(在被函数从字符串接收到单词数组之前被拆分)。

所以我一直在尝试做的是一个函数,它检查整个数组中是否有一个字母显示超过 3 次(至少 3 个)。

比如这句话:

为什么 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;
}

【问题讨论】:

  • 那么,到目前为止,您想出了什么?也许是一些代码?
  • 你试过什么?请分享您的代码示例。你得到了什么例外?
  • 请帮帮我...我给了你我想出的东西...
  • 我相信this question应该能帮到你
  • "因为一个单词中出现的字母不超过 3 次,所以不存在这样的单词。" 我不敢苟同。这个词怎么样:senseless?或者这个词怎么样:unintentionally?你只是声称这样的词不存在。 ;-)

标签: c# project


【解决方案1】:

使用扩展方法判断一个单词是否有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;
    }
}

答案很简单,只需返回运行长度至少为 3 的单词:

var ans = words.Where(w => w.WordHasConsecutive(3));

【讨论】:

    【解决方案2】:

    我希望这会奏效

    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.");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多