Posts by hkotsubo • 55,826 points
1,422 posts
-
5
votes1
answer369
viewsA: Problems with correct return date with "new date()"
According to the documentation, when a string is passed to the constructor of Date, it accepts the same formats accepted by Date.parse. This method, in turn, follows some rules. When the string has…
-
2
votes1
answer79
viewsA: Datetime with unknown parameters
This format is defined by ISO 8601 standard. According to this standard, the capital letter "T" is used to separate date and time fields. So we have "year-month-day," followed by the letter "T,"…
-
5
votes3
answers379
viewsA: How to take a value among many keys, but ignoring the keys, except the most internal?
First I think it’s worth explaining why your regex didn’t work. By default, the quantifiers (as * and +) sane "greedy" (Greedy) and try to pick up as many characters as possible. In your case, the…
-
2
votes2
answers58
viewsA: Content is not being saved to the file using the Printwriter class
Internally, PrintWriter has a buffer in which the writings are made. When the buffer is full, there its content is actually written in stream corresponding (in this case, the file). In your case,…
-
3
votes2
answers183
viewsA: How to check if the entered option is among the valid options
As already said in another answer, the problem is using or when you should actually use and. Using or, will enter the if if any of the conditions are true (i.e., if you type in f, will be no…
-
4
votes1
answer91
viewsA: Error reading Jsonobject inside an array
The error occurs because this array has no JSON objects, but strings. All because of the quotes: "ativos":["{\"ativo\":1,\"quantidade\":25}", "{\"ativo\":2,\"quantidade\":33}"] ^ ^ ^ ^ These quotes…
-
2
votes1
answer502
viewsA: Check if Jsonobject has a certain key
According to the documentation of JSONObject, the method get throws an exception (JSONException) if the key does not exist. And from what I understand, either JSON has the key "erro", or has the key…
-
4
votes2
answers253
viewsA: Check if a string matches multiple conditions
From what I’ve seen, you want to check out the following: in the first 2 positions have exactly "N1" in positions 3 to 16 (i.e., 14 below), they may only have digits from 0 to 9 the total size of…
-
4
votes1
answer106
viewsA: Read numbers using dot as decimal separator
No use setting the default locale after creating the scanner, as it was already created with the locale previously set. Also not a good option to change the default locale (Locale.setDefault),…
-
3
votes2
answers100
viewsA: Recursive program that accepts a non-negative integer as input and displays its vertically stacked digits
A tip to understand how any algorithm works is to do the table test. All right, for recursive algorithms it can be a little more confusing, but either way it’s worth a try. The basic idea of…
-
2
votes3
answers222
viewsA: re.error: bad escape c at position 0
Complementing the reply from @fernandosavio (which already explains why to use zip is wrong in this case), follows an explanation about why your regex failed. You are using '\\{}\\b'.format(x) to…
-
6
votes3
answers2923
viewsA: Intl to format date in Javascript?
First you need to understand something: dates have no format. A date is just a concept, which represents the idea of a specific point in the timeline (or in a calendar system). In the case,…
-
3
votes3
answers269
viewsA: How to reduce the capture of groups in a regular expression?
In the comments you said you’re using preg_match. And if we look at documentation, it is said that in the results of pouch capture groups are also returned. In the case, catch groups are created by…
-
1
votes2
answers40
viewsA: Using Intunaryoperator Java Interface to return n functions
An alternative to your solution is to create a stream with the functions and use the method Stream::reduce to return a composition of all functions: public static IntUnaryOperator…
-
2
votes1
answer114
viewsA: Correct way to get JSON "Sub Array’s" values in PHP
The problem is in this foreach: foreach ( $deps as $d ); Notice the semicolon at the end. This means that this foreach is "empty" (i.e., it iterates by $deps but does nothing in every iteration). In…
-
4
votes1
answer198
viewsA: Method "include?" returning false when there is a character in the Ruby string
This is because gets returns not only the typed text, but also the character \n corresponding to ENTER that you type. This can be seen if you check the size of the string and its codepoints*: print…
-
4
votes1
answer1083
viewsA: Limit contents of an Array
An alternative is to use slice: const arr = [1, 2, 3, 4]; console.log(arr.slice(0, 2)); // [1, 2] The method slice receives two parameters: the initial and the final index, the initial is included…
-
10
votes2
answers1052
viewsA: What is the difference between foreach and map in Javascript?
forEach serves to traverse the array and do something with its elements. But this "something" does not necessarily need to return something (so much so that the his return is undefined). Ex: const…
-
4
votes1
answer129
viewsA: Regex to take a stretch that may or may not occur
Just get the part after the _ be optional: Regex r = new Regex(@"\{\{[^\}]+?\}\}(_\(\d+\))?"); I’m assuming the ID is numerical, so I used \d+ (one or more digits). The parentheses around the number…
-
1
votes4
answers405
viewsA: Search a specific line in a . txt file and change the content
What you have is a file CSV (Comma-separated values, literally "comma separated values"), so a good alternative is to use the module csv to read it. An initial outline: import csv with…
-
1
votes1
answer94
viewsA: How to create a sequence of files whose names come from a numerical sequence with predetermined intervals?
Just use seq: for i in $(seq $num1 $num2) do touch $i.log done seq $num1 $num2 generates a numerical sequence ranging from $num1 until $num2. Then we make a for to iterate in this sequence, creating…
shell-scriptanswered hkotsubo 55,826 -
1
votes1
answer90
viewsA: Replace this for with a Lambda solution
The break, by itself, it is not a problem (see a more detailed discussion here and here). Even, in this case seems to be the most appropriate, because the intention of the code seems to be precisely…
-
5
votes1
answer77
viewsA: How to express a predicate of equality in Java?
The solutions below are a compilation of the answers of this question. If you want to use the method Reference and does not want to create a variable just to save the Predicate, can even do. It’s…
-
1
votes2
answers270
viewsA: Traverse string with PHP and return searched values
First I think it’s worth explaining why your regex didn’t work. Basically, the square brackets [] have special meaning in regex: they serve to determine a character class. For example, [abc] is a…
-
1
votes2
answers219
viewsA: Regex to search for word inside tag with CDATA
If you are reading or manipulating XML/HTML, you better use parsers specific, rather than regex (because regex is not the best tool for these cases). In Java, a good alternative is to use the jsoup…
-
1
votes2
answers350
viewsA: Recursive algorithm for calculating arithmetic mean
Just remembering that recursion is not the best way to calculate the average (and below we will see the reasons). Anyway, here are some alternatives. In your code, every recursive call you create a…
-
1
votes2
answers622
viewsA: How to Split Javascript using multiple tabs and without removing them?
One thing is not clear (as already indicated in the comments): in your example, the spaces are removed from the final result. So much so that, to get exactly what was indicated, it would be enough…
-
1
votes1
answer82
viewsA: How to find, retrieve and delete a certain value in a multiline string in Javascript?
A detail that was not clear: you say "delete the record", does that mean that the snippet in question should be removed from the string? Anyway, let’s see some alternatives... If you only want to…
-
3
votes3
answers843
viewsA: Show the amount of each alphabet letter in a String
You don’t have to make a loop within another, for this is very inefficient: for each character of the String, you want to compare it to each of the 26 letters, then at the end you will be doing N *…
-
3
votes2
answers781
viewsA: How to make an object inside a JSON object in Java?
It’s not very clear what you want to do. If you want to create a JSONObject with the 3 movies, add the third movie in the String that you already have: str =…
-
1
votes2
answers315
viewsA: How to avoid code repetition to average a variable amount of banknotes
Your code is too repetitive. The idea to eliminate this repetition is to identify what repeats and what varies, and put it all into one loop. In this case, what varies is the number of notes, but…
-
3
votes1
answer360
viewsA: How to format minutes using Java Localdatetime
One way to do it is to create a java.time.format.DateTimeFormatter, indicating the format to be used: private static DateTimeFormatter FMT = DateTimeFormatter.ofPattern("HH:mm"); public String…
-
3
votes3
answers1495
viewsA: Merge elements from a list with the last different tab
Just complementing the other answers, here are some other options. First a solution similar to answer from Miguel, but checking the cases where the list is empty or only contains one element (after…
-
2
votes1
answer134
viewsA: Picking up div’s contents inside an HTML
The problem of using explode is that it breaks the string without taking into account the semantics of HTML (i.e., the meaning of each tag, the separation between what is a tag and what is the…
-
2
votes1
answer171
viewsA: Python Hashlib Function: Typeerror: update() takes in keyword Arguments
If you look at documentation of update, will see that he only receives one parameter, which is a bytes-like Object (but as you are passing two parameters, gives error). In case, if you want to…
-
3
votes3
answers504
viewsA: JSON validation with more than one object
Gives invalid JSON error because JSON is actually invalid. According to syntax of a JSON, This is an object: { "resultado":" resultado 111", "peso":98, "sinal":"-44", "nome":"divergencia ativos",…
-
2
votes1
answer296
viewsA: Period of days between two dates, when the final date is less than the initial
DatePeriod has a builder in which, instead of the final date, you pass the amount of recurrences (ie the amount of times the range will be applied). So instead of passing the final date, just pass…
-
2
votes2
answers678
viewsA: Return data from an HTTP request at Angular
What happens is that http.get is a call asynchronous, that is, it is not guaranteed that it will return the results immediately, and the return may even execute before the get break up. http.get…
-
1
votes1
answer55
viewsA: Is it possible to do two different variable expansions in a single expansion?
According to the bash documentation on expansions: The basic form of Parameter Expansion is ${parameter}. The value of Parameter is substituted. The Parameter is a shell Parameter as described above…
-
2
votes1
answer402
viewsA: Illegal group Reference error
The problem is that your string contains the character $ (in various parts, such as "Total A (R$)", among others). And in the second parameter of method replaceAll the character $ has special…
-
1
votes1
answer723
viewsA: Validation of the last 2 digits of CPF in a single loop for
You can even do everything with a single bow, I just don’t know if it does so much difference like this. But before we try to do with two loops, then we compare with the version "one-loop-only".…
-
5
votes1
answer183
viewsA: Difference between the metacharacters b and B
First of all, it is worth clarifying that the shortcut \b (also known as word Boundary, something like "boundary between words") is a zero-length match (or a match zero): it does not correspond to a…
-
7
votes1
answer82
viewsA: Expression does not return the desired result when the data is captured by the input
This happens because the value of an element is always a string. So when you pass the document.getElementById(...).value, the function jurosCompostosComAporte is receiving a string. And when you…
javascriptanswered hkotsubo 55,826 -
4
votes3
answers115
viewsA: Regular expression of citations in R
It depends on the format these quotes may have. An option would be: str_extract_all(line, "(?<=@)\\w+") Returning: [1] "REF1" "REF2" "REF3" "REF4" "REF5" This regex uses lookbehind - the stretch…
-
2
votes2
answers881
viewsA: .addeventlistener is not a Function
As already said in answer from Ricardo, getElementsByTagName and getElementsByClassName return an array of elements. But if you want, there is the alternative to use querySelector and…
-
3
votes2
answers93
viewsA: Regular expression by string index
If you want the lyrics o is the second letter of the string, you can use the bookmark ^, indicating the start of the string: str_subset(string = x, regex(pattern = '^.o')) So we have the beginning…
-
7
votes1
answer665
viewsA: How does the metacharacter t work in a regex?
Yes, \t looks for a TAB. The last case works because the brackets form a character class, and the regex finds a match if the string has any character belonging to the class. In the case, [ \\t] is a…
-
8
votes4
answers729
viewsA: How to get the month formatted with zero left in Typescript
getMonth() returns a number. If you want a string, create another variable to receive the properly formatted value: const today = new Date(); const mes = today.getMonth() + 1; let todayMonth; if…
-
3
votes1
answer363
viewsA: I can’t remove leaf in binary tree
From what I understand, this is a binary search tree. Therefore, the algorithm to remove a node should take into account some cases: if the node is a leaf (that is, it has no right or left subtree),…
-
4
votes1
answer127
viewsA: Doubt with loop for counter
No need to manipulate the value of i. Just make a loop infinite, and only interrupt it when the number is positive: for i in range (1, 5): while True: num = int(input("Digite um numero positivo: "))…