473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how can you create a histogram which outputs how many times a character appeared in a

create a new class on JavaScript called “MyString” that computes a histogram representing the number of times each letter of the alphabet is in a given text.
Nov 30 '17 #1
1 2434
gits
5,390 Expert Mod 4TB
well - it looks like an assignment but i guess we can answer it now after a few months to help those that might find it through a search or such. a pretty quick and straight forward solution to that could look like this:

Expand|Select|Wrap|Line Numbers
  1. class TextHandler {
  2.  
  3.     constructor(txt) {
  4.  
  5.         if (typeof txt !== 'string') {
  6.             throw 'cant create object - param has to be a string';
  7.         }
  8.  
  9.         this.txt = txt;
  10.     }
  11.  
  12.     __initList() {
  13.  
  14.         return {
  15.             a:0, b:0, c:0, d:0, e:0, f:0, g:0,
  16.             h:0, i:0, j:0, k:0, l:0, m:0, n:0,
  17.             o:0, p:0, q:0, r:0, s:0, t:0, u:0,
  18.             v:0, w:0, x:0, y:0, z:0
  19.         };
  20.     }
  21.  
  22.     get histogram() {
  23.  
  24.         let charHistogram = this.__initList();
  25.  
  26.         for (let i in charHistogram) {
  27.  
  28.             let re = new RegExp('([' + i + '])', 'gi');
  29.             let ma = this.txt.match(re);
  30.  
  31.             charHistogram[i] = ma !== null ? ma.length : 0;
  32.         }
  33.  
  34.         return charHistogram;
  35.     }
  36. }
it should fulfill the minimum requirements that can be read from the post - and it should be adapted to what the real! requirements would be.

PS: usage could look like:
Expand|Select|Wrap|Line Numbers
  1. new TextHandler('foobar').histogram;
PPS: since i now was interested :) it appears that such a task can be optimized much by using another approach that simply reduces the needed operations - so during every loop-step we could reduce the text by just replacing the already checked characters like that:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     get histogram() {
  3.  
  4.         let charHistogram = this.__initList();
  5.         let txt = this.txt;
  6.  
  7.         for (let i in charHistogram) {
  8.  
  9.             let le = txt.length;
  10.             let re = new RegExp('([' + i + '])', 'gi');
  11.  
  12.             txt = txt.replace(re, '');
  13.  
  14.             charHistogram[i] = le - txt.length;
  15.         }
  16.  
  17.         return charHistogram;
  18.     }
  19.  
that made creating the histogram much faster with longer texts - i roughly measured 5x faster execution times with that method (text used for measurement had 21793 characters). note that this consumes the text we use for that - thats why i dont operate on it but assign it to a local variable - and the nice side effect would be that we can have an easy access to the residue at the end that we didnt count in - in the above case all characters like special chars or numbers and such (we count only whats in the initial list).

PPPS: could even be more optimized - probably slightly only but still - by ordering our counting map according to the common frequency of letters in a text in a language. i ordered the list according to here:

https://en.wikipedia.org/wiki/Letter_frequency

that squeezed out another few milliseconds per pass - was only noticable when i increased the text size though to 130758 characters. got roughly 15% faster execution times with it by just starting with the letter e and t instead of a and b.
Mar 15 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mark Fisher | last post by:
I have a Java desktop GUI application that the user can run multiple times. In order to keep one instance of the application distinct from another, I'd like to put the instance number of the...
6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
1
by: Marcin Floryan | last post by:
Hello! My question regards opening (and re-opening) Form and the Load event. I have a main form (frmMain) and I also have a data form (frmData). In the main form I have created: Private...
4
by: Arpan | last post by:
Consider the following sentence: Peter and I play together and also go to the same school and both of us are in class V. How do I find out how many times the string "and" has appeared in the...
17
by: keith | last post by:
I am trying to get a exact count of different distinct entries in an Access column. At first, I was trying to work with three columns, but I've narrowed it down to one to simplify it. I've searched...
2
by: Steve | last post by:
A main form contains ten textboxes and ten subforms. All ten subforms have the same query (Query2) for their recordsource. Query2 is based on a query (Query1). Each subform is linked to a different...
1
by: Joey Martin | last post by:
I've done this before but cannot remember how. I have a field (FIELDNAME) in a sql2k environment. I just need to know how many times an "_" appears in that field. Pretty simple, but I cannot...
1
by: GS | last post by:
the windows form application works but the eventhandler for binding source postionChanged got executed too many times: 12 times from saveitemclicked for the data navigator many time from additem ...
1
by: dinesh1440 | last post by:
Hi, I want to retrieve how many times a job runs each week. I checked in sysschedules and syjobschedules tables under msdb database. But I could not find a way to retreive information. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.