From 0279d251aeb65d5fdc2cdd26a0a552efa994d701 Mon Sep 17 00:00:00 2001 From: prashantnegii <22f2001236@ds.study.iitm.ac.in> Date: Wed, 8 Jan 2025 20:15:05 +0530 Subject: [PATCH] Question by Prashant Negi --- Questions/data_processing.md | 91 +++++++++++++++++ Questions/data_processing_io.md | 81 +++++++++++++++ Questions/data_types.md | 91 +++++++++++++++++ Questions/problem_solving.md | 168 ++++++++++++++++++++++++++++++++ 4 files changed, 431 insertions(+) create mode 100644 Questions/data_processing.md create mode 100644 Questions/data_processing_io.md create mode 100644 Questions/data_types.md create mode 100644 Questions/problem_solving.md diff --git a/Questions/data_processing.md b/Questions/data_processing.md new file mode 100644 index 0000000..0fe1409 --- /dev/null +++ b/Questions/data_processing.md @@ -0,0 +1,91 @@ +--- +title: Data Processing- Group by Key and Aggregate +--- + +# Problem Statement + +Write a function group_and_sum(data) that takes a list of tuples data, where each tuple contains a category (string) and a value (int). The function should return a dictionary where the keys are the unique categories and the values are the sum of all values for that category. + +**Example** +```py3 +group_and_sum([("A", 10), ("B", 5), ("A", 7)]) +# Output: {'A': 17, 'B': 5} +``` + +# Solution + +```py3 test.py -r 'python test.py' + +def group_and_sum(data: list): + """ + Group by key and sum values. + + Args: + data: list - List of tuples containing category and value. + + Returns: + dict: A dictionary with summed values for each category. + """ + + + +{% include '../function_type_and_modify_check_suffix.py.jinja' %} + +``` + +# Public Test Cases + +## Input 1 + +``` +group_and_sum([("X", 2), ("Y", 3), ("X", 4)]) +``` + +## Output 1 + +``` +{'X': 6, 'Y': 3} +``` + +## Input 2 + +``` +group_and_sum([]) +``` + +## Output 2 + +``` +{} +``` + + +# Private Test Cases + +## Input 1 + +``` +group_and_sum([("Cat", 1), ("Dog", 3), ("Cat", 2), ("Bird", 5)]) +group_and_sum([("January", 200), ("February", 150), ("January", 250), ("March", 300)]) +group_and_sum([("Electronics", 100), ("Furniture", 200), ("Electronics", 150), ("Clothing", 50)]) +group_and_sum([("Alice", 8), ("Bob", 6), ("Alice", 7), ("Charlie", 9), ("Bob", 5)]) +``` + +## Output 1 + +``` +{'Cat': 3, 'Dog': 3, 'Bird': 5} +{'January': 450, 'February': 150, 'March': 300} +{'Electronics': 250, 'Furniture': 200, 'Clothing': 50} +{'Alice': 15, 'Bob': 11, 'Charlie': 9} +``` \ No newline at end of file diff --git a/Questions/data_processing_io.md b/Questions/data_processing_io.md new file mode 100644 index 0000000..23e93d8 --- /dev/null +++ b/Questions/data_processing_io.md @@ -0,0 +1,81 @@ +--- +title: Data Processing I/O- Find Unique Words +tags: [aggregation, filtering, I/O, definite input] +--- + +# Problem Statement + +Write a program that reads a paragraph from standard input and prints all unique words (case-insensitive) sorted alphabetically. + +# Solution +```python test.py -r 'python test.py' + +``` + +# Public Test Cases + +## Input 1 +``` +Hello world! Hello again. +``` + +## Output 1 +``` +again +hello +world +``` + +## Input 2 +``` +Python is great. Python is fun! +``` + +## Output 2 +``` +fun +great +is +python +``` + + +# Private Test Cases + + +## Input 1 +``` +One fish, two fish, red fish, blue fish. +``` + +## Output 1 +``` +blue +fish +one +red +two +``` + +## Input 2 +``` +Hello, world! This is a test. Hello world. +``` + +## Output 2 +``` +a +hello +is +test +this +world +``` \ No newline at end of file diff --git a/Questions/data_types.md b/Questions/data_types.md new file mode 100644 index 0000000..5f316e7 --- /dev/null +++ b/Questions/data_types.md @@ -0,0 +1,91 @@ +--- +title: Data Types- Perform Basic Operations +--- + +# Problem Statement + +Write a function datatype_operations(a, b, c, lst) that performs the following operations: + +Returns the sum of a and b if a and b are integers. +Returns the concatenation of c and the string " completed!" if c is a string. +Appends a to the list lst and returns the updated list. + +**Example** +```py3 +datatype_operations(10, 20, "Task", [1, 2, 3]) +# Output: (30, 'Task completed!', [1, 2, 3, 10]) +``` + +# Solution + +```py3 test.py -r 'python test.py' + +def datatype_operations(a: int, b: int, c: str, lst: list): + """ + Perform operations based on the inputs' data types. + + Args: + a: int + b: int + c: str + lst: list + + Returns: + tuple: A tuple containing the results of the operations. + """ + + + +{% include '../function_type_and_modify_check_suffix.py.jinja' %} + +``` + +# Public Test Cases + +## Input 1 + +``` +datatype_operations(5, 7, "Done", [2, 4]) +``` + +## Output 1 + +``` +(12, 'Done completed!', [2, 4, 5]) +``` + +## Input 2 + +``` +datatype_operations(3, 9, "Test", []) +``` + +## Output 2 + +``` +(12, 'Test completed!', [3]) +``` + + +# Private Test Cases + +## Input 1 + +``` +datatype_operations(-10, 10, "Example", [0]) +datatype_operations(0, 100, "Practice", [4,8]) +``` + +## Output 1 + +``` +(0, 'Example completed!', [0, -10]) +(100, 'Practice completed!', [4, 8, 0]) +``` \ No newline at end of file diff --git a/Questions/problem_solving.md b/Questions/problem_solving.md new file mode 100644 index 0000000..30c5449 --- /dev/null +++ b/Questions/problem_solving.md @@ -0,0 +1,168 @@ +--- +title: Problem-Solving- Employee Data Analysis +--- + +# Problem Statement + +Write the following functions for analyzing employee data: + +average_salary(data) - Given a list of dictionaries, calculate the average salary. +department_count(data) - Return a dictionary with the number of employees in each + +**Example** +```py3 +employees = [ + {"name": "Alice", "department": "HR", "salary": 50000}, + {"name": "Bob", "department": "IT", "salary": 70000}, + {"name": "Charlie", "department": "IT", "salary": 60000}, +] + +average_salary(employees) +# Output: 60000.0 + +department_count(employees) +# Output: {'HR': 1, 'IT': 2} +``` + +# Solution +```python test.py -r 'python test.py' + + +employees = input_employees() +print(average_salary(employees)) +print(department_count(employees)) + + +def input_employees(): + # Prompt the user for the input in the form of a list of dictionaries + employees_input = input("Enter the list of employees as a list of dictionaries: ") + + # Use eval() to convert the input string into a list of dictionaries + try: + employees = eval(employees_input) + + # Validate that the input is a list of dictionaries + if isinstance(employees, list) and all(isinstance(emp, dict) for emp in employees): + return employees + else: + print("Invalid format: The input must be a list of dictionaries.") + return [] + except Exception as e: + print(f"Invalid input! Error: {e}") + return [] + +``` + +# Public Test Cases + +## Input 1 +``` +employees = [ + {"name": "Alice", "department": "HR", "salary": 50000}, + {"name": "Bob", "department": "IT", "salary": 70000}, +] +``` + +## Output 1 +``` +60000.0 +{'HR': 1, 'IT': 1} +``` + + +# Private Test Cases + + +## Input 1 +``` +employees = [ + {"name": "Alice", "department": "HR", "salary": 50000}, + {"name": "Bob", "department": "IT", "salary": 70000}, + {"name": "Charlie", "department": "Finance", "salary": 85000}, + {"name": "Diana", "department": "HR", "salary": 55000}, + {"name": "Eve", "department": "Finance", "salary": 75000}, +] +``` + +## Output 1 +``` +67000.0 +{'HR': 2, 'IT': 1, 'Finance': 2} +``` + +## Input 2 +``` +employees = [] +``` + +## Output 2 +``` +0 +{} +``` + +## Input 3 +``` +employees = [ + {"name": "Alice", "department": "HR", "salary": 0}, + {"name": "Bob", "department": "IT", "salary": -5000}, + {"name": "Charlie", "department": "Finance", "salary": 30000}, + {"name": "Diana", "department": "IT", "salary": 20000}, +] +``` + +## Output 3 +``` +11250.0 +{'HR': 1, 'IT': 2, 'Finance': 1} +``` + +## Input 4 +``` +employees = [ + {"name": "Alice", "department": "HR and Admin", "salary": 60000}, + {"name": "Bob", "department": "Finance/Accounts", "salary": 75000}, + {"name": "Charlie", "department": "HR and Admin", "salary": 65000}, + {"name": "Diana", "department": "IT", "salary": 70000}, + {"name": "Eve", "department": "Finance/Accounts", "salary": 80000}, +] +``` + +## Output 4 +``` +70000.0 +{'HR and Admin': 2, 'Finance/Accounts': 2, 'IT': 1} +``` + +## Input 5 +``` +employees = [ + {"name": "Alice", "department": "HR", "salary": 100000}, + {"name": "Bob", "department": "IT", "salary": 20000}, + {"name": "Charlie", "department": "Finance", "salary": 30000}, + {"name": "Diana", "department": "IT", "salary": 25000}, + {"name": "Eve", "department": "Finance", "salary": 50000}, + {"name": "Frank", "department": "HR", "salary": 90000}, +] +``` + +## Output 5 +``` +52500.0 +{'HR': 2, 'IT': 2, 'Finance': 2} +``` \ No newline at end of file