diff --git a/snippets/least-common-multiple/main.go b/snippets/least-common-multiple/main.go new file mode 100644 index 0000000..0739555 --- /dev/null +++ b/snippets/least-common-multiple/main.go @@ -0,0 +1,46 @@ +package main + +import "fmt" + +func main() { + fmt.Println(lcm([]int{2, 4, 8}), " -> expect 8") + fmt.Println(lcm([]int{25, 15, 18}), " -> expect 450") +} + +// Better solution to finding a least common multiple +func lcm(sNum []int) int { + ans := 1 + + // start iterating from the number 2 + for i := 2; ; { + // booleans to track if a number has been added to the ans (LCM) + // and if all of the values in the slice are 1, in which case we are done + changeMade, allOnes := false, true + // iterate over all elements in the sNum slice + for index, num := range sNum { + if num%i == 0 && !changeMade { + changeMade = true // update this boolean flag + ans *= i // increment answer + sNum[index] /= i // need to use this notation because num is pass by value + // do not increment i + } else if num%i == 0 { + // need to divide the element, but not increment ans (same prime factor as a previous element) + sNum[index] /= i + } + + // if any of the values in the slice are not one, flip the allOnes flag to false + // to indicate that we're not done with our outer loop + if num != 1 { + allOnes = false + } + } + + // if all values in sNum are 1, we're done & can return the answer + if allOnes { + return ans + } else if !changeMade { + // if a change was not made & not all values are one, increment i + i++ + } + } +} diff --git a/perms/main.go b/snippets/perms/main.go similarity index 100% rename from perms/main.go rename to snippets/perms/main.go diff --git a/snippets/read-file/input.txt b/snippets/read-file/input.txt new file mode 100644 index 0000000..7e72a58 --- /dev/null +++ b/snippets/read-file/input.txt @@ -0,0 +1,4 @@ + + + + diff --git a/snippets/read-file/main.go b/snippets/read-file/main.go new file mode 100644 index 0000000..4f49ed3 --- /dev/null +++ b/snippets/read-file/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "path/filepath" +) + +func main() { + stringSlice := readInputFile("./input.txt") + fmt.Println(stringSlice) +} + +// helper function to put the input file into a slice of strings +// (each elements is a line of the txt file) +func readInputFile(path string) []string { + // var pixelString string + resultSlice := make([]string, 0) + absPath, _ := filepath.Abs(path) + + file, err := os.Open(absPath) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + // pixelString = line + resultSlice = append(resultSlice, line) + } + + // return pixelString + return resultSlice +}