I was studying F# for professional purposes. I know Python/Django and C#/.NET Core. So I wanted to translate some code that I wrote in Python into F#. I was getting hung up or just plain tired, not sure, thought I could use some collaboration. I wanted to do else continue but found out thats not an option. Original python code:
#!/Users/ryandines/.local/share/virtualenvs/netcoreapp2.0-5nmzfUwt/bin/python
NUMBER_OF_TESTS = input()
INT_MIN = -2**32
def can_represent_bst(pre):
stack = []
root = INT_MIN
for value in pre:
if value < root:
return False
while(stack and stack[-1] < value):
root = stack.pop()
stack.append(value)
return True
for test in range(int(NUMBER_OF_TESTS)):
node_count = int(input())
nodes = list(map(int, input().split(' ')))
print("NODES: " + str(nodes))
if can_represent_bst(pre=nodes):
print("YES")
else:
print("NO")
And so far this is what I got that compiles and works in F#:
open System
let NumberOfTests = Console.ReadLine() |> int
let root = Int32.MinValue
let seq1 = seq { for i in 0 .. NumberOfTests-1 -> (i, i*i) }
let CanRepresentBST args =
printfn "Passed args: %s" args
let result = args.Split ' '
let IntList = [for i in result -> i |> int32]
for value in IntList do
printfn "%d" value
[<EntryPoint>]
let main argv =
printfn "Hello World from F#!"
printfn "Number of tests: %d" NumberOfTests
printfn "Int min value: %d" root
for _ in seq1 do
let NodeCount = Console.ReadLine()
let Nodes = Console.ReadLine()
printfn "NodeCount: %s" NodeCount
let _ = CanRepresentBST Nodes
0 |> ignore
0
The not done part is this:
if value < root:
return False
while(stack and stack[-1] < value):
root = stack.pop()
stack.append(value)
Probably am going to sleep on it but would love if someone did the heavy lifting for me so I can knock it out when I wake up.