From 888653b3400f345014446a08ff4bf76cfcda1ce9 Mon Sep 17 00:00:00 2001 From: Devanshu-gif <23f2004399@ds.study.iitm.ac.in> Date: Wed, 8 Jan 2025 00:43:47 +0530 Subject: [PATCH] Add problem statements, solutions, and test cases for 4 questions --- question1.md | 87 ++++++++++++++++++++++++++ question2.md | 94 ++++++++++++++++++++++++++++ question3.md | 144 +++++++++++++++++++++++++++++++++++++++++++ question4.md | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 493 insertions(+) create mode 100644 question1.md create mode 100644 question2.md create mode 100644 question3.md create mode 100644 question4.md diff --git a/question1.md b/question1.md new file mode 100644 index 0000000..a08c764 --- /dev/null +++ b/question1.md @@ -0,0 +1,87 @@ +## Problem Statement + +Write a function `Intro(F_name: str, L_name: str, height: float, weight: float) -> str` that: +1. Takes four parameters: + - `F_name`: First name (string). + - `L_name`: Last name (string). + - `height`: Height in meters (float). + - `weight`: Weight in kilograms (float). + +2. Returns a string of the form: `"Hi, my name is XY and my BMI is __"` where: + - `X` is the first character of `F_name`. + - `Y` is the first character of `L_name`. + - BMI is calculated using the formula: `BMI = weight / (height ** 2)`. + +## Solution + +```python +def Intro(F_name: str, L_name: str, height: float, weight: float) -> str: + bmi = weight / (height ** 2) + return f"Hi, my name is {F_name[0]}{L_name[0]} and my BMI is {bmi:.2f}" +``` + +## Public Test Cases + +### Test Case 1: +**Input:** +```python +("John", "Doe", 1.75, 70) +``` +**Expected Output:** +``` +Hi, my name is JD and my BMI is 22.86 +``` + +### Test Case 2: +**Input:** +```python +("Alice", "Smith", 1.60, 55) +``` +**Expected Output:** +``` +Hi, my name is AS and my BMI is 21.48 +``` + +## Private Test Cases + +### Test Case 1: +**Input:** +```python +("James", "Bond", 1.83, 80) +``` +**Expected Output:** +``` +Hi, my name is JB and my BMI is 23.88 +``` + +### Test Case 2: +**Input:** +```python +("Mark", "Ruffalo", 1.73, 72) +``` +**Expected Output:** +``` +Hi, my name is MR and my BMI is 24.06 +``` + +### Test Case 3: +**Input:** +```python +("Chris", "Evans", 1.80, 85) +``` +**Expected Output:** +``` +Hi, my name is CE and my BMI is 26.23 +``` + +### Test Case 4: +**Input:** +```python +("Henry", "Cavill", 1.85, 92) +``` +**Expected Output:** +``` +Hi, my name is HC and my BMI is 26.88 +``` + + diff --git a/question2.md b/question2.md new file mode 100644 index 0000000..93b8d00 --- /dev/null +++ b/question2.md @@ -0,0 +1,94 @@ +## Problem Statement + +Write a function `group_by_first_letter(words: list) -> dict` which takes a list of words as its argument. The function should **return** a **dictionary** where: +- The **keys** of the dictionary are the first letters of ther words. +- The **values** are lists of words starting with the letter the corresponding **key**. +- Words in each list should **sorted** in **ascending alphabetical order** + + ### Constraints + - The input list will only contain strings. + - Words will be lowercase alphabets only (No special characters or numbers.) + - The list may contain duplicate words. + + ## Solution + +```python +def group_by_first_letter(words: list) -> dict: + grouped = {} + for word in words: + f_letter = word[0] + if f_letter not in grouped: + grouped[f_letter] = [] + grouped[first_letter].append(word) + + #sorting each list of words + for key in grouped: + grouped[key].sort() + + return grouped +``` + +## Public Test Cases + +### Test Case 1: +**Input:** +```python +['apple', 'banana', 'ant', 'blueberry', 'cherry'] +``` +**Expected Output:** +```python +{'a': ['ant', 'apple'], 'b': ['banana', 'blueberry'], 'c': ['cherry']} +``` + +### Test Case 2: +**Input:** +```python +["dog", "cat", "deer", "antelope", "apple", "car"] +``` +**Expected Output:** +```python +{'d': ['deer', 'dog'], 'c': ['car', 'cat'], 'a': ['antelope', 'apple']} +``` + +## Private Test Cases + +### Test Case 1: +**Input:** +```python +["zoo", "zebra", "apple", "ant", "ant", "zebra", "antelope"] +``` +**Expected Output:** +```python +{'z': ['zebra', 'zebra', 'zoo'], 'a': ['ant', 'ant', 'antelope', 'apple']} +``` + +### Test Case 2: +**Input:** +```python +["kiwi", "kale", "kangaroo", "apple", "ant", "banana", "caulifla"] +``` +**Expected Output:** +```python +{'k': ['kale', 'kangaroo', 'kiwi'], 'a': ['ant', 'apple'], 'b': ['banana'], 'c': ['caulifla']} +``` + +### Test Case 3: +**Input:** +```python +["carrot", "cucumber", "celery", "date", "dragonfruit", "gogeta", "gojo"] +``` +**Expected Output:** +```python +{'c': ['carrot', 'celery', 'cucumber'], 'd': ['date', 'dragonfruit'], 'g': ['gogeta', 'gojo']} +``` + +### Test Case 4: +**Input:** +```python +["pine", "pear", "pomegranate", "peach", "plum"] +``` +**Expected Output:** +```python +{'p': ['peach', 'pear', 'pine', 'plum', 'pomegranate']} +``` + diff --git a/question3.md b/question3.md new file mode 100644 index 0000000..df10440 --- /dev/null +++ b/question3.md @@ -0,0 +1,144 @@ +## Problem Statement + +You are given the number of student `n` and their names along with their marks. Write a program to process them and print the following: +1. The total number of students. +2. The names of student who scored more than 50, sorted alphabetically. +3. The average marks of all students. + +The **Input** will be in the following format: +- The first line contains an integer `n` (1 ≤ n ≤ 1000), which represents the number of students. +- Each of the next `n` lines contains a student's name and marks (separated by a space). + +**Output Format:** +- Print total number of students. +- Print the list of names of students who scored above 50, sorted alphabetically. +- Print the average marks of all students, rounded to two decimal places. + +*(If in doubt about output format, refer to the test cases.)* + +## Solution +```python +n = int(input()) # Number of students +students = [] +total_marks = 0 + +for i in range(n): + name, marks = input().split() # Input name and marks + marks = int(marks) + students.append((name, marks)) + total_marks += marks + +stud_above_50 = [] +for x in students: + if x[1] > 50: + stud_above_50.append(x[0]) +stud_above_50.sort() +avg_marks = total_marks/n + +print(f"Total students: {n}") +print(f"Students who scored above 50: {stud_above_50}") +print(f"Average marks: {avg_marks:.2f}") +``` + +## Public Test Cases + +### Test Case 1: +**Input:** +``` +5 +John 60 +Alice 40 +Bob 80 +Charlie 55 +David 45 +``` +**Expected Output:** +``` +Total Students: 5 +Students who scored above 50: ['Bob', 'Charlie', 'John'] +Average marks: 56.00 +``` + +### Test Case 2: +**Input:** +``` +3 +Eve 30 +Frank 60 +Grace 90 +``` +**Expected Output:** +``` +Total students: 3 +Students who scored above 50: ['Frank', 'Grace'] +Average marks: 60.00 +``` + +## Private Test Cases + +### Test Case 1: +**Input:** +``` +4 +Adam 45 +Ben 55 +Cathy 65 +Diana 75 +``` +**Expected Output:** +``` +Total students: 4 +Students who scored above 50: ['Ben', 'Cathy', 'Diana'] +Average marks: 60.00 +``` + +### Test Case 2: +**Input:** +``` +6 +Liam 60 +Nina 80 +Ivy 100 +Jack 95 +Maya 55 +Oscar 50 +``` +**Expected Output:** +``` +Total students: 6 +Students who scored above 50: ['Ivy', 'Jack', 'Liam', 'Maya', 'Nina'] +Average marks: 73.33 +``` + +### Test Case 3: +**Input:** +``` +2 +Olivia 20 +Paul 45 +``` +**Expected Output:** +``` +Total students: 2 +Students who scored above 50: [] +Average marks: 32.50 +``` + +### Test Case 4: +**Input:** +``` +7 +Fiona 85 +Diana 60 +Aaron 80 +Ethan 45 +Charlie 40 +George 70 +Bella 55 +``` +**Expected Output:** +``` +Total students: 7 +Students who scored above 50: ['Aaron', 'Bella', 'Diana', 'Fiona', 'George'] +Average marks: 62.14 +``` diff --git a/question4.md b/question4.md new file mode 100644 index 0000000..b3bcb17 --- /dev/null +++ b/question4.md @@ -0,0 +1,168 @@ +## Problem Statement + +You are required to implement a simple banking system. The bank details are stored in a **dictionary** named **BANK**, where the keys are `account number` and values are corresponding `account balance`. The banking system should consist of the following functions: +1. `create_account(acc_num)`: Creates a new account with the given acc_num and initializes the balance to 0. Returns "Account created successfully" if the account was created, or "Account Number already exists" if the account already exists +2. `check_balance(acc_num)`: Takes the `account number` as argument and returns the balance in that account. +3. `deposit(acc_num, amount)`: Takes `account number` and `amount` to be deposited as arguments and deposite the specified `amount` in the given `account number`. Return "Deposited Amount: ____, New Balance: ____" +4. `withdraw(acc_num, amount)`: Takes `account number` and amount to be withdrawn as arguments and withdraws the specified `amount` from the given `account_number`. Returns "Withdrew Amount: ____, New balance: ____" or "Insufficient Balance" if the withdrawal amount exceeds the balance. +5. `Transaction(from_acc, to_acc, amount)`: Takes 3 arguments. First two are `account numbers` and third is the `amount`. The function transfers the amount specified from `from_acc` to `to_acc`. Returns "Transaction successfull" or "Insufficient Balance" if the `from_acc` does not have enough funds. + +### Additional Instructions: +- For all functions, if the `acc_num` does not exist in the system, return "Account does not exist". +- If there is an invalid entry for `account number` or `amount`, return "Invalid Input".* + +*The system must keep track of accounts in a dictionary where keys are account numbers and values are account balances. Each function must handle input validation.* + +### Constraints: +- Account number will always be a positive integer greater than or equal to 10000. +- Amount to be deposited or withdrawn will always greater than or equal to 0. +- Balance will be a floating-poin number. +- The account number will be unique. + +## Solution + +```python +BANK = {} + +def create_account(acc_num): + if acc_num in BANK: + return "Account Number already exists" + BANK[acc_num] = 0.0 + return "Account created successfully" + +def check_balance(acc_num): + if acc_num not in BANK: + return "Account does not exist" + return BANK[acc_num] + +def deposit(acc_num, amount): + if acc_num not in BANK: + return "Account does not exist" + if amount <= 0: + return "Invalid Input" + BANK[acc_num] += amount + return f"Deposited Amount: {amount}, New Balance: {BANK[acc_num]}" + +def withdraw(acc_num, amount): + if acc_num not in BANK: + return "Account does not exist" + if amount <= 0: + return "Invalid Input" + if BANK[acc_num] < amount: + return "Insufficient Balance" + BANK[acc_num] -= amount + return f"Withdrew Amount: {amount}, New Balance: {BANK[acc_num]}" + +def transaction(from_acc, to_acc, amount): + if from_acc not in BANK or to_acc not in BANK: + return "Account does not exist" + if amount <= 0: + return "Invalid Input" + if BANK[from_acc] < amount: + return "Insufficient Balance" + BANK[from_acc] -= amount + BANK[to_acc] += amount + return f"Transaction successfull" +``` + +## Public Test Cases + +### Test Case 1: +**Input:** +``` +print(create_account(10001)) +print(check_balance(10001)) +print(deposit(10001, 500.0)) +print(withdraw(10001, 200.0)) +print(withdraw(10001, 400.0)) +``` +**Expected Output:** +``` +Account created successfully +0.0 +Deposited Amount: 500.0, New Balance: 500.0 +Withdrew Amount: 200.0, New Balance: 300.0 +Insufficient Balance +``` + +### Test Case 2: +**Input:** +``` +print(create_account(10002)) +print(deposit(10002, 1000.0)) +print(withdraw(10002, 500.0)) +print(transaction(10002, 10001, 200.0)) +print(check_balance(10002)) +print(check_balance(10001)) +``` +**Expected Output:** +``` +Account created successfully +Deposited Amount: 1000.0, New Balance: 1000.0 +Withdrew Amount: 500.0, New Balance: 500.0 +Transaction successfull +300.0 +500.0 +``` + +## Private Test Cases + +### Test Case 1: +**Input:** +``` +print(create_account(10003)) +print(deposit(10003, 1500.0)) +print(withdraw(10003, 300.0)) +print(transaction(10003, 10001, 200.0)) +print(transaction(10001, 10003, 200.0)) +print(check_balance(10003)) +``` +**Expected Output:** +``` +Account created successfully +Deposited Amount: 1500.0, New Balance: 1500.0 +Withdrew Amount: 300.0, New Balance: 1200.0 +Transaction successfull +Transaction successfull +1200.0 +``` + +### Test Case 2: +**Input:** +``` +print(create_account(10004)) +print(deposit(10004, 800.0)) +print(withdraw(10004, 500.0)) +print(withdraw(10004, 400.0)) +print(transaction(10001, 10004, 200.0)) +print(check_balance(10004)) +``` +**Expected Output:** +``` +Account created successfully +Deposited Amount: 800.0, New Balance: 800.0 +Withdrew Amount: 500.0, New Balance: 300.0 +Insufficient Balance +Transaction successfull +500.0 +``` + +### Test Case 3: +**Input:** +``` +print(create_account(10005)) +print(deposit(10005, 2000.0)) +print(withdraw(10005, 300.0)) +print(transaction(10005, 10001, 1800.0)) +print(transaction(10001, 10005, 100.0)) +print(check_balance(10005)) +``` +**Expected Output:** +``` +Account created successfully +Deposited Amount: 2000.0, New Balance: 2000.0 +Withdrew Amount: 300.0, New Balance: 1700.0 +Insufficient balance +Transaction successfull +1800.0 +``` \ No newline at end of file