Skip to main content
15 votes

Create a words histogram of a given string in python

str.split method without arguments It does the same filtering as you do. Just skip ' ' argument. list(dict.fromkeys(list comprehension)) It looks overburdened, right? You can create a set of list ...
Pavlo Slavynskyy's user avatar
13 votes
Accepted

Histogram word counter in Python

Several things can be improved in your existing code. First and foremost being, replacing your usage of re module. Regex matching is very heavy. You have a defined ...
hjpotter92's user avatar
  • 8,791
11 votes

Get histogram of bytes in any set of files in C++14

This key loop looks incorrect: while (!stream.eof() && stream.good()) { unsigned char ch; stream >> ch; histogram[ch]++; } If ...
Toby Speight's user avatar
  • 81.8k
10 votes
Accepted

An ASCII histogram of a die rolled n times

The roll function generates a random sequence of integers. The loop body shows me that you know how to seed a pseudo-RNG with a true source of randomness but you're ...
Indiana Kernick's user avatar
8 votes

Sparse 3D binning/histogram function

Some suggestions: Passing the code through at least one linter such as flake8 or pycodestyle will make for more idiomatic and therefore readable code. Do you have any automated tests? If so, do they ...
l0b0's user avatar
  • 8,977
8 votes

Python 3.6 Dice Histogram

Various small comments: you can write a[x+y] += 1 you always call random.seed with the same input so you'll always have the ...
SylvainD's user avatar
  • 29.3k
8 votes
Accepted

Populating a structure

A case cursor == NULL does not free(out->data). A test i > 1 is strange, and buggy. If ...
vnp's user avatar
  • 57.3k
8 votes

Query to plot a histogram of song ratings

It would be better to use GROUP by in your SQL. By this way you will get rid of long SQL query. ...
Unnamed's user avatar
  • 81
8 votes
Accepted

Draw a histogram of the characters in input

That's some surprisingly compact but still very readable code! Very good use of C++20's ranges. Casting numbers You are performing several explicit casts. While I don't think there is anything wrong ...
G. Sliepen's user avatar
  • 61.7k
8 votes
Accepted

Histogram manipulation

profiling This submission is about performance, yet it includes no profiling measurements. All we are told is that hist() completes within 44 msec. temporal ...
J_H's user avatar
  • 35.2k
7 votes
Accepted

Turning a grayscale image into a histogram of the intensity of the pixels of that image

JPG vs PNG Somehow, your code seems to produce wrong results with the above JPG. When used with the same image converted to PNG, the histogram looks much better, and much closer to what Photoshop ...
Eric Duminil's user avatar
  • 3,960
7 votes
Accepted

Get histogram of bytes in any set of files in C++14 - take II

The code looks much improved compared to version 1. Most of what I have to say may be subjective preference rather than required actions. I'll step through the code in sequential order, rather than ...
Toby Speight's user avatar
  • 81.8k
6 votes

Populating a structure

One additional problem is that the memcpy is not copying all the data. It is copying ds bytes, not ...
1201ProgramAlarm's user avatar
6 votes
Accepted

Reading and processing histograms stored in ROOT files

Given both the documentation of Python 3 contextlib or contextlib2, I’d say your usage is pretty standard for the tools at play. However, there is something bothering me a bit in your code: ...
301_Moved_Permanently's user avatar
6 votes
Accepted

Detecting if image taken by camera is too dark

Intelligibility I can't quite work out what's going on with the main loop. ...
Peter Taylor's user avatar
  • 24.2k
6 votes
Accepted

Sparse 3D binning/histogram function

Since you don't care about the distinct values, only the sum in each bin, you can avoid one of these for loops: ...
Graipher's user avatar
  • 41.1k
6 votes

Draw a histogram of the characters in input

C++20 ranges and views are not available for all compilers yet. The default C++ compiler on Ubuntu 22.04 does not support them because it is gcc11 and not gcc12. Since the problem is in the first ...
pacmaninbw's user avatar
  • 24.9k
5 votes

Histogram word counter in Python

In addition to hjpotter92's answer you can improve your counting using dict.get() ...
RandomDude's user avatar
5 votes
Accepted

Image processing and plotting

Usually, when using for loops and numpy together, you’re probably doing it wrong. Some things you could replace in your code: Use ...
301_Moved_Permanently's user avatar
5 votes

Detecting if image taken by camera is too dark

Sampling: Don't process ALL pixels The speed mainly depend on the number of operations, in our case, pixels. And a lot of pixels. This means that the larger the image is, the slower your code will ...
Rob Audenaerde's user avatar
5 votes
Accepted

K&R 1-14 - Histogram

Bugs. The loop printing the histogram lines for numbers, for(int i = 0; i <= 10; ++i) lets i become 10, which accesses <...
vnp's user avatar
  • 57.3k
5 votes

Turning a grayscale image into a histogram of the intensity of the pixels of that image

I have some suggestions for your code. Extract the expression to variables when used multiple times. In your code, you can extract the similar expressions into variables; this will make the code ...
Doi9t's user avatar
  • 3,334
5 votes
Accepted

Print a character frequencies as a vertical graph

This is a pretty good start. You should be warned that K&R describes a very early version of C, and the language has evolved considerably since the 1980s. For example, we normally always write ...
Toby Speight's user avatar
  • 81.8k
5 votes
Accepted

K&R "The C Programming Language" exercise 1-13: histograms

for (int j = (c = putchar('\n')) - c; is too clever to be good. You save one line of vertical space by the price of a reviewer's mental effort. Don't do this. Be ...
vnp's user avatar
  • 57.3k
5 votes

Histogram manipulation

There is one major issue with the code, and then some performance issues (the other answer already touched on some of these). Major Issue In the histogram computation, you loop over pixels in parallel,...
Cris Luengo's user avatar
  • 6,247
5 votes

Get histogram of bytes in any set of files in C++20

It's got all my suggestions incorporated (apart from a late edit to recommend std::uint_fast64_t from <cstdint> instead of ...
Toby Speight's user avatar
  • 81.8k
4 votes

Find maximum area of rectangles within a histogram

Implementation It's not clear why you defined a class with no data members and only one function member. This would be much simpler as a plain function. The various commented-out assignments to ...
Toby Speight's user avatar
  • 81.8k
4 votes

Print the frequency of characters as a horizontal histogram

You have assumed that the input character coding is ASCII, but the C standard allows other codings (notoriously EBCDIC) that use different numbers for the printable characters. Even with only ASCII-...
Toby Speight's user avatar
  • 81.8k
4 votes
Accepted

Histogram of letters in string

You've got way too many comments - you should only use comments when you need to explain an oddity. You don't need a comment to tell you when a method ends. I know this for loop for string.length() is ...
Ben Harris's user avatar
4 votes
Accepted

Finding the 20th percentile pixel of an image using a histogram instead of std::sort

Well, index=std::floor(0.2*(srcSor.size())/100 is equivalent to the much simpler index = srcSor.size() / 500, ignoring some ...
Deduplicator's user avatar
  • 19.3k

Only top scored, non community-wiki answers of a minimum length are eligible