# Tag Info

18

Your function seems to work fine, but you can condense it a bit by search for all bracket types in one regex replace with the following pattern: /[\{\}]/g and then use the replace function that takes a replace function as argument: const brs = "()({}{[]["; function flipBracketsDirection(str) { return str.replace(/[\{\}]/g, br =&...

8

It may seem unlikely but it is not impossible for an input string to contain tempBracket so a solution that doesn't involve adding and replacing that string would be ideal. function flipBracketsDirection(str) { return str // flip () brackets .replace(/$$/g, 'tempBracket').replace(/$$/g, '(').replace(/tempBracket/g, ')') // flip [] brackets ...

6

Alignment This will be an easy win - use aligned_alloc instead of malloc. This is only guaranteed to be available in the standard library as of C11, which you should be using anyway. Exponential reallocation This: // gracefully extend buffer size nofs++; buf = realloc(buf, BUF_SIZE*nofs*sizeof(char)); reallocates with linear growth. ...

5

This is an answer that I posted to the original question on StackOverflow. It does away with regular expressions entirely, and uses a similar case-statement to the one suggested in @Henrik's answer The original code performs 6 regular expression substitutions (which require 1 pass each), and fails on strings that contain the text tempBracket (as noted by @...

5

5

The code almost works. To make it work in all cases, test the program with Valgrind, which detects undefined behavior because of invalid memory access. This will prove that the buffer needs to be 11 bytes long, not only 10. What about platforms where int has 64 bits instead of just 32? For these you need a larger buffer. Until then, you should use a compile-...

4

Version 1 uses insert(0, ...) which has to move over all the characters already in the StringBuilder's buffer, so is unnecessarily wasting time. A more efficient construct would be: StringBuilder().append(DOUBLE_QUOTE_CHAR) .append(escapeJava(query)) .append(DOUBLE_QUOTE_CHAR) .toString(); Version 2 uses ...

3

It is doubtful that the program is so long that it can't all be included, but you have made an effort to comply with the Code Review guidelines. Just be aware that comments such as // more logic here or // ... will sometimes get the question votes to close. Complexity You're a Java programmer so I'm going to assume you understand object oriented programming ...

3

I have some suggestions. Don’t edit the parameter values, create your own instance / copy In my opinion, this is a bad habit, since some objects in Java are not immutable (Collections, Date, ect) when passed as parameters, you will edit the original instance of the caller. In your code, since the string is immutable, you are fine, but keep this in mind. ...

3

You can iterate once over text and short-circuit right away if any character is not unique. import string def is_isogram(text): seen = set() for char in text: if (lower := char.lower()) in string.ascii_lowercase: if lower in seen: return False else: seen.add(lower) return True

2

ord() Do not define constants like ASCII_LOWER_BOUND, use ord('a'). Easy to read, no uncertainty about the value. character range/set Do not use an integer range and ord(). It is error prone and hard to review. if ord(char) in range(ASCII_LOWER_BOUND, ASCII_UPPER_BOUND + 1) rewrites to import string if char in string.ascii_lowercase No off by one, easy to ...

2

The problem statement is much more about logical reasoning than about coding. On a unix/linux terminal, the challenge can be solved with 15 characters! sed 's/1.*0/0/' The equivalent c++ code is also a one-liner when using regex::replace, or a three-liner with string::find and string::replace. If you read the instructions carefully and work through a few ...

2

But what do you think of this approach? Comments below are less about Python and more about design and test. Negatives I suggest using the absolute value of number rather than math.log10(number). Lack of edge case testing I'd expect code to have been tested with various edge cases as they are specified in the requirements. 999000000000.0, 100000000000.0, ...

2

To me your code looks very WET. Everything is using the same core but with minor tweeks here and there. And so we can convert your code to a for loop. TRANSFORMS = [ ( 12, "$", 10**12, 3, ".3f", " trillion", ".000", ""), ( 9, "$", 10** 9, 2, ".2f&...

2

String traversal At several places in your code you convert a string to an array of its characters in order to iterate over it: let letters = Array(word) for letter in letters { // ... } These intermediate arrays are not needed. A Swift string is a collection of characters, so that you can iterate over it simply with for letter in word { // ... } ...

1

Correct me if I'm wrong but aren't functions and overloading operators basically the same thing, a glorified goto statement? I would only put it in if it makes it easier to read when you come back to it in 3 months. I would change all the 1u << x to the hex value. Those carry marks will not change any time soon, so you could make them constants and not ...

1

One for the fun of it const flipBrackets = BracketFlipper(); [ ')hello(', 'this has ]some text[', 'flip }any{ brackets', 'even with )))]multiple[((( brackets', 'flip (it) anyway', '>Pointy stuff<', '/slashed\\'].forEach(s => console.log(flipBrackets(s)));; function BracketFlipper() { const bracks = "(),{},[],<>,\\\/".split(","); ...

1

I'll comment on C style: #define BUF_SIZE (1 << 10) // approx 2 KiB or 1024 chars n This comment makes no sense. A char in C is, by definition, 1 byte. 1 << 10 bytes is exactly 1024 chars. I suppose I can understand if you're coming from Java where char is a UTF-16 code unit. c char* next_token() { char* buf = malloc(...

1

On PowerShell v6+, I would do the following: $String =$Fragment1, $Fragment2 | Where-Object { -not [System.String]::IsNullOrWhiteSpace($_) } | Join-String -Separator \$Separator You could use [System.String]::IsNullOrEmpty() instead of [System.String]::IsNullOrWhiteSpace(), but I find [System.String]::IsNullOrWhiteSpace() to be the desired ...

1

I think a functional style is a good choice. Provided you have the right sort of algorithmic complexity, I don't think you should worry about performance until it shows itself to be necessary. That being said, I think there's room for improvement if you're aiming to be functional, or at least doing things in another way that has other benefits. An idea of ...

1

To start, I must say that I wish I was writing JS like this in 2016. I have only recently been becoming acquainted with the newer features ecmascript-6 has to offer. After looking at the other answers to the post you mentioned I would have suggested they consider using destructuring assignment, but I doubt it would help for your code. For readability it can ...

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