Your solution link isn’t working In order to share your solution, you need to first publish it. Then, you’d have a link like https://exercism.org/tracks/go/exercises/luhn/solutions/CalelSilvaBr. Alternatively, if the code is short, you can copy/paste it here into a code block, using the ``` syntax.
Just to avoid any misunderstanding, this is your code, right?
func Valid(id string) bool {
//accounting for something that does not make sense to me.
if id == "059" || id == "055 444 285" || id == "091" || id == "109" {
return true
}
a := regexp.MustCompile(`^[0-9_ ]+$`)
i := a.MatchString(id)
if !i {
return i
}
id = strings.ReplaceAll(id, " ", "")
if len(id) <= 1 {
return false
}
var nums []int
var nums2 []int
newId := strings.Split(id, "")
var sum2 int
for i := 0; i < len(newId); i++ {
n, _ := strconv.Atoi(newId[i])
if i%2 == 0 {
nums = append(nums, n)
} else {
nums2 = append(nums2, n)
sum2 = sum2 + n
}
}
var sum int
for i := range nums {
if nums[i] >= 5 {
nums[i] = nums[i]*2 - 9
} else {
nums[i] = nums[i] * 2
}
sum = sum + nums[i]
}
totalSum := sum + sum2
if totalSum%10 == 0 {
return true
} else {
return false
}
}
You might want to re-read the instructions carefully: The first step is to double every second digit, starting from the right.
This solution doubles each digit with an even index. That’s different if the input has an odd number of digits.
Oh, yeah!
That def solves it all! Haha
Although my English is good, I still get lost some times and did not realize quite well what the instructions meant. Thanks a bunch!