PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: jeevanjyot
Testers: mexomerf, rivalq
Editorialist: iceknight1093
DIFFICULTY:
TBD
PREREQUISITES:
None
PROBLEM:
An integer is said to be lucky if it contains the digit 7.
Given a positive integer X, is it lucky?
EXPLANATION:
All that needs to be done is to check, for each digit of X, whether it equals 7.
Extracting all the digits of an integer can be done in a couple of different ways:
- The simplest way is to read X as a string instead of an integer. Then, you can simply iterate over its characters and check whether each one equals
'7'
or not. - Alternately, run the following algorithm:
- If X = 0, stop.
- Otherwise, let
d = X%10
.d
is the last digit of X, check whether it equals 7 or not. - Next, divide X by 10. This essentially removes the last digit of X.
- Run the above three steps in a loop; hence extracting every digit of X.
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Code (Python) (String)
for _ in range(int(input())):
x = input()
print('Yes' if '7' in x else 'No')
Code (Python) (Loop)
for _ in range(int(input())):
x = int(input())
ans = 'No'
while x > 0:
d = x%10
if d == 7: ans = 'Yes'
x //= 10
print(ans)