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.180 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.179 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.178 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.177 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.176 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.175 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.174 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.173 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.172 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.171 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.170 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.169 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.167 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.166 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.165 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.164 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.163 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.162 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.161 tracks/bash/exercises/luhn/example.sh
trackler-2.2.1.160 tracks/bash/exercises/luhn/example.sh