Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Package luhn daxil edilən kodun Luhn alqortiminə uyğun olub olmadığını yoxlayır
- package luhn
- import (
- "fmt"
- "strconv"
- )
- // Valid: daxil edilən kodun Luhn alqortiminə uyğun olub olmadığını yoxlayır
- func Valid(number string) bool {
- var total float64
- var currentNumber rune
- var shouldDouble bool
- if len(number) < 2 {
- return false
- }
- for i := len(number) - 1; i >= 0; i-- {
- currentNumber = rune(number[i])
- if unicode.IsSpace(currentNumber) {
- continue
- }
- if !unicode.IsDigit(currentNumber) {
- return false
- }
- v, err := strconv.ParseFloat(string(number[i]), 64)
- if err != nil {
- fmt.Println(err)
- return false
- }
- if !shouldDouble {
- shouldDouble = true
- total += v
- } else {
- shouldDouble = false
- if v*2 > 9 {
- total += v*2 - 9
- } else {
- total += v * 2
- }
- }
- }
- if total == 0 && len(number) > 2 {
- return true
- }
- if total == 0 && len(number) <= 2 {
- return false
- }
- if ok := int(total) % 10; ok == 0 {
- return true
- }
- return false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement