MyNixOS website logo
Description

Implementation of the 'Verhoeff' Check Digit Algorithm.

An implementation of the 'Verhoeff' algorithm for calculating check digits (Verhoeff, J. (1969) <doi:10.1002/zamm.19710510323>). Functions are provided to calculate a check digit given an input number, calculate and append a check digit to an input number, and validate that a check digit is correct given an input number.

verhoeff

This package is my take on implementing the Verhoeff algorithim for calculating check digits. My influence for the algorithims implementation was primarily from the wikipedia outline (https://en.wikipedia.org/wiki/Verhoeff_algorithm), and the paper by A. Dunn (Computer program for the calculation and validation of Verhoeff check digits, https://www.ccamlr.org/en/wg-fsa-sam-05/11)

Installation

You can install verhoeff from CRAN with:

install.packages("verhoeff")

Or grab the development version from github with:

devtools::install_github("condwanaland/verhoeff")

Example

To calculate the Verhoeff Check Digit for a given number, use verhoeff_calculate

library(verhoeff)
verhoeff::verhoeff_calculate(123)
#> [1] 3

To calculate for many numbers, just pass a vector.

verhoeff_calculate(c(1234, 5678, 9, 10))
#> [1] 0 8 1 9

If you would prefer the output to be in a list, with one check digit per element, use the as_list parameter.

verhoeff_calculate(c(1234, 5678, 9, 10), as_list = TRUE)
#> [[1]]
#> [1] 0
#> 
#> [[2]]
#> [1] 8
#> 
#> [[3]]
#> [1] 1
#> 
#> [[4]]
#> [1] 9

The output of verhoeff_calculate is designed so it can be a new column in a dataframe (i.e., as the output of a dplyr::mutate call)

suppressPackageStartupMessages(library(dplyr))

# Make a random dataframe that has integer columns
mtcars$name <- rownames(mtcars)
mtcars %>% 
  select(name, gear) %>% 
  mutate(check_digit = verhoeff_calculate(gear))
#>                   name gear check_digit
#> 1            Mazda RX4    4           3
#> 2        Mazda RX4 Wag    4           3
#> 3           Datsun 710    4           3
#> 4       Hornet 4 Drive    3           6
#> 5    Hornet Sportabout    3           6
#> 6              Valiant    3           6
#> 7           Duster 360    3           6
#> 8            Merc 240D    4           3
#> 9             Merc 230    4           3
#> 10            Merc 280    4           3
#> 11           Merc 280C    4           3
#> 12          Merc 450SE    3           6
#> 13          Merc 450SL    3           6
#> 14         Merc 450SLC    3           6
#> 15  Cadillac Fleetwood    3           6
#> 16 Lincoln Continental    3           6
#> 17   Chrysler Imperial    3           6
#> 18            Fiat 128    4           3
#> 19         Honda Civic    4           3
#> 20      Toyota Corolla    4           3
#> 21       Toyota Corona    3           6
#> 22    Dodge Challenger    3           6
#> 23         AMC Javelin    3           6
#> 24          Camaro Z28    3           6
#> 25    Pontiac Firebird    3           6
#> 26           Fiat X1-9    4           3
#> 27       Porsche 914-2    5           8
#> 28        Lotus Europa    5           8
#> 29      Ford Pantera L    5           8
#> 30        Ferrari Dino    5           8
#> 31       Maserati Bora    5           8
#> 32          Volvo 142E    4           3

In addition to verhoeff_calculate, verhoeff offers verhoeff_append and verhoeff_validate.

Use verhoeff_append to append a check digit to a provided number:

verhoeff_append(123)
#> [1] "123-3"
verhoeff_append(c(123, 5, 0))
#> [1] "123-3" "5-8"   "0-4"

Use verhoeff_validate to check whether a check digit is correct for the provided number

verhoeff_validate(number = 123, check_digit = 3)
#> [1] TRUE
verhoeff_validate(number = 123, check_digit = 4)
#> [1] FALSE

Other implementations

See https://github.com/fascinatingfingers/CheckDigit for another implementation of check digits that considers more than just the Verhoeff algorithim.

Metadata

Version

0.4.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD 13
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd13
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd13
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows