diff --git a/README.md b/README.md
index b618e9a..073156e 100644
--- a/README.md
+++ b/README.md
@@ -3,18 +3,27 @@
Tests for puzzles found on Leetcode and other interview preparation websites.
Minimum Height Tree
-To run the solution:
+Procedure:
-1. Enter your input and expected output arguments in testcases/minimumheighttree.json as per schema defined in the file.
-2. Run `python3 solutions/python3/minimumheighttree.py`
- python3 testharness.py rotatearray.py
+1. Write the solution for desired problem in workspace/python3/
+2. To check the solution,run `python3 testharness.py filename(without extension)`
+ Example: `python3 testharness.py rotatearray`
The results can be seen as
-Your Answer : [0, 1] True Answer: [0, 1]
-Correct answer
-For now, it only works for python3.
+```Running testcase workspace/python3workspace/rotatearray.py
+Correct Answer
+Your Answer: [5, 6, 7, 1, 2, 3, 4] Expected Answer: [5, 6, 7, 1, 2, 3, 4]
+Correct Answer
+Your Answer: [1, 2, 3] Expected Answer: [1, 2, 3]
+Correct Answer
+Your Answer: [2, 1] Expected Answer: [2, 1]
+All testcases passed. Good going.
+```
+\
+For now, it only works for python3.
+\
TODO:
Add support for more languages.
Add code for many more leetcode puzzles.
diff --git a/createProblem.py b/createProblem.py
new file mode 100644
index 0000000..92fec91
--- /dev/null
+++ b/createProblem.py
@@ -0,0 +1,57 @@
+from shutil import copyfile
+import os
+
+newfile = 'zigzagconversion'
+oldfile = 'dummy'
+src = f'./goldFiles/python3/{oldfile}.py'
+dst = f'./goldFiles/python3/{newfile}.py'
+copyfile(src, dst)
+
+src = f'./testcases/{oldfile}.json'
+dst = f'./testcases/{newfile}.json'
+copyfile(src, dst)
+
+src = f'./workspace./python3workspace./{oldfile}.py'
+dst = f'./workspace./python3workspace./{newfile}.py'
+copyfile(src, dst)
+
+
+########
+#populate __init__.py
+def py(s):
+ if s[-3:] == '.py' and s[:2] != '__':
+ return True
+
+ return False
+
+fileslist = os.listdir('./goldFiles/python3/')
+print(fileslist)
+
+solsNames = list(filter(py,fileslist))
+
+solsImports = ["import sys","sys.path.insert(0,'./goldFiles/python3')",""]
+for s in solsNames:
+ sol = f"from {s[:-3]} import *"
+ solsImports.append(sol)
+
+init = open('./goldFiles/python3/__init__.py','w')
+init.write('\n'.join(solsImports))
+init.close()
+print('\n'.join(solsImports))
+# print(solsImports)
+
+
+fileslist = os.listdir('./workspace/python3workspace/')
+print(fileslist)
+
+solsNames = list(filter(py,fileslist))
+
+solsImports = ["import sys","sys.path.insert(0,'./workspace/python3workspace')",""]
+for s in solsNames:
+ sol = f"from {s[:-3]} import *"
+ solsImports.append(sol)
+
+init = open('./workspace/python3workspace/__init__.py','w')
+init.write('\n'.join(solsImports))
+init.close()
+print('\n'.join(solsImports))
\ No newline at end of file
diff --git a/createTestcase.py b/createTestcase.py
new file mode 100644
index 0000000..1a13ef2
--- /dev/null
+++ b/createTestcase.py
@@ -0,0 +1,45 @@
+import json
+import random,string
+import numpy as np
+import sys
+problem_keyword = sys.argv[1]
+# problem_keyword is used specifying the program that needs to be run
+# problem_keyword = 'permutations'
+
+def genArgUsingDtype(d,type):
+ # Read the datatype and generate random testcase
+ if d == "string": # generate a random string of length between 3 and 50
+ length = random.randint(3,50)
+ return ''.join(random.choice(string.ascii_letters) for m in range(length))
+ elif d == "integer": # integer numbers
+ return random.randint(0,50)
+ elif d == "array":
+ if type['items']['type'] == 'number': # generate a random array
+ return list(np.random.randint(-50,50,random.randint(1,50)))
+ elif d == "number": #float numbers
+ return random.random()*random.randint(-100,100)
+ elif d == "matrix": # generate a square matrix of size matrixSize
+ matrixSize = random.randint(1,15)
+ return np.random.randint(0,2,(matrixSize,matrixSize))
+
+testcases = open(f'./testcases/{problem_keyword}.json')
+testcases = json.load(testcases)
+# print(testcases)
+
+
+inputArgs = list(testcases['testcases'][0]['args'].keys())
+schema = testcases['schema']
+args = testcases['testcases'][0]['args'].copy()
+
+if 'checkUnorderedLists' in inputArgs:
+ inputArgs.remove('checkUnorderedLists')
+for i in inputArgs:
+ """
+ pass all the input args types to the generator
+ function and get random testcase for input argument
+ """
+ dtype = schema['properties']['args']['properties'][i]['type']
+ arg = genArgUsingDtype(dtype,schema['properties']['args']['properties'][i])
+ args[i] = arg
+
+print(args)
\ No newline at end of file
diff --git a/goldFiles/python3/__init__.py b/goldFiles/python3/__init__.py
new file mode 100644
index 0000000..e17d524
--- /dev/null
+++ b/goldFiles/python3/__init__.py
@@ -0,0 +1,15 @@
+import sys
+sys.path.insert(0,'./goldFiles/python3')
+
+from dummy import *
+from firstmissingpositive import *
+from medianoftwosortedarrays import *
+from minimumheighttree import *
+from multiplystrings import *
+from palindromepartitioning import *
+from permutations import *
+from pow_x_n import *
+from pythonsolution import *
+from rotatearray import *
+from rotateimage import *
+from zigzagconversion import *
\ No newline at end of file
diff --git a/goldFiles/python3/dummy.py b/goldFiles/python3/dummy.py
new file mode 100644
index 0000000..e9f6207
--- /dev/null
+++ b/goldFiles/python3/dummy.py
@@ -0,0 +1,6 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ """Add your methods here for gold solution"""
\ No newline at end of file
diff --git a/goldFiles/python3/firstmissingpositive.py b/goldFiles/python3/firstmissingpositive.py
new file mode 100644
index 0000000..7bb42b5
--- /dev/null
+++ b/goldFiles/python3/firstmissingpositive.py
@@ -0,0 +1,26 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ def firstMissingPositive(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ Basic idea:
+ 1. for any array whose length is l, the first missing positive must be in range [1,...,l+1],
+ so we only have to care about those elements in this range and remove the rest.
+ 2. we can use the array index as the hash to restore the frequency of each number within
+ the range [1,...,l+1]
+ """
+ nums.append(0)
+ n = len(nums)
+ for i in range(len(nums)): #delete those useless elements
+ if nums[i]<0 or nums[i]>=n:
+ nums[i]=0
+ for i in range(len(nums)): #use the index as the hash to record the frequency of each number
+ nums[nums[i]%n]+=n
+ for i in range(1,len(nums)):
+ if nums[i]/n==0:
+ return i
+ return n
\ No newline at end of file
diff --git a/goldFiles/python3/medianoftwosortedarrays.py b/goldFiles/python3/medianoftwosortedarrays.py
new file mode 100644
index 0000000..2c47f45
--- /dev/null
+++ b/goldFiles/python3/medianoftwosortedarrays.py
@@ -0,0 +1,25 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ # @return a float
+ def findMedianSortedArrays(self, A, B):
+ l=len(A)+len(B)
+ return self.findKth(A,B,l//2) if l%2==1 else (self.findKth(A,B,l//2-1)+self.findKth(A,B,l//2))/2.0
+
+
+ def findKth(self,A,B,k):
+ if len(A)>len(B):
+ A,B=B,A
+ if not A:
+ return B[k]
+ if k==len(A)+len(B)-1:
+ return max(A[-1],B[-1])
+ i=len(A)//2
+ j=k-i
+ if A[i]>B[j]:
+ #Here I assume it is O(1) to get A[:i] and B[j:]. In python, it's not but in cpp it is.
+ return self.findKth(A[:i],B[j:],i)
+ else:
+ return self.findKth(A[i:],B[:j],j)
\ No newline at end of file
diff --git a/goldFiles/python3/minimumheighttree.py b/goldFiles/python3/minimumheighttree.py
new file mode 100644
index 0000000..196bd46
--- /dev/null
+++ b/goldFiles/python3/minimumheighttree.py
@@ -0,0 +1,7 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ def multiply(self, num1: str, num2: str) -> str:
+ return str(int(num1)*int(num2))
\ No newline at end of file
diff --git a/goldFiles/python3/multiplystrings.py b/goldFiles/python3/multiplystrings.py
new file mode 100644
index 0000000..196bd46
--- /dev/null
+++ b/goldFiles/python3/multiplystrings.py
@@ -0,0 +1,7 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ def multiply(self, num1: str, num2: str) -> str:
+ return str(int(num1)*int(num2))
\ No newline at end of file
diff --git a/goldFiles/python3/palindromepartitioning.py b/goldFiles/python3/palindromepartitioning.py
new file mode 100644
index 0000000..509f366
--- /dev/null
+++ b/goldFiles/python3/palindromepartitioning.py
@@ -0,0 +1,21 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ # palindromepartition
+ def partition(self, s):
+ res = []
+ self.dfs(s, [], res)
+ return res
+
+ def dfs(self, s, path, res):
+ if not s:
+ res.append(path)
+ return
+ for i in range(1, len(s)+1):
+ if self.isPal(s[:i]):
+ self.dfs(s[i:], path+[s[:i]], res)
+
+ def isPal(self, s):
+ return s == s[::-1]
diff --git a/goldFiles/python3/permutations.py b/goldFiles/python3/permutations.py
new file mode 100644
index 0000000..b14ed32
--- /dev/null
+++ b/goldFiles/python3/permutations.py
@@ -0,0 +1,9 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ def permute(self, nums):
+ return [[n] + p
+ for i, n in enumerate(nums)
+ for p in self.permute(nums[:i] + nums[i+1:])] or [[]]
\ No newline at end of file
diff --git a/goldFiles/python3/pow_x_n.py b/goldFiles/python3/pow_x_n.py
new file mode 100644
index 0000000..cb1b81c
--- /dev/null
+++ b/goldFiles/python3/pow_x_n.py
@@ -0,0 +1,7 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ def myPow(self, x: float, n: int) -> float:
+ return x**n
\ No newline at end of file
diff --git a/solutionsfortestcasegeneration/python3/pythonsolution.py b/goldFiles/python3/pythonsolution.py
similarity index 62%
rename from solutionsfortestcasegeneration/python3/pythonsolution.py
rename to goldFiles/python3/pythonsolution.py
index 79acb1f..8e85f59 100644
--- a/solutionsfortestcasegeneration/python3/pythonsolution.py
+++ b/goldFiles/python3/pythonsolution.py
@@ -3,8 +3,8 @@
from schema import Schema, And, Use, Optional, SchemaError
class Solution:
-
#One method for each keyword.
+ # minimumheighttree
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
try:
n = Schema(int).validate(n)
@@ -39,6 +39,8 @@ def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
except :
return "Check the format of input variables"
+
+ # rotatearray
def reverse(self, nums,start,end):
while start < end:
nums[start],nums[end] = nums[end],nums[start]
@@ -52,3 +54,37 @@ def rotate(self, nums: List[int], k: int) -> None:
self.reverse(nums,0,k-1)
self.reverse(nums,k,n-1)
# nums[:] = nums[-k:] + nums[:-k]
+
+ # rotatematrix
+ def rotate(self, matrix: List[List[int]]) -> None:
+ self.transpose(matrix)
+ self.reflect(matrix)
+
+ def transpose(self, matrix):
+ n = len(matrix)
+ for i in range(n):
+ for j in range(i, n):
+ matrix[j][i], matrix[i][j] = matrix[i][j], matrix[j][i]
+
+ def reflect(self, matrix):
+ n = len(matrix)
+ for i in range(n):
+ for j in range(n // 2):
+ matrix[i][j], matrix[i][-j - 1] = matrix[i][-j - 1], matrix[i][j]
+
+ # palindromepartition
+ def partition(self, s):
+ res = []
+ self.dfs(s, [], res)
+ return res
+
+ def dfs(self, s, path, res):
+ if not s:
+ res.append(path)
+ return
+ for i in range(1, len(s)+1):
+ if self.isPal(s[:i]):
+ self.dfs(s[i:], path+[s[:i]], res)
+
+ def isPal(self, s):
+ return s == s[::-1]
diff --git a/goldFiles/python3/rotatearray.py b/goldFiles/python3/rotatearray.py
new file mode 100644
index 0000000..317ce4d
--- /dev/null
+++ b/goldFiles/python3/rotatearray.py
@@ -0,0 +1,20 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ # rotatearray
+ def reverse(self, nums,start,end):
+ while start < end:
+ nums[start],nums[end] = nums[end],nums[start]
+ start,end = start+1,end-1
+
+ def rotate(self, nums: List[int], k: int) -> None:
+ n = len(nums)
+ k %= n
+
+ self.reverse(nums,0,n-1)
+ self.reverse(nums,0,k-1)
+ self.reverse(nums,k,n-1)
+ # nums[:] = nums[-k:] + nums[:-k]
+
diff --git a/goldFiles/python3/rotateimage.py b/goldFiles/python3/rotateimage.py
new file mode 100644
index 0000000..fe71207
--- /dev/null
+++ b/goldFiles/python3/rotateimage.py
@@ -0,0 +1,23 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution:
+ #One method for each keyword.
+
+ # rotateimage
+ def rotate(self, matrix: List[List[int]]) -> None:
+ self.transpose(matrix)
+ self.reflect(matrix)
+
+ def transpose(self, matrix):
+ n = len(matrix)
+ for i in range(n):
+ for j in range(i, n):
+ matrix[j][i], matrix[i][j] = matrix[i][j], matrix[j][i]
+
+ def reflect(self, matrix):
+ n = len(matrix)
+ for i in range(n):
+ for j in range(n // 2):
+ matrix[i][j], matrix[i][-j - 1] = matrix[i][-j - 1], matrix[i][j]
\ No newline at end of file
diff --git a/goldFiles/python3/zigzagconversion.py b/goldFiles/python3/zigzagconversion.py
new file mode 100644
index 0000000..c24c350
--- /dev/null
+++ b/goldFiles/python3/zigzagconversion.py
@@ -0,0 +1,26 @@
+from typing import List
+import json
+from schema import Schema, And, Use, Optional, SchemaError
+
+class Solution(object):
+ def convert(self, s, numRows):
+ """
+ :type s: str
+ :type numRows: int
+ :rtype: str
+ """
+ if numRows == 1 or numRows >= len(s):
+ return s
+
+ L = [''] * numRows
+ index, step = 0, 1
+
+ for x in s:
+ L[index] += x
+ if index == 0:
+ step = 1
+ elif index == numRows -1:
+ step = -1
+ index += step
+
+ return ''.join(L)
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..bc359f1
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,4 @@
+schema==0.7.4
+typing==3.7.4.3
+typing-extensions==3.10.0.0
+jsonschema==3.2.0
\ No newline at end of file
diff --git a/testcases/dummy.json b/testcases/dummy.json
new file mode 100644
index 0000000..bfe72b0
--- /dev/null
+++ b/testcases/dummy.json
@@ -0,0 +1,34 @@
+{
+ "schema": {
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "var1": {
+ "type": "array",
+ "items": {
+ "type": "number"
+ }
+ },
+ "var2": { "type": "integer" }
+ }
+ },
+ "expected": {
+ "type": "array",
+ "items": {
+ "type": "number"
+ }
+ }
+ }
+ },
+ "testcases": [
+ {
+ "args": {
+ "var1": [1, 2, 3, 4, 5, 6, 7],
+ "var2": 3
+ },
+ "expected": [5, 6, 7, 1, 2, 3, 4]
+ }
+ ]
+}
diff --git a/testcases/firstmissingpositive.json b/testcases/firstmissingpositive.json
new file mode 100644
index 0000000..ef36ffb
--- /dev/null
+++ b/testcases/firstmissingpositive.json
@@ -0,0 +1,28 @@
+{
+ "schema": {
+ "args": {
+ "nums": "List[int]"
+ },
+ "expected": "int"
+ },
+ "testcases": [
+ {
+ "args": {
+ "nums": [1, 2, 0]
+ },
+ "expected": 3
+ },
+ {
+ "args": {
+ "nums": [3, 4, -1, 1]
+ },
+ "expected": 2
+ },
+ {
+ "args": {
+ "nums": [7, 8, 9, 11, 12]
+ },
+ "expected": 1
+ }
+ ]
+}
diff --git a/testcases/medianoftwosortedarrays.json b/testcases/medianoftwosortedarrays.json
new file mode 100644
index 0000000..7bfbb1e
--- /dev/null
+++ b/testcases/medianoftwosortedarrays.json
@@ -0,0 +1,61 @@
+{
+ "schema": {
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "nums1": {
+ "type": "array",
+ "items": {
+ "type": "number"
+ }
+ },
+ "nums2": {
+ "type": "array",
+ "items": {
+ "type": "number"
+ }
+ }
+ }
+ },
+ "expected": {
+ "type": "number"
+ }
+ }
+ },
+ "testcases": [
+ {
+ "args": { "nums1": [1, 3], "nums2": [2] },
+ "expected": 2.0
+ },
+ {
+ "args": {
+ "nums1": [1, 2],
+ "nums2": [3, 4]
+ },
+ "expected": 2.5
+ },
+ {
+ "args": {
+ "nums1": [0, 0],
+ "nums2": [0, 0]
+ },
+ "expected": 0
+ },
+ {
+ "args": {
+ "nums1": [1],
+ "nums2": []
+ },
+ "expected": 1
+ },
+ {
+ "args": {
+ "nums1": [2],
+ "nums2": []
+ },
+ "expected": 2
+ }
+ ]
+}
diff --git a/testcases/minimumheighttree.json b/testcases/minimumheighttree.json
index f7528dd..47fe379 100644
--- a/testcases/minimumheighttree.json
+++ b/testcases/minimumheighttree.json
@@ -1,10 +1,26 @@
{
"schema": {
- "args": {
- "n": "int",
- "edges": "List[List[int]]"
- },
- "expected": "List[int]"
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "edges": {
+ "type": "array",
+ "items": {
+ "type": "array"
+ }
+ },
+ "n": { "type": "integer" }
+ }
+ },
+ "expected": {
+ "type": "array",
+ "items": {
+ "type": "number"
+ }
+ }
+ }
},
"testcases": [
{
diff --git a/testcases/multiplystrings.json b/testcases/multiplystrings.json
new file mode 100644
index 0000000..dc78df5
--- /dev/null
+++ b/testcases/multiplystrings.json
@@ -0,0 +1,31 @@
+{
+ "schema": {
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "num1": { "type": "string" },
+ "num2": { "type": "string" }
+ }
+ },
+ "expected": { "type": "string" }
+ }
+ },
+ "testcases": [
+ {
+ "args": {
+ "num1": "2",
+ "num2": "3"
+ },
+ "expected": "6"
+ },
+ {
+ "args": {
+ "num1": "123",
+ "num2": "456"
+ },
+ "expected": "56088"
+ }
+ ]
+}
diff --git a/testcases/palindromepartitioning.json b/testcases/palindromepartitioning.json
new file mode 100644
index 0000000..94d4ffa
--- /dev/null
+++ b/testcases/palindromepartitioning.json
@@ -0,0 +1,38 @@
+{
+ "schema": {
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "s": {
+ "type": "string"
+ }
+ }
+ },
+ "expected": {
+ "type": "array",
+ "items": {
+ "type": "array"
+ }
+ }
+ }
+ },
+ "testcases": [
+ {
+ "args": {
+ "s": "aab"
+ },
+ "expected": [
+ ["a", "a", "b"],
+ ["aa", "b"]
+ ]
+ },
+ {
+ "args": {
+ "s": "a"
+ },
+ "expected": [["a"]]
+ }
+ ]
+}
diff --git a/testcases/permutations.json b/testcases/permutations.json
new file mode 100644
index 0000000..7908be8
--- /dev/null
+++ b/testcases/permutations.json
@@ -0,0 +1,57 @@
+{
+ "schema": {
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "nums": {
+ "type": "array",
+ "items": {
+ "type": "number"
+ }
+ }
+ }
+ },
+ "expected": {
+ "type": "array",
+ "items": {
+ "type": "array"
+ }
+ }
+ }
+ },
+ "testcases": [
+ {
+ "args": {
+ "nums": [0, 1],
+ "checkUnorderedLists": true
+ },
+ "expected": [
+ [0, 1],
+ [1, 0]
+ ]
+ },
+ {
+ "args": {
+ "nums": [1, 2, 3],
+ "checkUnorderedLists": true
+ },
+ "expected": [
+ [1, 2, 3],
+ [1, 3, 2],
+ [2, 1, 3],
+ [2, 3, 1],
+ [3, 1, 2],
+ [3, 2, 1]
+ ]
+ },
+ {
+ "args": {
+ "nums": [1],
+ "checkUnorderedLists": true
+ },
+ "expected": [[1]]
+ }
+ ]
+}
diff --git a/testcases/pow_x_n.json b/testcases/pow_x_n.json
new file mode 100644
index 0000000..d54196e
--- /dev/null
+++ b/testcases/pow_x_n.json
@@ -0,0 +1,40 @@
+{
+ "schema": {
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "x": { "type": "number" },
+ "n": { "type": "integer" }
+ }
+ },
+ "expected": {
+ "type": "number"
+ }
+ }
+ },
+ "testcases": [
+ {
+ "args": {
+ "x": 2.0,
+ "n": 10
+ },
+ "expected": 1024.0
+ },
+ {
+ "args": {
+ "x": 2.1,
+ "n": 3
+ },
+ "expected": 9.261
+ },
+ {
+ "args": {
+ "x": 2.0,
+ "n": -2
+ },
+ "expected": 0.25
+ }
+ ]
+}
diff --git a/testcases/rotatearray.json b/testcases/rotatearray.json
index 10e2d3f..2f973d9 100644
--- a/testcases/rotatearray.json
+++ b/testcases/rotatearray.json
@@ -1,10 +1,26 @@
{
"schema": {
- "args": {
- "nums": "List[int]",
- "k": "int"
- },
- "expected": "List[int]"
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "nums": {
+ "type": "array",
+ "items": {
+ "type": "number"
+ }
+ },
+ "k": { "type": "integer" }
+ }
+ },
+ "expected": {
+ "type": "array",
+ "items": {
+ "type": "number"
+ }
+ }
+ }
},
"testcases": [
{
diff --git a/testcases/rotateimage.json b/testcases/rotateimage.json
new file mode 100644
index 0000000..5368a59
--- /dev/null
+++ b/testcases/rotateimage.json
@@ -0,0 +1,40 @@
+{
+ "schema": {
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "matrix": {
+ "type": "array",
+ "items": {
+ "type": "array"
+ }
+ }
+ }
+ },
+ "expected": {
+ "type": "array",
+ "items": {
+ "type": "array"
+ }
+ }
+ }
+ },
+ "testcases": [
+ {
+ "args": {
+ "matrix": [
+ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9]
+ ]
+ },
+ "expected": [
+ [7, 4, 1],
+ [8, 5, 2],
+ [9, 6, 3]
+ ]
+ }
+ ]
+}
diff --git a/testcases/zigzagconversion.json b/testcases/zigzagconversion.json
new file mode 100644
index 0000000..6473878
--- /dev/null
+++ b/testcases/zigzagconversion.json
@@ -0,0 +1,42 @@
+{
+ "schema": {
+ "type": "object",
+ "properties": {
+ "args": {
+ "type": "object",
+ "properties": {
+ "s": {
+ "type": "string"
+ },
+ "numRows": { "type": "integer" }
+ }
+ },
+ "expected": {
+ "type": "string"
+ }
+ }
+ },
+ "testcases": [
+ {
+ "args": {
+ "s": "PAYPALISHIRING",
+ "numRows": 3
+ },
+ "expected": "PAHNAPLSIIGYIR"
+ },
+ {
+ "args": {
+ "s": "PAYPALISHIRING",
+ "numRows": 4
+ },
+ "expected": "PINALSIGYAHRPI"
+ },
+ {
+ "args": {
+ "s": "A",
+ "numRows": 1
+ },
+ "expected": "A"
+ }
+ ]
+}
diff --git a/testharness.py b/testharness.py
index 70c9742..31c39e1 100644
--- a/testharness.py
+++ b/testharness.py
@@ -1,31 +1,63 @@
-import json
import sys
+import json
+import jsonschema
+from jsonschema import validate
sys.path.insert(1, './workspace/')
-import python3
+import python3workspace
if ((len(sys.argv) != 2) or (sys.argv[1] == "--help")):
- print ("Usage: " + sys.argv[0] + " ")
+ print ("Usage: python testharness.py ")
+ quit()
-problem_keyword = sys.argv[1]
+def checkLists(l1,l2):
+ # Method to compare two lists if they are unordered
+ l1 = list(map(tuple,l1))
+ l2 = list(map(tuple,l2))
+ if set(l1) == set(l2):
+ return 1
+ return 0
+
+problem_keyword = sys.argv[1]
+# problem_keyword is used specifying the program that needs to be run
+# problem_keyword = 'rotatearray'
loc = 'testcases/'+problem_keyword+'.json'
testcases = open(loc,'r')
testcasesJson = json.load(testcases)
-print ("Running testcase workspace/python3/" + problem_keyword + ".py")
+allTestcasesPassed = True
+print ("Running testcase workspace/python3workspace/" + problem_keyword + ".py")
+schema = testcasesJson['schema']
+
for testcase in testcasesJson['testcases']:
- userAnswer = getattr(python3,problem_keyword)(testcase['args'])
- allTestcasesPassed = True;
try:
+ validate(testcase,schema)
+ # Validate the testcase with schema present in json file.
+ print("testcase in correct format")
+ except jsonschema.exceptions.ValidationError as err:
+ print(testcase )
+ raise Exception("Check the testcase format")
+ # raise error if the testcase didnot pass validation
+ userAnswer = getattr(python3workspace,problem_keyword)(testcase['args'])
+
+ if "checkUnorderedLists" in testcase['args'].keys() and testcase['args']['checkUnorderedLists'] == True:
+ # If the output of the user solution need not be ordered, then use this to check.
+ print('check for unordered lists')
+ result = checkLists(userAnswer,testcase['expected'])
+ else:
+ result = userAnswer != testcase['expected']
+
+ if result:
print('Correct Answer')
- print("Your Answer: ",userAnswer," Expected Answer: ",testcase['expected'])
- except:
- if (userAnswer != testcase['expected']):
- allTestcasesPassed = false;
- print('Wrong Answer, Please check your solution ')
- print("Your Answer: ",userAnswer," Expected Answer: ",testcase['expected'])
-
- if (allTestcasesPassed):
- print ("All testcases passed. Good going.")
+ print("Your Answer: ",userAnswer," Expected Answer: ",testcase['expected'])
else:
- print ("Some testcases failed. Please fix your testcases and try again")
- assert allTestcasesPassed
+ allTestcasesPassed = False
+ print('Wrong Answer, Please check your solution ')
+ print("Your Answer: ",userAnswer," Expected Answer: ",testcase['expected'])
+
+if (allTestcasesPassed):
+ print ("All testcases passed. Good going.")
+else:
+ print ("Some testcases failed. Please fix your solution and try again")
+ assert allTestcasesPassed
+ # Raise error if all testcases didnot pass
+
diff --git a/workspace/python3/__init__.py b/workspace/python3/__init__.py
deleted file mode 100644
index 426843d..0000000
--- a/workspace/python3/__init__.py
+++ /dev/null
@@ -1,4 +0,0 @@
-import sys
-sys.path.insert(0,'./workspace/python3')
-from minimumheighttree import *
-from rotatearray import *
\ No newline at end of file
diff --git a/workspace/python3/requirements.txt b/workspace/python3/requirements.txt
deleted file mode 100644
index 01883b9..0000000
--- a/workspace/python3/requirements.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-schema==0.7.4
-typing==3.7.4.3
-typing-extensions==3.10.0.0
\ No newline at end of file
diff --git a/workspace/python3workspace/__init__.py b/workspace/python3workspace/__init__.py
new file mode 100644
index 0000000..531f287
--- /dev/null
+++ b/workspace/python3workspace/__init__.py
@@ -0,0 +1,14 @@
+import sys
+sys.path.insert(0,'./workspace/python3workspace')
+
+from dummy import *
+from firstmissingpositive import *
+from medianoftwosortedarrays import *
+from minimumheighttree import *
+from multiplystrings import *
+from palindromepartitioning import *
+from permutations import *
+from pow_x_n import *
+from rotatearray import *
+from rotateimage import *
+from zigzagconversion import *
\ No newline at end of file
diff --git a/workspace/python3workspace/dummy.py b/workspace/python3workspace/dummy.py
new file mode 100644
index 0000000..ccdc087
--- /dev/null
+++ b/workspace/python3workspace/dummy.py
@@ -0,0 +1,5 @@
+def dummy(testcases):
+
+ #"Write your code here and run with testHarness"
+
+ return
\ No newline at end of file
diff --git a/workspace/python3workspace/firstmissingpositive.py b/workspace/python3workspace/firstmissingpositive.py
new file mode 100644
index 0000000..e0b1462
--- /dev/null
+++ b/workspace/python3workspace/firstmissingpositive.py
@@ -0,0 +1,9 @@
+from typing import List
+
+def firstmissingpositive(testcases) -> int:
+ nums = testcases['nums']
+ firstpositive = 1
+ """Write your code here"""
+
+ return firstpositive
+
diff --git a/workspace/python3workspace/medianoftwosortedarrays.py b/workspace/python3workspace/medianoftwosortedarrays.py
new file mode 100644
index 0000000..f1b063e
--- /dev/null
+++ b/workspace/python3workspace/medianoftwosortedarrays.py
@@ -0,0 +1,12 @@
+from typing import List
+
+
+def medianoftwosortedarrays(testcases) -> List[int]:
+
+ #"Write your code here and run with testHarness"
+ nums1,nums2 = testcases['nums1'],testcases['nums2']
+ median = 0
+
+ """Write your code"""
+
+ return median
diff --git a/workspace/python3/minimumheighttree.py b/workspace/python3workspace/minimumheighttree.py
similarity index 100%
rename from workspace/python3/minimumheighttree.py
rename to workspace/python3workspace/minimumheighttree.py
diff --git a/workspace/python3workspace/multiplystrings.py b/workspace/python3workspace/multiplystrings.py
new file mode 100644
index 0000000..d3aab96
--- /dev/null
+++ b/workspace/python3workspace/multiplystrings.py
@@ -0,0 +1,12 @@
+from typing import List
+
+def multiplystrings(testcases) -> List[int]:
+
+ #"Write your code here and run with testHarness"
+ num1,num2 = testcases['num1'],testcases['num2']
+
+ product = 1
+ """Write your code here"""
+
+ return product
+
\ No newline at end of file
diff --git a/workspace/python3workspace/palindromepartitioning.py b/workspace/python3workspace/palindromepartitioning.py
new file mode 100644
index 0000000..c110252
--- /dev/null
+++ b/workspace/python3workspace/palindromepartitioning.py
@@ -0,0 +1,7 @@
+from typing import List
+
+def palindromepartitioning(testcase):
+ s = testcase['s']
+
+ """Write your code."""
+ return []
diff --git a/workspace/python3workspace/permutations.py b/workspace/python3workspace/permutations.py
new file mode 100644
index 0000000..8b1e330
--- /dev/null
+++ b/workspace/python3workspace/permutations.py
@@ -0,0 +1,10 @@
+from typing import List
+
+def permutations(testcases) -> List[List[int]]:
+
+ #"Write your code here and run with testHarness"
+ nums = testcases['nums']
+ allPermutations = [[]]
+
+ return allPermutations
+
\ No newline at end of file
diff --git a/workspace/python3workspace/pow_x_n.py b/workspace/python3workspace/pow_x_n.py
new file mode 100644
index 0000000..0ef554b
--- /dev/null
+++ b/workspace/python3workspace/pow_x_n.py
@@ -0,0 +1,10 @@
+from typing import List
+
+def pow_x_n(testcases) -> float:
+
+ x,n = testcases['x'],testcases['n']
+ pow = 1
+ """Write your code here"""
+
+ return pow
+
diff --git a/workspace/python3/rotatearray.py b/workspace/python3workspace/rotatearray.py
similarity index 91%
rename from workspace/python3/rotatearray.py
rename to workspace/python3workspace/rotatearray.py
index 647d4c7..f466fc3 100644
--- a/workspace/python3/rotatearray.py
+++ b/workspace/python3workspace/rotatearray.py
@@ -5,7 +5,7 @@ def reverse(nums,start,end):
nums[start],nums[end] = nums[end],nums[start]
start,end = start+1,end-1
-def rotatearray(testcases) -> None:
+def rotatearray(testcases) -> List[int]:
#"Write your code here and run with testHarness"
nums,k = testcases['nums'],testcases['k']
diff --git a/workspace/python3workspace/rotateimage.py b/workspace/python3workspace/rotateimage.py
new file mode 100644
index 0000000..f1d97ed
--- /dev/null
+++ b/workspace/python3workspace/rotateimage.py
@@ -0,0 +1,7 @@
+from typing import List
+
+def rotateimage(testcase):
+ matrix = testcase['matrix']
+
+ """Write your code."""
+ return []
diff --git a/workspace/python3workspace/zigzagconversion.py b/workspace/python3workspace/zigzagconversion.py
new file mode 100644
index 0000000..901f2aa
--- /dev/null
+++ b/workspace/python3workspace/zigzagconversion.py
@@ -0,0 +1,5 @@
+def zigzagconversion(testcases):
+
+ #"Write your code here and run with testHarness"
+
+ return ""
\ No newline at end of file