From d91ae5f4d51f668710c1824fbe293b30d6936771 Mon Sep 17 00:00:00 2001 From: ryanmoehs Date: Tue, 14 Feb 2023 22:46:32 +0700 Subject: [PATCH] Assignment 1 --- Assignment/Assignment1/p1.go | 6 ++++-- Assignment/Assignment1/p2.go | 12 ++++++++++-- Assignment/Assignment1/p3.go | 8 +++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Assignment/Assignment1/p1.go b/Assignment/Assignment1/p1.go index ae5421f..3af05cb 100644 --- a/Assignment/Assignment1/p1.go +++ b/Assignment/Assignment1/p1.go @@ -24,6 +24,8 @@ Output: -1 func DivideWhole(a int, b int) int { // TODO: Your code here - return 0 + if b == 0 { + return -1 + } + return a / b } - diff --git a/Assignment/Assignment1/p2.go b/Assignment/Assignment1/p2.go index b0c0ea6..b3e5442 100644 --- a/Assignment/Assignment1/p2.go +++ b/Assignment/Assignment1/p2.go @@ -1,5 +1,9 @@ package Assignment1 +import ( + "regexp" +) + /* Clean up sentences. [WEIGHT = 1] @@ -32,6 +36,10 @@ Explanation: Replace the : and , and then remove all trailing/leading spaces func CleanUp(s string) string { // TODO: Your code here - return "" -} + var special *regexp.Regexp = regexp.MustCompile(`[^\w\s]+`) + s = special.ReplaceAllString(s, "") + // var space *regexp.Regexp = regexp.MustCompile(`[\t\n\f\r]`) + // s = space.ReplaceAllString(s, " ") + return s +} diff --git a/Assignment/Assignment1/p3.go b/Assignment/Assignment1/p3.go index 031362d..7106128 100644 --- a/Assignment/Assignment1/p3.go +++ b/Assignment/Assignment1/p3.go @@ -21,6 +21,12 @@ Explanation: All numbers are odd, so return 0 func EvenSum(nums []int) int { // TODO: Your code here + for _, num := range nums { + var even = 0 + if num%2 == 0 { + even += num + return even + } + } return 0 } -