Skip to content

Instantly share code, notes, and snippets.

@ChlorUpload
Created December 17, 2019 13:14
Show Gist options
  • Save ChlorUpload/06d1a622f2b050da3d2cc0172265ac0e to your computer and use it in GitHub Desktop.
Save ChlorUpload/06d1a622f2b050da3d2cc0172265ac0e to your computer and use it in GitHub Desktop.
Solving "number of cases" math problem in python
from itertools import permutations
li_Consanant = [ 'B', 'C', 'D', 'F', 'G', 'H']
li_Vowel = ['A', 'E']
# check every consanant is neighbor in a statement
def isValid(statement): # statement list of code
def isConsanant(code):
global li_Consanant
global li_Vowel
return True if code in li_Consanant else False
if len(statement) <= 1:
return False
# cons_cnt records how many times changed
# from vowel to consanant in a statement
last_char = isConsanant(statement[0])
cons_cnt = 1 if last_char else 0
for code in statement[1:]:
cons_state = isConsanant(code)
if cons_state != last_char and cons_state == True:
cons_cnt += 1
last_char = cons_state
if cons_cnt <= 1:
return True
else:
return False
# execute
validnum = 0
for i in permutations('ABCDEFGH'):
if isValid(i):
validnum += 1
print(validnum)
# 3! x 6! = 4320 is the answer of this problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment