Junior
Describe an algorithm that takes numbers from 1 to 1000 and counts how many numbers contain the digit seven.
sobes.tech AI
Answer from AI
# Function to check if a number contains the digit 7
def contains_seven(n):
return '7' in str(n)
# Initialize counter
count = 0
# Loop through numbers from 1 to 1000
for i in range(1, 1001):
# If the number contains the digit 7, increment the counter
if contains_seven(i):
count += 1
# Result: total count of numbers containing the digit 7
Checking for the presence of the digit 7 is done by converting the number to a string and searching for the character '7' in that string.