Question

In: Computer Science

Suppose that you have a program that can read through all posts within a given time...

  1. Suppose that you have a program that can read through all posts within a given time period (e.g. the last 7 days) on a social network web site (e.g., Facebook), and generate a list of pairs (ui, pi) representing texts in all posts found, where i=1, 2, ..., and ui and pi represent the author username and the text content of the i-th post found, respectively. Describe an algorithm that outputs the list of usernames in the decreasing order of their total numbers of words posted among the posts described above. Analyze the running time of your algorithm. You may assume that the input of your algorithm is two arrays U and P that store the pairs (ui, pi) as defined above.

Please answer differently than solution already on chegg, I would like explanations to steps. Thank you.

Solutions

Expert Solution

Algorithm for above will be :-

1) Funtion which takes two input , one is the author array , and other is the posts array.

2)This functions returns the authors array , in the decreasing order of their words in the post.

3)The function will contain :-

  • Call another function , passing the posts array.
    • In this function , start a loop over the posts array.
    • In each iteration calculate the number of words in each post[i] ( i being the iterator).
    • You can easily calculate the nmber of words using the stringstream (this is present in C++).
    • Create another array postlength , which will contain the number of words in each post[i].
    • Return the postlength array from this function.
  • Accept the postlength array returned from above funtion.
  • Now, you will have two arrays , author array(containing the authors name) and secondly postlength array(contianing the number of words in each posts).
  • Now, initialise a map , which will contain author's name as key and number of words as value.
  • Now start a loop over the author's array. In each itereation you will do :-
    • If the author[i] ( i being the iterator) is present in the map's key , just fetch the value from the map , corresponding to the author[i] and add the postlenght[i] value to the value retrieved. Now insert new value with the author[i] in the map.
    • If the author[i] is not present in the map's key, insert the pair(author[i],postlength[i]) in the map.
  • Now sort the map according to the values, and return the map's key by storing them in an array.
  • For sorting the map you can use the insertion sort , where just create a new array and insert the map's key according to its value in the map.
  • The time complexity willl be O(n^2) because of the time complexity of insertion sort which will be maximum and for other loops it will be O(n) as we are iterating over the array once only in each loops . So time complexity is max of all time complexity which will be O(n^2).

This solution is according to me , and if you need any help , just comment , I will be happy to help.


Related Solutions

Write a JAVA program read a number within the range of 1 through 1000 to display...
Write a JAVA program read a number within the range of 1 through 1000 to display the Roman numeral version of that number. Include screenshot of the output
Draw the cache tables and the state of all bits within them. Suppose you have a...
Draw the cache tables and the state of all bits within them. Suppose you have a 16 byte cache with 2 byte long cachelines that is 2-way set associative and write-back. Further assume that prior to processing any read/write requests the state of memory is M[a] = a, or another words, the byte at address 0 is 0, address 1 is 1, address 2 is 2, and so on. Assume an *8 bit* long address. Along with depicting the cache...
How can you apply some of the concepts regarding team work this week through writing posts?...
How can you apply some of the concepts regarding team work this week through writing posts? How can other students apply team building and critical thinking when posting? Minimum 100 Words
I believe you have all been through the D.A.R.E. program in high school and college orientations...
I believe you have all been through the D.A.R.E. program in high school and college orientations with the warnings of alcohol, what 2 things stood out
Have you ever signed a contract that you may not have, had you really read through...
Have you ever signed a contract that you may not have, had you really read through the entire document and understood all of the terms (e.g. a gym membership, magazine subscription, or cell phone service?) What happened and what might you do differently next time?
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
/ READ BEFORE YOU START: // You are given a partially completed program which consist of...
/ READ BEFORE YOU START: // You are given a partially completed program which consist of a class 'Employee' defined in employee.h // The definitions of class member functions are to be filled in employee.cpp (this file). // You should start completing the program from from Q1. Question numbers are given around line 24. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow...
Suppose you have a production technology that can be characterized by a learning curve. Every time...
Suppose you have a production technology that can be characterized by a learning curve. Every time you increase production by 1 unit, your costs decrease by $6. The first unit costs you $64 to produce. 1)You estimate you can win another project for two more units. Have your returns diminished? Why or why not? 2)If you receive a request for 4 units, what is your break-even price?
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
Read “It's Time for Principles-Based Accounting Ethics” which can be accessed through the DeVry online library....
Read “It's Time for Principles-Based Accounting Ethics” which can be accessed through the DeVry online library. In 3-4 pages (12-pt type, double-spaced) answer the following questions: Do you agree with the authors that a code of ethics should do more than establish minimum acceptable standards? Why or why not? Describe the five cardinal virtues of professional accountants that the article’s authors discuss. We’ve talked about rules-based versus principles-based accounting standards. Should we have rules-based ethics standards? Why or why not?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT