Question

In: Computer Science

I need to,  Modify my mapper to count the number of occurrences of each character (including punctuation...

I need to,  Modify my mapper to count the number of occurrences of each character (including punctuation marks) in the file.

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 'word TAB 1' line

Solutions

Expert Solution

Assuming you need the number of occurrences for each character (not word). We use python's dictionary data structure to hold the unique characters and their counts. Iterate through each character in the line and add them into the dictionary with count 1 if not present in the dictionary. If the character is already present, increment its count by 1. At the end of the loop, you will have the total number of occurrences for all unique characters in the dictionary.

count_dict ={} # Initialize an empty dictionary. If you need to process multiple lines, do this inside a loop
for char in line: # Iterate through each character in the line
if char in count_dict: # If the character is already present in the dictionary, increment its value by 1
count_dict[char] = count_dict[char] + 1
else: # Else if it is a new character, add it into the dictionary with value as 1
count_dict[char] = 1

# Iterate through the dictionary and print the key- value pairs. Character is the key & count is the value in the dictionary

for key,value in count_dict.items():
print("%s\t%d" % (key,value))


Related Solutions

In this program: ================================================================== /* Program to count number of occurrences of a given string in...
In this program: ================================================================== /* Program to count number of occurrences of a given string in original string*/ #include <iostream> #include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() { const int SIZE = 40; char str[SIZE]; char str1[SIZE]; char searchString[SIZE]; int n; int l1, l2; int count = 0; printf("Enter a sentence: \n"); fgets(str,SIZE,stdin); printf("Enter a search word: \n"); fgets(searchString,SIZE,stdin); if (str1[strlen(str1) - 1] == '\n') { str1[strlen(str1)-1] = '\0'; } if (str[strlen(str) - 1] == '\n')...
This is my coded "multipath adventure" that I created. Now I have to modify it to...
This is my coded "multipath adventure" that I created. Now I have to modify it to include loops but I am really struggling to do so. Below are the instructions and my current code that needs to be modified. Thank you in advance! You must have at least 1 for loop in your code The for loop must actually be used as a loop, and it must have real effect in your program (it is theoretically possible to use the...
9. Modify the quicksort and mergesort programs we learnt during the class to count the number...
9. Modify the quicksort and mergesort programs we learnt during the class to count the number of element comparisons for the two sorting methods. Use the following test drive to test them. public class Sorts {    int numComparisions = 0;    public void quicksort(int [] x, int l, int h)    { // your modifies codes go here    }    public void mergesort(int [] x, int l, int h)    { // your modifies codes go here   ...
Modify the following code to count the number of recursive calls made for the Manhattan-path problem...
Modify the following code to count the number of recursive calls made for the Manhattan-path problem we studied earlier. Next, modify to include stored values and see how that reduces the number of calls made. public class ManhattanWithCallCount { public static void main (String[] argv) { // Test case 1: go from (2,2) to (0,0) int r = 2; int c = 2; int n = countPaths (r, c); System.out.println ("r=" + r + " c=" + c + "...
please check my assignment for punctuation and grammatical errors. I really struggle with that. please copy...
please check my assignment for punctuation and grammatical errors. I really struggle with that. please copy and paste it and do the corrections for me. Currently, there are chronic shortages of transplantable human organs. Waiting lists are long, and many of those waiting die before an organ becomes available. Would it be more efficient than current arrangements if we were to allow an open market for buying and selling of human organs to develop? Be sure to consider how scarce...
I am playing a game of Dungeon and Dragons with my family. My son’s character must...
I am playing a game of Dungeon and Dragons with my family. My son’s character must roll a critical hit, (19 or 20) on a 20 sided die. Without a critical hit, he cannot do enough damage to the troll. The troll will regenerate and kill me on the next turn. In addition to getting a high roll for hit chance, my son’s character must also get a high roll for damage. His weapon is a great sword and does...
Q. Given a document with 32 distinct characters, with equal number of occurrences of each, what...
Q. Given a document with 32 distinct characters, with equal number of occurrences of each, what would be the ratio of the length of the Huffman coded document to the original, if each character in the input was coded in 8 bits? Show your analysis.
i need a conclusion for my paper on ADHD. I need this asap please have to...
i need a conclusion for my paper on ADHD. I need this asap please have to turn my paper in one hour!!!! thank you            In the recent times, ADHD is being seen in the light of cerebral dysfunction with too much focus on clinical treatment using medication at the same time keeping psychotherapy at bay which shows a dangerous trend. Psychotherapy even though cannot explain the cause of the condition, but it can help the children in coping his...
a) The number of voices I hear at 7am is 12000 at 9am the count is...
a) The number of voices I hear at 7am is 12000 at 9am the count is 36000, if the number of voices grows exponentially, find the time it takes to reach 81000 voices? b) assume one voice tells me to invest 20000 in a south american bank where the interest is compounded continiously, after 6 years i have 24000 dollars, what must the interest rate have been?
Use pd.crosstab() to count the number of regions of each cover type there are for each...
Use pd.crosstab() to count the number of regions of each cover type there are for each of the 40 soil types. Pass this function the Cover_Type column as its first argument and the Soil_Type column as the second argument. Store the results in a DataFrame named ct_by_st and then display this DataFrame. soil = np.unique(fc['Soil_Type']) palette = ['orchid', 'lightcoral', 'orange', 'gold', 'lightgreen', 'deepskyblue', 'cornflowerblue'] Perform the following steps in a single cell: 1. Start by converting the count information into...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT