-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdabble.go
More file actions
234 lines (194 loc) · 5.54 KB
/
dabble.go
File metadata and controls
234 lines (194 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
// author: Oliver Bonham-Carter
// mail: obonhamcarter@allegheny.edu
// date: 20 June 2023
// Version: 0.7.0
// comment: A basic shell to learn how a shell might work and to spend some quality time with Golang. Uh-huh
import (
"bufio"
"fmt"
"log"
"math"
"math/rand"
"os"
)
func printMenu() {
// prints the system commands
syscmds_map := make(map[string]string) // define a map (i.e., a dictionary)
syscmds_map["hello"] = ":Say hello"
syscmds_map["pwd"] = ":Show current path"
syscmds_map["exit"] = ":End program"
syscmds_map["help"] = ":Show the menu of commands"
syscmds_map["square"] = ":Draw a square"
syscmds_map["ffile"] = ":Create a file with a fortune in it"
syscmds_map["ran"] = ":Create n random numbers between min and max bounds"
syscmds_map["prime"] = ":Create n random prime numbers between min and max bounds"
fmt.Print(("\n [+] System commands:"))
for i := range syscmds_map {
print("\n\t [+=+] ", i, " ", syscmds_map[i])
}
fmt.Print("\n")
}
func main() {
fmt.Print("\t :: OBC's Dabble Terminal ::\n")
fmt.Print("\n [+] Enter a system command (help) :")
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
// fmt.Print("\n\t [+] type 'help' for list of commands ")
if input.Text() == "exit" {
break
}
getArguments(input.Text()) // determine what to do with this command
if input.Text() == "exit" {
fmt.Print(" [+] Exiting...\n")
break
}
if input.Text() == "help" {
printMenu()
}
if input.Text() == "square" {
drawSquare(10)
}
if input.Text() == "ffile" {
fortuneFile()
}
if input.Text() == "prime" {
getPrime()
}
fmt.Print("\n [+] Enter a system command (help) :")
}
fmt.Print("\n") // Drop a line on exit
}
func getPrime() {
fmt.Print("\t [+] Finding all prime numbers between two values")
fmt.Print("\n\t Enter a lower bounds :")
var min int
fmt.Scan(&min)
fmt.Print("\n\t Enter an upper bounds for a number :")
var max int
fmt.Scan(&max)
if min < 2 || max < 2 {
fmt.Println("\t Both Numbers must be greater than 2")
return
}
fmt.Print("\nPrimes")
if max <= min {
fmt.Print("\n\t [-] Error of bounds values")
return
}
for min <= max {
isPrime := true
for i := 2; i <= int(math.Sqrt(float64(min))); i++ {
if min%i == 0 {
isPrime = false
break
}
}
if isPrime {
fmt.Printf("\n%d ", min) //print out the primes on own line
}
min++
}
fmt.Println()
}
func fortuneFile() {
fmt.Print("\t [+] Enter the filename to create :")
var fname string
fmt.Scan((&fname))
var message = "You are special and unique just like everyone else in the herd."
// make a file
f, err := os.Create(fname + "_fortune.md")
if err != nil {
log.Fatal(err)
}
f.Write([]byte(message))
defer f.Close()
fmt.Println("\n\t [+] Saving file :", f.Name())
}
func getArguments(command string) {
if command == "hello" {
fmt.Print("\t Why, Hello to you too!\n")
}
if command == "details" {
fmt.Print("\n\t [+] Enter Your First Name: ")
var first string
fmt.Scan(&first)
fmt.Print("\n\t [+] Enter Last Name: ")
var last string
fmt.Scan(&last)
fmt.Println(" Nice to meet you:", first, last, "\n")
}
if command == "pwd" {
/// print out the current directory
getPath()
}
if command == "ran" {
getRandomNumber()
}
}
func getRandomNumber() {
/// ask user for min and max values; choose random numbers in this window
// var min int = 0 // Debugging Min value for random numbers
// var max int = 100 // Debugging Max value for random numbers
// var nNums int = 10 // Debugging number of random numbers to produce
fmt.Print("\t We will find n random numbers between lower a and upper bounds")
fmt.Print("\n\t Enter a lower bounds :")
var min int
fmt.Scan(&min)
fmt.Print("\n\t Enter an upper bounds for a number :")
var max int
fmt.Scan(&max)
/////////////////////////////////////////////////////////////
// The Intn() function of the rand package can be used to generate an integer in the interval of 0 and n. It takes only one argument, the n or the upper bound. It throws an error if the given argument is less than 0.
// v := rand.Int() // generates a random integer
// We can improve it so that we can define lower and upper bounds and the function will generate random within that specified range. Here is how to do that.
// v := rand.Intn(max-min) + min // range is min to max
// ref: https://golangdocs.com/generate-random-numbers-in-golang
/////////////////////////////////////////////////////////////
fmt.Print("\n\t Number of random numbers to create :")
var nNums int
fmt.Scan(&nNums)
number_slice := []int{} // define a slice (i.e., a list)
fmt.Print("\n\t Data")
for i := 0; i < nNums; i++ {
randomNum := rand.Intn(max-min) + min
// number_slice[i] = randomNum
number_slice = append(number_slice, randomNum)
fmt.Print("\n\t ", randomNum)
}
fmt.Print("\n")
// check the randomness of the numbers
for i := range number_slice {
// fmt.Print("\nTesting: ",number_slice[i])
findFrequency(number_slice, number_slice[i])
}
}
func findFrequency(arr []int, num int) {
count := 0
// fmt.Print("length of array: ",len(arr))
for _, item := range arr {
if item == num {
count++
}
}
var freq float64
freq = (float64(count)) / (float64(len(arr))) //convert the numbers to a floats
fmt.Printf("\n\t Frequency( %d ) = %f", num, freq)
}
func getPath() {
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
fmt.Println(path)
// return path
}
func drawSquare(count int) {
/// draw a square in X's
for i := 0; i < count; i++ {
for j := 0; j < count; j++ {
fmt.Print(" X")
}
fmt.Print("\n")
}
}