Skip to content

Commit

Permalink
Solve SSN 1 in python
Browse files Browse the repository at this point in the history
  • Loading branch information
deniscostadsc committed Aug 20, 2024
1 parent caf2992 commit da3fa02
Show file tree
Hide file tree
Showing 6 changed files with 20,106 additions and 0 deletions.
18 changes: 18 additions & 0 deletions solutions/beecrowd/1769/1769.py
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')
54 changes: 54 additions & 0 deletions solutions/beecrowd/1769/generate_in.sh
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

Loading

0 comments on commit da3fa02

Please sign in to comment.