Question

In: Computer Science

def check(s): #Conditions applied 1,5,6 if len(s)=9 and s[0].isupper() and s[-1].isdigit(): upper_count = sum(1 for c...

def check(s):
     #Conditions applied 1,5,6
     if len(s)=9 and s[0].isupper() and s[-1].isdigit():
         upper_count = sum(1 for c in s if c.isupper())
         lower_count = sum(1 for c in s if c.islower())
         number_count = sum(1 for c in s if c.isdigit())

         #Conditions 2,3,4
         if upper_count=3 and lower_count==3 and number_count==3:

             #Condition 7 { Two consecutive alphabets can’t be small }
             for i in range(1,len(s)):
                 if s[i-1].islower() and s[i].islower() :
                     return 'reject'
         else:
             return 'reject'
         #All conditions satisfies here, so accept
         return 'accept'

     else:
         return 'reject'


### TEST CASES ###
print(check('ABiG1h2f3')) #ALL correct


# BELOW ALL SHOULD BE REJECTED
print(check('AbiG1h2f3')) # bi consecutive two small letters

print(check('aBiG1h2f3')) # Starts with small letter

print(check('ABiG1h2fe')) # doesn't end with number

print(check('ABiG1h')) #Length is not 9

print(check('ABiG1h334')) # has 4 numbers. Also only 2 small letters

i need a definition of this python code?

Solutions

Expert Solution

Definition of each part of the provide code is explained in comments. Please go through and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Also fixed some small bugs (i.e. found two usages of = instead of == for value comparisons, fixed it)

'''
The below method accepts a string and returns either 'accept' or 'reject' based on the
value of the string. The method returns 'accept' only if the following conditions are met
> The length of string must be 9
> There should be three upper case, 3 lower case and 3 digits characters
> There should not be any consecutive small letters like 'ac' or 'xp' etc
> First character must be a capital letter
> Last character must be a digit
The method returns 'reject' if any of these case(s) is/are violated.
'''

def check(s):
    # checking if length is 9 and first character is upper case and last
    #character is a digit
   
if len(s)==9 and s[0].isupper() and s[-1].isdigit():
        #counting the number of upper, lower and numeric characters
        #(1 for c in s if c.isupper()) will return a set/list of 1's where
        #number of 1s equal to the number of upper case characters, sum() will
        #sum all these 1s, which will return the total number of upper case chars
       
upper_count = sum(1 for c in s if c.isupper())
        lower_count = sum(1 for c in s if c.islower())
        number_count = sum(1 for c in s if c.isdigit())

        # checking if number of upper, lower, numeric characters are exactly 3 each
       
if upper_count==3 and lower_count == 3 and number_count == 3:

            # checking if there exists a two consecutive occurrences of small letters
           
for i in range(1, len(s)):
                if s[i - 1].islower() and s[i].islower():
                    #characters at i-1 and i are lower case
                   
return 'reject'
       
else:
            #upper or lower or numeric count not equals 3
           
return 'reject'
       
# All conditions satisfies here, so accept
       
return 'accept'

   
else:
        #length is not 9 or first character is not upper case or last character
        #is not numeric
       
return 'reject'


### TEST CASES ###
print(check('ABiG1h2f3')) # ALL correct

# BELOW ALL SHOULD BE REJECTED

print(check('AbiG1h2f3')) # bi consecutive two small letters

print(check('aBiG1h2f3')) # Starts with small letter

print(check('ABiG1h2fe')) # doesn't end with number

print(check('ABiG1h')) # Length is not 9

print(check('ABiG1h334')) # has 4 numbers. Also only 2 small letters

#OUTPUT

accept

reject

reject

reject

reject

reject


Related Solutions

PYTHON CODE: def square_matrix_multiplication(matrix1,matrix2): C=[[0 for i in range(len(matrix1))] for i in range(len(matrix2))] for i in...
PYTHON CODE: def square_matrix_multiplication(matrix1,matrix2): C=[[0 for i in range(len(matrix1))] for i in range(len(matrix2))] for i in range(len(matrix1)): for j in range(len(matrix2[0])): C[i][j]=0 for k in range(len(matrix2)): C[i][j] += matrix1[i][k]*matrix2[k][j]    return C I use that function in my code. When I type like "return C", It does not work. If I type print(C), then my code works. Why is it happening? I created two user-entered matrices above the function, then I called the function.
The functions are tested in the following main(): def recSum1(somelist): if len(somelist) == 1: return somelist[0]...
The functions are tested in the following main(): def recSum1(somelist): if len(somelist) == 1: return somelist[0] else: a = recSum1(somelist[:len(somelist)//2]) b = recSum1(somelist[len(somelist)//2:]) return a + b def recSum2(somelist): if len(somelist) == 1: return somelist[0] else: return somelist[0] + recSum2(somelist[1:]) import random def main(): N = 100 myList = [random.randint(-500, 500) for i in range(N)] print(myList) print("The sum of the numbers is: " + str(sum(myList))) print("The sum of the numbers using recSum1 is: " + str(recSum1(myList))) print("The sum of the...
def myLength(mylist):    count = 0    for index in range(len(myList)):        print(myList[index])       ...
def myLength(mylist):    count = 0    for index in range(len(myList)):        print(myList[index])        count = count + 1    return count def myLength2(mylist):    count = 0    for element in myList:        print(element)        count = count + 1    return count       Use these two functions as examples to write your Python function below! Function 1: Write a function called myCount that takes in two parameters (a list and an item) like...
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current...
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current string has greater length than #max start and end end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('Teh average is',avg/(end-start)) s=input('Enter a string') longest(s) i need a definition and explanation of this code that how it works?
def anagramSolution1(s1,s2): alist = list(s2) pos1 = 0 stillOK = True while pos1 < len(s1) and...
def anagramSolution1(s1,s2): alist = list(s2) pos1 = 0 stillOK = True while pos1 < len(s1) and stillOK: pos2 = 0 found = False while pos2 < len(alist) and not found: if s1[pos1] == alist[pos2]: found = True else: pos2 = pos2 + 1 if found: alist[pos2] = None else: stillOK = False pos1 = pos1 + 1 return stillOK include all operations and calculate the Big-O and the values for c and n0.
EXPLAIN ANS PLEASE: PYTHON 1. s = 'abc' print(s[0:] + s[-2:2] + s[:1]) 2. def mirrorOnWheels(s):...
EXPLAIN ANS PLEASE: PYTHON 1. s = 'abc' print(s[0:] + s[-2:2] + s[:1]) 2. def mirrorOnWheels(s): prev = 0 for current in range(1, len(s) - 1): prev = current - 1 next = current + 1 if s[prev] == s[next]: break else: continue return 0 return prev s = 'Good decision!' print(mirrorOnWheels(s)) 3. mixture = {1:[1, 2, 0], 2:[2, 0, 1], 0:[0, 1, 2]} print(mixture[2][2]) 4. noOddHeroes = [] heroes = ['superman', 'batman', 'aquaman'] for hero in heroes: if len(hero)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
Solve using Laplace Transform ty'' + 2(t-1)y'-2y = 0 y(0)=0, y'(0)=0, Y(s) = C/(s^2+2s)^2 (C is...
Solve using Laplace Transform ty'' + 2(t-1)y'-2y = 0 y(0)=0, y'(0)=0, Y(s) = C/(s^2+2s)^2 (C is arbitrary)
0~9 is put in circle , proof that if we choose 3 number the total sum...
0~9 is put in circle , proof that if we choose 3 number the total sum is bigger than 14 use pigeonhole principle please explain in detail thanks Arrange 0-9 on the circular table There is a section where the sum of three adjacent numbers must be 14 or more
Type or paste question here ax+by+c=0.ax+by+c=0. Let (s′,t′)(s′,t′) be the reflection of the point (s,t)(s,t) in...
Type or paste question here ax+by+c=0.ax+by+c=0. Let (s′,t′)(s′,t′) be the reflection of the point (s,t)(s,t) in ℓℓ. Find a formula that computes the coordinates of (s′,t′)(s′,t′) if one knows the numbers s,t,a,bs,t,a,b and cc. Your formula should depend on the variables s,t,a,bs,t,a,b and cc. It should work for arbitrary values of s,t,a,bs,t,a,b and cc as long as (a,b)≠(0,0)(a,b)≠(0,0). Its output should be a point.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT