Question

In: Computer Science

Write a function called 'make_triangle(char,n)' that uses a nested loop to return a string that forms...

Write a function called 'make_triangle(char,n)' that uses a nested loop to return a string that forms a triangle. The return string should contain: one instance of char on the first line, two instances of char on the second line, and so on up to n instances of char on line n. Then n-1 instances of char on line n+1, and so on until the triangle is complete. Sample runs of the function looks as follows (this function called from IDLE, please note the end of line character at the end of the return string):

>>> make_triangle("+",6)
'+\n++\n+++\n++++\n+++++\n++++++\n+++++\n++++\n+++\n++\n+\n'

>>> make_triangle('*',3)
'*\n**\n***\n**\n*\n'

From IDLE, we could as print out the actual triangle as follows:

>>> print(make_triangle("+",6))
+
++
+++
++++
+++++
++++++
+++++
++++
+++
++
+

>>> print(make_triangle('*',3))
*
**
***
**
*

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================


def make_triangle(symbol, n):
    triangle = ''
    for i in range(1, n):
        triangle += symbol * i + "\n"
    for i in range(n, 0, -1):
        triangle += symbol * i + "\n"
    return triangle


print(make_triangle("+",6))
print(make_triangle("*",3))

=============================================================


Related Solutions

Write a function that uses a local char queue and a local char stack to determine...
Write a function that uses a local char queue and a local char stack to determine if its string parameter is a palindrome. Your solution will look like: #include <stack> #include <queue> ... bool isPalindrome(const string& candidate) { stack<char> s; queue<char> q; //add only upper case letters and digits to the stack and the queue //if a letter is not upper case then convert it to upper case   } Note: I need help to write out this problem in C++...
(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
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
Write a program that uses a for loop output a string in reverse printing it character...
Write a program that uses a for loop output a string in reverse printing it character by character. C++ Program Example: "Enter some text:" this is a test "your text in reverse is:" tset a si siht Please note that you can find the length of the string by using the length function: string str = "hello" cout << str.length(); Also you can et a character in a string by providing an index to the function string str = "hello"...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
IN JAVA Create a program that uses a nested for loop to display a rectangle of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below). Get the number of rows and columns from the user, and display the result. Examples: If the user provided rows=4, and columns=7, the program should display a pattern as follows: # # # # # # # # #...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your code should use pointer arithmetic (it may increment and decrement pointers, but it may not use array indexing). Here is a piece of code that shows the behavior of reverse: char buf[100]; strcpy(buf, “hello”); reverse(buf); printf(“%s\n”, buf); // output should be olleh
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array...
(C LANGUAGE) Write a function called upperLower that inputs a line of text into char array s[100]. Output the line in uppercase letters and in lowercase letters This is what I have so far: void * upperLower (const char * s) { char *word[sizeof(s)]; for(int i = 0; i < sizeof(s); i++){ *word[i] = s[i]; } word = strtok(word, " "); int counter = 0; while(word != NULL){ if(counter % 2 == 0){ word[counter] = toupper(word); printf("%s ", word); }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT