计算句子的难度

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

null
Input : str = "Difficulty of sentence"Output : 13Hard 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 <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>


输出:

1211

本文由 萨希尔·查布拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享