Questions about Luhn

Hey, guys! Glad to see the amazing upgrades on Exercism.
I have a question about an specific exercise in Go Track, which is luhn.

My solution:

As you can see, I have “cheated” on line 10, because my code is failing 4 tests.

The code seems good for me, so I see two possibilities here:

I’m missing something in the instructions (althogh I have read them thoroughly) or there is something wrong in the instructions.

Your solution link isn’t working :slight_smile: 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.

The solution they meant to share indeed can be found here.

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.

1 Like

That’s what I get for not using copy-paste.

1 Like

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!