Skip to content

Instantly share code, notes, and snippets.

@craftybones
Last active December 10, 2024 06:19
Show Gist options
  • Save craftybones/36f870ef15608e18e34ff1d34a306b53 to your computer and use it in GitHub Desktop.
Save craftybones/36f870ef15608e18e34ff1d34a306b53 to your computer and use it in GitHub Desktop.

Assignment: Diamond Pattern

Instructions

Your task is to extend the functionality of the generatePattern(style, dimensions) function to support a new pattern type: diamond.

For this assignment, the style parameter will be "diamond", and the dimensions parameter will be an array containing a single number [size] where:

  • size specifies the width and height of the diamond (both are the same).
  • Only odd numbers are valid for size:
    • If size is an even number, your function should generate a diamond for the closest odd number below it.
    • For example, if size = 4, the function should generate a diamond of size 3.

Pattern Description

  • The diamond is symmetrical, with a maximum width of size at its middle row.
  • Spaces ( ) are used to center-align the rows so that the diamond is properly shaped.
  • There should be no trailing spaces after the * on any row.
  • If size is 0 or less, the diamond is considered empty, and the function should return an empty string.

Examples

generatePattern("diamond", [3]);
// Output:
 *
***
 *

generatePattern("diamond", [5]);
// Output:
  *
 ***
*****
 ***
  *

generatePattern("diamond", [4]); // Closest odd size is 3
// Output:
 *
***
 *

generatePattern("diamond", [1]);
// Output:
*

generatePattern("diamond", [0]);
// Output: (empty string)

Notes

  • The diamond pattern should properly handle edge cases:
    • If size is 1, the function should return a single *.
    • If size is 0 or negative, the function should return an empty string.
    • If size is even, generate a diamond using the closest odd number below it.
  • The number of * in each row increases by 2 for the top half, reaches size in the middle row, and decreases symmetrically for the bottom half.
  • Ensure that the function works for both small and large values of size.
  • Each line of the output should be separated by a newline character (\n).

Spaced Alternating Rectangle Pattern

Instructions

Your task is to extend the functionality of the generatePattern(style, dimensions) function to support a new pattern type: spaced-alternating rectangle.

For this assignment, the style parameter will be "spaced-alternating-rectangle", and the dimensions parameter will be an array [columns, rows] where:

  • columns is the number of columns in the rectangle.
  • rows is the number of rows in the rectangle.

Pattern Description

  • Each row alternates in the following cycle:
    1. A row of * characters.
    2. A row of - characters.
    3. A row of spaces ( ) (not an empty line) with no visible characters.
  • After the third row, the cycle repeats starting with *.
  • If either dimension is 0 (e.g., [0, X] or [X, 0]), the rectangle is considered empty, and the function should return an empty string.

Examples

generatePattern("spaced-alternating-rectangle", [3, 4]);
// Output:
***
---

***

generatePattern("spaced-alternating-rectangle", [5, 6]);
// Output:
*****
-----

*****
-----


generatePattern("spaced-alternating-rectangle", [4, 3]);
// Output:
****
----


generatePattern("spaced-alternating-rectangle", [6, 2]);
// Output:
******
------

generatePattern("spaced-alternating-rectangle", [0, 3]);
// Output: (empty string)

generatePattern("spaced-alternating-rectangle", [7, 0]);
// Output: (empty string)

Notes

  • Important: The third line in the pattern contains spaces ( ), which are not visible characters but should still occupy the width of the rectangle. It is not an empty line.
  • The spaced-alternating rectangle should properly handle edge cases:
    • If rows is 1, the rectangle will just be a single row of *.
    • If columns is 1, the rectangle will alternate between *, -, and a single blank row for each row.
    • For larger values of rows, the cycle (*, -, ) repeats as described.
  • Each line of the output should be separated by a newline character (\n).
  • Ensure that the function works for both small and large dimensions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment