Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion exercise1/problem1/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package main

func addUp() {}
func addUp(num int) int {
if num == 0 || num == 1 {
return num
}
total := 0
for i := num; i > 0; i-- {
total += i
}
return total
}
47 changes: 46 additions & 1 deletion exercise1/problem10/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
package main

func sum() {}
import "errors"

func main() {
// fmt.Println("Hello, 世界")
// sum("10", "20") // "30", nil
sum("10", "20")
}

func sum(str1 string, str2 string) (string, error) {
if !isInt(str1) || !isInt(str2) {
return "", errors.New("string: a cannot be converted")
}
num1 := atoi(str1)
num2 := atoi(str2)
result := itoa(num1 + num2)
return result, nil
}

func atoi(s string) int {
base := 1
num := 0
for i := len(s) - 1; i >= 0; i-- {
num += int(s[i]-'0') * base
base *= 10
}
return num
}

func itoa(n int) string {
num := ""
for n > 0 {
num = string(rune(n%10+'0')) + num
n /= 10
}

return num
}

func isInt(s string) bool {
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return true
}
20 changes: 19 additions & 1 deletion exercise1/problem2/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
package main

func binary() {}
import "fmt"

func main() {
//{666, "1010011010"},
fmt.Println(binary(0))
}

func binary(num int) string {
if num == 0 {
return "0"
}
bin := ""
for num > 0 {
remainder := num % 2
bin = string(rune(remainder)+'0') + bin
num = num / 2
}
return bin
}
18 changes: 17 additions & 1 deletion exercise1/problem3/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
package main

func numberSquares() {}
import "fmt"

func main() {
fmt.Println(numberSquares(2))
}

func numberSquares(n int) int {
// 1^2 + 2^2 + 3^2 + … + N^2
if n == 0 {
return 0
}
var total = 0
for i := 1; i <= n; i++ {
total += i * i
}
return total
}
16 changes: 15 additions & 1 deletion exercise1/problem4/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package main

func detectWord() {}
import "fmt"

func main() {
fmt.Println(detectWord("UcUNFYGaFYFYGtNUH"))
}

func detectWord(s string) string {
letter := ""
for _, c := range s {
if c >= 'a' && c <= 'z' {
letter += string(c)
}
}
return letter
}
29 changes: 28 additions & 1 deletion exercise1/problem5/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
package main

func potatoes() {}
import "fmt"

func main() {
fmt.Println(potatoes("potatoapple"))
}

func potatoes(s string) int {
target := "potato"
count := 0
targetLen := len(target)

for i := 0; i <= len(s)-targetLen; {
match := true
for j := 0; j < targetLen; j++ {
if s[i+j] != target[j] {
match = false
break
}
}
if match {
count++
i += targetLen
} else {
i++
}
}
return count
}
55 changes: 54 additions & 1 deletion exercise1/problem6/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
package main

func emojify() {}
import "fmt"

func main() {
fmt.Println(emojify("smile, grin, sad, mad"))
}

func emojify(s string) string {
words := splitIntoWords(s)
fmt.Println(words)
text := ""

for _, word := range words {
text += getEmoji(word)
}
return text

}
func getEmoji(word string) string {
switch word {
case "smile":
return "🙂"
case "grin":
return "😀"
case "sad":
return "😥"
case "mad":
return "😠"
default:
return word
}
}
func isSpace(char byte) bool {
return char == ' ' || char == '\t' || char == '\n' || char == '\r' || char == '(' || char == ')' || char == ','
}

func splitIntoWords(text string) []string {
var words []string
tempText := ""

for i := 0; i < len(text); i++ {
if i == len(text)-1 && !isSpace(text[i]) {
tempText += string(text[i])
words = append(words, tempText)
} else if !isSpace(text[i]) {
tempText += string(text[i])
} else {
words = append(words, tempText)
words = append(words, string(text[i]))
tempText = ""
}
}

return words
}
24 changes: 23 additions & 1 deletion exercise1/problem7/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
package main

func highestDigit() {}
import "fmt"

func main() {
fmt.Println(highestDigit(379))
}

func highestDigit(n int) int {
var digits []int
var highest int
for n > 0 {
digit := n % 10
n = n / 10
digits = append(digits, digit)
}
fmt.Println(digits)

for _, num := range digits {
if num > highest {
highest = num
}
}
return highest
}
13 changes: 12 additions & 1 deletion exercise1/problem8/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package main

func countVowels() {}
func countVowels(s string) int {
var vowels = []rune{'a', 'e', 'i', 'o', 'u'}
count := 0
for _, c := range s {
for _, vowel := range vowels {
if vowel == c {
count++
}
}
}
return count
}
12 changes: 9 additions & 3 deletions exercise1/problem9/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

func bitwiseAND() {}
func bitwiseAND(n1 int, n2 int) int {
return n1 & n2
}

func bitwiseOR() {}
func bitwiseOR(n1 int, n2 int) int {
return n1 | n2
}

func bitwiseXOR() {}
func bitwiseXOR(n1 int, n2 int) int {
return n1 ^ n2
}
8 changes: 7 additions & 1 deletion exercise2/problem1/problem1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
package problem1

func isChangeEnough() {
func isChangeEnough(arr [4]int, amount float32) bool {
quarters := 25 * arr[0]
dimes := 10 * arr[1]
nickels := 5 * arr[2]
pennies := 1 * arr[3]
sum := (float32)(quarters + dimes + nickels + pennies)
return sum-amount*100 > -1
}
12 changes: 11 additions & 1 deletion exercise2/problem10/problem10.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
package problem10

func factory() {}
func factory() (map[string]int, func(string2 string) func(int)) {
brands := make(map[string]int)
makeBrand := func(brandName string) func(int) {
brands[brandName] = 0
return func(inc int) {
brands[brandName] = brands[brandName] + inc
}
}

return brands, makeBrand
}
13 changes: 12 additions & 1 deletion exercise2/problem11/problem11.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
package problem11

func removeDups() {}
func removeDups[T comparable](slice []T) []T {
list := make(map[T]struct{})
result := make([]T, 0)

for _, key := range slice {
if _, exists := list[key]; !exists {
list[key] = struct{}{}
result = append(result, key)
}
}
return result
}
26 changes: 25 additions & 1 deletion exercise2/problem12/problem12.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
package problem11

func keysAndValues() {}
func keysAndValues[T string | int, Y comparable](slice map[T]Y) ([]T, []Y) {
keys := make([]T, 0, len(slice))
values := make([]Y, 0, len(slice))

for key := range slice {
keys = append(keys, key)
}

// sort
for i := 0; i < len(keys); i++ {
for j := i + 1; j < len(keys); j++ {
if keys[i] > keys[j] {
temp := keys[j]
keys[j] = keys[i]
keys[i] = temp
}
}
}

for _, key := range keys {
values = append(values, slice[key])
}

return keys, values
}
34 changes: 33 additions & 1 deletion exercise2/problem2/problem2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
package problem2

func capitalize() {
func capitalize(arr []string) []string {
if len(arr) < 1 {
return arr
}
for i, el := range arr {
if len(el) == 0 {
continue
}
arr[i] = toCapital(el)
}

return arr
}

func toCapital(s string) string {
runes := []rune(s)
for i, char := range runes {
if i == 0 {
if char >= 'a' && char <= 'z' {
runes[i] = char - 32
continue
}
runes[i] = char
} else {
if char >= 'A' && char <= 'Z' {
runes[i] = char + 32
continue
}
runes[i] = char
}
}

return string(runes)
}
Loading