Skip to content

Instantly share code, notes, and snippets.

@moonblade
Created October 20, 2019 14:13
Show Gist options
  • Save moonblade/f8904bef08f34057e2bf63243c4c4654 to your computer and use it in GitHub Desktop.
Save moonblade/f8904bef08f34057e2bf63243c4c4654 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python
# generic test case maker for hacker rank questions
from __future__ import print_function
from enum import Enum
import random
# Input types, Attributes
# INT : minVal, maxVal, name
# STATIC: value
# LOOP: length, elements (list of other inputs)
defaults = {
"intMinVal": 1,
"intMaxVal": 10000
}
named = {}
class Type(Enum):
INT="INT"
STATIC="STATIC"
LOOP="LOOP"
class Input():
def __init__(self, dict):
self.type = Type.INT
self.name = None
self.dict = dict
if "type" in dict:
self.type = Type[dict["type"]]
if self.type == Type.INT:
self.minVal = defaults["intMinVal"]
self.maxVal = defaults["intMaxVal"]
if "minVal" in dict:
self.minVal = self.getNumber("minVal")
if "maxVal" in dict:
self.maxVal = self.getNumber("maxVal")
if self.type == Type.STATIC:
self.value = " "
if "value" in dict:
self.value = dict["value"]
if self.type == Type.LOOP:
self.elements = []
self.length = self.getNumber("length")
if "elements" in dict:
for y in range(self.length):
for x in dict["elements"]:
self.elements.append(Input(x))
self.generate()
if "name" in dict:
self.name = dict["name"]
named[self.name] = self
def getNumber(self, key):
returnVal = None
if key in defaults:
returnVal = defaults[key]
if key in self.dict:
if isinstance(self.dict[key], int):
returnVal = self.dict[key]
if isinstance(self.dict[key], str):
if self.dict[key] in named:
returnVal = named[self.dict[key]].value
return returnVal
def generate(self):
if self.type == Type.INT:
self.value = random.randint(self.minVal, self.maxVal)
if self.type == Type.LOOP:
self.value = [x.value for x in self.elements]
class Generator():
def __init__(self, input):
self.input = []
for x in input:
self.input.append(Input(x))
def generate(self, testCases):
string = []
for x in range(testCases):
testCase = []
for y in self.input:
testCase.append(y.value)
string.append(testCase)
return string
def pprint(self, string):
for x in string:
for y in x:
if isinstance(y, list):
for z in y:
print(z, end='')
else:
print(y, end='')
print()
if __name__ == "__main__":
input = [
{
"type": "INT",
"minVal": 1,
"maxVal": 10,
"name": "firstNumber"
},
{
"type": "STATIC",
"value": "\n"
},
{
"type": "LOOP",
"length": "firstNumber",
"elements": [{
"type": "INT"
}, {
"type": "STATIC",
"value": " "
}]
},
]
g=Generator(input)
g.pprint(g.generate(1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment