0

If I have a password, "rusty", and I input the sequence: "rusty123", "Rusty" and "rush" (which in turn is saved to a list newList), when I print out newList, how do I display a result that says:

rusty123, wrong by 3 characters
Rusty, wrong by 1 characters
rush, wrong by 2 characters

?

What I need to add is a function like (countDifference) that allows me to compare the right password 'rusty' with wrong passwords entered. So if I enter 'rusty123', it should compare 'rusty' to 'rusty123' and save the result as a integer (3 - because the password is off by 3 characters i.e. 123 is wrong). I then convert this integer to a string and record it to the file newFile.

I think something that takes (password ='rusty') as a constant, and then reads every line of a new password input, and compares it so 'rusty' will do the trick, but I just don't know how.

password = "rusty"
user_input = raw_input("Please enter the password")

so the user inputs: "Rusty" and the function reads that the password is wrong by 1 character, namely "R" - (should have been lower case)

SOLVED: If you have the same problem, follow the link that @Chris Beck provides at the end of his explanation. It solved this problem perfectly.

asked Sep 1, 2015 at 14:23
6
  • So... Levenshtein distance? Commented Sep 1, 2015 at 14:34
  • Any code example that you can post so that we can get a clear picture of your problem? Commented Sep 1, 2015 at 14:39
  • i will add my code quickly Commented Sep 1, 2015 at 14:52
  • Out of curiosity, why do you want to do this? Having a password system tell me "wrong password, but you're getting warmer" seems like it would be easy for malicious users to crack. Commented Sep 1, 2015 at 14:56
  • Its for an exercise I need to do. I'm preparing for a upcoming test, and this will help a lot. It's not meant for a password, the password is just an example I use in this program, I want to be able to use it to determine by how many characters a entry is off from a right answer... Commented Sep 1, 2015 at 15:04

1 Answer 1

2

Is there a function that can help me determine by how many characters (in integers) the wrong password (entered as a string) was from the right password?

So, this could mean a few different things. There's a few different notions of "by how many characters does this string differ from that string" that people use. Some of them are easier to program than others, if you are new then you might not want to use the most sophisticated versions.

"Distance" from string A to string B

Simplest:

At how many indices i does A[i] != B[i]? (If one string is longer than the other then count all those indices as mismatching also.)

That's the easiest one to implement. However it doesn't always give the most intuitive results. If I have

A = "abracadabra"
B = "abrarcadabra"

the distance of these strings is going to be 8, even though they are only off "by one letter".

Harder: Edit Distance

Under the edit distance, A and B above would have distance 1. Edit distance means, how many insertions / deletions would have to be performed to change A into B. Under some variations, a swap of two adjacent characters is also thought to count as distance only 1.

The usual way to compute edit distance is using dynamic programming. You can see examples of this here: Edit Distance in Python

answered Sep 1, 2015 at 14:36

2 Comments

thanks I will do so! What I need: I need to compare wrong passwords to the right one and say by how many characters they got the password wrong eg. user enters 'noob123' where it should be 'noob'. the program needs to determine that character 0-4 is correct but character 5-7 is wrong, so wrong by 3 characters.
Thank you very much Chris Beck! I opted to use link you provided "edit distance in python" and I use the levenshtein distance algorithm. I appreciate your time and great answer to my question.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.