####################
# MIN_MAX INTEGER
##################

## MAPPER

#!/usr/bin/env python

import sys

for line in sys.stdin:
    line = line.strip()
    # Split the line into numbers. Assuming numbers are space-separated.
    numbers_str = line.split()
    
    for num_str in numbers_str:
        try:
            num = int(num_str)
            # Output each number twice, once with a 'min' tag and once with a 'max' tag.
            # This allows the reducer to process both min and max for each number.
            print '%s\t%s' % ('min', num)
            print '%s\t%s' % ('max', num)
        except ValueError:
            # Skip if it's not a valid integer
            continue


### REDUCER

#!/usr/bin/env python

import sys

current_key = None
current_min = None
current_max = None

for line in sys.stdin:
    line = line.strip()
    key, value_str = line.split('\t', 1)

    try:
        value = int(value_str)
    except ValueError:
        continue # Skip if value is not an integer

    if current_key == key:
        if key == 'min':
            if current_min is None or value < current_min:
                current_min = value
        elif key == 'max':
            if current_max is None or value > current_max:
                current_max = value
    else:
        # If we encounter a new key, print the results for the previous key
        if current_key == 'min' and current_min is not None:
            print '%s\t%s' % (current_key, current_min)
        elif current_key == 'max' and current_max is not None:
            print '%s\t%s' % (current_key, current_max)
        
        # Reset for the new key
        current_key = key
        if key == 'min':
            current_min = value
            current_max = None # Reset max as we are processing 'min'
        elif key == 'max':
            current_max = value
            current_min = None # Reset min as we are processing 'max'

# Don't forget to output the last key's results
if current_key == 'min' and current_min is not None:
    print '%s\t%s' % (current_key, current_min)
elif current_key == 'max' and current_max is not None:
    print '%s\t%s' % (current_key, current_max)

## INPUT.TXT 

10 2 5 80 15
30 -5 99 1
7 42


## COMMAND

$ chmod +x minmax_reducer.py
$ hdfs dfs -mkdir /minmax_input
$ hdfs dfs -put input.txt /minmax_input
$ hdfs dfs -ls /minmax_input

$ hadoop jar /usr/lib/hadoop-mapreduce/hadoop-streaming.jar \
   -input /minmax_input \
   -output /minmax_output \
   -mapper minmax_mapper.py \
   -reducer minmax_reducer.py \
   -file minmax_mapper.py \
   -file minmax_reducer.py

hdfs dfs -cat /minmax_output/part-00000

