Question

In: Computer Science

Copy-paste the given function header (with or without the docstring) into the answer window and then...

Copy-paste the given function header (with or without the docstring) into the answer window and then complete the body of the function. Then below, write the call expressions that match the given test cases, printing out the returned results of each call expression.

For full marks:

def centre_align(s: str) -> str:
    """ Returns a string that contains s "centred" and "highlighted"
        when it is displayed on a single line on a typical screen.
        Highlighting is to be done with the addition the
        SAME number of *'s before AND after the string itself. The number of
        stars should fill the line but not overfill it.
        (Look at the test cases to understand).
              
        The typical screen width is truly 80 characters wide
        but to make things easier on the exam, we will assume
        that the typical screen width is 20 characters wide.
      
        Assume that s is a valid string of length 1 to 20 characters
        You do not have to handle exceptional cases in this question.

        >>> centre_align('my title')
        '******my title******'
        >>> centre_align ('a')
        '*********a*********'
        >>> centre_align('012345678901234567890')
        '012345678901234567890'

  • Proper indentation and syntax are required. (The editor does not allow TABs. Use 4-spaces instead)
  • You may not use any conditional or loop statements

Solutions

Expert Solution

We get the length fo the string using the len() function. Then we get the total number of '*' to be padded by subtracting 20. We pad the string with stars and return the new string.

Code:



def centre_align(s):
    l = len(s)
    stars = 20 - l#total number of stars assuming the total length is 20
    #left and right stars are divided
    leftStar = int(stars/2)
    rightStar = int(stars/2)
    s = ('*' * leftStar) + s + ('*' * rightStar)

    return s


print(centre_align('my title'))
print(centre_align('a'))
print(centre_align('12345678901234567890'))



Code screenshot:

Output:


Related Solutions

Block copy, and paste, the argument into the window below, and do a proof to prove...
Block copy, and paste, the argument into the window below, and do a proof to prove that the argument is valid. 1. (p   ⊃ z)   •   (q • ~z) 2. (~p • q)    ⊃   s 3. m   v ~s     : .    m
Block copy, and paste, the argument into the window below, and do a proof to prove...
Block copy, and paste, the argument into the window below, and do a proof to prove that the argument is valid. 1. (p   • q)   •   (q ⊃   x) 2. ~x v ~s 3. n   v   s     : .    t ⊃ n
Block copy, and paste, the argument into the window below, and do a proof to prove...
Block copy, and paste, the argument into the window below, and do a proof to prove that the argument is valid. 1. (p   ⊃   x) • (x ⊃ ~q) 2. p •   n 3.   t v q     : .     t
Block copy, and paste, the argument into the window below, and do a proof to prove...
Block copy, and paste, the argument into the window below, and do a proof to prove that the argument is valid. 1. (p   ⊃ z)   •   (q • ~z) 2. (~p • q)    ⊃   s 3. m   v ~s     : .    m
Block copy, and paste, the argument into the window below, and do a proof to prove...
Block copy, and paste, the argument into the window below, and do a proof to prove that the argument is valid. This question is worth 25 points.    1. (p   ⊃   x) • (x ⊃ ~q) 2. p •   n 3.   t v q     : .     t
Block copy, and paste, the argument into the window below, and do a proof to prove...
Block copy, and paste, the argument into the window below, and do a proof to prove that the argument is valid. This question is worth 25 points. 1. (p   • q)   •   (q ⊃   x) 2. ~x v ~s 3. n   v   s     : .    t ⊃ n
Looking for an original answer. Not a copy and paste: What are the major reasons that...
Looking for an original answer. Not a copy and paste: What are the major reasons that motivate networking? Consider some of the following: What are the real/hidden costs of networks? How have networks changed the way we do business? Are some business sectors more affected than others?
Write in your own words without copy and paste from the net about " describe the...
Write in your own words without copy and paste from the net about " describe the differences between mitosis and meiosis " total words ( 600 - 700 ) in detail the short essay must contain 1- Introduction 2- Body 3- Conclusion Note: please do not copy and paste from the net
Write in your own words without copy and paste from the net about " describe the...
Write in your own words without copy and paste from the net about " describe the differences between mitosis and meiosis" total words ( 700-800 ) In detail please the short essay must contain 1- Introduction 2- Body 3- Conclusion Note: please do not copy and paste from the net
Classroom with Window (X) Classroom with Window (Y) Classroom without Window (X) Classroom without Window (Y)...
Classroom with Window (X) Classroom with Window (Y) Classroom without Window (X) Classroom without Window (Y) 80 105 80 95 75 100 85 100 85 105 90 105 70 100 85 100 90 110 95 105 Malani wants to determine whether children whose preschool classroom has a window differ in their receptive vocabulary as compared to children whose classroom does not have a window. At the beginning of the school year, Malani randomly assigns 10 children at Rainbow Preschool to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT