Sha256: e8cfff69e01cbe34a64432af42343aa5577d1c382ed286cdd2f8f6f092b0352b

Contents?: true

Size: 583 Bytes

Versions: 95

Compression:

Stored size: 583 Bytes

Contents

#!/bin/bash

num=$(echo "$1" | sed 's/[\ ]//g')

if ! [[ 10#$num -ge 0 ]] 2>/dev/null || ! [[ ${#num} -gt 1 ]] ; then
    echo "false"
else
    shift 1

    len=${#num}
    is_odd=1
    sum=0

    for((t = len - 1; t >= 0; --t)) {
        digit=${num:$t:1}

        if [[ $is_odd -eq 1 ]]; then
            sum=$(( sum + $digit ))
        else
            sum=$(( $sum + ( $digit != 9 ? ( ( 2 * $digit ) % 9 ) : 9 ) ))
        fi

        is_odd=$(( ! $is_odd ))
    }

    if [[ 0 -eq $(( 0 != ( $sum % 10 ) )) ]] ; then
        echo "true"
    else
        echo "false"
    fi

fi

Version data entries

95 entries across 95 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.97 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.96 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.95 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.94 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.93 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.92 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.91 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.90 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.89 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.88 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.87 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.86 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.85 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.84 tracks/bash/exercises/luhn/example.sh