Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The pattern method accepts a single integer as a parameter. If that integer is l

ID: 3579892 • Letter: T

Question

The pattern method accepts a single integer as a parameter. If that integer is less than 1, the pattern method should return an error message "Argument must be greater than or equal to 1". Otherwise, the method should return a string containing a pattern of integers as demonstrated in the table below. There is a space between each integer pair and a newline ( ) after each integer greater than 5. The underlines and boldface are hints to help you see that pattern and are not included in the String created by your program. Because of the length of the string, do not test with values of n greater than 10. String that is returned (bold and underline only shown to help you recognize pattern) This produces a numeric pattern symmetric about n where each half before and after n is also symmetric around n-1. The newline after printing any integer larger than 5 provides a nice line length and also makes the pattern of the bigger numbers along the right edge reading down. If you get the normal pattern, that will just happen. Don't think about it. Output for an input of 7 is:

Explanation / Answer

def pattern(n):
    if n<1:
        print "Argument must be greater or equal to 1"
        return
    s = ''
    for i in range(1,n+1):
        s += str(i)
        if i>=5:
            s+=" "
        else:
            s+=" "
    for i in range(n-1, 0, -1):
        s += str(i)
        if i>=5:
            s+=" "
        else:
            s+=" "
    return s

print pattern(7)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote