Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-python/6a0dcd03ee4e68698080ef6c.md
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

106 lines
2.2 KiB
Markdown

---
id: 6a0dcd03ee4e68698080ef6c
title: "Challenge 308: Credit Card Validator"
challengeType: 29
dashedName: challenge-308
---
# --description--
Given a string of digits for a credit card number, determine if it's a valid card number using the following method:
- Starting from the second-to-last digit, double every other digit moving left.
- If doubling a digit results in a number greater than 9, subtract 9.
- Sum all the digits (doubled and undoubled).
- If the total is divisible by 10, the number is valid.
# --hints--
`is_valid_card("4532015112830366")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_card("4532015112830366"), True)`)
}})
```
`is_valid_card("5425233430109903")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_card("5425233430109903"), True)`)
}})
```
`is_valid_card("371449635398431")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_card("371449635398431"), True)`)
}})
```
`is_valid_card("6011111111111117")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_card("6011111111111117"), True)`)
}})
```
`is_valid_card("4532015112830367")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_card("4532015112830367"), False)`)
}})
```
`is_valid_card("1234567890123456")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_card("1234567890123456"), False)`)
}})
```
`is_valid_card("4532015112830368")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_card("4532015112830368"), False)`)
}})
```
# --seed--
## --seed-contents--
```py
def is_valid_card(number):
return number
```
# --solutions--
```py
def is_valid_card(number):
total = 0
for i, digit in enumerate(reversed(number)):
d = int(digit)
if i % 2 == 1:
d *= 2
if d > 9:
d -= 9
total += d
return total % 10 == 0
```