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.

Lee Venter
  • 53
  • 1
  • 3
  • 12
  • So... [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance)? – Kevin Sep 01 '15 at 14:34
  • Any code example that you can post so that we can get a clear picture of your problem? – giri-sh Sep 01 '15 at 14:39
  • i will add my code quickly – Lee Venter Sep 01 '15 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. – Kevin Sep 01 '15 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... – Lee Venter Sep 01 '15 at 15:04
  • Cheers Kevin, it was the Levenshtein distance, thanks! Thanks for all you guys who gave attention to my question – Lee Venter Sep 01 '15 at 21:51

1 Answers1

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

Community
  • 1
  • 1
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
  • 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. – Lee Venter Sep 01 '15 at 14:48
  • 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. – Lee Venter Sep 01 '15 at 21:50