New answers tagged

0

Use consistent indentation I don't know if the indentation style of the code you pasted here is as you wrote it, but if it looks the same in your code editor, you should definitely try to fix it and make it more consistent. I would not bother trying to fix this manually, instead use either code formatting functions of your editor, or use an external tool ...


3

Reading into the question somewhat, there is emphasis on not only matching to a date pattern where the digits and separators are in the right places, such as 98/76/5432 which your pattern would accept; but to narrow this - with the pattern itself - knowing what digits are allowable in which positions. One basic improvement is to constrain the range of the ...


4

You can import and use datetime rather than validating date yourself. >>> import datetime >>> datetime.date(2020, 9, 9) datetime.date(2020, 9, 9) >>> datetime.date(1990, 2, 29) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: day is out of range for month As such we can ...


0

std::string user_input;//user input string In C++ you don't need to declare variables before you use them. I would strongly recommend doing so, otherwise readers will have problems with the text. You certainly should not use namespace / global variables unless they are required - and here they are certainly not. Use method parameters and local variables ...


0

Your code doesn’t have great complexity. I think it’s \$\mathcal{O}(2^n)\$, but I’m rusty so maybe somebody can come in with an assist. The thing to remember is that Haskell lists are linked lists and laziness doesn’t get you out of paying the cost of walking them. Each call to everyOther is \$\mathcal{O}(n)\$, and you pay that twice with every recursive ...


2

Much of this feedback overlaps with that of @JakobLovern who has already covered some great points; nevertheless: You should break up main into subroutines You can avoid play being needed if you have a subroutine that returns a boolean Avoid aggressive abbreviation of your local variable names Consider adding an input validation loop Move your 'Goodbye' to ...


3

Formatting & Conventions Always run your code through an autoformatter. On Linux, I use clang-format, and on Win10 I use VSCode's autoformatting intellisense features. I'm not sure of your exact setup, but formatting is big. Next, variable names. The variable choose is wonderfully descriptive. I know immediately what it's for. The variable P_h on the ...


1

First, in R we use <- for assignment. It has its advantages. It is prefered that you don't use T and F as variable names as these are aliases to TRUE and FALSE. Instead of this P <- rep(P_0, T) you could have P <- vector(mode = "numeric", length = T). This is just a suggestion: But there is no reason to not name your parameters: para <-...


0

Bug - tolower() word[j] = tolower(word[j]); We need to be careful with the functions in <ctype.h>. They generally accept int parameters that hold unsigned representations of characters; the only negative value that may be passed is EOF. So a robust program always converts any char value to unsigned char before allowing it to be promoted to int: ...


3

and welcome to C programming! Also, to Code Review. Style There are a couple of "style" issues with your code. The most obvious one is that your indentation is not consistent. I don't know if that's due to pasting it into the browser, or if it appears that way in your code. But computescore needs to be cleaned up. Next is an issue of "...


2

"I would like to increase the speed of the SHA256 algorithm" You're using a library for the hash so there's really no way for you to make it faster. In theory, if you're an expert in cryptography and in computer science you could maybe build something faster yourself but most likely it'll be slower because it's actually hard... From the resources I ...


2

I see you've absorbed many things I mentioned in my last review — well done! Coincidentally (or perhaps not, since tic-tac-toe is a common exercise for beginners), I wrote my version of tic-tac-toe last year. I still considered myself a nauplius at that time, but I hope you find certain aspects of my code worth learning from. Notably, there was a lot of ...


2

The first thing I notice is the two separate maps. Since the only actual information you're storing is X / O positions in a 3x3 grid, I think it is good practice to only store those values. Then add any additional formatting when you print. I've also used snake_case for function and variable names, which is the preferred style. The first section could be ...


2

XV6 bizarreness XV6 has made at least one ill-advised decision in forcing a non-standard signature of printf that requires a file descriptor be passed in. Maybe they should have omitted printf entirely and made you use fprintf instead, but anyway: at the least, you should make a stdout constant equal to 1 and use this instead of the numeric literal. You ...


2

For a beginner this code looks okay. Good job using strict equality comparisons - i.e. ===. The indentation is mostly consistent, although the first indentation level is 4 spaces while all subsequent levels appear to be 2 spaces. The biggest thing I would change is spacing. For the sake of readability, it helps to add spacing around operators - especially ...


1

Code looks clean, some variables should maybe have a little bit better name, some comments would be nice, overall very good for your first game. Some points: You should use a package, this makes it also easier to create a jar, and jars are good for distributing programs/games. I would combine Start.java, GameFrame.java and GameBoard.java: and make class ...


3

Just noticed this and noticed the one thing the other answer omitted was: System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone(); It is acceptable to initialize variables using var keyword to avoid long redundant declarations. And I don't believe using the Clone() ...


2

An idiomatic approach would be to put MyText in a dataframe (if not already) and assign() the results of str.count(). I'd also suggest using an actual dict for the regex mapping. It's not strictly necessary, but the syntax is just cleaner given that this use case aligns with dict. (Here I created terms from MyDictionary just for continuity with the existing ...


1

Unfortunately, there is a bug in glCons. The employee's fun should get added, not replace the current fun level: glCons :: Employee -> GuestList -> GuestList glCons emp (GL a b) = GL (a ++ [emp]) (empFun emp) -- ^ But let's stay on glCons. We can expect glCons to be used via foldr. However, the ++ operator leads to quadratic behaviour. ...


2

Helper Variables and Cells Helper variables and cells should be used to better describe simplify our code. The cell formulas are complicating the process. They are forcing you to process Merge Up and Merge Down differently from Merge Left and Merge Right. Use a simple 4 x 4 matrix instead. Using a 4 x 4 matrix would allow you to load the data into an ...


2

power() problems double power() does not return a value. Incorrect result when y is not a value in the int range. Code attempts a linear power calculation: O(y). Research Exponentiation by squaring. It is faster and less precision loss. double r parameter serves no purpose. Instead make a local double r variable. A floating point power function is one of ...


1

Create a testable function Instead of plonking everything in main(), I would prefer to see a function which can be tested with many different inputs from one program (perhaps accepting a std::istream&, or maybe a pair of iterators to words). Yes, you could write a shell script to invoke the program with different input streams, but that gets awkward when ...


2

OP's pow() inefficient Rather than iterate n times, form the power by squaring and iterate log2(n) times. // Shown without overflow protection for brevity. int pow_i(int base, int expo) { assert(expo >= 0); // Leave expo < 0 as TBD code int ipow = 1; while (expo > 0) { if (expo % 2) { ipow *= base; } base *= base; expo /...


1

Have you tried Suggested Actions There is a new feature called Suggested Actions that enables easy access to common properties when a control is selected. This feature is available in Visual Studio 2019 Preview version 16.6 and later. To use it, first enable it through Options > Preview Features > XAML Suggested Actions.Once enabled, click on a ...


0

I don't have any Python implementation feedback, but I do have a little bit of security design feedback. When you ask the user to specify an initial password, you should not echo the output to the screen (in case an interloper is watching them). When you ask the user to specify an initial password, you should ask them to enter it twice, and then check ...


1

Missing const I see you are using const in a lot of places, but you have missed a few: check_unit_existence(): both arguments can be made const char * read_reg_syntax(): the first arugment can be made const char * read_list_syntax(): same as read_reg_syntax() split_configs(): the first argument can be made const char * exec_command(): the second argument ...


2

Self document Consider users do not want to see or may not have access to the implementation and are left to declarations. These deserve parameters names and at least a comment about what each function does and how to use. (e.g. Call vector_init() first. It overwrites all vector_t members.) // Deserve for documentation here. bool vector_init(vector_t *, ...


1

Welcome to the wonderful world of programming! While writing code it is helpful to think of similar problems you can solve. Not sure if you are specifically looking for a password verification routine (which you should use an established library instead of rolling your own), or if you are just going through an academic exercise. Exercises are good to help ...


3

Nothing awful here. If this was production code then putting things in named functions would be nice to help with self documenting code. Don't particularly like this. using std::cin; using std::cout; using std::endl; using std::string; But I am not going to complain about it very much. Using snake case is unusual but not unheard of in C++. ...


3

I'm not going to retype what everyone already said, but I'll add a quick suggestion. Use Pylint to help you write better code. It helped me tremendously when starting. As an example, Pylint would mark the following in your code. On the first line: Line too long (141/100) On each res == true (etc..): Comparison 'res == True' should be 'res is True' if ...


0

compile with warnings enabled. Then fix those warnings. gcc -c -Wall -Wextra -Wconversion -pedantic -std=gnu90 several header files are being included but their contents are not being used. This is a very bad programming practice. The makefile needs a .clean label+commands for elimination of produced/reproducable files The makefile needs a .ALL label as ...


3

In C, as in C++, object pointers can be freely converted to void*, so quite a few casts can be removed. It's probably convenient to store data as a char*, since that's how we use it. We should still use void* as external interface, of course. This is a dangerous anti-pattern: p->data = realloc(p->data, VECTOR_RESERVE_SIZE); If the ...


1

This is not at all a bad effort for someone new to the C++ language. Here are some things that may help you improve your program. Use the required #includes The code uses std::fill which means that it should #include <algorithm>. It was not difficult to infer, but it helps reviewers if the code is complete. Eliminate global variables where practical ...


9

There are two separate questions here, and I'll try to tackle both. You're asking how to improve the code, which I'll separate into two halves: "How can I make this into better code that does the same thing?", and "How can I make this do better things?". Making the code better while doing the same thing: if len(password) >= 8 and len(...


2

In addition to Reinderien's answer, may I suggest: Yes, Use Memoryviews to speed up access In addition to the code you have, I would also type the a_mat and b_mat matrixes as double[:,::1] following the Typed Memoryviews guide. (the "1" means contiguous and is allows for slightly faster access). You are right in that you can not cdef declare ...


1

Use C++ standard library headers Don't use C headers such as <stdlib.h>, use the equivalent C++ headers like <cstdlib>. You should be using the C++ library functions anyway. Don't rely on transitive includes It happens that <iostream> will include <string> itself in most implementations, which is why you can use string without ...


15

void main is a really bad habit. It must be int main. Always check what scanf returns. For example, try to enter a non-numeric input, and see your program entering the infinite loop. Avoid conio.h (and hence getch). It is very non-portable.


3

It looks good for a first program in C, good job. An improvement would be to ask the user to insert a number which will be the maximum number to guess (instead of the default 100 value). You also could change the try variable name to tries.


12

Not much more to add here, but you'll quickly find that this routine is too basic for the need and regular expressions are the way to go. This routine does not guarantee that the resulting password will be balanced, that is sufficiently "random" and hard to guess. For example AAAbbb123 or Abcdef123 will pass your test. These are not strong ...


27

Two general points about your code: Variable naming: res, res2, res3 are not descriptive, so it's harder to understand at first glance what their purpose is. Instead I would recommend something similiar to contains_digit, contains_lower_char, contains_upper_char. Variable naming is always up to a bit of personal preference, but explicit names are generally ...


3

Structure your data The reason you need nested matches is that even after parsing the YAML document into a Yaml object, it's still essentially unstructured data. Not every YAML object will have the right shape, so you have to validate it piece by piece as you traverse it. Worse, you'll have to validate it all over again next time you try to traverse it. This ...


0

Just one additional aspect not yet mentioned by the other answers so far. Don't use wildcard imports as in import java.awt.*; import java.awt.event.*; import javax.swing.*; They look so convenient, but there's a risk. Why? It creates possible ambiguities that you can't control. Imagine that e.g. in Java20 a new class javax.swing.KeyAdapter gets introduced. ...


0

Beside all hints that you might gather from PMD, Findbugs and Checkstyle? I would suggest to review the design. The inheritance of GamePanel is GamePanel extends JPanel implements ActionListener whearat inheritance is meant to be used for something that "is" something, and fields are used to use for something that "has" something. From ...


1

You can use subcommands in a command: echo "This $(date) is a date calculated in the echo command" When you don't need additional control statements, you can use combined code like: find /data/data/com.termux \ $(awk '{printf("-path %s%s -prune -o ", "/data/data/com.termux/", $0)}' conf.file) \ -print >> output The ...


2

Welcome to Rust, and welcome to Code Review! rustfmt & clippy Always run cargo fmt and cargo clippy first. cargo fmt formats your code according to the official Rust Style Guide, and cargo clippy detects common mistakes and provides feedback on improving your code (none in this case). main.rs The biggest problem with the main function is its structure. ...


1

The page you linked to has pseudocode, and it does s = ((s × s) − 2) mod M. And right below the pseudocode it says: Performing the mod M at each iteration ensures that all intermediate results are at most p bits (otherwise the number of bits would double each iteration). Do that and it'll be much faster. And better use the same variable names as in the ...


1

String Constants An alternative way for representing your constants if you don't need their string representations would be to use the iota identifier built into golang to assign auto-incrementing enumerations like so: const ( Plus = iota // 0 Minus // 1 Number // 2 Skip // 3 Newline // 4 ) This is ...


0

Answers to questions Allocator equality The first piece of the puzzle is understanding what it means when two allocators are equal. If two allocators are equal, then any memory allocated with one can be deallocated with the other. For example: auto a1 = std::allocator<int>{}; auto a2 = std::allocator<int>{}; std::cout << std::boolalpha <...


Top 50 recent answers are included