counter

Javascript Second Counter

不羁岁月 提交于 2019-12-04 07:33:50
On my website, I am trying to count (and display) how many seconds (not minutes or hours) the user has been on my site. So, if they have been on it for 5 minutes, it will display 300, Not 5 minutes. I am Very Unexperienced with JavaScript, So please help. You can use the setInterval function to run another function as often as you choose. For example: var seconds = 0; var el = document.getElementById('seconds-counter'); function incrementSeconds() { seconds += 1; el.innerText = "You have been here for " + seconds + " seconds."; } var cancel = setInterval(incrementSeconds, 1000); <div id=

Counting changes in an array

Deadly 提交于 2019-12-04 07:06:48
问题 I would like to count the number of times "red" is followed by "green" in this array: ["red", "orange", "green", "red", "yellow", "blue", "green"] If it is another color, the code should ignore it and proceed to the next item in the array. event_type.each_slice(2) do |red, green| break unless green count = count + 1 end p "The count is #{count}" Step 1: Look for red Step 2: IF not last item Compare with next item on array ELSE Go to Step 4 Step 3: IF green, count = count + 1 Go to Step 1 ELSE

Test if python Counter is contained in another Counter

拈花ヽ惹草 提交于 2019-12-04 06:51:11
How to test if a python Counter is contained in another one using the following definition: A Counter a is contained in a Counter b if, and only if, for every key k in a , the value a[k] is less or equal to the value b[k] . The Counter({'a': 1, 'b': 1}) is contained in Counter({'a': 2, 'b': 2}) but it is not contained in Counter({'a': 2, 'c': 2}) . I think it is a poor design choice but in python 2.x the comparison operators ( < , <= , >= , > ) do not use the previous definition, so the third Counter is considered greater-than the first. In python 3.x , instead, Counter is an unorderable type

SSE 4 popcount for 16 8-bit values?

南楼画角 提交于 2019-12-04 06:27:53
I have the following code which compiles with GCC using the flag -msse4 but the problem is that the pop count only gets the last four 8-bits of the converted __m128i type. Basically what I want is to count all 16 numbers inside the __m128i type but I'm not sure what intrinsic function call to make after creating the variable popA . Somehow popA has to be converted into an integer that contains all the 128-bits of information? I suppose theres _mm_cvtsi128_si64 and using a few shuffle few operations but my OS is 32-bit. Is there only the shuffle method and using _mm_cvtsi128_si32 ? EDIT: If the

Count and insert unique values - Can this code be optimized?

99封情书 提交于 2019-12-04 06:01:54
问题 I needed to generate an output from my Access database that was unavailable using standard functions. I did extensive searching, but when I found example code - it ultimately failed. So, I started from scratch, pulling from others' work where possible. The code below is probably very primitive, but it works for me and the operation in the database. What I'd really like to see is how this code could be made more compact and efficient. I'm not dealing with many lines today (<20), but I could in

Union of many Counters

无人久伴 提交于 2019-12-04 04:28:33
问题 What's the best way (in terms of readability and efficiency) of finding the union of a list of Counters? For example, my list might look like this: counters = [Counter({'a': 6, 'b': 3, 'c': 1}), Counter({'a': 2, 'b': 5}), Counter({'a': 4, 'b': 4}), ...] I want to calculate the union, i.e. counters[0] | counters[1] | counters[2] | ... . One way of doing it would be this: def counter_union(iterable): return functools.reduce(operator.or_, iterable, Counter()) Is there a better approach? 回答1:

How are Counter / defaultdict ordered in Python 3.7?

柔情痞子 提交于 2019-12-04 03:10:05
问题 We know in Python 3.6 dictionaries are insertion ordered as an implementation detail, and in 3.7 insertion ordering can be relied upon. I expected this to also be the case for subclasses of dict such as collections.Counter and collections.defaultdict . But this appears to only hold true for the defaultdict case. So my questions are: Is it true that ordering is maintained for defaultdict but not for Counter ? And, if so, is there a straightforward explanation? Should ordering of these dict

CountDownTimer in android - how to restart it

橙三吉。 提交于 2019-12-04 00:30:29
I to restart a CountDownTimer. I read a lot of question here but no one of the answer helped me. When I use the following code if(Const.counter != null){ Const.counter.cancel(); Const.counter = null; } Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000); Const.counter.start(); I start a new counter but the old one also continues work. Please hekp me solve it. You can realize it by cancelling and restarting. The following example should work. CountDownTimer mCountDownTimer = new CountDownTimer(500, 1000) { @Override public void onTick(long millisUntilFinished) {}

Summing list of counters in python

柔情痞子 提交于 2019-12-03 22:39:53
I am looking to sum a list of counters in python. For example to sum: counter_list = [Counter({"a":1, "b":2}), Counter({"b":3, "c":4})] to give Counter({'b': 5, 'c': 4, 'a': 1}) I can get the following code to do the summation: counter_master = Counter() for element in counter_list: counter_master = counter_master + element But I am confused as to why counter_master = sum(counter_list) results in the error TypeError: unsupported operand type(s) for +: 'int' and 'Counter' ? Given it is possible to add counters together, why is it not possible to sum them? The sum function has the optional start

How to count co-ocurrences with collections.Counter() in python?

房东的猫 提交于 2019-12-03 20:21:54
I learned about the collections.Counter() class recently and, as it's a neat (and fast??) way to count stuff, I started using it. But I detected a bug on my program recently due to the fact that when I try to update the count with a tuple, it actually treats it as a sequence and updates the count for each item in the tuple instead of counting how many times I inserted that particular tuple . For example, if you run: import collections counter = collections.Counter() counter.update(('user1', 'loggedin')) counter.update(('user2', 'compiled')) counter.update(('user1', 'compiled')) print counter