langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
44
Task/Balanced-brackets/PureBasic/balanced-brackets.purebasic
Normal file
44
Task/Balanced-brackets/PureBasic/balanced-brackets.purebasic
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 ballanced.")
|
||||
Else
|
||||
PrintN(" is not ballanced")
|
||||
EndIf
|
||||
Next
|
||||
Loading…
Add table
Add a link
Reference in a new issue