UNIT-II
Rubric: 10m Code + 5m Viva +
5m Record
EASY
10 Marks
Session: 59:59
Bubblewrap Sandbox Active
Problem Statement
Implement the Mapper and Reducer aggregation logic for a simulated Hadoop MapReduce task. Given a stream of input lines, your program should normalize all words to lowercase, remove punctuation, and print each distinct word followed by its overall occurrence count in alphabetical order.
This experiment simulates Phase 1 of Hadoop MapReduce: Mapping tokens into key-value pairs (word, 1) and Shuffling/Reducing them into aggregate counts (word, total).
Input Format
Line 1 contains an integer N representing the number of sentences.
The following N lines contain textual strings.
Output Format
Print each unique word and its frequency separated by a single space, sorted alphabetically line by line.
Constraints
1 <= N <= 100
Total words <= 10^4
Input strings consist of alphanumeric characters and spaces.
Status
Lang
Score
Time
Submitted
No prior submissions for this problem.
Ready
Click Run Code to evaluate against sample test
cases, or Submit Solution for full evaluation.
No compiler or execution errors recorded.
#include <stdio.h>
// C implementation for Tokenizer
int main() {
return 0;
}
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
int main() {
int n;
if (!(cin >> n)) return 0;
map<string, int> counts;
string word;
while (cin >> word) {
for (char &c : word) c = tolower(c);
counts[word]++;
}
for (auto const& [w, cnt] : counts) {
cout << w << " " << cnt << endl;
}
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if (!sc.hasNextInt()) return;
int n = sc.nextInt();
sc.nextLine();
Map<String, Integer> map = new TreeMap<>();
for (int i = 0; i < n; i++) {
String line = sc.nextLine();
String[] words = line.trim().split("\\s+");
for (String w : words) {
if (w.isEmpty()) continue;
w = w.toLowerCase();
map.put(w, map.getOrDefault(w, 0) + 1);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
import sys
from collections import Counter
def main():
lines = sys.stdin.read().splitlines()
if not lines:
return
n = int(lines[0].strip())
words = []
for line in lines[1:n+1]:
words.extend([w.lower() for w in line.strip().split() if w.strip()])
counts = Counter(words)
for w in sorted(counts.keys()):
print(f"{w} {counts[w]}")
if __name__ == "__main__":
main()