A shared tag with AI prompts and code snippets
From workspace: FreeCodeCamp
Team: Main
Total snippets: 19
19 snippets
Replace all vowels in words using nested list comprehension and join.
sentence = "code with python" vowels = 'aeiou' masked = [''.join(['*' if ch in vowels else ch for ch in word]) for word in sentence.split()] print(masked) # ['c*d*', 'w*th', 'pyth*n']
Reverse characters in each word using slicing inside list comprehension.
sentence = "list comprehension python" reversed_words = [word[::-1] for word in sentence.split()] print(reversed_words) # ['tsil', 'noisneherpmoc', 'nohtyp']
Return only words longer than 4 characters using list comprehension.
sentence = "this is a list comprehension demo" filtered = [word for word in sentence.split() if len(word) > 4] print(filtered) # ['list', 'comprehension']
Flip case of each word using .swapcase() in list comprehension.
sentence = "HeLLo WoRLD" toggled = [word.swapcase() for word in sentence.split()] print(toggled) # ['hEllO', 'wOrld']
Convert each word to title case using list comprehension.
sentence = "hello world from python" titled = [word.title() for word in sentence.split()] print(titled) # ['Hello', 'World', 'From', 'Python']
Use list comprehension to convert all words in a sentence to uppercase.
sentence = "hello world from python" uppercased = [word.upper() for word in sentence.split()] print(uppercased) # ['HELLO', 'WORLD', 'FROM', 'PYTHON']
Sort expenses in descending order using lambda as the sorting key.
expenses = [10, 250, 75, 19] sorted_expenses = sorted(expenses, key=lambda x: -x) print(sorted_expenses) # Output: [250, 75, 19, 10]
Use lambda with map and conditional expression to tag each expense.
expenses = [120.50, 32.00, 5.75, 50.00] labels = list(map(lambda x: "High" if x > 50 else "Low", expenses)) print(labels) # Output: ['High', 'Low', 'Low', 'Low']
Calculate the total cost using reduce and a lambda expression.
from functools import reduce expenses = [120.50, 32.00, 5.75, 50.00] total = reduce(lambda x, y: x + y, expenses) print(total) # Output: 208.25
Add 21% tax to each expense using a lambda inside a map function.
expenses = [100, 40, 10] with_tax = list(map(lambda x: round(x * 1.21, 2), expenses)) print(with_tax) # Output: [121.0, 48.4, 12.1]
Use a lambda function to filter all expenses greater than a threshold (e.g. 50).
expenses = [120.50, 32.00, 5.75, 50.00, 89.99] high = list(filter(lambda x: x > 50, expenses)) print(high) # Output: [120.5, 89.99]
A commented version of the Luhn validator to explain how numbers and strings are processed.
def luhn_verbose(card_number): digits = [int(d) for d in str(card_number)][::-1] # reverse digits for i in range(1, len(digits), 2): # double every second digit doubled = digits[i] * 2 digits[i] = doubled if doubled < 10...
Use the Luhn algorithm to generate test card numbers for simulations.
import random def generate_card(): base = [random.randint(0, 9) for _ in range(15)] digits = base[::-1] for i in range(1, len(digits), 2): doubled = digits[i] * 2 digits[i] = doubled if doubled < 10 else doubled - 9 ...
Write a helper function to format card numbers using string manipulation.
def format_card(number): s = str(number).replace(" ", "") return "-".join([s[i:i+4] for i in range(0, len(s), 4)])
Extend the Luhn logic to generate a valid check digit for any number.
def luhn_generate(number): digits = [int(d) for d in str(number)] digits.append(0) for i in range(len(digits) - 2, -1, -2): doubled = digits[i] * 2 digits[i] = doubled if doubled < 10 else doubled - 9 checksum =...
Build a function that takes a number as input and checks if it passes the Luhn checksum validation.
def luhn_check(card_number): digits = [int(d) for d in str(card_number)][::-1] for i in range(1, len(digits), 2): doubled = digits[i] * 2 digits[i] = doubled if doubled < 10 else doubled - 9 return sum(digits) % 10 == 0
Group multiple assertions in a single test function.
def test_numbers(): assert 1 + 1 == 2 assert 2 * 2 == 4 assert 3 - 1 == 2
Follow Pytest’s naming convention for test discovery.
# Use files starting with 'test_' # Example: test_math.py
A minimal test function using Pytest with assert.
def test_addition(): assert 2 + 2 == 4