Posts by hkotsubo • 55,826 points
1,422 posts
-
2
votes3
answers156
viewsA: Regular expression to reorder string
without clutter using explode and recombine the array I’m sorry to say, but with regex it doesn’t get simpler. In fact, I would say it gets even more "stuffed", and there’s no escape from…
-
5
votes3
answers204
viewsA: How to serialize and deserialize objects containing Bigint values in Javascript?
According to the documentation, JSON.stringify checks whether the object to be serialized has the method toJSON (and if so, the result of this is used). We can also see this in language…
-
0
votes1
answer68
viewsA: What is the regex for all accented, whole words coming from a variable?
In Javascript the shortcut \b only consider ASCII characters, so accented characters are not taken into account. An alternative is to simulate your behavior using lookarounds along with Unicode…
-
6
votes1
answer72
viewsA: Regex for a valid YYYY year in java?
It’s like I set in my regex an interval of years It does, but it’s not worth it. Regex basically works with text, and even digits are treated as mere characters, regardless of their numerical value.…
-
1
votes3
answers235
viewsA: Why is it not possible to use comparison operators with Nan?
The short answer is "Because they defined it that way". To elaborate better and give a little more foundation, follows below an adaptation of Soen’s reply, whose author claims to have been a member…
-
4
votes3
answers153
viewsA: What is the way to check and react to a change of state of an array using pure Javascript?
An option, which does not modify the array properties, would be to use a Proxy: // funções que quero alterar const functions = ['push', 'pop']; // handler que intercepta uma chamada de função/método…
-
2
votes2
answers686
viewsA: How to join two object arrays by different keys?
Complementing the another answer, use map/find is a good solution, but depending on the size of the arrays, it may not climb as well. That’s because, according to the language specification, the…
-
2
votes2
answers71
viewsA: How to extract substring in string array with Javascript using match?
Any tips on how to do it in a minimally performatic way Whenever you want something "performatic", regex usually is not among the first options (for a number of reasons, such as the fact of engine…
-
2
votes1
answer40
viewsA: How to identify words that start with a character and end with the same plus a space using regex?
It depends on what you need, but from the reported cases, the rules seem to be: after the first asterisk can not have space (i.e., it is not "any character" as stated in the question) after the…
-
1
votes2
answers61
viewsA: What is the static method of that returns an object instance in Java
This is a Pattern design known as Static Factory Method. The idea is not to "swindle" anything, but to provide some features that would not be possible only with the constructor. One of the…
-
2
votes3
answers126
viewsA: Formal definition of operations = and ==
Well, I can’t imagine a more formal definition than official language documentation. So let’s go to it: Assignment Statements The = is called assignment statement (declaration of award), the…
-
2
votes2
answers65
viewsA: Delete prepositions and text articles in python
Note: if you are working with natural language processing, you better follow what was indicated in another answer and use a specific library for it. Anyway, here is an alternative to do "at hand":…
-
2
votes1
answer33
viewsA: Negative lookahead in php
First, about .*?: the point corresponds to any character, and the *? is a quantifier Lazy, which takes zero or more characters, but always the smallest possible amount that satisfies the expression.…
-
2
votes1
answer26
viewsA: How to search within a file in different subfolders using grep?
Your command (grep -R "busca" /home/apps/*/teste.php) search only the file teste.php in folders that are a level below /home/apps/. That is, if the file is /home/apps/pasta/teste.php, it is found.…
-
1
votes1
answer29
viewsA: Java - Uninitialized "x" variable
First of all, this doesn’t make sense: boolean validate = false; while (validate = true) { You initialize the variable with false, then change its value to true (yes, the operator = is attributable,…
-
2
votes1
answer38
viewsA: Change from day to month when adding minutes: Why does it occur? How to fix it?
The function date, despite the name, returns a string (which in turn represents a date in a given format). Then the $inicio is a string in the "day/month/year" format, which in turn is passed to…
-
2
votes2
answers92
viewsA: Does Javascript have the equivalent of Python’s list comprehension?
In Python this is called comprehensilist on, and it is something that Javascript does not have (it has already "got" more or less, as we will see below). So the way is to make one loop even normal,…
-
1
votes2
answers69
viewsA: Compare arrays or strings and return an array with all repeated elements (javascript)
You are unnecessarily complicating things. If you want to calculate MDC, you do not need to factor the numbers, there is a very simple algorithm for this: // calcula o MDC entre dois números…
-
0
votes1
answer27
viewsA: Remove an hour with minutes or not from a date
According to the documentation, the builder of DateInterval accepts strings in the format described by ISO 8601 standard. And according to this rule, durations at all times begin with the letter…
-
0
votes3
answers605
viewsA: Save a JSON file inside square brackets and separated by comma
If you want in this format: [{"nome":"fulano","numero":"1"},{"nome":"fulano2","numero":"3"}] Then you have to generate an array, in which each element is one of the objects containing the name and…
-
4
votes2
answers72
viewsA: How to filter an array with Timestamp elements?
Depends much what you want to do, but first you need to understand what is the timestamp. Understanding the timestamp Basically, a timestamp such as 1565308800000 indicates that more than 1.5…
-
3
votes2
answers80
viewsA: How to understand mapping (map) of functions list in Python?
Let’s start from the beginning. According to documentation: Functions are first-class Objects. That is, in Python the functions are "first-class citizens": they are treated as if they were "normal"…
-
3
votes1
answer77
viewsA: Why is it that when I use tabulation on a printf, there are cases where it doesn’t tabulate correctly?
When a \t, he advances to the next tab stop. And this varies according to the size that is configured on the terminal you are using. For example, in my terminal the size tab stop is 8, so the output…
-
2
votes1
answer46
viewsA: Regex python to find all values with Math in Brazilian currency
\d{0,} means "zero or more digits," so if you don’t have any digits, you’ll also find a match. Anyway, a monetary value the way you want it follows the following rules: may have up to 3 initial…
-
0
votes2
answers81
viewsA: Check which numbers are primes within a vector
Your variable c (counting the number of divisors) must be reset to each number being tested. The way you did, she ends up counting the total amount of divisors of all the numbers. Also, you should…
-
2
votes1
answer60
viewsA: Reading numbers in C
If running on a terminal, what generates EOF is Ctrl+D (Linux/Unix) or Ctrl+Z (Windows). Type ENTER generates a valid entry, then feof will return false and the loop continues. Here’s another…
-
1
votes1
answer50
viewsA: Problems with choosing the 'role' option in a program that 'plays' jokenpo with me
Your list stayed ['pedra', 'papel', 'tesoura']. That is, the first element loses the second, the second loses the third, and the third loses the first. Then it would be enough to take the position…
-
2
votes1
answer73
viewsA: Calculate price per minute
First you need to calculate the total duration in minutes. Then, just see if the duration has passed 30 minutes. If you didn’t pass, the total amount is $30, and if you passed, subtract 30 minutes…
-
3
votes2
answers487
viewsA: String Sweep in Search of Substrings Ignoring Accent and Case and For Each Match Perform an Action
The basic idea is to take what was typed and do the search disregarding accents and differences between upper and lower case. Then, for each occurrence that is found, you break the text between…
-
5
votes6
answers47964
viewsA: What is JSON? What is it for and how does it work?
An important point that was not addressed in the other replies: a valid JSON nay must be a set of key/value pairs. All right it is the most commonly used format, but do not want it to be the only…
-
5
votes2
answers100
viewsA: Sort two lists simultaneously, by notes in ascending order, then by name
If the idea is to synchronize the two lists (i.e., sort the notes, and then maintain the order of their names), you don’t need all this complication. Instead of having two lists, I suggest having…
-
3
votes2
answers432
viewsA: Permutations of an integer disregarding the zeros on the left
The formulas below have been removed from here. Given any number, and considering: toi is the number of times the digit i occurs in this number (therefore 0 <= i <= 9) Ex: in paragraph 644,…
-
3
votes1
answer53
viewsA: Object Destructuring and Map - Doubt with Arrow Function
The problem isn’t that I’ve changed function for Arrow Function. The problem is the way each one is getting the arguments. At first you did: function({ name }) Notice the brackets around name. This…
javascriptanswered hkotsubo 55,826 -
3
votes1
answer54
viewsA: convert input in seconds to days, hours, minutes in seconds from day on another planet. C code
Well, in calculating the hours that dia * 24 is the cause of the problem. As the total of days is 7481752 (over 7 million), by multiplying by 24 will be an even greater number (over 170 million),…
-
2
votes1
answer33
viewsA: Error reading a file and saving it in dynamically allocated space in c
The problem is that fseek(file, 0, SEEK_END) arrow the position to the end of the file. Then the fread starts reading from there, but as it is already at the end of the file, there is nothing more…
-
1
votes3
answers119
viewsA: How do you not display repeated JAVA values
Depends. Whether the order of the elements is important, that is, if they should be printed in the same order they appear in the array, you can use the another answer, but remembering that it is a…
-
2
votes3
answers74
viewsA: How to check how many times the number 2 appears inside a matrix?
Your matrizA is actually a list of lists: it is a list in which each element is another list (and those yes have numbers). Then just go through the "lines" (actually the lists), and then go through…
-
5
votes1
answer298
viewsA: PYTHON: Nameerror: name is not defined
Although you commented on another answer that "worked", I would like to leave an alternative - in my opinion - better. First, using global variables - especially the way it was done in your program…
-
3
votes3
answers79
viewsA: Select right line with Split in TXT
Here: with open(file=nome_arquivo, mode='r', encoding='utf8') as fp: linha = fp.readline() count = 1 You only read one line of the file (hence the first). Then, in the while count < numero_linha…
-
6
votes2
answers94
viewsA: Special characters are not displayed by the console.log
The character \ is not "removed from the string". Actually it is part of a escape sequence. Basically, when you create a string, you have to put its contents in quotes: var string = 'conteúdo da…
-
7
votes2
answers764
viewsA: What is the difference between a map, a dictionary, an associative array and a hash table?
As the question is language-independent, let’s focus first on definitions that... do not depend on a specific language, and then move on to the specifics of some languages that are relevant to the…
-
4
votes3
answers74
viewsA: Picking numbers from a string - python
Just exchange for: re.findall(r"<SCANNER A(\d+)", txt_espelho) In the case, \d is a shortcut to "digits" and the quantifier + means "one or more". Already parentheses create a capture group, and…
-
0
votes1
answer37
viewsA: Write to a. txt file with for loop in Python
The problem is that you set a value default for the parameter start_at. And according to the documentation, this value is evaluated only once, when the function is created, and then no longer…
-
1
votes1
answer73
viewsA: List Comprehension returning zero
The problem is that zip returns an iterable which can only be traversed/iterated once. Example: x = [1, 2, 3] y = [ 'a', 'b', 'c'] z = zip(x, y) print('primeira vez:', [ d for d in z ])…
-
1
votes1
answer24
viewsA: How to compare two dates using input date and new date object in Javascript
The problem is that in the event of click the variables agora and d2 have the values they had initially, before the click. That is, the value of d2 is not updated (just put a console.log(d2) within…
-
2
votes1
answer47
viewsA: Why does the HTTP request response not recognize special characters?
In short, it is a problem of encoding (if you want to understand in depth what is a encoding, read here). But basically, all the traffic information comes and goes in the form of bytes, which are…
-
2
votes1
answer62
viewsA: How to read <br/> in HTML files and print as line breaks?
Like text returns the text without the tags, the <br> is also removed. So the way would be to replace the tags <br> before obtaining the text: # obtém a tag (e não o text) example =…
-
1
votes3
answers154
viewsA: Doubt Normalizer + Regex
The other answers focused on explaining the regex, but failed to explain the Normalizer, and how they both work together. Regex Just to complement/reaffirm, as it has already been covered in detail…
-
2
votes4
answers177
viewsA: Insert names to a list depending on their value
When you do [ algo ], is creating a list (because of [ ]), with a single element (algo). In your case, this algo is nome * 3, which is basically the string nome "multiplied by 3". In Python, when…
-
4
votes2
answers99
viewsA: Why is it necessary to instantiate a Python class when I can call it directly?
You didn’t charge anything In doing p1 = People, you have not created a class instance People. In fact you did that p1 receive the class itself People, see: class People: def talk(): print('hello')…