Hot answers tagged

9

Find my suggestions below. goalString = input() Let the user know what to do, input accepts a string as an argument: goalString = input('Type a string:') alphanum = '''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !@#&()–[{}]:;‘,'?/*1234567890''' If you need all printable characters you can use string.printable: import string alphabet = ...


6

pcmpistri This seems like the obvious choice searching withing a string. However, while pcmpistri is very general/powerful, it is also not very fast. On typical Intel processors it consists of 3 µops that all go to execution port p0 (therefore limiting this loop to at best running one iteration every 3 cycles), on AMD Zen(1/2) it's slightly less bad coming ...


4

This would obviously be better as a function, so we can call with different parameters. Instead of repeating the constant 12 in the fgets() call, we could simply use sizeof v. Then it remains consistent if we change v. Instead of strcspn() with only one character, I'd prefer strchr(). Conveniently, that returns a null pointer if not found, so we don't need ...


3

It took me a while to understand the code but I hope that I understood it properly. Lets say you have the string "abcd" goal string "abcd" 1st iteration "ftcP" -> you save the 3rd position as found 2nd iteration "aXlL" -> At this pint both 1st and 3rd positions have been found 3rd ...


3

Does not detect input errors on stdin When fgets() returns NULL due to an input error, code simply loops when a loop exit is more common. If the input error is permanent, code is stuck in a infinite loop. Avoid naked magic numbers Rather than 12, 11, etc, use a #define BUF_N 12 and code accordingly. // while (!fgets(v, 12, stdin) || strcspn(v, "\n"...


2

correct - As far as I can tell, yes. concise - A filter() followed by a .length (or .size) can be simplified: large.sliding(small.length).count(_.diff(small).isEmpty) fast - Could be faster, but it wouldn't be as concise. Consider the following: findPermutations("abcxcba", "cab") Under the current design that would be 5 invocations of _....


2

A short review; You want to put this in a function That function should either create the numeronyms or do the outputting, not both You are using && as an if statement, great for code golfing, not so great for code review Using .map() doesnt really make sense if you are not actually mapping, .forEach() makes more sense If you insist on split and map ...


2

Know the language Part of being a programmer is being familiar with the language/s you are using. These days languages are changing yearly thus keeping up to date is very important. MDN JavaScript provides a good JavaScript reference. Every now and then its good to peruse the site to keep your knowledge up to date. Strings String.slice will slice to the end ...


2

The current implementation of isAlpha ... let isAlpha = (char) => { // match any alphabetic character. let pat = /[a-zA-z]/igm; if(char.match(pat)){ return true; } else { return false; } }; ... can be refactored into something as short as this ... function isAlpha(char) { return (/[a-zA-z]/).test(char); }; ... or maybe even that ... ...


2

Really nice style; super clear. I'll try not to duplicate existing and really good answers. What I think you intend to do is a kind of state machine looking something like this: The error, in this case, includes stdin EOF before return was pressed. However, you are only checking for EOF and ignoring other errors, as pointed out. I believe that the fgets, ...


1

IMO a much simpler solution is to use POSIX getline(). And if your platform doesn't provide getline(), there are more than a few open-source implementations available. Bare code to illustrate the processing in a smaller number of lines (as in without scroll bars...): int main() { char *line = NULL; size_t len = 0; for ( ;; ) { printf(...


1

You ask: Should I assign each string to a variable? Many strings are not shared and will only be used once. Obviously, in the other case, where the string is reused/shared, then of course one would (and should) define the constant/pointer once rather than ever repeating the same string literal in multiple places. This makes code easier to maintain. You ...


1

I just want to show you your code when it has been correctly formatted: #include <iostream> using namespace std; int main() { string a = "w is the time for all good people to come to the aide of their country."; for (int i = 0; a[i] != '\0'; i++) { switch (a[i]) { case 'a':a[i] = '#'; ...


1

Since your result is only the number of iterations, you don't actually have to work with strings. Just keep track of how many characters are still wrong. import random goalString = input() alphaLen = 82 def getMatchString(): wrong = len(goalString) iteration = 0 while wrong: wrong = sum(random.choices((0, 1), (1, alphaLen-1), k=wrong)) ...


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