Posts by hkotsubo • 55,826 points
1,422 posts
-
2
votes1
answer37
viewsA: Substring does not return empty string
According to the documentation, the method substringAfter takes as second parameter the string to be returned if the delimiter is not found (and if this parameter is not passed, it returns the…
-
2
votes2
answers102
viewsA: How to convert the If-Modified-Since header to a date (and vice versa)?
Just use the format 'D, d M Y H:i:s T' (see the description of each field in documentation). To transform the string into date, use date_create_from_format, and from the date returned by this…
-
1
votes2
answers115
views -
4
votes1
answer60
viewsA: Variable in loops and function in C
First, first of all, do yourself a favor and properly format your code: int main() { int i, x = 1; printf("%d\n", x); for (i = 0; i < 4; i++) { int x = 2; printf("%d/ ", x); { int x = 3;…
-
2
votes3
answers253
viewsA: How to resolve "Valueerror: max() Arg is an Empty Sequence" error when the list is empty?
This here: if None in classe1 or classe2 or classe3: Not does what you think you do. In fact, what you have are 3 conditions: None in classe1: this checks out if None is on the list classe1 (that…
-
2
votes3
answers68
viewsA: Passage by reference in C
First, that line inside the main: float CalculaHora(horas, minutos, segundos, &conversao); This is an attempt to function statement, shouldn’t be there. On this line you should be calling for…
-
5
votes12
answers208289
viewsA: How to format date in Javascript?
Complementing the other answers... You mention "Brazilian format" and then "dd/mm/yyyy". Although they are "the same thing" (since it is the most common format in Brazil), there are some…
-
0
votes2
answers221
viewsA: JAVA - How to compare columns of an int[][] array
Note: for the code below, I am assuming that the matrix is with the correct dimensions. I say this because there are actually no matrices in Java: what we have are arrays of arrays (an array in…
-
2
votes2
answers164
viewsA: How to Formulate Pattern to Limit Date
In a input type="date", it is possible to limit the minimum and maximum values. For example, to accept only 4-digit years, you can set the maximum value to 31 December 9999 (remembering that the…
-
4
votes3
answers116
viewsA: How to make after running Alert(e), go back to the first line of Code
If you want something to happen over and over again, use a loop - That’s what they’re for :-) Besides, it makes no sense to launch a Error inside the block try just to catch it right away. And the…
javascriptanswered hkotsubo 55,826 -
7
votes6
answers3379
viewsA: How to find out if the year is leap in PHP?
We can use the rule of gregorian calendar, in which a leap year follows the following rules: if not divisible by 4, is not leap if divisible by 4: if divisible by 100, it is only leap if it is also…
-
1
votes2
answers195
viewsA: How to return a list where the size is set according to a number typed in the python input?
Make a table test would already make you realize the mistake: entrada is the amount of pairs that you must return to, but in for you are using range(3, entrada + 1, 2), that is, that for only checks…
-
1
votes1
answer34
viewsA: Function does not work in all test cases
First of all, [[]] is not empty: this is a list that has an element (and this element is an empty list). Then just check this condition, ie the size of the matrix is 1 and the size of the first…
-
3
votes1
answer52
viewsA: Python sequence lists
I haven’t tried to understand your code, but it seems to me you’re complicating things for no reason. Just make a loop and save a reference to the previous element. When the current element is the…
-
4
votes1
answer358
viewsA: Encoding error (Non-utf-8 code) while running script
In Python 3, the parser assumes, by default, that the source code is in UTF-8. Then you probably saved the file in another encoding. I did a test here and saved your code in a file, using the UTF-8…
-
4
votes1
answer44
viewsA: Validate from 1 to 6 digits, the first of which cannot be zero
The problem is in + in [1-9]+. The quantifier + means "one or more occurrences", therefore [1-9]+ takes one or more occurrences of digits from 1 to 9. If you want exactly one digit of 1 to 9, just…
-
5
votes2
answers141
viewsA: What is the difference between the functions Rand, mt_rand, random_int?
rand and mt_rand, according to the documentation (here and here), are not considered cryptographically secure (for this purpose, the use of random_int, about which we will speak below). So much rand…
-
3
votes2
answers85
viewsA: Formatting to number of decimals dynamically using Python
In the format it is possible to do so: import math def formating_pi(n): fmt = "pi com {} casas decimais é {:.{size}f}" return fmt.format(n, math.pi, size=n) print(formating_pi(5)) # pi com 5 casas…
-
4
votes2
answers65
viewsA: Correct unbalanced parentheses
You don’t need regex for that. A simpler way is to count the parentheses. You iterate by the string characters, and if you find a (, increments the counter, and if you find a ), decrease. There is…
-
2
votes2
answers82
viewsA: How to store different values in a single variable
Use lists (although similar to the array of other languages, the documentation calls the list): idades = [] # lista vazia for i in range(1, 8): idades.append(int(input(f'Em que ano a {i}ª pessoa…
-
3
votes1
answer46
viewsA: How to convert 10:00:00 to 10
First you have to understand two different concepts: dates/times and durations. Consider the phrases below: the event will be at 10 am the event lasted 10 hours In the first case, "10 hours" is a…
-
0
votes1
answer61
viewsA: To sort an array with tiebreaker criteria if one of the fields is equal
In fact this logic of adding the goals to the points is stuck. It can even work in many cases (since in thesis a team that won more games, usually scored more goals than the others), but we know…
javascriptanswered hkotsubo 55,826 -
4
votes2
answers55
viewsA: How to save a dictionary to two independent objects in Python?
An alternative is create a copy of dic1 and then do the update in the copy: dic1 = {'k1':'Ford','k2':'GM','k3':'Dodge'} dic2 = {'k4':'Audi','k5':'Mercedes','k6':'VW'} dic3 =…
-
0
votes1
answer37
viewsA: Delete an item and add again by starting index by counting 1 {ERROR}
Showcase 2 because the function excluir does not change the array a (it continues having the elements, the problem is that the function adicionar only adds the data in the array, but only prints the…
-
1
votes2
answers45
viewsA: Why does isNaN(true) return 'false'?
See the documentation: true if the Given value is Nan; otherwise, false. That is, it returns true if the value is global ownership NaN, otherwise (for anything other than NaN) returns false. Of…
-
3
votes1
answer30
viewsA: When using the . extend() command in Python, how do you store the result in a new object?
Just concatenate the lists and assign to a new one: lista1 = ["a", "b", "c", "d"] lista2 = [5, 6, 7, 8] lista4 = lista1 + lista2 print(lista1) # ['a', 'b', 'c', 'd'] print(lista2) # [5, 6, 7, 8]…
-
6
votes3
answers214
viewsA: Calculate increments so that 2 numbers become equal
Complementing the another answer (that as the author himself acknowledged, in some cases may fail), follows a small adjustment. The idea remains the same: we take the difference between the numbers…
-
1
votes2
answers292
viewsA: java.util.Illegalformatconversionexception: d != java.lang.Integer
In the documentation of String.format there is a link to the format string syntax. And there is said the following: The format specifiers for types which are used to represent Dates and times have…
-
3
votes2
answers200
viewsA: Eliminate consecutive duplicate string letters
The indexes of a string start at zero, so they go from zero to length - 1. So it’s wrong to do j<=x.length(), so you’re getting an extra index at the end and error when trying to access a…
-
4
votes1
answer58
viewsA: Get date from a string
As explained above here, a regex can help to find something that if seem with a date, but it will still be important to validate it - read the link already indicated for more details, but in…
-
2
votes3
answers11403
viewsA: Find larger and smaller unique values
An alternative to get unique elements is to use Counter to get the count of each element of the array, and then only check the largest and smallest among those whose amount is 1. Another detail is…
-
1
votes1
answer39
viewsA: preg_match_all catch tag and attributes
Do not use regex to read XML/HTML/any-other-ML (see here and here for more details, and at the end there is a brief explanation about this). Anyway, if you’re dealing with XML, a better option is to…
-
4
votes1
answer96
viewsA: Format decimal places in PHP
Why 1000 (i.e., "thousand") becomes 10.00 (i.e., "ten" formatted with 2 decimal places), simply divide the value by 100 and use the function number_format, with which you choose the number of…
-
1
votes2
answers102
viewsA: How to create a function that counts the characters of a sentence and shows the frequency of one?
Unlike the other answer, you don’t need to sort characters or anything like that. Just scroll through the characters and go computing the totals. And how one char in C always has 1 byte (and…
-
3
votes2
answers78
viewsA: Why is the input type number reset when I have a value starting with "-"?
It is possible to print all the entered value --155,5 without altering the type of input for text? I’m afraid that’s not possible. According to the WHATWG, in the case of a input type="number", the…
-
4
votes2
answers58
viewsA: How to add specific intervals of several lines?
As you are going through one line at a time, you will only have one value each iteration, so it makes no sense to use sum. Just update variables with each iteration: total_chargeable_units = 0…
-
2
votes6
answers342
viewsA: Get a list of the first multiples of a number
To have the first multiples of 3, just create a list with the result of 3 * 1, 3 * 2, 3 * 3, etc., until 3 * N. Then it is enough that the range go from 1 to N, and you enter the result of the…
-
5
votes2
answers135
viewsA: Is it possible to delete previous commits and only remain the current one?
I don’t know if the Github interface can do it, but anyway I prefer the command line. First you clone the repository on your machine: git clone http://url.do.repositorio Obviously, by changing…
-
0
votes1
answer36
viewsA: Doubt JS code
This is all wrong: valorComponente = () => elemento.textContent You know what this syntax is (() => etc...)? You are creating a Arrow Function, that is to say, valorComponente is a function…
javascriptanswered hkotsubo 55,826 -
0
votes1
answer165
viewsA: Regex to catch files with certain characters in the name
Instead of making a regex that takes what it wants, you can choose to take what you don’t want: r = re.compile(r'^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)?$') arquivosTxt = [] caminhoAbsoluto =…
-
1
votes2
answers177
viewsA: How to find the umpteenth occurrence of a substring?
find returns the position of the first occurrence it finds, but it is possible to pass as parameter the position from which the search is made. I mean, just call find several times, using the…
-
5
votes2
answers149
viewsA: How to swap the last character in a string?
If you already know which index you want (in the latter case), you don’t need to make a for to reach it, just access it directly. But as the another answer I have already said, strings are immutable…
-
3
votes2
answers37
viewsA: Why does entering a value in a list become None?
This is because append returns None. If you want to read the values and save them in the list, do these steps separately: valores = [] # primeiro lê os valores valor1 = int(input('Valor 1: '))…
-
0
votes2
answers56
viewsA: Search string inside Java list
At first, just turn everything into tiny: if (filmes.getNomeFilme().toLowerCase().contains(name.toLowerCase())) Or in capital letters: if…
-
0
votes4
answers72
viewsA: Get minutes between two times
Instead of using asHours and round with Math.floor, you can just use hours (as indicated in the documentation, the getters that begin with as return the full duration, while its versions without as…
-
1
votes1
answer80
viewsA: I need help understanding this code
because the variable "notas_cem" is attributed to the serve divided by 100? and because the serve is equal to the rest of this division? Well, let’s assume that the value of the withdrawal is 256…
-
1
votes2
answers174
viewsA: Find out which number from 1 to N is missing from a list with N - 1 numbers
Given the constraints of the problem and the fact that there is no need to validate the list (that is, I can trust that the numbers are between 1 and N and the list always has N - 1 numbers and…
-
2
votes1
answer95
viewsA: How to receive only letters and spaces in the input?
Like isalpha() returns True only if all characters are letters, and the name can have letters and spaces, then the way is to check the characters one by one. So: while True: name = input("Digite seu…
-
1
votes2
answers329
viewsA: Function that returns uppercase consonants
The problem is that upper returns another string. It does not change the original (by the way, this applies to all methods that "modify" the string - in Python, strings are immutable, and these…
-
3
votes2
answers139
viewsA: How to check if there is a property in an object?
In doing user.finished === true, you are checking if the value of the "finished" property is true, not if it exists. To do what you want, an alternative is to use hasOwnProperty, passing the name of…
javascriptanswered hkotsubo 55,826