📜  计算一个句子的难度

📅  最后修改于: 2022-05-13 01:57:08.625000             🧑  作者: Mango

计算一个句子的难度

计算给定句子的难度。如果一个单词有 4 个连续的辅音或辅音的数量多于元音的数量,则这里的单词被认为是困难的。别的词很简单。句子难度定义为 5*(难词数)+ 3*(易词数)。
例子:

Input : str = "Difficulty of sentence"
Output : 13
Hard words = 2(Difficulty and sentence)
Easy words = 1(of)
So, answer is 5*2+3*1 = 13

提问:微软

执行:
开始遍历字符串并执行以下步骤:-

  • 如果当前字符是元音并设置连续辅音计数=0,则增加元音计数。
  • 否则增加辅音计数,也增加连续辅音计数。
  • 检查连续辅音是否变为 4,则当前单词很难,因此增加其计数并移动到下一个单词。将所有计数重置为 0。
  • 否则检查一个单词是否完成并且辅音数量大于元音数量,
    那么这是一个很难的词,否则是一个容易的词。将所有计数重置为 0。

C++
// C++ program to find difficulty of a sentence
#include 
using namespace std;
 
// Utility function to check character is vowel
// or not
bool isVowel(char ch)
{
    return ( ch == 'a' || ch == 'e' ||
             ch == 'i' || ch == 'o' ||
             ch == 'u');
}
 
// Function to calculate difficulty
int calcDiff(string str)
{
 
    int count_vowels = 0, count_conso = 0;
    int hard_words = 0, easy_words = 0;
    int consec_conso = 0;
 
    // Start traversing the string
    for (int i = 0; i < str.length(); i++)
    {
        // Check if current character is vowel
        // or consonant
        if (str[i] != ' ' && isVowel(tolower(str[i])))
        {
            // Increment if vowel
            count_vowels++;
            consec_conso = 0;
        }
 
        // Increment counter for consonant
        // also maintain a separate counter for
        // counting consecutive consonants
        else if (str[i]!= ' ')
        {
            count_conso++;
            consec_conso++;
        }
 
        // If we get 4 consecutive consonants
        // then it is a hard word
        if (consec_conso == 4)
        {
            hard_words++;
 
            // Move to the next word
            while (i < str.length() && str[i]!= ' ')
                i++;
 
            // Reset all counts
            count_conso = 0;
            count_vowels = 0;
            consec_conso = 0;
        }
 
        else if ( i < str.length() &&
                  (str[i] == ' ' || i == str.length()-1))
        {
            // Increment hard_words, if no. of consonants are
            // higher than no. of vowels, otherwise increment
            // count_vowels
            count_conso > count_vowels ? hard_words++
                                       : easy_words++;
 
            // Reset all counts
            count_conso = 0;
            count_vowels = 0;
            consec_conso = 0;
        }
    }
 
    // Return difficulty of sentence
    return 5 * hard_words + 3 * easy_words;
}
 
// Drivers code
int main()
{
    string str = "I am a geek";
    string str2 = "We are geeks";
    cout << calcDiff(str) << endl;
    cout << calcDiff(str2) << endl;
 
    return 0;
}


Java
// Java program to find difficulty of a sentence
 
class GFG
{
    // Utility method to check character is vowel
    // or not
    static boolean isVowel(char ch)
    {
        return ( ch == 'a' || ch == 'e' ||
                 ch == 'i' || ch == 'o' ||
                 ch == 'u');
    }
      
    // Method to calculate difficulty
    static int calcDiff(String str)
    {
      
        int count_vowels = 0, count_conso = 0;
        int hard_words = 0, easy_words = 0;
        int consec_conso = 0;
      
        // Start traversing the string
        for (int i = 0; i < str.length(); i++)
        {
            // Check if current character is vowel
            // or consonant
            if (str.charAt(i) != ' ' && isVowel(Character.toLowerCase(str.charAt(i))))
            {
                // Increment if vowel
                count_vowels++;
                consec_conso = 0;
            }
      
            // Increment counter for consonant
            // also maintain a separate counter for
            // counting consecutive consonants
            else if (str.charAt(i)!= ' ')
            {
                count_conso++;
                consec_conso++;
            }
      
            // If we get 4 consecutive consonants
            // then it is a hard word
            if (consec_conso == 4)
            {
                hard_words++;
      
                // Move to the next word
                while (i < str.length() && str.charAt(i)!= ' ')
                    i++;
      
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
      
            else if ( i < str.length() &&
                      (str.charAt(i) == ' ' || i == str.length()-1))
            {
                // Increment hard_words, if no. of consonants are
                // higher than no. of vowels, otherwise increment
                // count_vowels
                if(count_conso > count_vowels)
                    hard_words++;
                else
                    easy_words++;
      
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
        }
      
        // Return difficulty of sentence
        return 5 * hard_words + 3 * easy_words;
    }
     
    // Driver method
    public static void main (String[] args)
    {
        String str = "I am a geek";
        String str2 = "We are geeks";
        System.out.println(calcDiff(str));
        System.out.println(calcDiff(str2));
    }
}


Python 3
# Python3 program to find difficulty
# of a sentence
 
# Utility function to check character
# is vowel or not
def isVowel(ch):
    return (ch == 'a' or ch == 'e' or
            ch == 'i' or ch == 'o' or
            ch == 'u')
 
# Function to calculate difficulty
def calcDiff(str):
    str = str.lower()
    count_vowels = 0
    count_conso = 0
    consec_conso = 0
    hard_words = 0
    easy_words = 0
 
    # Start traversing the string
    for i in range(0, len(str)):
         
        # Check if current character is
        # vowel or consonant
        if(str[i]!= " " and isVowel(str[i])):
             
            # Increment if vowel
            count_vowels += 1
            consec_conso = 0
             
        # Increment counter for consonant
        # also maintain a separate counter for
        # counting consecutive consonants
        elif(str[i] != " "):
            count_conso += 1
            consec_conso += 1
 
        # If we get 4 consecutive consonants
        # then it is a hard word
        if(consec_conso == 4):
            hrad_words += 1
 
            # Move to the next word
            while(i < len(str) and str[i] != " "):
                i += 1
                 
            # Reset all counts
            count_conso = 0
            count_vowels = 0
            consec_conso = 0
        elif(i < len(str) and (str[i] == ' ' or
                          i == len(str) - 1)):
                               
            # Increment hard_words, if no. of
            # consonants are higher than no. of
            # vowels, otherwise increment count_vowels
            if(count_conso > count_vowels):
                hard_words += 1
            else:
                easy_words += 1
 
            # Reset all counts
            count_conso = 0
            count_vowels = 0
            consec_conso = 0
             
    # Return difficulty of sentence    
    return (5 * hard_words + 3 * easy_words)
     
# Driver Code
if __name__=="__main__":
    str = "I am a geek"
    str2 = "We are geeks"
    print(calcDiff(str))
    print(calcDiff(str2))
 
# This code is contributed
# by Sairahul Jella


C#
// C# program to find difficulty
// of a sentence
using System;
     
public class GFG {
     
    // Utility method to check character
    // is vowel or not
    static bool isVowel(char ch)
    {
        return (ch == 'a' || ch == 'e' ||
                ch == 'i' || ch == 'o' ||
                ch == 'u');
    }
     
    // Method to calculate difficulty
    static int calcDiff(string str)
    {
        int count_vowels = 0, count_conso = 0;
        int hard_words = 0, easy_words = 0;
        int consec_conso = 0;
     
        // Start traversing the string
        for (int i = 0; i < str.Length; i++)
        {
             
            // Check if current character
            // is vowel or consonant
            if (str[i] != ' ' &&
                isVowel(char.ToLower( str[i])))
            {
                // Increment if vowel
                count_vowels++;
                consec_conso = 0;
            }
     
            // Increment counter for consonant
            // also maintain a separate counter for
            // counting consecutive consonants
            else if (str[i]!= ' ')
            {
                count_conso++;
                consec_conso++;
            }
     
            // If we get 4 consecutive consonants
            // then it is a hard word
            if (consec_conso == 4)
            {
                hard_words++;
     
                // Move to the next word
                while (i < str.Length && str[i]!= ' ')
                    i++;
     
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
     
            else if ( i < str.Length &&
                    (str[i] == ' ' || i == str.Length-1))
            {
                 
                // Increment hard_words, if no. of
                // consonants are higher than no.
                // of vowels, otherwise increment
                // count_vowels
                if(count_conso > count_vowels)
                    hard_words++;
                else
                    easy_words++;
     
                // Reset all counts
                count_conso = 0;
                count_vowels = 0;
                consec_conso = 0;
            }
        }
     
        // Return difficulty of sentence
        return 5 * hard_words + 3 * easy_words;
    }
     
    // Driver code
    public static void Main ()
    {
        string str = "I am a geek";
        string str2 = "We are geeks";
        Console.WriteLine(calcDiff(str));
        Console.WriteLine(calcDiff(str2));
    }
}
 
// This code is contributed by Sam007.


Javascript


输出:

12
11