Skip to content

Instantly share code, notes, and snippets.

@moonblade
Created October 20, 2019 16:40
Show Gist options
  • Save moonblade/7bffa57265809631f8a1b1a0e9357665 to your computer and use it in GitHub Desktop.
Save moonblade/7bffa57265809631f8a1b1a0e9357665 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python3
# generic test case maker for hacker rank questions
from __future__ import print_function
from enum import Enum
import random
# import lorem
# import nltk
from datetime import datetime
from random import randrange
from datetime import timedelta
# Input types, Attributes
# common: name(string), print(bool)
# Int : minVal, maxVal
# Static: value
# Loop: length, elements (list of other inputs)
# Date: from, to, format
defaults = {
"intMinVal": 1,
"intMaxVal": 10000,
"loopLength": 10,
"staticValue": " ",
"dateFrom": "1970-01-01",
"dateTo": "2100-12-31",
"dateFormat": "%Y-%m-%d",
"print": True
}
named = {}
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = randrange(int_delta)
return start + timedelta(seconds=random_second)
class Type(Enum):
Int="Int"
Static="Static"
Loop="Loop"
Date="Date"
class Input():
def __init__(self, dict):
self.dict = dict
self.name = self.getData("name")
self.type = Type[dict["type"]]
self.print = self.getData("print")
named[self.name] = self
def getData(self, key, defKey=None):
returnVal = None
if defKey is None and key in defaults:
returnVal = defaults[key]
if defKey is not None and defKey in defaults:
returnVal = defaults[defKey]
if key in self.dict:
if isinstance(self.dict[key], int):
returnVal = self.dict[key]
if self.dict[key] in named:
returnVal = named[self.dict[key]].value
else:
returnVal = self.dict[key]
# print(key, returnVal)
return returnVal
def generate(self):
pass
@staticmethod
def getInput(dict):
return globals()[dict["type"]](dict)
class Int(Input):
def __init__(self, dict):
super(Int, self).__init__(dict)
self.minVal = self.getData("minVal", "intMinVal")
self.maxVal = self.getData("maxVal", "intMaxVal")
self.generate()
def generate(self):
self.value = random.randint(self.minVal, self.maxVal)
class Static(Input):
def __init__(self, dict):
super(Static, self).__init__(dict)
self.generate()
def generate(self):
self.value = self.getData("value", "staticValue")
class Loop(Input):
def __init__(self, dict):
super(Loop, self).__init__(dict)
self.elements = []
self.length = self.getData("length", "loopLength")
if "elements" in dict:
for y in range(self.length):
for x in dict["elements"]:
self.elements.append(Input.getInput(x))
self.generate()
def generate(self):
if self.type == Type.Loop:
self.value = [x.value for x in self.elements]
class Date(Input):
def __init__(self, dict):
super(Date, self).__init__(dict)
self.format = self.getData("format", "dateFormat")
self.dateFrom = datetime.strptime(self.getData("from", "dateFrom"), self.format)
self.to = datetime.strptime(self.getData("to", "dateTo"), self.format)
self.generate()
def generate(self):
self.value = random_date(self.dateFrom, self.to).strftime(self.format)
class Generator():
def __init__(self, input):
self.input = []
for x in input:
self.input.append(Input.getInput(x))
def generate(self, testCases):
string = []
for _ in range(testCases):
testCase = []
for y in self.input:
if y.print:
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",
"print": False
},
{
"type": "Date"
},
{
"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