Question

Using Python write a program that reads an int N >= 0, then prints out each...

Using Python write a program that reads an int N >= 0, then prints out each of the following patterns of *. If you do NOT use the string repetition operator * in either problem, you will earn 0.25 points of Extra Credit for each. Also, each pattern must be output via a single function call to the given named function with single parameter N. Examples for N==4 follow, with explanations of how the displayed diagram reflects this value of N:

(a) def triangle(N) => N lines, all right justified: first having N stars, second having N-1, ..., and finally the Nth having 1 star):


****
***
**

   *

(b) def hollow_box(N) => N lines, first and last having N stars, others with star in first and Nth column:


****
* *
* *
****

0 0
Add a comment Improve this question Transcribed image text
Answer #1
SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.


def triangle(N):
    # Outer loop
    for i in range(N):
        # Inner loop
        for j in range(i, N):
            # print a *
            print('*', end='')
        # print new line
        print()


def hollow_box(N):
    for i in range(N):
        # first and last rows, print full
        if i == 0 or i == N - 1:
            for j in range(N):
                print('*', end='')
        else:
            for j in range(N):
                # print * only if it's first or last column
                if j == 0 or j == N - 1:
                    print('*', end='')
                else:
                    # else, print space
                    print(' ', end='')
        # new line
        print()


# TESTING
print('TRIANGLE TESTING...')
print('N=4')
triangle(4)
print('\nN=7')
triangle(7)

print('\n\nHALLOW BOX TESTING...')
print('N=4')
hollow_box(4)
print('\nN=8')
hollow_box(8)

========

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Using Python write a program that reads an int N >= 0, then prints out each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python Write a program that reads an int N >= 0, then prints out each...

    Using Python Write a program that reads an int N >= 0, then prints out each of the following patterns of *. Here, you may use the * repetition operator, if you wish. Also, each pattern must be output via a single function call to the given named function with single parameter N. Examples for N==4 follow, with explanations of how the displayed diagram reflects this value of N: ) Square with number diagonals, with outer side length of N,...

  • [EC1-1] (patterns3.py) More fun with * patterns... Each of these is worth 0.25 points, except for...

    [EC1-1] (patterns3.py) More fun with * patterns... Each of these is worth 0.25 points, except for (f) and (g), which are worth 1 point each. Write a program that reads an int N >= 0, then prints out each of the following patterns of *. Here, you may use the * repetition operator, if you wish. Also, each pattern must be output via a single function call to the given named function with single parameter N. Examples for N==4 follow,...

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Using Python Please Question 1: Pine Tree Write a program that, prints a 'pine tree' consisting...

    Using Python Please Question 1: Pine Tree Write a program that, prints a 'pine tree' consisting of triangles of increasing sizes, filled with a character (' oror '$' etc). Your program should consist of three functions: 1. A function print_shifted_triangle (n, m, symbol). It prints an n-line triangle, filled with symbol characters, shifted m spaces from the left margin. For example, if we call print_shifted_triangle (3, 4, +"), the expected output is: +++ Left margin +++++ 4 spaces 2. A...

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • Write a program that reads a list of integers in the range (0 – 49) and prints out the numbers that does not repeat twice

    IN PYTHONWrite a program that reads a list of integers in the range (0 – 49) and prints out the numbers that does not repeat twice.The first input of the program is the number of integers contained in this list, followed by the integers contained in the list. Print the numbers in in ascending order.Case 1:INPUT: 5 2 2 3 3 1OUTPUT:1Case 2:INPUT:6 1 2 1 2 2 2OUTPUT:2Case 3:INPUT:4 3 2 1 2OUTPUT: 1 3343 2 1 213

  • In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and...

    In Python 5. Write a function named listComprehensionDivisors that has 3 integer inputs, N, n1, and n2. Using a single list comprehension to make a list of all of the numbers in the range 1..N that are either divisible by n1 or n2, where n1 and n2 can be any positive integers. After creating the list, your code should print out the following 2 lines: “We are printing all numbers from 1 to that are divisible by or ” “These...

  • (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the...

    (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the four basic arithmetic operations, i.e., addition, subtraction, multiplication and integer division. A sample partial output of the math quiz program is shown below. The user can select the type of math operations that he/she would like to proceed with. Once a choice (i.e., menu option index) is entered, the program generates a question and asks the user for an answer. A sample partial output...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT