diff --git a/src/easy/AbCheck.go b/src/easy/AbCheck.go new file mode 100644 index 0000000..ffd4965 --- /dev/null +++ b/src/easy/AbCheck.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "regexp" +) + +// ABCheck takes the str parameter and returns true if the characters a and b are separated by exactly 3 places anywhere in the string at least once +func ABCheck(str string) string { + re := regexp.MustCompile(`(a...b|b...a)`) + return fmt.Sprintf("%t", re.MatchString(str)) +} + +func main() { + result1 := ABCheck("lane borrowed") + fmt.Println(result1) + result2 := ABCheck("australia") + fmt.Println(result2) +} diff --git a/src/easy/AdditivePersistence.go b/src/easy/AdditivePersistence.go new file mode 100644 index 0000000..c8f2a84 --- /dev/null +++ b/src/easy/AdditivePersistence.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "strconv" +) + +// AdditivePersistence calculates the additive persistence of a positive integer +func AdditivePersistence(num int) int { + times := 0 + added := num + for added > 9 { + sum := 0 + digits := strconv.Itoa(added) + for _, digit := range digits { + digitInt, _ := strconv.Atoi(string(digit)) + sum += digitInt + } + added = sum + times++ + } + return times +} + +func main() { + result1 := AdditivePersistence(199) + fmt.Println(result1) + result2 := AdditivePersistence(913) + fmt.Println(result2) +} diff --git a/src/easy/AlphabetSoup.go b/src/easy/AlphabetSoup.go new file mode 100644 index 0000000..ffdfae9 --- /dev/null +++ b/src/easy/AlphabetSoup.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "sort" + "strings" +) + +// AlphabetSoup takes the str string parameter and returns the string with the letters in alphabetical order +func AlphabetSoup(str string) string { + letters := strings.Split(str, "") + sort.Strings(letters) + return strings.Join(letters, "") +} + +func main() { + result1 := AlphabetSoup("leftfield") + fmt.Println(result1) + result2 := AlphabetSoup("underworld") + fmt.Println(result2) +} diff --git a/src/easy/ArithGeo.go b/src/easy/ArithGeo.go new file mode 100644 index 0000000..c31e210 --- /dev/null +++ b/src/easy/ArithGeo.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" +) + +// ArithGeo takes the array of numbers stored in arr and returns "Arithmetic" if the sequence follows an arithmetic pattern, +// "Geometric" if it follows a geometric pattern, or -1 if neither pattern is found. +func ArithGeo(arr []int) string { + arithInterval := arr[1] - arr[0] + geoInterval := arr[1] / arr[0] + arithCount := 0 + geoCount := 0 + + for i := 0; i < len(arr)-1; i++ { + if arr[i+1]-arr[i] == arithInterval { + arithCount++ + } + if arr[i+1]/arr[i] == geoInterval { + geoCount++ + } + } + + if arithCount == len(arr)-1 { + return "Arithmetic" + } + + if geoCount == len(arr)-1 { + return "Geometric" + } + + return "-1" +} + +func main() { + result1 := ArithGeo([]int{2, 4, 6, 8}) + fmt.Println(result1) + result2 := ArithGeo([]int{2, 6, 18, 54}) + fmt.Println(result2) + result3 := ArithGeo([]int{-3, -4, -5, -6, -7}) + fmt.Println(result3) + result4 := ArithGeo([]int{3, 12, 48, 192, 768}) + fmt.Println(result4) +} diff --git a/src/easy/ArrayAdditionOne.go b/src/easy/ArrayAdditionOne.go new file mode 100644 index 0000000..2ad9963 --- /dev/null +++ b/src/easy/ArrayAdditionOne.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "sort" +) + +// ArrayAdditionOne checks if any combination of numbers in the array (excluding the largest number) can be added up to equal the largest number +func ArrayAdditionOne(arr []int) string { + // Find the largest number in the array + sort.Ints(arr) + largest := arr[len(arr)-1] + + // Recursive function to find the sum of combinations + var sumCombination func(int, int) bool + sumCombination = func(target, index int) bool { + // Base case: If target is 0, combination found + if target == 0 { + return true + } + // Base case: If index is out of range or target is negative, no combination found + if index < 0 || target < 0 { + return false + } + // Try including the current number in the sum + include := sumCombination(target-arr[index], index-1) + // Try excluding the current number from the sum + exclude := sumCombination(target, index-1) + // Return true if either including or excluding the current number results in a valid combination + return include || exclude + } + + // Start exploring combinations, excluding the largest number + return map[bool]string{true: "true", false: "false"}[sumCombination(largest, len(arr)-2)] +} + +func main() { + result1 := ArrayAdditionOne([]int{4, 6, 23, 10, 1, 3}) + fmt.Println(result1) + result2 := ArrayAdditionOne([]int{1,2,3, 5 ,7,8, 10}) + fmt.Println(result2) +} diff --git a/src/easy/BasicRomanNumerals.go b/src/easy/BasicRomanNumerals.go new file mode 100644 index 0000000..3de82ed --- /dev/null +++ b/src/easy/BasicRomanNumerals.go @@ -0,0 +1,45 @@ +package main + +import "fmt" + +// BasicRomanNumerals returns the decimal equivalent of the Roman numeral given +func BasicRomanNumerals(str string) int { + // Define a map to store the values of each Roman numeral + romanValues := map[byte]int{ + 'I': 1, + 'V': 5, + 'X': 10, + 'L': 50, + 'C': 100, + 'D': 500, + 'M': 1000, + } + + // Initialize the result + result := 0 + + // Iterate through the Roman numeral string + for i := 0; i < len(str); i++ { + // Get the value of the current Roman numeral + value := romanValues[str[i]] + + // If the current numeral is smaller than the next numeral, subtract its value + if i < len(str)-1 && romanValues[str[i]] < romanValues[str[i+1]] { + result -= value + } else { + // Otherwise, add its value + result += value + } + } + + return result +} + +func main() { + // Test cases + result1 := BasicRomanNumerals("MDCXXI") + fmt.Println(result1) // Output: 24 + + result2 := BasicRomanNumerals("XLVI") + fmt.Println(result2) // Output: 46 +} diff --git a/src/easy/BinaryReversal.go b/src/easy/BinaryReversal.go new file mode 100644 index 0000000..09bc90d --- /dev/null +++ b/src/easy/BinaryReversal.go @@ -0,0 +1,35 @@ +package main + +// TO-DO: +// 1: Convert to binary +// 2: pad to N * 8 +// 3: Reverse the binary +// 4: Convert to decimal + +import ( + "fmt" + "strconv" +) + +func BinaryReversal(val int64) int64 { + binVal := strconv.FormatInt(val, 2) + + for(len(binVal)<8) { + binVal = string("0") + binVal + } + + result := "" + for _,v := range binVal { + result = string(v) + result + } + + strVal, _ := strconv.ParseInt(result, 2, 64) + fmt.Println(result) + return strVal +} + +func main(){ + // Test cases + result1 := BinaryReversal(47) + fmt.Println(result1) +} \ No newline at end of file diff --git a/src/easy/BitwiseOne.go b/src/easy/BitwiseOne.go new file mode 100644 index 0000000..76fa059 --- /dev/null +++ b/src/easy/BitwiseOne.go @@ -0,0 +1,28 @@ +package main + +import "fmt" + +func BitwiseOne(arr []string) string{ + s1 := arr[0] + s2 := arr[1] + + result := "" + + for i := 0; i < len(s1); i++ { + if s1[i] == s2[i] { + result += string(s1[i]) + }else{ + result += string("1") + } + } + + return result +} + +func main(){ + // Test cases + result1 := BitwiseOne([]string{"001110", "100000"}) + fmt.Println(result1) + result2 := BitwiseOne([]string{"110001", "111100"}) + fmt.Println(result2) +} \ No newline at end of file diff --git a/src/easy/BitwiseTwo.go b/src/easy/BitwiseTwo.go new file mode 100644 index 0000000..77883dd --- /dev/null +++ b/src/easy/BitwiseTwo.go @@ -0,0 +1,29 @@ +package main + +import "fmt" + +func BitwiseTwo(arr []string) string { + s1 := arr[0] + s2 := arr[1] + + result := "" + + for i := 0; i < len(s1); i++ { + if string(s1[i]) == "1" && string(s2[i]) == "1" { + result += string("1") + } else { + result += string("0") + } + } + + return result +} + +func main() { + + // Test cases + result1 := BitwiseTwo([]string{"010111", "101101"}) + fmt.Println(result1) + result2 := BitwiseTwo([]string{"110001", "111100"}) + fmt.Println(result2) +} diff --git a/src/easy/CamelCase.go b/src/easy/CamelCase.go new file mode 100644 index 0000000..545ff93 --- /dev/null +++ b/src/easy/CamelCase.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "strings" +) + +// CamelCase converts the input string to camel case format. +func CamelCase(str string) string { + words := strings.FieldsFunc(strings.ToLower(str), func(r rune) bool { + return !('a' <= r && r <= 'z') + }) + var camel string + for i, word := range words { + if i == 0 { + camel += word + } else { + camel += strings.Title(word) + } + } + return camel +} + +func main() { + result1 := CamelCase("_good_looking_blues_") + fmt.Println(result1) + + result2 := CamelCase("-=last-night-on-earth=-") + fmt.Println(result2) +} diff --git a/src/hard/Calculator.go b/src/hard/Calculator.go new file mode 100644 index 0000000..3dc9cd6 --- /dev/null +++ b/src/hard/Calculator.go @@ -0,0 +1,137 @@ +package main + +import ( + "fmt" + "strconv" +) + +func Calculator(str string) int { + tokens := tokenize(str) + result, _ := evaluate(tokens) + return result +} + +func tokenize(str string) []string { + var tokens []string + + for i := 0; i < len(str); i++ { + switch { + case str[i] == ' ': + // Skip spaces + continue + case isDigit(str[i]): + // If it's a digit, extract the entire number + start := i + for i < len(str) && isDigit(str[i]) { + i++ + } + tokens = append(tokens, str[start:i]) + i-- + default: + // If it's not a digit, add the operator or parenthesis as a token + tokens = append(tokens, string(str[i])) + } + } + + return tokens +} + +func isDigit(char byte) bool { + return char >= '0' && char <= '9' +} + +func evaluate(tokens []string) (int, []string) { + var stack []int + var operators []string + + for len(tokens) > 0 { + token := tokens[0] + tokens = tokens[1:] + + switch token { + case "(": + // Recursively evaluate the expression inside the parentheses + result, remainingTokens := evaluate(tokens) + stack = append(stack, result) + tokens = remainingTokens + case ")": + // Return the result and the remaining tokens + return stack[0], tokens + case "+", "-", "*", "/": + // Handle operators + operators = append(operators, token) + default: + // Handle numbers + num, _ := strconv.Atoi(token) + stack = append(stack, num) + } + + // Check if there are operators on the stack that can be applied + for len(operators) > 0 && len(stack) >= 2 && precedence(operators[len(operators)-1]) >= precedence(token) { + operator := operators[len(operators)-1] + operators = operators[:len(operators)-1] + + num2 := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + num1 := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + // Apply the operator to the top two numbers on the stack + result := applyOperator(num1, num2, operator) + stack = append(stack, result) + } + } + + // Evaluate any remaining operators on the stack + for len(operators) > 0 && len(stack) >= 2 { + operator := operators[len(operators)-1] + operators = operators[:len(operators)-1] + + num2 := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + num1 := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + result := applyOperator(num1, num2, operator) + stack = append(stack, result) + } + + // Return the final result and the remaining tokens + return stack[0], tokens +} + +func precedence(operator string) int { + switch operator { + case "+", "-": + return 1 + case "*", "/": + return 2 + } + return 0 +} + +func applyOperator(num1, num2 int, operator string) int { + switch operator { + case "+": + return num1 + num2 + case "-": + return num1 - num2 + case "*": + return num1 * num2 + case "/": + return num1 / num2 + default: + return 0 + } +} + +func main() { + // Example usage + result := Calculator("2+(3-1)*3") + fmt.Println(result) // Output: 8 + + result = Calculator("(2-0)(6/2)") + fmt.Println(result) // Output: 6 +} diff --git a/src/medium/Division.go b/src/medium/Division.go new file mode 100644 index 0000000..00c23c7 --- /dev/null +++ b/src/medium/Division.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +/** + * Division function. + * + * @param num1 input number 1 + * @param num2 input number 2 + * @return the GCF + */ +func division(num1, num2 int) int { + if num1 == 0 || num2 == 0 { + return num1 + num2 + } + + return division(num2, num1%num2) +} + +func main() { + result1 := division(20, 40) + fmt.Println(result1) + result2 := division(12, 16) + fmt.Println(result2) + result3 := division(21, 54) + fmt.Println(result3) +}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: