Showing posts with label dictionary. Show all posts
Showing posts with label dictionary. Show all posts

Monday, November 25, 2024

Party Bill Split with Lucky Friend Feature

The task is to simulate a party scenario where a group of friends, including the user, attend a party. The program calculates and divides the total bill among the friends, with an option for one friend to be lucky, meaning they don’t have to pay. The program asks for inputs to gather information about the number of people attending, their names, and the total bill. It also includes a feature where a random "lucky friend" can be selected, who will not pay for the party.

### Solution Explanation

1. **Input Phase:**
   - **Number of Friends:** The program asks the user to enter the number of friends attending the party (including the user). If no one is attending, the program will inform the user and exit.
   - **Friends' Names:** After the number of attendees is entered, the program requests the names of each person attending the party.
   - **Total Bill:** The program then asks for the total amount of the bill to be paid by the group.

2. **Lucky Feature:**
   - The user is given the option to enable the "lucky friend" feature. If the user chooses "Yes", the program randomly selects one person to be the "lucky friend", and this person will not have to contribute to the bill. 
   - If the user chooses "No", the bill is divided evenly among all attendees.

3. **Bill Calculation:**
   - **With Lucky Friend:** If the "lucky friend" feature is activated, the bill is divided among the remaining friends (excluding the lucky one). The lucky friend’s contribution is set to 0.
   - **Without Lucky Friend:** If the feature is not enabled, the bill is divided evenly among all attendees.

4. **Output:**
   - The program outputs a dictionary where each friend's name is mapped to the amount they need to pay. The lucky friend will have a bill of 0 if the "lucky" feature is enabled.

### Example Scenario

- **Input:**
  - The user enters `4` as the number of people joining the party.
  - The names of the friends are entered as: Alice, Bob, Charlie, and Dave.
  - The total bill is `100`.
  - The user opts to use the "Who is lucky?" feature and selects "Yes".

- **Output:**
  - A random person, say "Charlie", is selected as the lucky one.
  - The bill for the other 3 friends (Alice, Bob, and Dave) is divided equally:
    - Total bill = `100`
    - Amount per person (excluding the lucky friend) = `100 / 3 = 33.33`
  - The output dictionary might look like this:
    
    {'Alice': 33.33, 'Bob': 33.33, 'Charlie': 0, 'Dave': 33.33}
    

### Summary:

- If the "lucky" mode is enabled, one friend gets selected randomly to not pay for the party.
- The remaining friends split the total bill.
- If no one is lucky, everyone shares the bill equally.
- The solution handles cases where no one is attending, and it ensures the calculation is correct regardless of whether the lucky mode is enabled.

Saturday, August 24, 2024

Creating Unique Flashcards with a User Quiz in Python


You need to create a dictionary for flashcards where each card consists of a term and its definition. The user inputs the number of cards, then provides terms and definitions for each card. The goal is to ensure that terms and definitions are unique. After creating the dictionary, you prompt the user to guess definitions for the terms and provide feedback on whether their guesses are correct, while also identifying if their guess matches a different term's definition.


1. Input Collection:
   - Start by initializing an empty dictionary to store terms and their definitions.
   - Ask the user to input the number of cards they want to create.
   
2. Unique Term and Definition Validation:
   - For each card, prompt the user to enter a term. Check if this term already exists in the dictionary. If it does, keep asking for a new term until a unique one is provided.
   - Similarly, prompt the user to enter a definition and check if it is already associated with a different term. If it is, keep asking for a new definition until a unique one is found.

3. Storing Data:
   - Once unique term and definition are obtained, store them in the dictionary with the term as the key and the definition as the value.

4. User Quiz:
   - After all terms and definitions are entered, prompt the user to input definitions for each term.
   - Compare the provided definition with the correct definition from the dictionary. If the definition is correct, notify the user; otherwise, provide feedback with the correct definition and indicate if their guess matches a different term’s definition.

By following these steps, you ensure that each term and definition is unique and provide informative feedback based on user input.

Thursday, August 22, 2024

Efficiently Tracking and Querying Word Positions in a List Using a Dictionary

You need to create a program that processes a list of words input by the user, stores the positions (indices) of each word, and then allows querying of these positions. If a user queries a word that exists in the list, the program should return all indices where that word appears. If the word does not exist in the list, it should return "not found." 

1. **Reading Input**:
   - The program starts by taking a single line of input, which consists of words separated by spaces. This line is split into a list of words.

2. **Building Index Dictionary**:
   - The program then constructs a dictionary to map each word to a list of its indices (positions) in the original list. This is achieved by iterating through the list of words and recording each word's position.

3. **Handling Queries**:
   - After building the dictionary, the program waits for another user input, which is a query for a specific word. It looks up this word in the dictionary.
   - If the word exists in the dictionary, it prints out all indices where this word appears. If the word is not found, it outputs "not found."

### Summary:

This approach allows you to efficiently track the positions of words within a list and handle user queries to retrieve these positions. It uses a dictionary to store and quickly access the positions of each word, making it effective for both storing and querying word positions.

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