From e071e7e72fdd5ee5607b187a0df1499be73cdd2a Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 29 Jan 2024 06:05:45 +0000 Subject: [PATCH 01/13] AbCheck resolved --- src/easy/AbCheck.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/easy/AbCheck.go 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) +} From aff6badfe308f0a56d6123ce1e5f4b4f61a96a82 Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 29 Jan 2024 06:16:39 +0000 Subject: [PATCH 02/13] AdditivePersistence resolved --- src/easy/AdditivePersistence.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/easy/AdditivePersistence.go 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) +} From af56dcd85d1439cd6a07a6b836a356144a5ed282 Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 29 Jan 2024 06:26:07 +0000 Subject: [PATCH 03/13] AlphabetSoup resolved --- src/easy/AlphabetSoup.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/easy/AlphabetSoup.go 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) +} From cecdc5802a8b61f8aac8e4bbaf78caa48f97bc51 Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 29 Jan 2024 06:50:08 +0000 Subject: [PATCH 04/13] ArithGeo resolved --- src/easy/ArithGeo.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/easy/ArithGeo.go 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) +} From 942ce4f263c38061997ad846ddbcc41c53074f65 Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 29 Jan 2024 07:31:19 +0000 Subject: [PATCH 05/13] Array Addition 1 --- src/easy/ArrayAdditionOne.go | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/easy/ArrayAdditionOne.go 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) +} From 545dd70aa1d56d49a95856945675d39181c7a989 Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 29 Jan 2024 15:45:34 +0000 Subject: [PATCH 06/13] Basic Roman Numerals resolve --- src/easy/BasicRomanNumerals.go | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/easy/BasicRomanNumerals.go 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 +} From e216742da665275afd03805176b2085fc97275da Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 29 Jan 2024 16:32:16 +0000 Subject: [PATCH 07/13] binary reversal resolved --- src/easy/BinaryReversal.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/easy/BinaryReversal.go 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 From 0ef2460f9824c60df9d2b4c468003c238a756baf Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Wed, 31 Jan 2024 13:59:33 +0000 Subject: [PATCH 08/13] BitwiseOne resolved --- src/easy/BitwiseOne.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/easy/BitwiseOne.go 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 From 860084ee38d9199713a6419c223dc6d14cfef026 Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Wed, 31 Jan 2024 19:12:19 +0000 Subject: [PATCH 09/13] BitwiseTwo resolved --- src/easy/BitwiseTwo.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/easy/BitwiseTwo.go 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) +} From 9118c40a30a50911dbd1ffa9502b6d8d61bf3e74 Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Wed, 31 Jan 2024 19:49:15 +0000 Subject: [PATCH 10/13] CamelCase resolved --- src/easy/CamelCase.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/easy/CamelCase.go diff --git a/src/easy/CamelCase.go b/src/easy/CamelCase.go new file mode 100644 index 0000000..9ae1753 --- /dev/null +++ b/src/easy/CamelCase.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "regexp" + "strings" +) + +// CamelCase converts the input string to camel case format. +func CamelCase(str string) string { + words := splitWords(str) + var camel strings.Builder + for i, word := range words { + if i == 0 { + camel.WriteString(word) + } else { + camel.WriteString(strings.ToUpper(word[:1]) + word[1:]) + } + } + return camel.String() +} + +// splitWords is a helper function which splits the input string into words. +func splitWords(str string) []string { + str = strings.ToLower(str) + re := regexp.MustCompile("[^a-z]+") + words := re.Split(str, -1) + var result []string + for _, word := range words { + if word != "" { + result = append(result, word) + } + } + return result +} + +func main() { + result1 := CamelCase("_good_looking_blues_") + fmt.Println(result1) + + result2 := CamelCase("-=last-night-on-earth=-") + fmt.Println(result2) +} From 6b076a448be1b5841a9c35d0b44b33bc609541cf Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Wed, 31 Jan 2024 19:51:42 +0000 Subject: [PATCH 11/13] refactor Camelcase --- src/easy/CamelCase.go | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/easy/CamelCase.go b/src/easy/CamelCase.go index 9ae1753..545ff93 100644 --- a/src/easy/CamelCase.go +++ b/src/easy/CamelCase.go @@ -2,36 +2,23 @@ package main import ( "fmt" - "regexp" "strings" ) // CamelCase converts the input string to camel case format. func CamelCase(str string) string { - words := splitWords(str) - var camel strings.Builder + 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.WriteString(word) + camel += word } else { - camel.WriteString(strings.ToUpper(word[:1]) + word[1:]) + camel += strings.Title(word) } } - return camel.String() -} - -// splitWords is a helper function which splits the input string into words. -func splitWords(str string) []string { - str = strings.ToLower(str) - re := regexp.MustCompile("[^a-z]+") - words := re.Split(str, -1) - var result []string - for _, word := range words { - if word != "" { - result = append(result, word) - } - } - return result + return camel } func main() { From fed102b86a591051918331575bc7443ca6f0987c Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 5 Feb 2024 14:20:06 +0000 Subject: [PATCH 12/13] calculator in go --- src/hard/Calculator.go | 137 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/hard/Calculator.go 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 +} From e53d45fff52636d518b6c88d595ce4554919251a Mon Sep 17 00:00:00 2001 From: "Ehsan A. Kian" Date: Mon, 5 Feb 2024 14:24:23 +0000 Subject: [PATCH 13/13] Division tested --- src/medium/Division.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/medium/Division.go 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) +}