diff --git a/questions_by_Chandan/Data_processing2.md b/questions_by_Chandan/Data_processing2.md new file mode 100644 index 0000000..a87384a --- /dev/null +++ b/questions_by_Chandan/Data_processing2.md @@ -0,0 +1,272 @@ +--- +title: Basic Data Processing +tags: [mapping, filter aggragation, sorting, grouping, Basic Data Processing] +--- + +# Problem Statement + +--- + +You need to implement the following functions: + +1. **square_numbers(d)** + - Description: Given a list 'd' of numbers, returns a new list with each number squared. + - Example: + + ```python + square_numbers([1,2,3,4,5]) # Output: [1,4,9,16,25] + square_numbers([-1,0,-3,5,2]) # Output: [1,0,9,25,4] + ``` + + +2. **filter_aggregation(d, k)** + - Description: Given a list 'd' of numbers and threshold, returns the sum of numbers greater than or equal to the given threshold. + - Example: + + ```python + filter_even_numbers([3,6,10,23,16], 10) # Output: 49 + filter_even_numbers([1,2,3,45,6], 6) # Output: 51 + ``` + +3. **`sort_strings(d)`** + - Description: Given a list 'd' of strings, return a new list with strings sorted alphabetically. + - Example: + ```python + sort_strings(['Rohan', 'Sohan', 'Bharath']) # Output: ['Bharath', 'Rohan', 'Sohan'] + sorti_strings(['Elizabeth', 'Sudharshini', 'Nanditha']) # Output: ['Elizabeth', Nanditha', Sudharshini'] + ``` + +4. **group_by_key(d)** + - Description: Given a list 'd' of tuples(key, value), returns a dictionary where keys map to list of values. + - Example: + + ```python + group_by_key([("fruit", "apple"), ("fruit", "banana"), ("veg", "carrot")]) # Output: {'fruit': ['apple', 'banana'], 'veg':['carrort']} + group_by_key([("electronics", "smartphone"),("clothing", "t-shirt"),("electronics", "laptop"),("clothing", "jeans"),("grocery", "milk")]) + # Output : {'electronics':['smartphone', 'laptop'], 'clothing': ['t-shirt', 'jean'], 'grocery':['milk']} + ``` + +# Solution +```py3 test.py -r 'python test.py' + + + + + +{% include '../function_type_and_modify_check_suffix.py.jinja' %} + +``` +# Public Test Cases + +## Input 1 + +``` +d = [1,2,3,4] +is_equal( + square_numbers(d), + [1,4,9,16] +) + +d = [23, 14, 7, 8, 5] +threshold = 8 +is_equal( + filter_aggregation(d, threshold), + 45 +) + +d = ['Bharath', 'Kavya', 'Sudharshini', 'David'] +is_equal( + sort_strings(d), + ['Bharath', 'David', 'Kavya', 'Sudharshini'] +) + +d = [("fruit", "apple"), ("fruit", "banana"), ("veg", "carrot")] +is_equal( + group_by_key(d), + {'fruit':['apple', 'banana'], 'veg':['carrot']} +) +``` +## Output 1 +``` +[1, 4, 9, 16] +45 +['Bharath', 'David', 'Kavya', 'Sudharshini'] +{'fruit': ['apple', 'banana'], 'veg': ['carrot']} +``` +## Input 2 + +``` +d = [5, 6, 7, 8] +is_equal( + square_numbers(d), + [25, 36, 49, 64] +) + +d = [12, 5, 9, 7, 3] +threshold = 9 +is_equal( + filter_aggregation(d, threshold), + 21 +) + +d = ['Zara', 'Alex', 'Chandan', 'Meera'] +is_equal( + sort_strings(d), + ['Alex', 'Chandan', 'Meera', 'Zara'] +) + +d = [("animal", "cat"), ("animal", "dog"), ("bird", "parrot")] +is_equal( + group_by_key(d), + {'animal': ['cat', 'dog'], 'bird': ['parrot']} +) +``` +## Output 2 +``` +[25, 36, 49, 64] +21 +['Alex', 'Chandan', 'Meera', 'Zara'] +{'animal': ['cat', 'dog'], 'bird': ['parrot']} +``` + +## Input 3 +``` +d = [10, 11, 12, 13] +is_equal( + square_numbers(d), + [100, 121, 144, 169] +) + +d = [18, 15, 7, 3, 2] +threshold = 10 +is_equal( + filter_aggregation(d, threshold), + 33 +) + +d = ['Ananya', 'Vikas', 'Sameer', 'Esha'] +is_equal( + sort_strings(d), + ['Ananya', 'Esha', 'Sameer', 'Vikas'] +) + +d = [("color", "red"), ("color", "blue"), ("shape", "circle")] +is_equal( + group_by_key(d), + {'color': ['red', 'blue'], 'shape': ['circle']} +) +``` +## Output 3 + +``` +[100, 121, 144, 169] +33 +['Ananya', 'Esha', 'Sameer', 'Vikas'] +{'color': ['red', 'blue'], 'shape': ['circle']} +``` + +# Private Test Cases + +## Input 1 + +``` +d = [2, 4, 6, 8] +is_equal( + square_numbers(d), + [4, 16, 36, 64] +) + +d = [25, 18, 10, 7, 3] +threshold = 15 +is_equal( + filter_aggregation(d, threshold), + 43 +) + +d = ['Ravi', 'Suresh', 'Neha', 'Alok'] +is_equal( + sort_strings(d), + ['Alok', 'Neha', 'Ravi', 'Suresh'] +) + +d = [("city", "Delhi"), ("city", "Mumbai"), ("country", "India")] +is_equal( + group_by_key(d), + {'city': ['Delhi', 'Mumbai'], 'country': ['India']} +) +``` +## Output 1 +``` +[4, 16, 36, 64] +43 +['Alok', 'Neha', 'Ravi', 'Suresh'] +{'city': ['Delhi', 'Mumbai'], 'country': ['India']} +``` \ No newline at end of file diff --git a/questions_by_Chandan/Data_processing_io.md b/questions_by_Chandan/Data_processing_io.md new file mode 100644 index 0000000..031f384 --- /dev/null +++ b/questions_by_Chandan/Data_processing_io.md @@ -0,0 +1,227 @@ +--- +title: Data Processing I/O +tags: [mapping, filtering, aggregation, sorting, grouping, I/O] +--- + +# Problem Statement + +You need to implement the following functions. These functions should read inputs from stdin and write the output to stdout in the specified format. + +1. **`sum_even_numbers(d)`** + - Description: Given a list of numbers, sum all the even numbers and print the result to stdout. + - Input: A list of integers `d`. + - Output: Print the sum of all even numbers in the list. + - Example: + ```python + sum_even_numbers([1, 2, 3, 4, 5]) # Output: 6 + sum_even_numbers([10, 21, 34, 50]) # Output: 94 + ``` + +2. **`filter_greater_than_threshold(d, threshold)`** + - Description: Given a list of numbers and a threshold, filter out the numbers greater than or equal to the threshold and print them to stdout. + - Input: A list of integers `d` and an integer `threshold`. + - Output: Print the filtered list. + - Example: + ```python + filter_greater_than_threshold([1, 2, 3, 4, 5], 3) # Output: [3, 4, 5] + filter_greater_than_threshold([10, 20, 30], 15) # Output: [20, 30] + ``` + +3. **`sort_and_print(d)`** + - Description: Given a list of strings, sort them alphabetically and print the sorted list to stdout. + - Input: A list of strings `d`. + - Output: Print the sorted list. + - Example: + ```python + sort_and_print(["banana", "apple", "cherry"]) # Output: ['apple', 'banana', 'cherry'] + sort_and_print(["mango", "kiwi", "apple"]) # Output: ['apple', 'kiwi', 'mango'] + ``` + +4. **`group_by_category(d)`** + - Description: Given a list of tuples (category, item), group the items by category and print the grouped dictionary to stdout. + - Input: A list of tuples `d`, where each tuple contains a category and an item. + - Output: Print the dictionary with categories as keys and lists of items as values. + - Example: + ```python + group_by_category([("fruit", "apple"), ("fruit", "banana"), ("veg", "carrot")]) + # Output: {'fruit': ['apple', 'banana'], 'veg': ['carrot']} + + group_by_category([("animal", "cat"), ("animal", "dog"), ("bird", "sparrow")]) + # Output: {'animal': ['cat', 'dog'], 'bird': ['sparrow']} + ``` + +# Solution + +```py3 test.py -r 'python test.py' + + + + + +{% include '../function_type_and_modify_check_suffix.py.jinja' %} + +``` + +# Public Test Cases + +## Input 1 +``` + +d = [1, 2, 3, 4, 5] +is_equal( + sum_even_numbers(d), + 6 +) + +d = [1, 2, 3, 4, 5] +threshold = 3 +is_equal( + filter_greater_than_threshold(d, threshold), + [3, 4, 5] +) + +d = ['apple', 'banana', 'cherry'] +is_equal( + sort_and_print(d), + ['apple', 'banana', 'cherry'] +) + +d = [("fruit", "apple"), ("fruit", "banana"), ("veg", "carrot")] +is_equal( + group_by_category(d), + {'fruit': ['apple', 'banana'], 'veg': ['carrot']} +) + +``` + +## Output 1 + +``` +6 +[3, 4, 5] +['apple', 'banana', 'cherry'] +{'fruit': ['apple', 'banana'], 'veg': ['carrot']} + +``` + +## Input 2 + +``` +d = [10, 21, 34, 50] +is_equal( + sum_even_numbers(d), + 94 +) + +d = [5, 6, 7, 8] +threshold = 6 +is_equal( + filter_greater_than_threshold(d, threshold), + [6, 7, 8] +) + +d = ['mango', 'kiwi', 'apple'] +is_equal( + sort_and_print(d), + ['apple', 'kiwi', 'mango'] +) + +d = [("animal", "cat"), ("animal", "dog"), ("bird", "sparrow")] +is_equal( + group_by_category(d), + {'animal': ['cat', 'dog'], 'bird': ['sparrow']} +) + +``` + +## Output 2 + +``` +94 +[6, 7, 8] +['apple', 'kiwi', 'mango'] +{'animal': ['cat', 'dog'], 'bird': ['sparrow']} + +``` + +# Private Test Cases + + +## Input 1 + +``` + +d = [12, 14, 16, 18] +is_equal( + sum_even_numbers(d), + 60 +) + +d = [30, 40, 50] +threshold = 35 +is_equal( + filter_greater_than_threshold(d, threshold), + [40, 50] +) + +d = ['zebra', 'elephant', 'tiger'] +is_equal( + sort_and_print(d), + ['elephant', 'tiger', 'zebra'] +) + +d = [("fruit", "mango"), ("veg", "carrot"), ("fruit", "apple")] +is_equal( + group_by_category(d), + {'fruit': ['mango', 'apple'], 'veg': ['carrot']} +) + +``` + +## Output 1 + +``` +60 +[40, 50] +['elephant', 'tiger', 'zebra'] +{'fruit': ['mango', 'apple'], 'veg': ['carrot']} + +``` + diff --git a/questions_by_Chandan/Data_type1.md b/questions_by_Chandan/Data_type1.md new file mode 100644 index 0000000..27374f8 --- /dev/null +++ b/questions_by_Chandan/Data_type1.md @@ -0,0 +1,340 @@ +--- +title: Basic Data types +tags: [truncation,float,string_functions,type_checking,Basic Data Types] +--- + +# Problem Statement + +--- + +You need to implement the following functions: + +1. **`float_truncate(d)`** + - Description: This function accepts a float and truncates it to an integer, then returns the result. + - Example: + ```python + float_truncate(10.9) # Output: 10 + float_truncate(-5.7) # Output: -5 + ``` + +2. **`is_it_integer(d)`** + - Description: This function accepts any data type and returns a boolean indicating whether the input is an integer. + - Example: + ```python + is_it_integer(5) # Output: True + is_it_integer("Hello") # Output: False + is_it_integer(3.14) # Output: False + ``` + +3. **`Reverse_str(s)`** + - Description: This function accepts a string and returns the reversed version of the string. You should not use loops to achieve this. + - Example: + ```python + Reverse_str("hello") # Output: "olleh" + Reverse_str("Python") # Output: "nohtyP" + ``` + +4. **`Max_set(s)`** + - Description: This function accepts a set `s` and returns the maximum element from the set. + - Example: + ```python + Max_set({1, 2, 3, 4}) # Output: 4 + Max_set({-10, -5, -1}) # Output: -1 + ``` + +5. **`List_sum(l)`** + - Description: This function accepts a list `l` and returns the sum of all elements in the list. You should not use loops. + - Example: + ```python + List_sum([1, 2, 3]) # Output: 6 + List_sum([10, -5, 7]) # Output: 12 + ``` + +6. **`Dict_keys_find(d)`** + - Description: This function accepts a dictionary `d` and returns a list of all the keys in the dictionary. + - Example: + ```python + Dict_keys_find({"a": 1, "b": 2}) # Output: ["a", "b"] + Dict_keys_find({"name": "Alice", "age": 25}) # Output: ["name", "age"] + ``` + +# Solution + +```py3 test.py -r 'python test.py' + + + + + +{% include '../function_type_and_modify_check_suffix.py.jinja' %} + +``` + +# Public Test Cases + +## Input 1 + +``` +d = 7.6 +is_equal( + float_truncate(d), + 7 +) + +d = "Hello" +is_equal( + is_it_integer(d), + False +) + +is_equal( + Reverse_str("Python"), + 'nohtyP' +) + +s = {1, 2, 3, 4} +is_equal( + Max_set(s), + 4 +) + +l = [1, 2, 3, 4] +is_equal( + List_sum(l), + 10 +) + +d = {"name": "Alice", "age": 25} +is_equal( + Dict_keys_find(d), + ['name', 'age'] +) + +``` + +## Output 1 + +``` +7 +False +'nohtyP' +4 +10 +['name', 'age'] + +``` + +## Input 2 + +``` +d = 3.14 +is_equal( + float_truncate(d), + 3 +) + +d = 5 +is_equal( + is_it_integer(d), + True +) + +is_equal( + Reverse_str("world"), + 'dlrow' +) + +s = {10, 20, 30} +is_equal( + Max_set(s), + 30 +) + +is_equal( + List_sum([5, 10, 15]), + 30 +) + +d = {"name": "Alice", "age": 25} +is_equal( + Dict_keys_find(d), + ['name', 'age'] +) + +``` + +## Output 2 + +``` +3 +True +'dlrow' +30 +30 +['name', 'age'] + +``` + +## Input 3 + +``` +d = 16.9 +is_equal( + float_truncate(d), + 16 +) + +d = [1, 2, 3] +is_equal( + is_it_integer(d), + False +) + +is_equal( + List_sum([5, 10, 15]), + 30 +) + +d = {"fruit": "apple", "color": "red"} +is_equal( + Dict_keys_find(d), + ['fruit', 'color'] +) + +``` + +## Output 3 + +``` +16 +False +30 +['fruit', 'color'] + +``` + +# Private Test Cases + +## Input 1 + +``` +d = 8.2 +is_equal( + float_truncate(d), + 8 +) + +d = 'Python' +is_equal( + is_it_integer(d), + False +) + +is_equal( + List_sum([10,20,30,40,50]), + 150 +) + +is_equal( + Max_set({1,2,3,4,5}), + 5 +) + +is_equal( + Reverse_str('Amit'), + 'timA' +) + +d = {'one':1, 'two':2, 'three': 3} +is_equal( + Dict_keys_find(d), + ['one', 'two', 'three'] +) + +``` + + +## Output 1 + +``` +8 +False +150 +5 +'timA' +['one', 'two', 'three'] + +``` \ No newline at end of file diff --git a/questions_by_Chandan/Problem_solving.md b/questions_by_Chandan/Problem_solving.md new file mode 100644 index 0000000..fa7bdf7 --- /dev/null +++ b/questions_by_Chandan/Problem_solving.md @@ -0,0 +1,173 @@ +--- +title: Order Management System +tags: [application, problem-solving, functions, dictionary, list] +--- + +# Problem Statement + +--- + +You are tasked to develop an **Order Management System** for a small business. Implement the following functions: + +1. **`add_order(order_list, order_id, product, quantity)`** + - Description: Adds a new order to the `order_list`. + - Arguments: + - `order_list` (list): A list where each order is a dictionary. + - `order_id` (int): Unique ID for the order. + - `product` (str): Name of the product ordered. + - `quantity` (int): Quantity of the product ordered. + - Returns: Updated `order_list`. + - Example: + ```python + add_order([], 101, 'Apple', 5) + # Output: [{'order_id': 101, 'product': 'Apple', 'quantity': 5}] + ``` + +2. **`update_order(order_list, order_id, new_quantity)`** + - Description: Updates the quantity of an existing order. If the order ID is not found, return "Order not found". + - Arguments: + - `order_list` (list): List of orders. + - `order_id` (int): ID of the order to update. + - `new_quantity` (int): Updated quantity for the order. + - Returns: Updated `order_list` or an error message. + - Example: + ```python + update_order([{'order_id': 101, 'product': 'Apple', 'quantity': 5}], 101, 10) + # Output: [{'order_id': 101, 'product': 'Apple', 'quantity': 10}] + ``` + +3. **`delete_order(order_list, order_id)`** + - Description: Deletes an order from the `order_list` based on the order ID. If the order ID is not found, return "Order not found". + - Arguments: + - `order_list` (list): List of orders. + - `order_id` (int): ID of the order to delete. + - Returns: Updated `order_list` or an error message. + - Example: + ```python + delete_order([{'order_id': 101, 'product': 'Apple', 'quantity': 5}], 101) + # Output: [] + ``` + +4. **`get_order_summary(order_list)`** + - Description: Returns a summary of all orders, grouped by product, showing the total quantity for each product. + - Arguments: + - `order_list` (list): List of orders. + - Returns: A dictionary where keys are product names, and values are the total quantities. + - Example: + ```python + get_order_summary([{'order_id': 101, 'product': 'Apple', 'quantity': 5}, {'order_id': 102, 'product': 'Apple', 'quantity': 3}, {'order_id': 103, 'product': 'Banana', 'quantity': 2}]) + # Output: {'Apple': 8, 'Banana': 2} + ``` + +--- + +# Solution + +```python +def add_order(order_list, order_id, product, quantity): + order_list.append({'order_id': order_id, 'product': product, 'quantity': quantity}) + return order_list + +def update_order(order_list, order_id, new_quantity): + for order in order_list: + if order['order_id'] == order_id: + order['quantity'] = new_quantity + return order_list + return "Order not found" + +def delete_order(order_list, order_id): + for order in order_list: + if order['order_id'] == order_id: + order_list.remove(order) + return order_list + return "Order not found" + +def get_order_summary(order_list): + summary = {} + for order in order_list: + product = order['product'] + quantity = order['quantity'] + if product in summary: + summary[product] += quantity + else: + summary[product] = quantity + return summary +``` +# Public Test Cases + +## Input 1 + +``` +order_list = [] +order_list = add_order(order_list, 101, 'Apple', 5) +order_list = add_order(order_list, 102, 'Banana', 2) +order_list = update_order(order_list, 101, 10) +order_list = delete_order(order_list, 102) +summary = get_order_summary(order_list) +print(order_list) +print(summary) +``` +## Output 1 + +``` +[{'order_id': 101, 'product': 'Apple', 'quantity': 10}] +{'Apple': 10} +``` + +## Input 2 + +``` +order_list = [] +order_list = add_order(order_list, 201, 'Orange', 6) +order_list = add_order(order_list, 202, 'Apple', 3) +order_list = add_order(order_list, 203, 'Banana', 5) +order_list = update_order(order_list, 202, 8) +order_list = delete_order(order_list, 204) # Invalid ID +summary = get_order_summary(order_list) +print(order_list) +print(summary) +``` +## Output 2 +``` +[{'order_id': 201, 'product': 'Orange', 'quantity': 6}, {'order_id': 202, 'product': 'Apple', 'quantity': 8}, {'order_id': 203, 'product': 'Banana', 'quantity': 5}] +{'Orange': 6, 'Apple': 8, 'Banana': 5} +``` + +# Private Test Cases + +## Input 1 +``` +order_list = [] +order_list = add_order(order_list, 301, 'Milk', 2) +order_list = add_order(order_list, 302, 'Bread', 4) +order_list = add_order(order_list, 303, 'Milk', 3) +order_list = update_order(order_list, 302, 6) +order_list = delete_order(order_list, 304) # Invalid ID +summary = get_order_summary(order_list) +print(order_list) +print(summary) +``` + +## Output 1 +``` +[{'order_id': 301, 'product': 'Milk', 'quantity': 2}, {'order_id': 302, 'product': 'Bread', 'quantity': 6}, {'order_id': 303, 'product': 'Milk', 'quantity': 3}] +{'Milk': 5, 'Bread': 6} +``` + +## Input 2 +``` +order_list = [] +order_list = add_order(order_list, 401, 'Pencil', 10) +order_list = add_order(order_list, 402, 'Notebook', 5) +order_list = add_order(order_list, 403, 'Notebook', 7) +order_list = update_order(order_list, 401, 15) +order_list = delete_order(order_list, 402) +summary = get_order_summary(order_list) +print(order_list) +print(summary) +``` +## Output 2 +``` +[{'order_id': 401, 'product': 'Pencil', 'quantity': 15}, {'order_id': 403, 'product': 'Notebook', 'quantity': 7}] +{'Pencil': 15, 'Notebook': 7} +```