Question

Modify your mapper to count the number of occurrences of each character (including punctuation marks) in...

Modify your mapper to count the number of occurrences of each character (including punctuation marks) in the file.

#!/usr/bin/env python

#the above just indicates to use python to intepret this file

#This mapper code will input a line of text and output <word, 1> #

import sys

sys.path.append('.')

for line in sys.stdin:

line = line.strip() #trim spaces from beginning and end

keys = line.split() #split line by space

for key in keys:

value = 1

print ("%s\t%d" % (key,value)) #for each word generate 'word TAB 1' line

Homework Answers

Answer #1

If you have any doubts, please give me comment...

#!/usr/bin/env python

inp_fname = input("Enter input filename: ")

fp = open(inp_fname)

occurances = {}

for line in fp.readlines():

    line = line.strip() #trim spaces from beginning and end

    for ch in line:

        if ch not in occurances:

            occurances[ch] = 0

        occurances[ch] += 1

fp.close()

print("Char\tCount")

for ch in occurances:

    print(ch+"\t"+str(occurances[ch]))    

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Modify your mapper to count words after removing punctuation marks during mapping. Mapper code below. #!/usr/bin/env...
Modify your mapper to count words after removing punctuation marks during mapping. Mapper code below. #!/usr/bin/env python #the above just indicates to use python to intepret this file #This mapper code will input a line of text and output <word, 1> # import sys sys.path.append('.') for line in sys.stdin: line = line.strip() #trim spaces from beginning and end keys = line.split() #split line by space for key in keys: value = 1 print ("%s\t%d" % (key,value)) #for each word generate...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...