Question

In: Computer Science

Use Python Return a password string constructed with the following rules: (1) If there are only...

Use Python Return a password string constructed with the following rules:
(1) If there are only 0 or 1 numeric among 3 parameters, concatenate
the string form of all parameters directly.
(2) If there are 2 numeric parameters, calculate the sum of 2 numeric
values if they are both int, or calculate the product of them and round
to 2 decimal places if any of them is float. Finally, append this
sum/product to the string parameter.

You may assume that there will be at least 1 string parameter.

eg.>>> password_2("Marina", "Langlois", "20")
'MarinaLanglois20'
>>> password_2("Marina", "Langlois", 20)
'MarinaLanglois20'
>>> password_2("Elvy", True, 20)
'ElvyTrue20'
>>> password_2(True, 20, "Elvy")
'True20Elvy'
>>> password_2(20, 40, "Elvy")
'Elvy60'
>>> password_2(20, "Elvy", 3)
'Elvy23'
>>> password_2(2, 3.333, "Elvy")
'Elvy6.67'

Solutions

Expert Solution

def password_2(var1, var2, var3):
    countNumeric=0
    # count numeric
    if (isinstance(var1, int) or isinstance(var1, float)) and not(isinstance(var1, bool)):
        countNumeric=countNumeric+1
    if (isinstance(var2, int) or isinstance(var2, float)) and not(isinstance(var2, bool)):
        countNumeric=countNumeric+1
    if (isinstance(var3, int) or isinstance(var3, float)) and not(isinstance(var3, bool)):
        countNumeric=countNumeric+1

    if countNumeric<=1:
        return str(var1)+str(var2)+str(var3)
    
    # check if two are strings
    if isinstance(var1, str) and isinstance(var2, str):
        return var1+var2+str(var3)
    elif isinstance(var2, str) and isinstance(var3, str):
        return str(var1)+var2+var3
    elif isinstance(var1, str) and isinstance(var3, str):
        return var1+str(var2)+var3
    
    # check if two are integer
    if isinstance(var1, int) and isinstance(var2, int):
        return var3+str(var1+var2)
    elif isinstance(var1, int) and isinstance(var3, int):
           return var2+str(var1+var3)
    elif isinstance(var2, int) and isinstance(var3, int):
        return var1+str(var2+var3)

    # check if one is integer another one is float
    if (isinstance(var1, int) and isinstance(var2, float)) or (isinstance(var2, int) and isinstance(var1, float)):
        return var3+str(round(var1*var2,2))
    elif (isinstance(var1, int) and isinstance(var3, float)) or (isinstance(var3, int) and isinstance(var1, float)):
           return var2+str(round(var1*var3,2))
    elif (isinstance(var2, int) and isinstance(var3, float)) or (isinstance(var3, int) and isinstance(var2, float)):
        return var1+str(round(var2*var3,2))

     # check if two are float
    if (isinstance(var1, float) and isinstance(var2, float)):
        return var3+str(round(var1*var2,2))
    elif (isinstance(var1, float) and isinstance(var3, float)):
           return var2+str(round(var1*var3,2))
    elif (isinstance(var2, float) and isinstance(var3, float)):
        return var1+str(round(var2*var3,2))


#driver code
# call the function and display result
print(password_2("Marina", "Langlois", "20"))
print(password_2("Marina", "Langlois", 20))
print(password_2("Elvy", True, 20))
print(password_2(True, 20, "Elvy"))
print(password_2(20, 40, "Elvy"))
print(password_2(20, "Elvy", 3))
print(password_2(5.2, 3.333, "Elvy"))

___________________________________________________________________

MarinaLanglois20
MarinaLanglois20
ElvyTrue20
True20Elvy
Elvy60
Elvy23
Elvy6.67

___________________________________________________________________


Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


Related Solutions

Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
Python: If I am given a string consisting only of parentheses - (, ), {, },...
Python: If I am given a string consisting only of parentheses - (, ), {, }, [, and ], how would I write a Boolean function that takes a string and decides if it consists of valid nested parenthesis? One of the hints given was to use stack. For example --> {()[{}]}' is valid.
Python Coding 1. validate_username_password(username, password, users): This function checks if a given username and password matches...
Python Coding 1. validate_username_password(username, password, users): This function checks if a given username and password matches in the stored users dictionary. This function returns True if a match found otherwise returns False. 2. validate_existing_user(users): This function asks for username and password and checks if user provided name and password matches. It prints an informational message and returns username if it does find a match. Call validate_username_password() function to perform this validation. A user has total of three chances to validate....
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
(Use the String Class to Solve This (and only the iostream, string and cctype libraries)) Write...
(Use the String Class to Solve This (and only the iostream, string and cctype libraries)) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or...
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Write the following Python script: Problem Statement A string X is an anagram of string Y...
Write the following Python script: Problem Statement A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which...
PYTHON PLEASE: Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string,...
PYTHON PLEASE: Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string, default is ‘Normal’) self.current_hp (int, starts out equal to max_hp) self.max_hp (int, is given as input when the class instance is created, default is 20) self.exp (int, starts at 0, is increased by fighting) self.attacks (a dict of all known attacks) self.possible_attacks (a dictionary of all possible attacks) The dictionary of possible_attacks will map the name of an attack (the key) to how many...
Pig Latin is a language constructed by transforming English words. The following rules are used to...
Pig Latin is a language constructed by transforming English words. The following rules are used to translate English into Pig Latin: *If the word begins with a consonant, then all consonants at the beginning of the word, up to the first vowel are removed then added to the end of the word, followed by “ay”. For example, “computer” becomes “omputercay” and “think” becomes “inkthay”. *If the word begins with a vowel, then “way” is added to the end of the...
Python programming 1. Use 0 and 1 only to complete the function that outputs how many...
Python programming 1. Use 0 and 1 only to complete the function that outputs how many N-long sequence are in total. Condition 1) Starts with zero and ends with zero 2) Zero does not exist twice in a row. 3) 1 does not exist three times in a row. 4) N is a natural number. - A sequence that satisfies the conditions. ex) 010, 0110, 01010 - A sequence that does not meet the conditions. ex) 0100, 01110, 01100110 Need...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT