Practice Exercise 10
Practice Exercise 10
CS110 - Week 10
Topic: File handling, Searching, Sorting
Note: For all three questions, take inputs from the input files provided with the
questions. For Q1 and Q2, print the outputs on the console. For Q3, print the
output to an output file. The answers to the questions are hidden from you. You
may leave the lab only after a TA verifies the outputs for all three questions. No
need to upload code to the server for this practice exercise.
Question 1
Create a program to validate a completed 9x9 Sudoku grid. A valid Sudoku grid
must satisfy the following rules:
Input format:
• The input is a 9x9 grid of integers separated by spaces. Each integer ranges
from 1 to 9.
• The grid will be read from an input text file.
Output format:
1
• Print 1 if the grid satisfies all the rules.
Take input for your code from one of the input files at a time and check if the
output of your code matches the content of the respective output file.
Question 2
You are given a list of envelopes represented as a 2D array of integers, where en-
velopes[i] = [wi , hi ] denotes the width and height of the ith envelope.
An envelope [wi , hi ] can fit into another envelope [wj , hj ] if and only if both:
• wi < wj
• hi < hj
Your task is to find the maximum number of envelopes that can be nested inside
each other (like a Russian Doll).
Input format:
• Each line of the file contains two integers separated by a space, representing
the width wi and height hi of an envelope.
Output format:
• Print a single integer, the maximum number of envelopes that can be nested.
Constraint: 1 ≤ wi , hi ≤ 105
Example:
Input:
54
64
67
23
Output: 3
Explanation: The maximum nesting sequence is [2, 3] → [5, 4] → [6, 7].
2
Question 3
Write a C program that reads a list of words, written in lowercase, from an
input text file and performs the following tasks:
• Ignores blank lines and extra spaces while processing the input.
Input format:
• The input file contains a set of words, in lowercase, each on a new line.
• Words can have extra spaces or blank lines. These should be ignored.
Output format:
Example:
Input:
zebra
dog
cat
elephant
dog
ant
Output:
ant
cat
dog
elephant
zebra