Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
44
Task/Balanced-brackets/PureBasic/balanced-brackets.basic
Normal file
44
Task/Balanced-brackets/PureBasic/balanced-brackets.basic
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
Procedure.s Generate(N)
|
||||
For i=1 To N
|
||||
sample$+"[]"
|
||||
Next
|
||||
For i=Len(sample$)-1 To 2 Step -1
|
||||
r=Random(i-1)+1
|
||||
If r<>i
|
||||
a.c=PeekC(@sample$+r*SizeOf(Character))
|
||||
b.c=PeekC(@sample$+i*SizeOf(Character))
|
||||
PokeC(@sample$+r*SizeOf(Character), b)
|
||||
PokeC(@sample$+i*SizeOf(Character), a)
|
||||
EndIf
|
||||
Next
|
||||
ProcedureReturn sample$
|
||||
EndProcedure
|
||||
|
||||
Procedure Balanced(String$)
|
||||
Protected *p.Character, cnt
|
||||
*p=@String$
|
||||
While *p\c
|
||||
If *p\c='['
|
||||
cnt+1
|
||||
ElseIf *p\c=']'
|
||||
cnt-1
|
||||
If cnt<0: Break: EndIf
|
||||
EndIf
|
||||
*p+SizeOf(Character)
|
||||
Wend
|
||||
If cnt=0
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
;- Test code
|
||||
OpenConsole()
|
||||
For i=1 To 5
|
||||
TestString$ = Generate(i)
|
||||
Print(TestString$)
|
||||
If Balanced(TestString$)
|
||||
PrintN(" is balanced.")
|
||||
Else
|
||||
PrintN(" is not balanced")
|
||||
EndIf
|
||||
Next
|
||||
Loading…
Add table
Add a link
Reference in a new issue