human
What is a code to number of ways to arrange a word such that no vowels occur together and the other steps required given the following:
Number of ways to arrange a word such that no vowels occur together | Function to check if a character is vowel or consonent.
Function to calculate factorial of a number.
Calculating no of ways for arranging vowels.
Iterate the map and count the number of vowels and calculate no of ways to arrange vowels.
calculating no of ways to arrange the given word such that vowels come together.
calculate no of ways to arrange vowels.
to store denominator of fraction.
count of consonents.
calculate the number of ways to arrange the word such that vowels come together.
To calculate total number of permutations.
To store length of the given word.
denominator of fraction.
return total number of permutations of the given word.
Function to calculate number of permutations such that no vowels come together.
to store frequency of character.
count frequency of acharacters.
calculate total number of permutations.
calculate total number of permutations such that vowels come together.
substrat vwl_tgthr from total to get the result.
return the result.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to number of ways to arrange a word such that no vowels occur together and the other steps required.
 def isVowel ( ch ) :
	if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) :
		return True
	else :
		return False
def fact ( n ) :
	if ( n < 2 ) :
		return 1
	return n * fact ( n - 1 )
def only_vowels ( freq ) :
	denom = 1
	cnt_vwl = 0
	for itr in freq :
		if ( isVowel ( itr ) ) :
			denom *= fact ( freq [ itr ] )
			cnt_vwl += freq [ itr ]
	return fact ( cnt_vwl ) // denom
def all_vowels_together ( freq ) :
	vow = only_vowels ( freq )
	denom = 1
	cnt_cnst = 0
	for itr in freq :
		if ( isVowel ( itr ) == False ) :
			denom *= fact ( freq [ itr ] )
			cnt_cnst += freq [ itr ]
	ans = fact ( cnt_cnst + 1 ) // denom
	return ( ans * vow )
def total_permutations ( freq ) :
	cnt = 0
	denom = 1
	for itr in freq :
		denom *= fact ( freq [ itr ] )
		cnt += freq [ itr ]
	return fact ( cnt ) // denom
def no_vowels_together ( word ) :
	freq = dict ( )
	for i in word :
		ch = i . lower ( )
		freq [ ch ] = freq . get ( ch , 0 ) + 1
	total = total_permutations ( freq )
	vwl_tgthr = all_vowels_together ( freq )
	res = total - vwl_tgthr
	return res
word = " allahabad "
ans = no_vowels_together ( word )
print ( ans )
word = " geeksforgeeks "
ans = no_vowels_together ( word )
print ( ans )
word = " abcd "
ans = no_vowels_together ( word )
print ( ans )