Showing posts with label zip function. Show all posts
Showing posts with label zip function. Show all posts

Friday, September 13, 2024

Using Zip to Assign Letter Grades Based on Score Thresholds

You want to create a program that assigns a letter grade based on a given numerical score. The goal is to:

1. Take an input value representing the score.
2. Compare the score to specific grade boundaries (e.g., 80, 70, 60, etc.).
3. Assign the corresponding letter grade based on the highest boundary the score meets or exceeds.
4. If the score does not meet any boundary, assign a default grade.

The solution should demonstrate the use of the `zip` function to pair letter grades with their corresponding score thresholds.

### Solution

1. **Input Score:**
   - The user inputs a score, which is stored in the variable `m`. This score will determine the corresponding letter grade.

2. **Pair Grades and Score Thresholds:**
   - Using `zip`, you pair each letter grade (A, B, C, D) with a corresponding score threshold (80, 70, 60, 50). The `zip` function creates an iterator that produces tuples like `('A', 80)`, `('B', 70)`, etc.

3. **Iterate and Compare:**
   - A `for` loop iterates over each pair of grade and score threshold. For each pair:
     - If the input score `m` is greater than or equal to the current score threshold, the corresponding letter grade is stored in the variable `g`, and the loop is exited using `break`. This ensures the highest grade threshold that is met or exceeded is selected.
   
4. **Handle Low Scores:**
   - If none of the thresholds are met (i.e., the input score is below 50), the `else` block executes, assigning the letter grade 'U' (representing "ungraded" or a failing grade). The `else` block runs only if the `for` loop completes without encountering a `break`.

5. **Output the Grade:**
   - Finally, the letter grade stored in `g` is printed.

### Key Concept (Using `zip`):
The `zip` function pairs up the letter grades with their corresponding score thresholds, allowing you to iterate over them together. This demonstrates how `zip` can be used to link related data (in this case, letter grades and score thresholds) and process them simultaneously.

Sunday, September 8, 2024

Removing Consecutive Duplicates from a String

String Processing: Remove Spaces & Condense Characters

String Processing: Remove Spaces & Condense Characters

You have a string consisting of various characters, including letters and dots, separated by spaces. The goal is to process this string to:

  • Remove all spaces
  • Condense consecutive duplicate characters into a single instance
  • Output the condensed characters as a continuous string

Solution Overview

1. Remove Spaces

Start by creating a list of characters from the string while ignoring any spaces. This step effectively filters out all the spaces, leaving only the non-space characters.

2. Condense Duplicates

Traverse the filtered list and remove consecutive duplicate characters. Only the first occurrence of each sequence is retained.

3. Output the Result

After condensing, output the characters as a continuous string without separators.

Step-by-Step Breakdown

Step 1: Filter Out Spaces

From the original string, extract all characters that are not spaces. This gives a list containing only relevant characters.

Step 2: Remove Consecutive Duplicates

Create a new list starting with the first character. Compare each character with the previous one and keep it only if it differs.

Step 3: Print the Result

Iterate through the condensed list and print each character consecutively.

Example CLI Output


Input:
a a a . . . b b c c . .

Processing:
- Remove spaces
- Condense duplicates

Output:
a.bc.

Python Implementation


s = "a a a . . . b b c c . ."

filtered = [c for c in s if c != " "]

result = [filtered[0]]
for c in filtered[1:]:
    if c != result[-1]:
        result.append(c)

print("".join(result))

๐Ÿ’ก Key Takeaways

  • Simplify input before processing
  • Adjacent comparison removes redundancy efficiently
  • Linear-time solutions scale well
  • Technique applies to logs, streams, and compression
Interactive HTML learning view • High contrast • Readable everywhere

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts