Here are several Input/Output (I/O) type Python questions, designed in the one-function format, suitable for students of the IIT Madras BS in Data Science program. These emphasize reading from standard input or files and writing to standard output.
🧠 1. Sum of Even Numbers
Function Name: sum_even_numbers()
Description:
Read a list of integers from input, and print the sum of all even numbers.
Input Format:
-
One line of space-separated integers.
Output Format:
-
A single integer: sum of even numbers.
pythondef sum_even_numbers():
# Your code here
📂 2. Count Words from a File
Function Name: count_words(filename)
Description:
Given a text file path, read the contents and count the number of words in it.
Input:
-
filename
: a string representing the file path.
Output:
-
Print the total word count.
pythondef count_words(filename):
# Your code here
🔄 3. Filter by Threshold
Function Name: filter_above_threshold()
Description:
Read a list of numbers and a threshold value from input. Print only the numbers above the threshold.
Input Format:
-
First line: space-separated numbers
-
Second line: threshold value (float or int)
Output:
-
Space-separated numbers above threshold.
pythondef filter_above_threshold():
# Your code here
📊 4. CSV Column Average
Function Name: average_column(filename, column_name)
Description:
Read a CSV file and calculate the average of the values in the given column.
Input:
-
filename
: path to the CSV file -
column_name
: name of the column
Output:
-
Print the average of that column.
pythondef average_column(filename, column_name):
# Your code here
🔢 5. Reverse Input Lines
Function Name: reverse_lines()
Description:
Read multiple lines from standard input until an empty line is encountered. Print the lines in reverse order.
Input Format:
-
Several lines of text (empty line to stop)
Output Format:
-
Same lines printed in reverse order.
pythondef reverse_lines():
# Your code here
Would you like the solutions or test cases for these as well?
You said:
ChatGPT said:
Sure! Here are more one-function I/O-type Python questions tailored for IIT Madras BS in Data Science students—simple but practical, reinforcing core Python skills:
🧮 6. Matrix Sum
Function Name: matrix_sum()
Description:
Read a matrix of integers from standard input. Print the sum of all its elements.
Input Format:
-
First line: two integers
R
andC
(rows and columns) -
Next
R
lines:C
space-separated integers
Output Format:
-
A single integer: the matrix sum
python
def matrix_sum(): # Your code here
📈 7. Max Score by Student
Function Name: highest_score()
Description:
Each line contains a student name and score (e.g., "Ram 83"). Find and print the student with the highest score.
Input Format:
-
Multiple lines of "Name Score" (until empty line)
Output Format:
-
"Name Score" of top scorer
pythondef highest_score():
# Your code here
🕵️ 8. Find Substring in File
Function Name: find_occurrences(filename, keyword)
Description:
Given a text file and a keyword, count how many times the keyword appears.
Input:
-
filename
: file path -
keyword
: word to search
Output:
-
Print number of occurrences
pythondef find_occurrences(filename, keyword):
# Your code here
🔡 9. Count Vowels
Function Name: count_vowels()
Description:
Read a sentence from input and count the total number of vowels.
Input Format:
-
A single line of text
Output Format:
-
Number of vowels
pythondef count_vowels():
# Your code here
🗃️ 10. Word Frequency
Function Name: word_frequency()
Description:
Read multiple lines of text from input. Print each unique word and its frequency.
Input Format:
-
Multiple lines of text (end input with empty line)
Output Format:
-
Each word with its frequency in any order
pythondef word_frequency():
# Your code here
🧾 11. Line Length Checker
Function Name: check_line_lengths(filename)
Description:
Read a file and print all lines whose length exceeds 80 characters.
Input:
-
filename
: path to a text file
Output:
-
Lines with length > 80
pythondef check_line_lengths(filename):
# Your code here
🔢 12. Extract Numbers from Text
Function Name: extract_numbers()
Description:
Read a line of text and print all numbers found in it.
Input Format:
-
A single line containing text and numbers
Output Format:
-
All numbers (space-separated)