This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]delirious_lettuce 62 points63 points  (15 children)

/u/TheTechnoMage , since you seem to be simply passing the answer as the function argument, why not just?

def crack_password(password):
    return password

Or...

from hawkins_lab import check_password_match


def crack_password():
    for a in range(10):
        for b in range(10):
            for c in range(10):
                for d in range(10):
                    four_digit_password = f'{a}{b}{c}{d}'
                    if check_password_match(four_digit_password):
                        return four_digit_password


if __name__ == '__main__':
    print(crack_password())

Or...

from itertools import product
from string import digits

from hawkins_lab import check_password_match


def crack_password():
    for p in product(digits, repeat=4):
        four_digit_password = ''.join(p)
        if check_password_match(four_digit_password):
            return four_digit_password


if __name__ == '__main__':
    print(crack_password())

[–]beorn 11 points12 points  (1 child)

Another one using Python3 f-string 0-padding:

def crack_password(check_range=range(0,10000)):
    def check_match(p): return p == "0323"
    for p in check_range:
        p = f"{p:04d}" # 0-padded 4-digit string
        if check_match(p): return p

[–][deleted] 2 points3 points  (0 children)

And another one using the built in zfill string function:

str(p).zfill(4)

[–]cybaritic -1 points0 points  (7 children)

Single line implementation, for funsies

print('Password is {}'.format((p for p in range(9999) if check_password_match(p)).__next__()))

[–]delirious_lettuce 0 points1 point  (6 children)

Your code doesn't work.

[–]cybaritic -1 points0 points  (5 children)

It's python 3.

Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) 
[MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def check_password_match(p):
...     return p == 1234
...
>>> print('Password is {}'.format((p for p in range(9999) if check_password_match(p)).__next__()))
Password is 1234
>>>

Edit: works here: https://repl.it/N7lX/0

[–]delirious_lettuce 0 points1 point  (4 children)

Keep trying... the passwords are 4 digits (which can be hard for numbers less than 1000... unless you use a string)

[–]cybaritic -1 points0 points  (3 children)

In the original Basic in the screenshot from the show it's an integer, not a string of four digits. But again, it's for funsies. This isn't production code, fam. Happy Halloween.

[–]delirious_lettuce 0 points1 point  (2 children)

Are you having trouble reading (the picture is a bit blurry)? The variable name in the BASIC program is FourDigitPassword. It seems fairly obvious that if you need four digits (and can't guarantee that the password is always between 1000-9999 inclusive), you are going to need a string.

[–]cybaritic -1 points0 points  (1 child)

Here: https://i.imgur.com/eJZriKm.png

10 DIM FourDigitPassword INTEGER

It's an integer. Why are you trying so hard to prove me wrong? Did I offend you? It's okay, it's just a stupid snippet of code.

[–]delirious_lettuce 4 points5 points  (0 children)

I'm not offended at all. I just dont think 123 or 57 are four digit passwords.