Question

In: Computer Science

def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...

def annoying_valley(n):
if n == 0:
print()
elif n == 1:
print("*")
elif n == 2:
print("./")
print("*")
print(".\\")
elif n == 3:
print("../")
print("./")
print("*")
print(".\\")
print("..\\")
elif n == 4:
print(".../")
annoying_valley(3)
print("...\\")
elif n == 5:
print("..../")
annoying_valley(4)
print("....\\")
elif n == 6:
print("...../")
annoying_valley(5)
print(".....\\")
else:
print("." * (n - 1) + "/")
annoying_valley(n - 1)
print("." * (n - 1) + "\\")

def annoying_int_sequence(n):
if n == 0:
return []
elif n == 1:
return [1]
else:
a = annoying_int_sequence(n-1)
b = a + [n]
return b * (n-1) + a

Please write two descriptions like below for these two function

def some_function(some_param):

"""

A summary/description of what the function does.

Parameters: give a brief description for each parameter (if there are any)

Returns: give a brief description of each of the return values (if there are any)

Pre-condition: detail any assumptions made by the function, i.e., any pre-condition that must be met in order for the function to work correctly

Post-condition: detail what we can expect to be true after the function has finished execution, i.e., its post-condition

""

Solutions

Expert Solution

The annoying_valley(n) function prints a sloping pattern while the function annoying_int_sequence(n)  prints a recursive pattern.

The DocString is included with the code.

The necessary comments that will help you understand the solution.

def annoying_valley(n):
    """This Function prints a slope pattern
       It takes a input number (n) which is used for the no of slope lines
       It returns None
       The input number must be >= 0 to see the output
       The function first prints a decreasing /(forward slash) pattern and after that *(as a seperator) and it prints mirror image of the pattern above *
    """
    if n == 0:
        print()
    elif n == 1:
        print("*")
    elif n == 2:
        print("./")
        print("*")
        print(".\\")
    elif n == 3:
        print("../")
        print("./")
        print("*")
        print(".\\")
        print("..\\")
    elif n == 4:
        print(".../")
        annoying_valley(3)
        print("...\\")
    elif n == 5:
        print("..../")
        annoying_valley(4)
        print("....\\")
    elif n == 6:
        print("...../")
        annoying_valley(5)
        print(".....\\")
    else :
        print("." * (n - 1) + "/")
        annoying_valley(n - 1)
        print("." * (n - 1) + "\\")

def annoying_int_sequence(n):
    """This Function prints a recursive series whose base series is [1,2,1]
       It takes a input number (n) 
       It returns a List of integers
       The input number must be >= 0 to see the output
       The functions returns a list which will repeat till(n-1) times and after that [n] will be printed.
       Example:
       For(n=3) The final return will be [1,2,1] * (3-1) + [1,2,1]. Here [1,2,1] is variable a = [1,2,1]
    """
    if n == 0:
        return []
    elif n == 1:
        return [1]
    else :
        a = annoying_int_sequence(n - 1)
        b = a + [n]
    return b * (n - 1) + a

I hope you have understood the solution. If you like the solution kindly upvote it.


Related Solutions

def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2:...
def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2: return 2 if n == 3: return 6 if n == 4: return 4 * annoying_factorial(3) if n == 5: return 5 * annoying_factorial(4) if n == 6: return 6 * annoying_factorial(5) else: return n * annoying_factorial(n-1) def annoying_fibonacci(n): if n==0: return 0 if n==1: return 1 if n==2: return 1 if n==3: return 2 if n==4: return annoying_fibonacci(4-1)+annoying_fibonacci(4-2) if n==5: return annoying_fibonacci(5-1)+annoying_fibonacci(5-2) if...
import os def process_dir(p, indent): for item in os.listdir(path): if os.path.isfile("{0}/{1}".format(path, item)): print("\t"*indent, item) elif os.path.isdir("{0}/{1}".format(path,...
import os def process_dir(p, indent): for item in os.listdir(path): if os.path.isfile("{0}/{1}".format(path, item)): print("\t"*indent, item) elif os.path.isdir("{0}/{1}".format(path, item)): print("\t"*indent, "\x1b[1;31m{}\x1b[0;0m".format(item)) process_dir("{0}/{1}".format(path, item), indent+1) else: print("Not a file or directory") path=input("Enter a valid system path: ") print("\x1b[1;31m{}\x1b[0;0m".format(path)) process_dir(path, 1) 1. Figure out what the program is trying to do. Add meaningful comments that tell what the program is doing. There is a bug in this program that causes it to work incorrectly; find the bug, and fix it c. Put in some...
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n...
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n (int) starting value for 3n+1 sequence return: None """ while(n != 1): print(n) if(n % 2) == 0: # n is even n = n // 2 else: # n is odd n = n * 3 + 1 print(n) # the last print is 1 def main(): seq3np1(3) main() Using the provided code, alter the function as follows: First, delete the print statements...
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)...
Given the code segment: def f1(a, b = 2): if a : print(1) f1(1) what is...
Given the code segment: def f1(a, b = 2): if a : print(1) f1(1) what is the result of executing the code segment? a) Syntax error b) Runtime error c) No error d) Logic error in which scenario(s) is using a dictionary useful? a) To store the dates of lesson that each student is absent for so that warning letters may be issued to student exceeding a limit. b) To store the student numbers for students who sat for an...
X=5,Y=7,Z=10 A. if(X<Y): print "LESS" else: print "OTHER" B. if (x == 1): print "ONE" elif...
X=5,Y=7,Z=10 A. if(X<Y): print "LESS" else: print "OTHER" B. if (x == 1): print "ONE" elif (x == 2): print "TWO" elif (x == 3): print "THREE" elif (x == 4): print "FOUR" elif (x == 5): print "FIVE" elif (x == 6): print "SIX" else: print "OTHER" C. if (X<Z): print X X = X + 1 D. while (X<Z): print X X = X + 1 Q11. What is the final value of X in D Q12. Is...
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...
import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1...
import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1     print(var)     function2(var) def function2(myVar):     print(myVar)     var = myVar + 1     print(var)     function3(var) def function3(myVar):     print(myVar)     var = myVar + 1     print(var) def main(argv):     var = 10     print(var)     function1(var) if __name__=="__main__":     main(sys.argv) 1. As the program runs, what is the first line that will be interpreted by Python, and what action will...
Given a difference equation x[n+2] + 5x[n+1]+6x[n]=n with start values x[0]= 0 and x[1]=0
Given a difference equation x[n+2] + 5x[n+1]+6x[n]=n with start values x[0]= 0 and x[1]=0
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT