Examples in Gambas (#5) by @vdelacruzjardon

This commit is contained in:
vdelacruzjardon
2016-09-16 13:41:52 -05:00
committed by Aditya Bhargava
parent e2a4153e80
commit 0aec97dbf2
41 changed files with 355 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=01_binary_search
Startup=binary_search
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,27 @@
' Gambas module file
Function binary_search(list As Float[], item As Float) As Variant
Dim low, high, Midd, guess As Float
low = 0
high = list.Length - 1
While low <= high
Midd = (low + high)
guess = list[Midd]
If guess = item Then
Return Midd
Else If guess > item Then
high = Midd - 1
Else
low = Midd + 1
Endif
Wend
Return Null
End
Public Sub Main()
Dim my_list As New Float[]
my_list = [1, 3, 5, 7, 9]
Print binary_search(my_list, 3) '1
Print binary_search(my_list, -1) 'null
End

View File

@@ -0,0 +1,7 @@
binary_search
01_binary_search
0
0
0.0.1

View File

@@ -0,0 +1,4 @@
# Gambas Project File 3.0
Title=01_selection_sort
Startup=MMain

View File

@@ -0,0 +1,40 @@
' Gambas module file
Function findSmallest(arr As Float[]) As Integer
Dim smallest As Float
Dim i, smallest_index As Integer
smallest_index = 0
smallest = arr[0]
For i = 1 To arr.Length - 1
If arr[i] < smallest Then
smallest = arr[i]
smallest_index = i
Endif
Next
Return smallest_index
End
Function selectionSort(arr As Float[]) As Float[]
Dim newArr As New Float[]
Dim smallest, i As Integer
Dim element As Float
For i = 0 To arr.Length - 1
smallest = findSmallest(arr)
newArr.Push(arr[smallest])
arr.Remove(smallest)
Next
Return newArr
End
Public Sub Main()
Dim r As New Float[]
Dim element As Float
Dim i As Integer
r = [5, 3, 6, 2, 10]
r = selectionSort(r)
For Each element In r
Print element
Next
End

View File

@@ -0,0 +1,7 @@
MMain
01_selection_sort
0
0

Binary file not shown.

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=01_countdown
Startup=countdown
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,15 @@
' Gambas module file
Function countdown(i As Integer) As Integer
Print i
If i <= 0 Then
Return
Else
countdown(i - 1)
Endif
End
Public Sub Main()
countdown(5)
End

View File

@@ -0,0 +1,7 @@
countdown
01_countdown
0
0
0.0.1

Binary file not shown.

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=02_greet
Startup=greet
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,17 @@
' Gambas module file
Function greet2(name As String)
Print "how are you, "; name; "?"
End
Function bye()
Print "ok bye!"
End
Function greet(name As String)
Print "hello, "; name; "!"
greet2(name)
Print "getting ready to say bye..."
bye()
End
Public Sub Main()
greet("adit")
End

View File

@@ -0,0 +1,7 @@
greet
02_greet
0
0
0.0.1

Binary file not shown.

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=03_factorial
Startup=factorial
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,14 @@
' Gambas module file
Function fact(x As Float) As Float
If x = 1 Then
Return 1
Else
Return x * fact(x - 1)
Endif
End
Public Sub Main()
Print fact(5)
End

View File

@@ -0,0 +1,7 @@
factorial
03_factorial
0
0
0.0.1

Binary file not shown.

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=01_loop_sum
Startup=loop_sum
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,15 @@
' Gambas module file
Function sum(arr As Integer[]) As Integer
Dim total, x As Integer
total = 0
For x = 0 To arr.Length - 1
total += arr[x]
Next
Return total
End
Public Sub Main()
Print sum([1, 2, 3, 4, 5])
End

View File

@@ -0,0 +1,7 @@
loop_sum
01_loop_sum
0
0
0.0.1

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=02_recursive_count
Startup=recursive_count
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,13 @@
' Gambas module file
Function count(list As Float[]) As Float
If list.Length = 0 Then
Return 0
Endif
Return 1 + count(list.Copy(1, list.Length - 1))
End
Public Sub Main()
Print count([0, 1, 2, 3, 4, 5]) '6
End

View File

@@ -0,0 +1,7 @@
recursive_count
02_recursive_count
0
0
0.0.1

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=02_recursive_sum
Startup=recursive_sum
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,13 @@
' Gambas module file
Function sum(list As Float[]) As Float
If list.Length = 0 Then
Return 0
Endif
Return list[0] + sum(list.Copy(1, list.Length - 1))
End
Public Sub Main()
Print sum([1, 2, 3, 4])
End

View File

@@ -0,0 +1,7 @@
recursive_sum
02_recursive_sum
0
0
0.0.1

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=04_recursive_max
Startup=recursive_max
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,25 @@
' Gambas module file
Function mymax(list As Float[]) As Float
Dim sub_max As Float
If list.Length = 2 Then
If list[0] > list[1] Then
Return list[0]
Else
Return list[1]
Endif
Endif
sub_max = mymax(list.Copy(1, list.Length - 1))
If list[0] > sub_max Then
Return list[0]
Else
Return sub_max
Endif
End
Public Sub Main()
Print mymax([1, 5, 10, 25, 16, 1]) '25
End

View File

@@ -0,0 +1,7 @@
recursive_max
04_recursive_max
0
0
0.0.1

Binary file not shown.

View File

View File

@@ -0,0 +1,7 @@
# Gambas Project File 3.0
# Compiled with Gambas 3.5.4
Title=05_quicksort
Startup=quicksort
Version=0.0.1
TabSize=2
Packager=1

View File

@@ -0,0 +1,39 @@
' Gambas module file
Function quicksort(myarray As Float[]) As Float[]
Dim pivot As Float
Dim i As Integer
Dim less, greater, r As New Float[]
If myarray.Length < 2 Then
Return myarray
Else
pivot = myarray[0]
For i = 1 To myarray.Length - 1
If myarray[i] <= pivot Then
less.add(myarray[i])
Else
greater.add(myarray[i])
End If
Next
Endif
r.Insert(quicksort(less))
r.Add(pivot)
r.Insert(quicksort(greater))
Return r
End
Public Sub Main()
Dim myarray As Float[]
Dim element As Float
Dim x As Integer
myarray = [10, 5, 2, 3]
myarray = quicksort(myarray)
For Each element In myarray
Print element
Next
End

View File

@@ -0,0 +1,7 @@
quicksort
05_quicksort
0
0
0.0.1