## i want to count the number of occurrences of certain values of my TagName column

i want to count the number of occurrences of different sensor rows in SQL, but i seem to be doing it wrong and seemingly i am not visualizing it correctly.

if i was doing this in pseudocode in a c style language i would do it like this:

FOR i in range(taglist[i]):          print(taglist[i], count(taglist[i]) ) 

i have been trying this:

Select Count(a) FROM (     SELECT Distinct [TagName]          FROM [A2ALMDB].[dbo].[AlarmMaster]         where (TagName like '%Sensor%' or GroupName like'%Sensors%')     ) a 

it returns ’66’, but i want it to return the count of each of the distinct tagnames that are returned in Select A.

Can anyone help me with how i should be trying to get all the counts of my different sensor occurrences to total instead of a count of all the distinct tagnames?

thanks for the help!

## How to compute the number of occurrences of a cron schedule over a time span?

For example, given a cron schedule such as: 0 * 1 7 6 and a time span, like the next year, can you directly compute the number of times the schedule will fire? It can be brute forced pretty trivially but I’d like to use this to sort a list so I’d rather not just brute force it. It’s pretty easy to compute the trivial cron patterns:

fn('* * * * *', 1 day) -> 60 * 24 fn('0,30 * * * *', 1 day) -> 2 * 24 

It’s easy up until the calendar gets involved with different length months and then you have to worry about days or the week coinciding with days of the month.

## Theoretical lower bound of finding number of occurrences of a target integer in a sorted array

Given a sorted array of integers and a target integer, let’s say we want to find the number of occurrences of the target integer.

It is well-known that a binary search can give time complexity $$O(\lg n)$$ where $$n$$ is the size of the array. For example, given an array $$[1,2,3,3,4,5]$$ and a target $$3,$$ the algorithm should return $$2$$ since there are two copies of $$3$$ in the array.

Question: Is there a faster algorithms which give time complexity less than $$P(\lg n)?$$ Otherwise, is there a proof to prove that $$O(\lg n)$$ is the theoretical lower bound?

## Counting occurrences of word in a text

Let’s say I have a long text of 1M words and I would like to create a table of all the words ordered by the number of occurrences in the text.

One approach would be populating a dynamic array with each word and linear search them to count the occurrences in $$O(n^2)$$ then sort the array by occurrences in $$O(n\cdot log~n)$$.

Another approach would be to use y priority queue and a trie. The insertion in the priority queue is $$O(log n)$$ and the build of the trie is $$O(n)$$. But traversing the trie to build the priority queue is somehow difficult to evaluate.

Eventually using a hash map seems to be the best solution, but computing the hash could cost a little bit of time even though it is just a constant. In this you have $$n$$ insertion/lookup in $$O(1)$$ then a final sort of the hashmap by occurrences in $$O(n\cdot log~n)$$.

So it is clear that the former approach is the worse and the latter the best. But how can I evaluate the complexity of the second one?

## Weighted automaton counting occurrences of the most frequent letter

Here’s a problem I’ve been dealing with for the past few weeks.

On the alphabet $$\Sigma=\{a, b\}$$, give a weighted (finite) automaton recognizing the language $$\{(u, \max(|u|_a, |u|_b))\mid u \in\Sigma^∗\}$$,

## Automatically create list item occurrences

I am trying to create a number of list items that have the same information except the date which is automatically filled in with, for example, 1 week between each list item.

Creating a recurrent event in a calendar web part isn’t an option because if I do so, the created events would be the same item and not a number of different items.

Do you have an idea on how to solve this?

## Mapping every character to its next occurrence based on the number of unique characters between the occurrences

To optimize my LF mapping, I was asked to do the following. Given a string, say $$abaxyxwxbx$$ I need to encode it in a way where every index stores the value of the number of unique characters encountered since its last occurrence $$+ 1$$ and $$0$$ otherwise. Using the earlier string. The encoding would be:

$$abaxyxwxbx$$
$$2502020200$$

For the first $$‘a’$$, we encounter a $$‘b’$$ and then an $$‘a’$$. So we count the one unique character, $$‘b’$$ and add $$1$$ to it and store it as the encoding for the first $$‘a’$$. For the first $$‘b’$$, we encounter the next $$‘b’$$ on index $$8$$. From the first $$‘b’$$ to the second $$‘b’$$, we encounter 4 unique letters $$(a, x, y, w)$$ and add $$1$$ to it, hence we store $$5$$ as the encoding for the first $$‘b’$$. For the second $$‘a’$$, there is no $$‘a’$$ that we can encounter hence its encoding will be $$0$$.

My first approach was to create a $$\sigma$$ x 3 sized array ($$\sigma$$ is the size of the alphabet). The first column would store the character, the second column would store the number of characters encountered since the last occurrence, and the third column would store the index of the last occurrence.

The second method I tested was to maintain an AVL tree with order statistic such that at a given time only $$\sigma$$ characters could exist inside the tree. This method is again $$O(n*\sigma)$$.

Is there a way to do this is $$O(n)$$ time?

## Find the starting indices of all occurrences of the pattern in the string – KMP algorithm follow-up

was initially solved here, but was too buggy:

Given a string and a pattern, find the starting indices of all occurrences of the pattern in the string. For example, given the string “abracadabra” and the pattern “abr”, you should return [0, 7].

My solution

Tried to solve it using KMP-algorithm:

function findStartingIndex(T, pattern) {   if (pattern.length > T.length) { return []; }    let next = 0;   const res = [];    const check = i => {     for (let j = 1; j < pattern.length; j++) {       if (next <= i && T[i + j] === pattern[0]) { next = i + j - 1; }       if (T[i + j] !== pattern[j]) { return false; }     }     if (next <= i) { next = i + pattern.length - 1; }     return true;   };    for (let i = 0; i < T.length; i++) {     if (i + pattern.length > T.length) { return res; }     if (T[i] === pattern[0] && check(i)) { res.push(i); }     i = next;   }   return res; } 

## Find the starting indices of all occurrences of the pattern in the string

Given a string and a pattern, find the starting indices of all occurrences of the pattern in the string. For example, given the string “abracadabra” and the pattern “abr”, you should return [0, 7].

My solution

function findStartingIndex(T, pattern) {   let S = T;   const res = [];   while(true) {     const i = S.indexOf(pattern);     if (i === -1) { return res; }      S = S.substring(i + 1);     res.push(i ? i + 1 : i);   }   return res; } 

# How to count the number of occurrences of english word and chinese character in some files?

I want to implement a function through Node.js:
count the number of occurrences of english word and chinese character in some files.

// filesDir = "../text"; // "../text/english.txt"; /* Since then, Short’s creative process has shifted to one that is digitally streamlined and mobile. “It used to be that I had my 4x5 camera, and I had my medium format camera and 35 mm and it got complicated,” she says. “Now it’s more about the image and less about the technical and the equipment. It’s made me more focused.” */ // "../text/chinese.txt"; /* 彭博亿万富翁指数统计显示，贝索斯目前的身家为1170亿美元。在周四发布的视频中，贝索斯赞扬了养父的勇敢和乐观。 贝索斯在视频中说，“很难想象”，迈克是在没有家人陪伴的情况下独自来到这个陌生国家的。迈克也在视频中称：“父母不被允许和我一起去机场，所以我只能独自登上飞机，45分钟后抵达迈阿密。” 迈克最初在佛罗里达州的Matecumbe难民营待了三个星期，后来被送到特拉华州的威尔明顿上高中，在那里他获得了去阿尔伯克基上大学的奖学金。 迈克说：“那时我对阿尔伯克基几乎一无所知。”但在那里，他遇见了吉斯和她的儿子贝索斯。他还称：“这真的令人难以置信。回顾我的一生，我曾在30年前实现过‘美国梦’，这真是太棒了！” */  const fs = require("fs"); const tasks = []; const wordCounts = {}; const filesDir = "../text"; let completedTasks = 0;  function checkIfComplete() {     completedTasks++;     if (completedTasks === tasks.length) {         for (let index in wordCounts) {             console.log(${index}:$  {wordCounts[index]});         }     } }  function addWordCount(word) {     wordCounts[word] = (wordCounts[word]) ? wordCounts[word] + 1 : 1; }  function countWordsInText(text) {     const words = text         .toString()         .toLowerCase()         // .split(/\W+/)         // .split(/^\p{Unified_Ideograph}/u)         .split("");      words         .filter(word => word)         .forEach(word => {             // console.log("word: " + word);             // console.log("**********************");             addWordCount(word);         }); }  fs.readdir(filesDir, (error, files) => {     if (error) {         throw error;     }     files.forEach(file => {         const task = (value => {             return () => {                 fs.readFile(value, (error, text) => {                     if (error) {                         throw error;                     }                     countWordsInText(text);                     checkIfComplete();                 });             };         })(${filesDir}/$  {file});         tasks.push(task);     });     tasks.forEach(task => task()); });