-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
caf2992
commit da3fa02
Showing
6 changed files
with
20,106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import sys | ||
|
||
for line in sys.stdin: | ||
cpf_digits = list( | ||
map(int, filter(lambda x: x not in ['.', '-'], line.strip())) | ||
) | ||
|
||
b1, b2 = 0, 0 | ||
for index, cpf_digit in enumerate(cpf_digits[:9]): | ||
b1 += cpf_digit * (index + 1) | ||
b2 += cpf_digit * (9 - index) | ||
b1 = 0 if b1 % 11 == 10 else b1 % 11 | ||
b2 = 0 if b2 % 11 == 10 else b2 % 11 | ||
|
||
if b1 == cpf_digits[9] and b2 == cpf_digits[10]: | ||
print('CPF valido') | ||
else: | ||
print('CPF invalido') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
TEST_CASES=10000 | ||
|
||
function print_valid_cpf { | ||
# https://pt.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas#C%C3%A1lculo_do_d%C3%ADgito_verificador | ||
digits=() | ||
verifier_1=0 | ||
verifier_2=0 | ||
for index in $(seq 0 8); do | ||
digit=$((RANDOM % 10)) | ||
digits+=("${digit}") | ||
verifier_1=$((verifier_1 + (digit * (9 - (index % 10))))) | ||
verifier_2=$((verifier_2 + (digit * (9 - ((index + 1) % 10))))) | ||
done | ||
for index in $(seq 8 0); do | ||
echo -n "${digits[${index}]}" | ||
if [[ ${index} -eq 6 ]] || [[ ${index} -eq 3 ]]; then | ||
echo -n "." | ||
fi | ||
done | ||
|
||
echo -n "-" | ||
|
||
[[ $((verifier_1 % 11)) -eq 10 ]] && verifier_1=0 || verifier_1=$((verifier_1 % 11)) | ||
echo -n "${verifier_1}" | ||
|
||
verifier_2=$((verifier_2 + (verifier_1 * 9))) | ||
[[ $((verifier_2 % 11)) -eq 10 ]] && echo "0" || echo "$((verifier_2 % 11))" | ||
} | ||
|
||
function print_probably_invalid_cpf { | ||
for index in $(seq 11); do | ||
echo -n "$((RANDOM % 10))" | ||
if [[ ${index} -eq 3 ]] || [[ ${index} -eq 6 ]]; then | ||
echo -n "." | ||
fi | ||
if [[ ${index} -eq 9 ]]; then | ||
echo -n "-" | ||
fi | ||
done | ||
echo | ||
} | ||
|
||
for _ in $(seq "${TEST_CASES}"); do | ||
if [[ $((RANDOM % 2)) -eq 0 ]]; then | ||
print_valid_cpf | ||
else | ||
print_probably_invalid_cpf | ||
fi | ||
done | ||
|
Oops, something went wrong.