Question

In: Computer Science

In Python 3.6 Question 1 for strings a) Write a function named longest_common_prefix that takes two...

In Python 3.6

Question 1 for strings

a) Write a function named longest_common_prefix that takes two strings and returns the longest common prefix of the two strings. For example, the longest common prefix of distance and disinfection is dis. If the two strings have no common longest common prefix, the method returns an empty string.

b) Write a function named reverse that takes a string argument and returns its reverse. For example, reverse(‘I am testing’) should return the string ‘gnitset ma I’. Use iteration for this and answers using slicing will not be accepted. Imma like... what.

c) Write a function named check_password that checks whether a string is a valid password. Return True if it is a valid password and False otherwise. The password rules are as follows:

• A password must have at least 8 characters.

• A password must consist of only letters and digits.

• A password must contain at least two digits.

Solutions

Expert Solution

Note : Both reverse() method works correctly. The only difference is one start traversing from string end and one from starting string.

CODE:

def longest_common_prefix(string1,string2):
    minimum = min(len(string1),len(string2))
    prefix = ''
    for i in range(minimum):
        if string1[i] == string2[i]:
            prefix += string1[i]
        else:
            break
    return prefix

print(longest_common_prefix('distance','disinfection'))
print(longest_common_prefix('distance','infection'))
print(longest_common_prefix('string1','string2'))

def reverse(string):
    rev = ''
    for i in range(len(string)-1,-1,-1):
        rev += string[i]
    return rev

def reverse(string):
    rev = ''
    for i in range(len(string)):
        rev = string[i] + rev
    return rev
print(reverse('I am testing!'))

def check_password(password):

    if len(password) < 8: # if length is less than 8
        return False
    else:
        c = 0 # counter for number of digits
        
        for i in password:
            if not(i.isdigit() or i.isalpha()): # If any of the character is neither number or alphabet
                return False
            
            if i.isdigit(): # Mainting count of numbers 
                c +=1
                
        if c<2:
            return False
        else:
            return True
        
print(check_password('maria1254'))     

OUTPUT:


Related Solutions

Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
please solve by utilizing python Define a function named checkOut which takes two JSON strings, each...
please solve by utilizing python Define a function named checkOut which takes two JSON strings, each representing a dictionary. The first has the following structure, { "customer" : name , "cart" : [ item1, item2, ... ] } The second has the following structure, { item1 : price1, item2 : price2, ... } The function must return a JSON string representing the following dictionary: { "customer" : name, "amount_due" : x } where x is the sum of the prices...
Write a Python program containing a function named scrabble_sort that will sort a list of strings...
Write a Python program containing a function named scrabble_sort that will sort a list of strings according to the length of the string, so that shortest strings appear at the front of the list. Words that have the same number of letters should be arranged in alphabetical order. Write your own logic for the sort function (you may want to start from some of the existing sorting code we studied). Do NOT use the built-in sort function provided by Python....
Question 12 PYTHON: Write a function named first_last that takes a single parameter, string_list (a list...
Question 12 PYTHON: Write a function named first_last that takes a single parameter, string_list (a list of strings). The function first_last should return a list of the strings in string_list that are not empty and that begin and end with the same letter. For example, the following would be correct input and output for the function first_last. response = ['to', 'that', 'I', 'say', '', 'hurrah'] print(first_last(response)) ['that', 'I', 'hurrah'] Question 13 (20 points) Write a function named number_luck. The function...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
Question: Write a method named reduce that: ● Takes a function of type (A, A) =>...
Question: Write a method named reduce that: ● Takes a function of type (A, A) => A ● Returns A ● Combines all the elements of the list into a single value by applying the provided function to all elements ○ You may assume the function is commutative ● If the list has size 1, return that element without calling the provided function Example: If head stores a reference to the List(4, 6, 2) head.reduce((a: Int, b: Int) => a...
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT