Posts by hkotsubo • 55,826 points
1,422 posts
-
1
votes3
answers81
viewsA: Confusing behavior in changing the value of variables in js functions
There is a difference between the definition of the function and the execution of the function. When you do time1 = function () etc, is defining the function. That is, it is only saying what it…
-
1
votes1
answer135
viewsA: Regex to find a dash not followed by letters
Your regex takes the hyphen that’s at the beginning (^-), or a hyphen that doesn’t have a letter immediately before. As in your code the string has a space immediately before the hyphen, regex finds…
-
2
votes1
answer41
viewsA: "this" inside Arrow Function used as a method of an object does not point to the object
According to this excerpt from the documentation: When a Function is called as a method of an Object, its this is set to the Object the method is called on. In free translation: When a function is…
javascriptanswered hkotsubo 55,826 -
1
votes1
answer1422
viewsA: Convert String to Date From yyyy-mm-dd to dd-mm-yyyy format
According to the documentation, for the month you should use the uppercase "M". The lowercase "m" is minutes. Then I would be: DateFormat formatUS = new SimpleDateFormat("yyyy-MM-dd"); Date date =…
-
2
votes1
answer138
viewsA: How does the cloneNode method work?
Basically, cloneNode copies the Node, but the clone doesn’t have one Parent and will not be part of the document unless you add it somewhere. It takes a parameter boolean indicating whether the copy…
-
2
votes2
answers279
viewsA: How does Undefined return work?
First of all, it is worth remembering that Undefined is one of the types defined by the language (as well as String, Number, Boolean, etc.). And the specification says the following: The Undefined…
javascriptanswered hkotsubo 55,826 -
1
votes3
answers269
viewsA: How to Restart a Python Program?
If you want something to happen over and over again, don’t make it up, use a loop. A very simple version would be: while True: # faz o que precisa aqui (lê os números, faz as contas, etc) if…
-
1
votes1
answer288
viewsA: Query two or more words in a string
Just to explain why your regex didn’t work: The brackets define a character class, that is, the expression will give match in any of the characters between [ and ]. For example, [abc] means "the…
-
2
votes2
answers41
viewsA: Within a method belonging to an object, the this of the called function does not point to the object
According to the documentation: Inside a Function (...) this will default to the global Object. That is, the code below (running in a browser) will print "true": function f() { // no browser, this é…
-
0
votes1
answer95
viewsA: Store return of XML
If you want to process each value individually, it is still possible to do inside the loop. Just check the value of the attribute nItem (assuming that all tags det have this attribute). Something…
-
3
votes1
answer670
viewsA: How does the module operator work (%)?
The relevant documentation is here, and says the operator % returns the rest of the division of the first number by the second ("The % (module) Operator yields the remainder from the Division of the…
-
1
votes1
answer123
viewsA: Problems with Java Scanner
This is because you made two calls in a row from nextLine: String comando = in.nextLine(); String yn = in.nextLine(); Every time you call nextLine, the program is waiting for you to type something…
-
3
votes2
answers272
viewsA: Function that compares characters from a string and returns true or false
Makes a mistake of out of range because the last string is empty, that is, its size is zero. So, it has no index (no index message[0], nor message[-1], or any other value), and any attempt to obtain…
-
1
votes1
answer63
viewsA: Inside setInterval, "this" does not point to the function that called it
This is explained in documentation: Code executed by setInterval() runs in a Separate Execution context than the Function from which it was called. As a consequence, the this keyword for the called…
-
1
votes2
answers60
viewsA: Is there any way to use methods within other python methods?
If you want to print everything at once, you can use join along with a Generator Expression: z = (1,2,3,4) print(f'os números pares foram: {", ".join(str(i) for i in z if i % 2 == 0)} são números…
-
0
votes3
answers380
views -
2
votes3
answers112
viewsA: Restart application - Python
The problem occurs when one is not typed a valid number, and the conversion to float spear one ValueError, which closes the implementation of the programme. The other answers are suggesting putting…
-
0
votes1
answer61
viewsA: Variable returning value that was declared initially and not what the user reported
I regret to inform you that the another answer (which has been deleted) is wrong. For example, if you type the numbers 3, 2 and 1 in this order, the result is: O maior valor é 1 e o menor valor é 1…
-
0
votes1
answer97
viewsA: Return a specific XML field
They are basically two problems: According to the documentation, findall search for the elements that are direct children of the element in question. Like the tag det is inside infNFe, which in turn…
-
0
votes2
answers40
viewsA: Returned value is being the same as the entered value
The attribute value of a input type="text" is a string, so first you must convert it to number. As you want a whole number, use parseInt: function convertDec() { var decimal =…
-
2
votes1
answer94
viewsA: Loop through the lines of a file and print parts of it in sequence
First let’s modify your loop a little bit: for IA in "$(cat teste.txt)"; do echo "- $IA" done That is, before each line, I am also printing a hyphen. The output is: - teste1:testee1 teste2:testee2…
-
3
votes3
answers713
viewsA: Combination of two lists in Python
The problem is that the method append only gets one argument, but you’re passing two: lpossiveis[i] and dpossiveis[i]. A solution would be to concatenate these values, as suggested in other answers.…
-
7
votes1
answer142
viewsA: Regex is not checking the accented character, even though it is in the expression
You fell for "The Unicode"! But let’s go in pieces. First, I copied and pasted the word Olá of your code and I did the following: from unicodedata import name for s in 'Olá': # para cada caractere…
-
2
votes3
answers64
viewsA: Why using 'IN" to filter records along with a subselect returns records that have, in certain columns, equal values
The IN serves to verify that the expression is equal to any of the given values (see the documentation). For example, supposing we have these tables: customers | id | country | |----|-----------| |…
-
1
votes3
answers133
viewsA: Round Decimal
Depends on what you mean by "round off the list items". If you want only print the rounded values, make a loop by the elements and prints them: for n in lista: print(f'{n:.2f}') In that case, as the…
-
2
votes1
answer80
viewsA: Class that implements Comparable must compare by a String field in lexicographic order
length() returns the size of the string, so you are sorting by the number of characters in the name. If you want the lexicographical order, compare the strings themselves. In this case, strings…
-
2
votes1
answer50
viewsA: Fileinputstream class read() method not finding end of file
The problem is on this line: r = (char) fin.read(); But before you understand, consider the code below: int x = (char) -1; System.out.println(x); The code above prints 65535. This happens basically…
-
2
votes1
answer51
viewsA: Create 2 zipped files with files from a folder
An alternative is to use pathlib.Path.glob passing a Pattern corresponding to the names of the files you want, and add only these files in the zip. Only then you will need a different name for each…
-
0
votes2
answers217
viewsA: How to make a random choice without repeating PHP
If so the $animal_escolhido as the other should be chosen randomly, do not need to choose one, and then try to catch the other. You can catch both at once. Just grab the key array you already have…
-
8
votes3
answers98
viewsA: Why is the expression ``` == '`' true?
Gives true because both expressions generate the same string (in this case, it is a string containing only the character `). Behold: console.log(`\``); // ` console.log('`'); // ` In the first case,…
-
1
votes1
answer54
viewsA: Receive 12 elements and separate them into 3 lists
You can use the syntax of slicing to take a specific excerpt from sys.argv - remembering that the first element of sys.argv (at zero position) is the name of the script, so the numbers will be at…
-
2
votes1
answer278
viewsA: Check if a date is before or after other dates
To compare whether one date comes before or after another, use the methods before and after: if (individuo.getDataNascimento().before(date1) || individuo.getDataNascimento().after(date2)) { etc... }…
-
1
votes2
answers1247
viewsA: How to check if one or more string has uppercase/lowercase initial character (C Language)
For starters, str[' ' + 1] doesn’t make sense. It doesn’t do what you expect (probably you thought it would be a position after space, or something?) - actually, char and int are sort of…
-
0
votes2
answers740
viewsA: Multiply a matrix by a vector
Given a matrix M x N (M rows and N columns) and a vector with N elements, the result is a vector with M elements, and the algorithm for multiplying the matrix by the vector is: matriz A vetor x…
-
1
votes1
answer46
viewsA: How to not return only the last sentence in for loop?
This here: analise_eventos = list([f'{i} é ' for i in eventos_advesos]) analise_previsao = list([f'classificado como {e} ' for e in previsibilidades]) analise_descricao = list([f'e {a} segundo a…
-
1
votes2
answers905
viewsA: check symmetrical matrix
"the program always returns true" It’s not true, I tested it with a non-symmetric matrix and it returned False: m = [ [3, 2, 5], [1, 5, 6], [15, 4, 7] ] print(simetrica(m)) # False Just a few…
-
1
votes1
answer69
viewsA: How to use recursiveness to iterate in array
In fact, in a recursive function, it is crucial to have a stop condition. But the problem is that when the array has zero size, it does not enter the if, and the function does not find any return.…
-
5
votes3
answers819
viewsA: Update content of JSON file
The problem is that just writing the data at the end of the file is not enough to do what you need. When writing at the end of the file with the option 'a', you are only writing the content of the…
-
2
votes1
answer452
viewsA: How to make the columns of the Floyd Triangle perfectly symmetrical?
Since you only want up to n equal to 40, the highest possible value is 820, so one option is to align the numbers on the left, occupying 4 positions. For this, just use the formatting options…
-
5
votes3
answers5809
viewsA: How to fix "Valueerror: invalid literal for int() with base 10: '' - Python?
If you want to manipulate numbers and make calculations, it’s almost always best to use mathematics. Turn numbers into strings, as suggested by another answer, may even "work", but it is not ideal…
-
1
votes1
answer41
viewsA: Error trying to print an array with larger elements from two other matrices - 'int' Object is not subscriptable
When you do m3 = m1[i][j], the variable m3 is receiving a number. That is, it is no longer a list, and therefore it has no positions to access. Hence the error "'int' Object is not subscriptable",…
-
1
votes1
answer89
viewsA: Use . bat cls command in Java language
This response from Soen explains why your approach doesn’t work, but basically Runtime.exec redirects the standard output to a pipe which can be read by the Java process. Since the output was…
-
0
votes1
answer56
viewsA: When a validation is negative I would like the program to return, so I can enter the data again
If you want to validate whether a number has been entered, I suggest reading it all as String, try to convert to int and display a message if you cannot. To do this, I suggest creating an auxiliary…
-
4
votes3
answers144
viewsA: Check magic numbers (those whose square root is a prime number) within a range
That has been detailed here and here, but basically, by doing: if algumacoisa == 2 or 3 In fact the two conditions analyzed are algumacoisa == 2 and 3 (only the value 3). And for the second case…
-
2
votes1
answer40
viewsA: Handling JSON in PHP
First, the JSON is wrong, missed put : between the "data" key and its value: $json = '{"dados": {"nome":"marcos"}}'; ^ aqui Second, if you have a string and want to transform it into an array, use…
-
3
votes3
answers230
viewsA: Remove lowercase letters from a list
As explained here and here, remove items from a list while iterating on it can bring unexpected results. Modifying your code a little, we can understand what happens: frase = ("A B C D e f g h") new…
-
2
votes1
answer120
viewsA: How can I add up times told by the user?
First of all, there are two important concepts you should know to understand how to solve your problem: "schedules" and "durations". To better understand, consider the two sentences below: the movie…
-
1
votes2
answers1407
viewsA: How to return all objects of a JSON array
To better understand how to do it, just first understand the structure (then the rest becomes easy). What you have is an array, as it is enclosed by square brackets. In an array, the elements are…
-
2
votes2
answers1284
viewsA: How to check if a character is letter or number?
The class Character has methods to check if a character is letter, number, etc. See all options in documentation, but a solution to your case would be: for (int i = 0; i < frase.length(); i++) {…
-
2
votes1
answer259
viewsA: Make a Map and take the values
If the maps interns have exactly a single key, you can get the entrySet of them and take the first (and only) element: for (Map.Entry<String, Map<String, String>> entry :…