Knowee
Questions
Features
Study Tools

You are given a list of strings consisting of grocery items, with prices in parentheses. Return a list of prices in float format. Use RegExp to solve.

Question

You are given a list of strings consisting of grocery items, with prices in parentheses. Return a list of prices in float format. Use RegExp to solve.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python solution using regular expressions (RegExp):

import re

def extract_prices(grocery_list):
    prices = []
    for item in grocery_list:
        match = re.search(r'\((.*?)\)', item)
        if match:
            price = match.group(1)
            prices.append(float(price))
    return prices

Here's how it works:

  1. We import the re module, which allows us to use regular expressions.
  2. We define a function extract_prices that takes a list of grocery items as input.
  3. We create an empty list prices to store the prices we extract.
  4. We iterate over each item in the grocery list.
  5. We use the re.search function to find a match for the regular expression r'\((.*?)\)'. This expression matches any text enclosed in parentheses.
  6. If a match is found, we use the group method to extract the text inside the parentheses. This is the price, but it's still a string.
  7. We convert the price to a float and append it to the prices list.
  8. After we've processed all the items in the grocery list, we return the prices list.

This problem has been solved

Similar Questions

A data analyst discovers that their database has recognized product price data as text strings. What SQL function can the analyst use to convert the text strings to floats? 1 pointCASTSUBSTRTRIMLENGTH

In  a retail store software , get the price of each stock as input with its price and print in the following format input:dinnerset10000mattress24000Invertor30000 output: dinnerset10,000mattress24,000Invertor30,000

Use regular expressions to extract data from strings

Objective:To work with OperatorsScenario:A new stationary shop has been opened in the city. The owner asks his accountant to take the list of items sold in the store. The list should contain the details of the items and their costs. Help the accountant to generate the prince list by writing a Python program.  Generate list with just 4 products -  A4sheets, pen, pencil and eraser and get the price of the items from the user.  Please refer the sample input and output statements for more clarifications.Guidelines:The amount must be displayed with 2 decimal places. On entering the product price to be a negative number, the program should display the message " Invalid input" and stop the program. Sample Input 1 :Cost of A4sheet:40Cost of pen:20Cost of pencil:10Cost of eraser:5Sample Output 1 :Items DetailsA4sheet:40.00Pen:20.00Pencil:10.00Eraser:5.00Sample Input 2 :Cost of A4sheet:40Cost of pen:20Cost of pencil:10Cost of eraser:-5Sample Output 2 :Invalid inputSample Input 3 :Cost of A4sheet:86Cost of pen:10Cost of pencil:-34Sample Output 3 :Invalid input

Problem StatementMaria loves shopping and is curious to know the total cost of her favorite items. She intends to purchase t items, each priced at p rupees, with a progressive increase of 5 rupees for every subsequent item.Write a program that takes the price of one item (p) and the number of items (t) as input, calculates the total cost using the formula (t/2) * (2p + (t-1) * 5), and displays the result.Input format :The first line of input consists of a float value p, representing the price of an item.The second line of input consists of an integer t, representing the number of items.Output format :The output displays "Rs. " followed by a float value, representing the total cost in rupees, rounded off to two decimal places.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:10.0 ≤ p ≤ 1000.01 ≤ t ≤ 100Sample test cases :Input 1 :10.03Output 1 :Rs. 45.00Input 2 :720.056Output 2 :Rs. 48020.00

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.