Question

In: Computer Science

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 = 1
       print ("%s\t%d" % (key,value)) #for each word generate 'word TAB 1' line

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

You can simply copy-paste the below code in Jupyter notebook.

import sys

sys.path.append('.')

# Take an empty dictionary for counting
count_dict = {}
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: # Loop through each word
for ch in key: # Loop through each character in the word
if ch in count_dict: # Check if it already exists
value = count_dict[ch]
count_dict[ch] = value + 1
else: # Else,, create that character in the dictionary and add count as 1
count_dict.update({ch: 1})

for ch, count in count_dict.items():
print('%s\t%d' % (ch, count))

===============================

SAMPLE INPUT:

hello , hi ! good ! morning!
bye!!
how are you?
I'am fine.!

CODE:

OUTPUT


Related Solutions

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...
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 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')...
Write a Python program to count occurrences of items (and retrieve the most 3 or least...
Write a Python program to count occurrences of items (and retrieve the most 3 or least 3 words). Write a Python program to sort a dictionary by keys or values in ascending or descending order by 2 methods.
The question asks to modify this for an unspecified number of months. This is the python...
The question asks to modify this for an unspecified number of months. This is the python code I have, please advise next steps def main(): months = range(1,13) rain_month = 0.0 rain_per_month = [] month = 0 count = 1 amount = 0.0 total_rain = 0.0 average_rain = 0.0 for month in months: print("Rain for month "+str(count)+":") rain_amount = get_rainfall() rain_per_month.append(rain_amount) count = count + 1 total_rain = calc_total_rain(rain_per_month) average_rain = rain_average(total_rain) print("Total Rain:", total_rain) print("Average Rain:", average_rain) def get_rainfall():...
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...
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 + "...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a',...
Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a', 'b', 'c','e','j']; list2 = ['a', 'c', 'd', 'b','e','z']; {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'j': 0} How do I get to this to work without the Collections counter?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT