Skip to content

Instantly share code, notes, and snippets.

@preetampvp
Created May 31, 2020 04:13
Show Gist options
  • Save preetampvp/480d9bf3933946cc299c4fd85b5a9413 to your computer and use it in GitHub Desktop.
Save preetampvp/480d9bf3933946cc299c4fd85b5a9413 to your computer and use it in GitHub Desktop.
Hackerrank 2nd question
import re
from collections import Counter
def buildDictionary():
# { "code": "char"}
return {
"0": "a",
"1": "b",
"2": "c",
"3": "d",
"4": "e",
"5": "f",
"6": "g",
"7": "h",
"8": "i",
"9": "j",
"0#": "k",
"1#": "l",
"2#": "m",
"3#": "n",
"4#": "o",
"5#": "p",
"6#": "q",
"7#": "r",
"8#": "s",
"9#": "t",
"20&": "u",
"21&": "v",
"22&": "w",
"23&": "x",
"24&": "y",
"25&": "z",
}
def frequencyCount(s):
andsMatches = re.findall(r"((\[\d+\])?2[0-6]&)", s)
ands = [i[0] for i in andsMatches if len(andsMatches) > 0]
hashMatches = re.findall(r"((\[\d+\])?\d#)", s)
hashes = [i[0] for i in hashMatches if len(hashMatches) > 0]
digitsStr = s
for i in ands:
s = s.replace(i, "")
for i in hashes:
s = s.replace(i, "")
digitsMatches = re.findall(r"((\[\d+\])?\d{1,1})", s)
digits = [i[0] for i in digitsMatches if len(digitsMatches) > 0]
allChars = []
for i in ands + hashes + digits:
if "]" in i:
splits = i.split("]")
multiplier = int(splits[0].replace("[", ""))
allChars.extend([splits[1] for m in range(multiplier)])
else:
allChars.append(i)
aggregated = Counter(allChars)
check = { v : aggregated.get(k, 0) for k, v in buildDictionary().items()}
print(check)
results = [ aggregated.get(k, 0) for k in buildDictionary().keys()]
return results
def main():
data = "[2]12[4]25&24&2#5#"
results = frequencyCount(data)
print(results)
if __name__ == "__main__":
main()
## 0-9
## 0#
## 20&
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment