Hot answers tagged

30 votes

Perl CGI script to serve a PDF file

I will go through your code line by line and give feedback. We will skip the general advice on don't use CGI as it's actually suited for what you are trying to do here. I wrote this answer in two ...
simbabque's user avatar
  • 993
25 votes
Accepted

Explicit song lyrics checker

I recommend practicing Python 3 rather than Python 2 these days. According to PEP 8, isClean() should be is_clean(). One or ...
200_success's user avatar
23 votes
Accepted

Can I slim this UTF8-encoding program?

Efficient file I/O By default, files opened with fopen() are buffered, meaning that not every call to fread() or ...
G. Sliepen's user avatar
22 votes
Accepted

92 Spoons AI, sort of an AI in C++

There's a lot to say here, so let's not waste any time and jump right into it: You said that you didn't want to hear the using namespace std;-drill, but ...
Ben Steffan's user avatar
  • 5,228
22 votes

Can I slim this UTF8-encoding program?

log is already declared in <math.h>. You don't need to declare it yourself. In fact, it could be harmful. As stated in ...
vnp's user avatar
  • 56.2k
21 votes

Explicit song lyrics checker

Splitting line into words using words = line.split(" ") will only split the line on space ...
AJNeufeld's user avatar
  • 33.4k
16 votes

92 Spoons AI, sort of an AI in C++

Here are some things that may help you improve your code. Don't abuse using namespace std I know you said you don't want to hear this, but your code is unusable ...
Edward's user avatar
  • 66.4k
16 votes

Explicit song lyrics checker

Some suggestions: Using argparse to parse arguments is preferable to any kind of interactive input, because it means that it's much easier to automate and ...
l0b0's user avatar
  • 8,907
16 votes
Accepted

A simple error messaging and logging system via macro(s) in C++

You’ve already noted that this could be done better without macros, so I won’t belabour the point. I will note, though, that your goal—“to refresh [your] skills at writing good solid macros”—makes ...
indi's user avatar
  • 10.9k
15 votes

Markov chains to generate text

Functions Split the code into functions, also split the generation and the presentation. Your algorithm has some clear distinct tasks, so split along these lines: read input assemble chain construct ...
Maarten Fabré's user avatar
15 votes

Program that finds the longest word not containing one of the disallowed characters

I see a number of things that may help you improve your program. Fix the bug There is a subtle bug in the original implementation. It contains these lines: ...
Edward's user avatar
  • 66.4k
15 votes
Accepted

Average spam confidence

Not duplicating any of @Peilonrayz's code review points ... Stop reading entire files into memory when you can process the file line by line in one pass, and stop creating huge lists in memory which ...
AJNeufeld's user avatar
  • 33.4k
13 votes

Read file into vector<byte>

Nice: using bytes = std::vector<std::byte>; I would call it Bytes to make it clear it is a type rather than an object. ...
Martin York's user avatar
  • 94.6k
12 votes

Non-curses pager in C

Bug You're code has a bug-ish. When using this, if instead of hitting enter when asked to enter a character you enter some other letter and then you press enter, then next newline will be skipped and ...
SirPython's user avatar
  • 13.3k
12 votes

Reading a DHT11 to a file

Here is a list of things I would improve: define constants in upper-case (PEP8 reference) put the main program logic into the if __name__ == '__main__': to avoid ...
alecxe's user avatar
  • 17.2k
12 votes

92 Spoons AI, sort of an AI in C++

Header files You have a lot of implementation code in that header file (files.h). Header files should only contain the function or class headers, the implementation should go into an extra cpp file. ...
Raimund Krämer's user avatar
12 votes

Comparing two large binary files in C

fread (tmp1, sizeof *tmp1, readsz, fp1); fread (tmp2, sizeof *tmp2, readsz, fp2); count += 16; if(memcmp(tmp1, tmp2, readsz)){ … } You are discarding the ...
200_success's user avatar
12 votes

Basic C copy file implementation

You test the return value of fwrite, which is good. However, fread may fail as well. Since ...
vnp's user avatar
  • 56.2k
12 votes

Average spam confidence

I think ask_file_name looks fine without using pathlib. The difference between the two comes down to LBYL vs EAFP. For the most ...
Peilonrayz's user avatar
  • 43k
11 votes

XOR encryption program in C

Use binary mode Currently, you are opening your files like this: in = fopen(optarg, "r"); out = fopen(optarg, "w"); On a UNIX system ...
JS1's user avatar
  • 28.5k
11 votes

Creating a unique file name in C#

The only way to guarantee that the generated filename is truly unique is to actually create the file. Any other method is vulnerable to a race condition. Therefore, if you are serious about ...
200_success's user avatar
11 votes

Creating index to a file in C

Magic Numbers Don't write things like if (a <= 90 && a >= 65) { /* A = 65, Z = 90 */ if you can just write ...
Ben Steffan's user avatar
  • 5,228
11 votes

Python code that compares two files byte by byte

The code in the post calls os.path.getsize for each block read from the two files. This could be avoided by remembering the value in a local variable. The code ...
Gareth Rees's user avatar
  • 49.6k
11 votes
Accepted

A minimal CLI password storage and retrieval manager

I would pinpoint the major problems in the code which I would address first: do not manually handle provided command-line arguments - there is a great built-in module for it - ...
alecxe's user avatar
  • 17.2k
11 votes
Accepted

Most common words in a text file of about 1.1 million words

This is actually quite good. Good use of the collections module. One improvement I can think of is switching to the ...
Ludisposed's user avatar
  • 11.5k
11 votes
Accepted

CSV file reader in PHP that supports large files (>15k lines)

Performance As performance is your main concern, let's face this first. To complete the example CSV-file with ~36k lines your original script needs around 139s*. The main bottlenecks are ...
insertusernamehere's user avatar
11 votes

Perl CGI script to serve a PDF file

...
choroba's user avatar
  • 1,393
11 votes
Accepted

Counting relevant entries in a large bioinformatics file

In order to speed this up, you'll need to avoid as many string creation operations as possible, because they are expensive. Especially the split operation is expensive. Not only does this create many ...
RoToRa's user avatar
  • 11k
11 votes
Accepted

Comparing two large binary files in C

I'm trying to read two sufficiently large binary files, comparing them and printing the offset at which they differ. compare_two_binary_files() is faulty for ...
chux - Reinstate Monica's user avatar
11 votes
Accepted

Basic C copy file implementation

Prefer Symbolic Constants Over Magic Numbers There is a header file that should be included, stdlib.h, that provides some standard symbolic constants such as EXIT_SUCCESS and EXIT_FAILURE. It might ...
pacmaninbw's user avatar
  • 23.8k

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