Calcular la dificultad de una oración.

Calcular la dificultad de una oración dada. Aquí una palabra se considera difícil si tiene 4 consonantes consecutivas o el número de consonantes es mayor que el número de vocales. Otra palabra es fácil. La dificultad de la oración se define como 5*(número de palabras difíciles) + 3*(número de palabras fáciles).

Ejemplos: 

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

Preguntado en: Microsoft

Implementación: 
Comience a atravesar la string y realice los siguientes pasos:

  • Incrementa el conteo de vocales si el carácter actual es una vocal y establece el consonante consecutivo = 0.
  • De lo contrario, incremente el recuento de consonantes, también incremente el recuento de consonantes consecutivas.
  • Compruebe si las consonantes consecutivas se convierten en 4, entonces la palabra actual es difícil, así que incremente su conteo y pase a la siguiente palabra. Restablezca todos los conteos a 0.
  • De lo contrario, verifique si una palabra está completa y el número de consonantes es mayor que el número de vocales, 
    entonces es una palabra difícil o una palabra fácil. Restablezca todos los conteos a 0.

Implementación:

C++

// C++ program to find difficulty of a sentence
#include <iostream>
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

<script>
 
    // JavaScript program to find
    // difficulty of a sentence
     
    // Utility method to check character
    // is vowel or not
    function isVowel(ch)
    {
        return (ch == 'a' || ch == 'e' ||
                ch == 'i' || ch == 'o' ||
                ch == 'u');
    }
       
    // Method to calculate difficulty
    function calcDiff(str)
    {
        let count_vowels = 0, count_conso = 0;
        let hard_words = 0, easy_words = 0;
        let consec_conso = 0;
       
        // Start traversing the string
        for (let i = 0; i < str.length; i++)
        {
               
            // Check if current character
            // is vowel or consonant
            if (str[i] != ' ' &&
                isVowel(str[i].toLowerCase()))
            {
                // 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;
    }
     
    let str = "I am a geek";
    let str2 = "We are geeks";
    document.write(calcDiff(str) + "</br>");
    document.write(calcDiff(str2));
     
</script>
Producción

12
11

Complejidad temporal : O(n) 
Espacio auxiliar : O(1)

Este artículo es una contribución de Sahil Chhabra . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks. 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *