Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.06 KB | None | 0 0
  1. // Package luhn daxil edilən kodun Luhn alqortiminə uyğun olub olmadığını yoxlayır
  2. package luhn
  3.  
  4. import (
  5.     "fmt"
  6.     "strconv"
  7. )
  8.  
  9. // Valid: daxil edilən kodun Luhn alqortiminə uyğun olub olmadığını yoxlayır
  10. func Valid(number string) bool {
  11.     var total float64
  12.     var currentNumber rune
  13.     var shouldDouble bool
  14.  
  15.     if len(number) < 2 {
  16.         return false
  17.     }
  18.  
  19.     for i := len(number) - 1; i >= 0; i-- {
  20.         currentNumber = rune(number[i])
  21.  
  22.         if unicode.IsSpace(currentNumber) {
  23.             continue
  24.         }
  25.  
  26.         if !unicode.IsDigit(currentNumber) {
  27.             return false
  28.         }
  29.        
  30.         v, err := strconv.ParseFloat(string(number[i]), 64)
  31.         if err != nil {
  32.             fmt.Println(err)
  33.             return false
  34.         }
  35.  
  36.         if !shouldDouble {
  37.             shouldDouble = true
  38.             total += v
  39.         } else {
  40.             shouldDouble = false
  41.  
  42.             if v*2 > 9 {
  43.                 total += v*2 - 9
  44.             } else {
  45.                 total += v * 2
  46.             }
  47.  
  48.         }
  49.  
  50.     }
  51.  
  52.    
  53.     if total == 0 && len(number) > 2 {
  54.         return true
  55.     }
  56.    
  57.     if total == 0 && len(number) <= 2 {
  58.         return false
  59.     }
  60.  
  61.     if ok := int(total) % 10; ok == 0 {
  62.         return true
  63.     }
  64.     return false
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement