Skip to content

Instantly share code, notes, and snippets.

@ganmahmud
Created September 2, 2021 17:52
Show Gist options
  • Save ganmahmud/03236b70eabc15e65aea4d0b8958ddcc to your computer and use it in GitHub Desktop.
Save ganmahmud/03236b70eabc15e65aea4d0b8958ddcc to your computer and use it in GitHub Desktop.
Consistency Problem From Facebook Hacker Cup 2021
import sys
def isVowel(char):
return char.lower() in 'aeiou';
def characterFreq(string):
v = {}
c = {}
out_dict = {}
vowel_num = 0
consonent_num = 0
hasDir = ''
hasDirOp = ''
isSpecial = False
for n in string:
if (isVowel(n)):
vowel_num += 1
v_keys = v.keys()
if n in v_keys:
v[n] += 1
else:
v[n] = 1
else:
consonent_num += 1
c_keys = c.keys()
if n in c_keys:
c[n] += 1
else:
c[n] = 1
out_dict['vowel'] = dict(sorted(v.items(), key=lambda item: item[1], reverse=True))
out_dict['consonent'] = dict(sorted(c.items(), key=lambda item: item[1], reverse=True))
if (vowel_num < consonent_num):
if (vowel_num != 0):
hasDir = 'vowel'
hasDirOp = 'consonent'
else:
hasDir = 'consonent'
hasDirOp = 'vowel'
isSpecial = True
elif (vowel_num == consonent_num):
if(len(out_dict['vowel'].keys()) == 1):
hasDir = 'vowel'
hasDirOp = 'consonent'
else:
hasDir = 'consonent'
hasDirOp = 'vowel'
else:
if(consonent_num != 0):
hasDir = 'consonent'
hasDirOp = 'vowel'
else:
hasDir = 'vowel'
hasDirOp = 'consonent'
isSpecial = True
out_dict['dir'] = hasDir
out_dict['dir_opp'] = hasDirOp
out_dict['special'] = isSpecial
return out_dict
def getConsistancyTime(string):
count = 0;
string = string.rstrip('\n')
if(len(string) != 1):
obj = characterFreq(string)
direction = obj['dir']
direction_opp = obj['dir_opp']
if(obj['special'] == False):
chosenHash = obj[direction]
count += sum(obj[direction_opp].values())
mostFrequent = next(iter( chosenHash.items() ));
count += (sum(chosenHash.values()) - mostFrequent[1]) * 2
else:
count += sum(obj[direction].values())
return count
if __name__ == "__main__":
#for multiline input, Ctrl/Command + Z to complete input
userInput = sys.stdin.readlines()[1:]
for index,value in enumerate(userInput):
num_of_secs = getConsistancyTime(value)
print(f"Case #{str(index+1)}: {num_of_secs}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment