What input will be successfully validated against this program? import sys def v
ID: 3823210 • Letter: W
Question
What input will be successfully validated against this program?
import sys
def verify(guess):
vals = [
83,
75,
89,
45,
79,
82,
68,
83,
45,
55,
57,
50,
54,
]
for i, c in enumerate(guess):
if ord(c) != vals[i]:
return False
return True
if len(sys.argv) != 1:
print "Usage: python check.pyc"
exit(1)
guess = raw_input("Enter your guess for the flag: ");
if verify(guess):
print "That's the correct flag!"
else:
print "Wrong flag."
Explanation / Answer
input that will be successfully validated against this program is :
SKY-ORDS-7926
The vals array contains the ASCII values for the above characters.
vals = [
83, -> S
75, -> K
89, -> Y
45, -> -
79, -> O
82, -> R
68, -> D
83, -> S
45, -> -
55, -> 7
57, -> 9
50, -> 2
54, -> 6
]
If you enter S only or SKY or any of the first N characters in the same order in 'SKY-ORDS-7926', all of them will be acceptable.
The code enumerates on the input string and calls ord() on each character, which returns the ASCII value and compares the returned value with the i'th value in the vals array.
Eg. Input : SKY
i=0 | ord(S) returns 83 and vals[0] = 83 returns true!
i=1 | ord(K) returns 75 and vals[1] = 75 returns true!
i=2 | ord(Y) returns 89 and vals[2] = 89 returns true!
and finally "That's the correct flag!" gets printed as the output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.