-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 04-11-2024 Solution and 05-11-2024 Question
Added 04-11-2024 Solution and 05-11-2024 Question
- Loading branch information
1 parent
e69be40
commit 45be64a
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Alternating Number Pattern | ||
|
||
## Overview | ||
|
||
This program generates a descending pattern of alternating numbers (1 and 0) based on a specified number of rows (`r`). The pattern starts with `1` on the first row, and alternates between `1` and `0` on each subsequent row. Each row contains one fewer element than the previous row until reaching a single element in the last row. | ||
|
||
## Requirements | ||
|
||
1. **Input**: The program prompts the user to enter an integer `r`, representing the number of rows. | ||
2. **Pattern**: | ||
- **Odd-numbered rows** start with `1`, and **even-numbered rows** start with `0`. | ||
- The first row has `r` elements, the second row has `r-1` elements, and so on until the last row, which contains just one element. | ||
|
||
## Example | ||
|
||
If the input `r` is `5`, the output pattern will be: | ||
|
||
``` | ||
1 1 1 1 1 | ||
0 0 0 0 | ||
1 1 1 | ||
0 0 | ||
1 | ||
``` | ||
|
||
### Code Walkthrough | ||
|
||
1. **Input and Initialization**: | ||
|
||
- The program starts by asking the user to input the number of rows, which we store in the variable `r`. | ||
- A newline is printed to cleanly separate the input prompt from the output pattern. | ||
|
||
2. **Loop Through Rows**: | ||
|
||
- We use a `for` loop to iterate from `r` down to `1` (inclusive). This allows us to print each row with one fewer element than the last. | ||
- In each iteration, we: | ||
- **Check if the row is even or odd**: | ||
- If the row number `i` is **even**, we print the character `0`, repeated `i` times, separated by spaces. | ||
- If the row number `i` is **odd**, we print the character `1`, repeated `i` times, separated by spaces. | ||
|
||
3. **Output**: | ||
- The loop prints each row in descending order, forming the alternating number pattern as per the requirements. | ||
|
||
## Additional Notes | ||
|
||
- This pattern alternates the starting character (`1` for odd rows and `0` for even rows) and reduces the count of elements in each row. | ||
- The loop ensures each row’s content is centered around its even or odd position, which gives it an alternating look. | ||
|
||
This program provides a simple yet effective way to practice loops and conditional statements in Python. It's particularly useful for understanding control flow and pattern-based logic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Number of Rows | ||
r = int(input("Enter Number of Rows: ")) # Prompt the user to input the number of rows | ||
|
||
# For Newline | ||
print() # Print a newline to separate input from output | ||
|
||
# Printing Pattern | ||
for i in range( | ||
r, 0, -1 | ||
): # Start from the total number of rows `r` and decrement down to 1 | ||
if i % 2 == 0: # Check if the current row number is even | ||
print("0 " * i) # If even, print '0' repeated `i` times | ||
else: # For odd rows | ||
print("1 " * i) # Print '1' repeated `i` times |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
### **Question: Increasing Letter Pattern** | ||
|
||
**Difficulty Level:** 🟢 Beginner | ||
**Domain:** Pattern Printing | ||
|
||
### **Objective:** | ||
|
||
Generate an **increasing pattern** of letters, where each row starts with the letter 'A' and each subsequent row continues with the next letter in alphabetical order. The number of letters in each row should match the row number, starting from 1. | ||
|
||
### **Requirements:** | ||
|
||
1. **Pattern Specifications**: | ||
|
||
- **Input**: An integer `r` representing the number of rows. | ||
- **Output**: Print a pattern where: | ||
- The first row contains the letter 'A' repeated once. | ||
- The second row contains the letter 'B' repeated twice. | ||
- This pattern continues, with each row containing the next letter and the count of the letter matching the row number, until reaching `r` rows. | ||
|
||
2. **Example**: | ||
|
||
- **Input**: `5` | ||
- **Output**: | ||
``` | ||
A | ||
B B | ||
C C C | ||
D D D D | ||
E E E E E | ||
``` | ||
3. **Solution Structure**: | ||
- **Loop** through each row, starting from 1 up to `r`. | ||
- For each row, **calculate** the appropriate letter by starting from 'A' and shifting by the row number. | ||
- **Repeat** the letter for the current row number times to form the pattern. | ||
### **Guidelines**: | ||
- Make sure the pattern increments the letters correctly and aligns each row properly. | ||
- Test with various values of `r` to ensure correct letter and row count in the output. |