Skip to content

Commit 0aec97d

Browse files
vdelacruzjardonegonSchiele
authored andcommitted
Examples in Gambas (#5) by @vdelacruzjardon
1 parent e2a4153 commit 0aec97d

File tree

41 files changed

+355
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+355
-0
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=01_binary_search
4+
Startup=binary_search
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
' Gambas module file
2+
3+
Function binary_search(list As Float[], item As Float) As Variant
4+
Dim low, high, Midd, guess As Float
5+
low = 0
6+
high = list.Length - 1
7+
While low <= high
8+
Midd = (low + high)
9+
guess = list[Midd]
10+
If guess = item Then
11+
Return Midd
12+
Else If guess > item Then
13+
high = Midd - 1
14+
Else
15+
low = Midd + 1
16+
Endif
17+
Wend
18+
Return Null
19+
End
20+
21+
Public Sub Main()
22+
23+
Dim my_list As New Float[]
24+
my_list = [1, 3, 5, 7, 9]
25+
Print binary_search(my_list, 3) '1
26+
Print binary_search(my_list, -1) 'null
27+
End
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
binary_search
2+
01_binary_search
3+
0
4+
0
5+
0.0.1
6+
7+
1.15 KB
Binary file not shown.

Diff for: 02_selection_sort/gambas/01_selection_sort/.project

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Gambas Project File 3.0
2+
Title=01_selection_sort
3+
Startup=MMain
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
' Gambas module file
2+
3+
Function findSmallest(arr As Float[]) As Integer
4+
Dim smallest As Float
5+
Dim i, smallest_index As Integer
6+
smallest_index = 0
7+
smallest = arr[0]
8+
For i = 1 To arr.Length - 1
9+
If arr[i] < smallest Then
10+
smallest = arr[i]
11+
smallest_index = i
12+
Endif
13+
Next
14+
Return smallest_index
15+
16+
End
17+
Function selectionSort(arr As Float[]) As Float[]
18+
Dim newArr As New Float[]
19+
Dim smallest, i As Integer
20+
Dim element As Float
21+
For i = 0 To arr.Length - 1
22+
smallest = findSmallest(arr)
23+
newArr.Push(arr[smallest])
24+
arr.Remove(smallest)
25+
Next
26+
27+
Return newArr
28+
29+
End
30+
Public Sub Main()
31+
Dim r As New Float[]
32+
Dim element As Float
33+
Dim i As Integer
34+
r = [5, 3, 6, 2, 10]
35+
r = selectionSort(r)
36+
37+
For Each element In r
38+
Print element
39+
Next
40+
End

Diff for: 02_selection_sort/gambas/01_selection_sort/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
MMain
2+
01_selection_sort
3+
0
4+
0
5+
6+
7+

Diff for: 03_recursion/gambas/01_countdown/.gambas/COUNTDOWN

580 Bytes
Binary file not shown.

Diff for: 03_recursion/gambas/01_countdown/.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=01_countdown
4+
Startup=countdown
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
' Gambas module file
2+
3+
Function countdown(i As Integer) As Integer
4+
Print i
5+
If i <= 0 Then
6+
Return
7+
Else
8+
countdown(i - 1)
9+
Endif
10+
11+
End
12+
13+
Public Sub Main()
14+
countdown(5)
15+
End

Diff for: 03_recursion/gambas/01_countdown/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
countdown
2+
01_countdown
3+
0
4+
0
5+
0.0.1
6+
7+

Diff for: 03_recursion/gambas/02_greet/.gambas/GREET

940 Bytes
Binary file not shown.

Diff for: 03_recursion/gambas/02_greet/.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=02_greet
4+
Startup=greet
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1

Diff for: 03_recursion/gambas/02_greet/.src/greet.module

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
' Gambas module file
2+
3+
Function greet2(name As String)
4+
Print "how are you, "; name; "?"
5+
End
6+
Function bye()
7+
Print "ok bye!"
8+
End
9+
Function greet(name As String)
10+
Print "hello, "; name; "!"
11+
greet2(name)
12+
Print "getting ready to say bye..."
13+
bye()
14+
End
15+
Public Sub Main()
16+
greet("adit")
17+
End

Diff for: 03_recursion/gambas/02_greet/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
greet
2+
02_greet
3+
0
4+
0
5+
0.0.1
6+
7+

Diff for: 03_recursion/gambas/03_factorial/.gambas/FACTORIAL

580 Bytes
Binary file not shown.

Diff for: 03_recursion/gambas/03_factorial/.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=03_factorial
4+
Startup=factorial
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
' Gambas module file
2+
3+
Function fact(x As Float) As Float
4+
If x = 1 Then
5+
Return 1
6+
Else
7+
Return x * fact(x - 1)
8+
Endif
9+
10+
End
11+
12+
Public Sub Main()
13+
Print fact(5)
14+
End

Diff for: 03_recursion/gambas/03_factorial/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
factorial
2+
03_factorial
3+
0
4+
0
5+
0.0.1
6+
7+

Diff for: 04_quicksort/gambas/01_loop_sum/.gambas/LOOP_SUM

676 Bytes
Binary file not shown.

Diff for: 04_quicksort/gambas/01_loop_sum/.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=01_loop_sum
4+
Startup=loop_sum
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1

Diff for: 04_quicksort/gambas/01_loop_sum/.src/loop_sum.module

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
' Gambas module file
2+
3+
Function sum(arr As Integer[]) As Integer
4+
Dim total, x As Integer
5+
total = 0
6+
For x = 0 To arr.Length - 1
7+
total += arr[x]
8+
Next
9+
Return total
10+
11+
End
12+
13+
Public Sub Main()
14+
Print sum([1, 2, 3, 4, 5])
15+
End

Diff for: 04_quicksort/gambas/01_loop_sum/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
loop_sum
2+
01_loop_sum
3+
0
4+
0
5+
0.0.1
6+
7+
652 Bytes
Binary file not shown.

Diff for: 04_quicksort/gambas/02_recursive_count/.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=02_recursive_count
4+
Startup=recursive_count
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
' Gambas module file
2+
3+
Function count(list As Float[]) As Float
4+
If list.Length = 0 Then
5+
Return 0
6+
Endif
7+
Return 1 + count(list.Copy(1, list.Length - 1))
8+
9+
End
10+
11+
Public Sub Main()
12+
Print count([0, 1, 2, 3, 4, 5]) '6
13+
End

Diff for: 04_quicksort/gambas/02_recursive_count/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
recursive_count
2+
02_recursive_count
3+
0
4+
0
5+
0.0.1
6+
7+
644 Bytes
Binary file not shown.

Diff for: 04_quicksort/gambas/02_recursive_sum/.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=02_recursive_sum
4+
Startup=recursive_sum
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
' Gambas module file
2+
3+
Function sum(list As Float[]) As Float
4+
If list.Length = 0 Then
5+
Return 0
6+
Endif
7+
Return list[0] + sum(list.Copy(1, list.Length - 1))
8+
9+
End
10+
11+
Public Sub Main()
12+
Print sum([1, 2, 3, 4])
13+
End

Diff for: 04_quicksort/gambas/02_recursive_sum/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
recursive_sum
2+
02_recursive_sum
3+
0
4+
0
5+
0.0.1
6+
7+
764 Bytes
Binary file not shown.

Diff for: 04_quicksort/gambas/04_recursive_max/.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=04_recursive_max
4+
Startup=recursive_max
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
' Gambas module file
2+
3+
Function mymax(list As Float[]) As Float
4+
Dim sub_max As Float
5+
6+
If list.Length = 2 Then
7+
If list[0] > list[1] Then
8+
Return list[0]
9+
Else
10+
Return list[1]
11+
Endif
12+
Endif
13+
sub_max = mymax(list.Copy(1, list.Length - 1))
14+
If list[0] > sub_max Then
15+
Return list[0]
16+
Else
17+
Return sub_max
18+
Endif
19+
20+
End
21+
22+
Public Sub Main()
23+
Print mymax([1, 5, 10, 25, 16, 1]) '25
24+
25+
End

Diff for: 04_quicksort/gambas/04_recursive_max/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
recursive_max
2+
04_recursive_max
3+
0
4+
0
5+
0.0.1
6+
7+

Diff for: 04_quicksort/gambas/05_quicksort/.gambas/QUICKSORT

1.02 KB
Binary file not shown.

Diff for: 04_quicksort/gambas/05_quicksort/.lock

Whitespace-only changes.

Diff for: 04_quicksort/gambas/05_quicksort/.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gambas Project File 3.0
2+
# Compiled with Gambas 3.5.4
3+
Title=05_quicksort
4+
Startup=quicksort
5+
Version=0.0.1
6+
TabSize=2
7+
Packager=1
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
' Gambas module file
2+
3+
Function quicksort(myarray As Float[]) As Float[]
4+
Dim pivot As Float
5+
Dim i As Integer
6+
Dim less, greater, r As New Float[]
7+
If myarray.Length < 2 Then
8+
Return myarray
9+
Else
10+
pivot = myarray[0]
11+
For i = 1 To myarray.Length - 1
12+
If myarray[i] <= pivot Then
13+
less.add(myarray[i])
14+
Else
15+
greater.add(myarray[i])
16+
End If
17+
Next
18+
19+
Endif
20+
r.Insert(quicksort(less))
21+
r.Add(pivot)
22+
r.Insert(quicksort(greater))
23+
Return r
24+
End
25+
26+
27+
Public Sub Main()
28+
Dim myarray As Float[]
29+
Dim element As Float
30+
Dim x As Integer
31+
32+
myarray = [10, 5, 2, 3]
33+
myarray = quicksort(myarray)
34+
35+
For Each element In myarray
36+
Print element
37+
Next
38+
39+
End

Diff for: 04_quicksort/gambas/05_quicksort/.startup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
quicksort
2+
05_quicksort
3+
0
4+
0
5+
0.0.1
6+
7+

0 commit comments

Comments
 (0)