human
What is a code to check a given sentence for a given set of simple grammer rules and the other steps required given the following:
Check a given sentence for a given set of simple grammer rules | Method to check a given sentence for given rules.
Calculate the length of the string ..
Check that the first character lies in [ A - Z ] . Otherwise return false ..
If the last character is not a full stop ( . ) no need to check further ..
Maintain 2 states . Previous and current state based on which vertex state you are . Initialise both with 0 = start state ..
Keep the index to the next character in the string ..
Loop to go over the string ..
Set states according to the input characters in the string and the rule defined in the description . If current character is [ A - Z ] . Set current state as 0..
If current character is a space . Set current state as 1..
If current character is a space . Set current state as 2..
If current character is a space . Set current state as 3..
Validates all current state with previous state for the rules in the description of the problem ..
If we have reached last state and previous state is not 1 , then check next character . If next character is ' \0' , then return true , else false.
Set previous state as current state before going over to the next character ..
Driver program"
Solution in Python:
assistant
#Here is code to check a given sentence for a given set of simple grammer rules and the other steps required.
 def checkSentence ( string ) :
	length = len ( string )
	if string [ 0 ] < ' A ' or string [ 0 ] > ' Z ' :
		return False
	if string [ length - 1 ] != ' . ' :
		return False
	prev_state = 0
	curr_state = 0
	index = 1
	while ( string [ index ] ) :
		if string [ index ] >= ' A ' and string [ index ] <= ' Z ' :
			curr_state = 0
		elif string [ index ] == ' ▁ ' :
			curr_state = 1
		elif string [ index ] >= ' a ' and string [ index ] <= ' z ' :
			curr_state = 2
		elif string [ index ] == ' . ' :
			curr_state = 3
		if prev_state == curr_state and curr_state != 2 :
			return False
		if prev_state == 2 and curr_state == 0 :
			return False
		if curr_state == 3 and prev_state != 1 :
			return True
		index += 1
		prev_state = curr_state
	return False
string = [ " I ▁ love ▁ cinema . " , " The ▁ vertex ▁ is ▁ S . " , " I ▁ am ▁ single . " , " My ▁ name ▁ is ▁ KG . " , " I ▁ lovE ▁ cinema . " , " GeeksQuiz . ▁ is ▁ a ▁ quiz ▁ site . " , " I ▁ love ▁ Geeksquiz ▁ and ▁ Geeksforgeeks . " , " ▁ You ▁ are ▁ my ▁ friend . " , " I ▁ love ▁ cinema " ]
string_size = len ( string )
for i in xrange ( string_size ) :
	if checkSentence ( string [ i ] ) :
		print " \ " " + string [ i ] + " \ " ▁ is ▁ correct "
	else :
		print " \ " " + string [ i ] + " \ " ▁ is ▁ incorrect "