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 python, Modify your mapper to count the number of occurrences of each character (including punctuation...
In python, Modify your mapper to count the number of occurrences of each character (including punctuation marks) in the file. Practice the given tasks in Jupyter notebook first before running them on AWS. If your program fails, check out stderr log file for information about the error. 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...
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')...
How can i modify my c code so that each number stored in the array is...
How can i modify my c code so that each number stored in the array is not the array index but the value of the array index converted to radians. I have already made a function for this converion above main(). Below is the code: #include <stdio.h> #include <stdlib.h> #include <math.h> float Deg2Rad (float degrees) { // Calculate & return value float Result; Result = ((M_PI*degrees)/180.0); return (Result); } int main(void) { // Declare variables int Array[90]; int i; //...
In python, 1- Modify your mapper to count words after removing punctuation marks during mapping. Practice...
In python, 1- Modify your mapper to count words after removing punctuation marks during mapping. Practice the given tasks in Jupyter notebook first before running them on AWS. If your program fails, check out stderr log file for information about the error. 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...
In Unity with C#, I need to implement a dice simulator in my character generator(D&D). I...
In Unity with C#, I need to implement a dice simulator in my character generator(D&D). I have 6 sides of UI images(2D). With my code, I am able to roll and get the number in my console text(randomly). But, I need help for rolling 5 dices, not the 1 dice. Also, I need to add all the three highest rolls with 5 dices and get a result in my display. Ex) 1. Players must roll 5d6 and add the three...
Modify the following C++ program to count the number of correct and incorrect responses typed by...
Modify the following C++ program to count the number of correct and incorrect responses typed by the student. After the student types 5 answers, your program should calculate the percentage of correct responses. If the percentage is lower than 75 percent, your program should print “Please ask for extra help” and then terminate. If the percentage is 75 percent or higher, your program should print “Good work!” and then terminate. The program is as follows: #include<iostream> #include<iomanip> #include<cstdlib> #include<time.h> using...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT