Python 3.8: require 2nd letter as uppercase value in password - python-3.x

I have to ask user to enter password which meets specific requirements like:
minimum 8 long;
contain at least on number;
to have at least one capital letter;
First three requirements I have solved but I'm unable to find solution for:
second letter has to be capital;
What should be in algorithm that checks if correct value is entered?
I guess string.upper() wont help me in this case because I need to ask user for correct input value not converting existing one.
Kind regards,
J.

You can use the .isupper() function to check if a character is uppercase.
string = "aBcdef"
if string[1].isupper():
# second letter is uppercase
For your case, I would be careful since you are also asking for a number in the password. You have to make sure the second character is not a number when you check it for uppercase because it will return false.

Related

Check if string contains consecutive repeated substring

I got an interview problem which asks to determine whether or not a given string contains substring repeated right after it. For example:
ATAYTAYUV contains TAY after TAY
AABCD contains A after A
ABCAB contains two AB, but they are not consecutive, so the answer is negative
My idea was to look at the first letter, find its second occurrence then check letter by letter if the letters after the first occurrence match the letters after the second occurrence. If they all do, the answer is positive. If not, once I get a mismatch, I can repeat the process but starting with the last letter I checked, since I would not be able to get a repeated sequence up to that point.
I am not sure if the approach is correct or if it is the mos efficient.
Assume that you are looking for a repeating pattern of length 3. If you write the string shifted right by three positions in front of itself (and trimmed), you can detect runs of 3 identical characters.
ATAYTAYUV
ATAYTA
Repeat this for all lengths up to N/2.

Excel formula on cell for valid password

I'm creating a template for importing users in bulk to the system, one of the columns requires to input password,
I would like to create a condition on the [password] cell, in order to indicate to the person that input the details that the password is valid
those are the conditions:
Password must contain at least
6 characters
1 symbol
1 number
1 letter
This is what I tried : MEDIAN(6) AND (OR),EXACT(LOWER()))) but no luck.
all the symbols are valid but the values of the characters must be in English
is it possible?
A password-validator immediately screamed 'regular-expressions'. Will you want to go down the path of VBA, you'd require the following pattern:
^(?=.*?[!#$])(?=.*?[A-Za-z])(?=.*?\d).{6,}$
See an online demo
To mimic this in relative easy native functionality you could go with xpath-expressions:
=NOT(ISERROR(FILTERXML("<t><s>"&LOWER(A1)&"</s></t>","//s[translate(.,'abcdefghijklmnopqrstuvwxyz','')!=.][translate(.,'0123456789','')!=.][translate(.,'!#$','')!=.][string-length()>=6]")))
translate(.,'abcdefghijklmnopqrstuvwxyz','')!=.] would mimic the positive lookahead for any letter;
[translate(.,'0123456789','')!=.] would mimic the positive lookahead for any number;
[translate(.,'!#$','')!=.] would mimic the positive lookahead for any symbol (as given in the character class);
[string-length()>=6] would mimic the 6+ characters needed for valid input.
Well, I have done this in the few minutes between posting the comment and this answer, you can expand it to include whatever other conditions you want:
and(len(A1)>=6,MAX(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9},A1,1),"")))
is the formula used in the data validation. Youi can see that A1 meets the length >=6 but I have not controlled that the count of numbers, hiowever you can add that.

Excel- Turn MARIA ANDERS to M.ANDERS as well as 99 other names, only if the first name is > 3 letters

I am trying to output a customer name using vlookup. The names are UPPERCASE but I need to limit them if the first name is longer than 3 characters, I need to just use the first letter of the first name. Here is the problem:
If the First name is longer than three characters you should just use the first character
of the first name and put a dot after that. For example if the customer name is “Steve
Johnson” the system should show “S. JOHNSON” or for “Ana Johnson” the system
should show “ANA JOHNSON”.
I should be able to do this without VB. Maybe an IF statement? Like if the first name is > 3 letters, take the first letter in the string?
Use Find to locate the space, and If test the position. Then either return the string as is, or manipulate the string to suit your needs. Wrap the whole thing in Upper to get upper case
=UPPER(IF(FIND(" ",A1)<=4,A1,LEFT(A1,1)&"."&MID(A1,FIND(" ",A1),999)))

Compare two strings and save the difference as an integer in Python

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.
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

String manipulation: randomly swapping out the first letter with one of the other letters of the word

I need to randomly swap out the first letter of a word with one of the other letters. What i am having trouble is with specifying that i only need to randomly generate ONE character. I cant use any conditionals, so can anyone please recommend a method to use?

Resources