Posts by hkotsubo • 55,826 points
1,422 posts
-
5
votes2
answers413
viewsA: How to copy files from one directory to another using regex?
When using | (also known as "pipe"), each of the parts (both before and after the |) must be a complete command. Basically, the output of the first command becomes the input of the second, so that…
-
3
votes2
answers151
viewsA: Why does changing a variable in a function not reflect in the passed variable itself?
The reason is described in documentation: Primitive Parameters (such as a number) are passed to functions by value; the value is passed to the Function, but if the Function changes the value of the…
-
1
votes2
answers68
viewsA: I can’t get the right timestamp time
The value of timestamp can be in different time units. The most common is to be in seconds or milliseconds, and each API/language usually works with one of them. In the case of Moment.js, it works…
-
1
votes1
answer191
viewsA: Swap the letters of the beginning of an email with another character
No need to regex, just go through the string and swap the letters for asterisks. When you find something that is not letter, stop: for ($i = 0; $i < strlen($email); $i++) { if…
-
2
votes1
answer46
viewsA: When trying to create date October 31, result is December 1
This happens because in a Date the months are indexed to zero (January is zero, February is 1, etc). And because you’re passing the value of the month equal to 10, so you’re actually creating dates…
-
2
votes1
answer79
viewsA: Why does changing items from a list with multiple assignment not work when I use "index()" inline in assignment?
According to the documentation, expressions are evaluated from left to right, and in case of assignments, everything that is to the right of the = is evaluated first. That is, in an expression like…
-
1
votes1
answer80
views -
1
votes1
answer47
views -
1
votes1
answer74
viewsA: How to remove string characters
strip receives the list of characters to be removed from the beginning and end of the string, i.e., you are removing all the zeroes and dots of the end, why "20,000" becomes "2". If strings…
-
7
votes2
answers1355
viewsA: What is SSL Pinning?
In a "normal" SSL (without pinning), the Handshake (the "initial conversation" that the client and the server do to connect) has several stages, and in one of them the server sends to the client its…
-
3
votes2
answers119
viewsA: Importing public packages into Deno
Heed, this reply contains opinions. I installed Deno these days (version 1.5) to "play" a little, so I’ll put my impressions, which can help with your doubts. One detail is that I started messing…
-
8
votes2
answers241
viewsA: Is there a difference between Number and parseFloat?
There’s a difference in some cases. For empty string or containing only spaces, or for null, Number returns zero and parseFloat returns NaN: var numero = ""; // string vazia…
-
2
votes2
answers114
viewsA: Most efficient algorithm to find indices of the 2 elements of a list whose sum is equal to a certain value
The problem is that index always scrolls through the list from the beginning to search for the element index, and you do this search several times (and as in many cases the difference is not in the…
-
2
votes2
answers73
viewsA: Using Regex to give match quotes after delimiter
Don’t use regex, use a parser json I know the regex "worked" for your specific case, but it won’t always work because regex is not the best tool to work with JSON. If you use what was proposed by…
-
4
votes1
answer64
views -
0
votes1
answer32
viewsA: re.search also validates invalid emails
See in the documentation that search returns None if nothing is found, but at no time does he cast exception, then he will not fall into the except. So just see if the return is None, something like…
-
3
votes2
answers119
viewsA: Search an array (list list)
Do not search using fixed indexes. What if the matrix has more than 3 records? What if you have less? Instead, make a loop through the headquarters and store the sector’s employees in a list: def…
-
2
votes1
answer39
viewsA: Degree of complexity
If n is even and greater than zero, the recursive call to n - 1, which is unique and therefore falls in the second if. There is another recursive call to n - 1, which is even (may or may not be…
-
2
votes1
answer62
viewsA: If always returning false no reduce()
It makes no sense to use reduce for that reason Read the documentation to understand what reduce makes. Basically, it takes a function, executes this function for each element of the array and…
javascriptanswered hkotsubo 55,826 -
3
votes3
answers341
viewsA: Sort a list based on two different criteria using Sorted
The problem of using itemgetter or tuples (as suggested in another answer) is that both elements will be ordered by the same criterion (either both in ascending order, or both in nonexistent order).…
-
3
votes11
answers37801
viewsA: Compute sum of digits of a number
Just to give one more option, and mention a case that was only cited - but not solved - in other answers (when the number is negative): # ajuste para o caso de n ser negativo n = abs(n) soma = 0…
-
2
votes1
answer974
viewsA: Attributeerror: 'Nonetype' Object has no attribute 'text'
Without more details, the problem seems to be in some of these lines: ICMS = det.find('.//nfe:vICMS', ns).text IPI = det.find('.//nfe:vIPI', ns).text quantidade = det.find('.//nfe:qCom', ns).text if…
-
3
votes3
answers342
viewsA: How to make a sum in a loop for or while?
Using x++ works because the operator ++ changes the value of x. So if you want to add seven, just do x + 7 does not work because it does not modify the value of x. The right thing to do x += 7 or x…
-
3
votes1
answer55
viewsA: Read file with list comprehension only works the first time
After you make the first for: txtfile = [item.split()[0] for item in fhand] The file will be read to the end. Then in the next iteration there will be nothing left to read, and so the second list…
-
4
votes2
answers81
viewsA: Compare two lists without interference of upper and lower case letters
When the user name is 'HIQUINHO', when calling lower he becomes hiquinho, and you compare it to the values on the other list, which only has 'Hiquinho', why he thinks this user has not yet been…
-
0
votes4
answers265
viewsA: Check if delta is less than zero (Bhaskara’s formula)
Just use if/else: from math import sqrt def raizes(a, b, c): delta = b ** 2 - 4 * a * c if delta < 0: print('Não existem raízes reais') else: dois_a = 2 * a if delta == 0: print(f'Raiz: {-b /…
-
1
votes3
answers148
viewsA: Count recurrences in a list
First, don’t use count, because each call needs to go through the entire list to count how many times the element occurs. That is, even if the list has no repeated element, for example, [1, 2, 3],…
-
2
votes1
answer33
viewsA: How do I make the code find the positions that x repeats and put them in output one per line? Python
If you want to print one position per line, make a for and prints each element one at a time. Another detail is that you don’t need to use defaultdict. If you only want the positions in which an…
-
1
votes2
answers61
viewsA: How do you filter a list with multiple words in python?
By your description ("if I don’t find the word sulfite paper I do a new search only for paper"), would do so: def busca_termo(termo, texto): # primeiro vê se o termo está contido no texto if termo…
-
2
votes2
answers188
viewsA: How to compare 3 variables?
First, you are calculating the areas - not the perimeters, as was said in the question - but anyway, the idea below would be the same. The problem is in this condition: if (areaX > areaY…
-
2
votes1
answer137
viewsA: How to create a regex equivalent to a BNF?
If the idea is to generate a regex equivalent to this BNF, then it would be something like this: import re r = re.compile('^[01]+$') print(r.match('010100111')) # retorna um objeto Match…
-
1
votes1
answer59
viewsA: How to use round in lambda function
If you want to call round within the lambda, just do this: lstNumerosF = [10.01, 7.03, 2.23] lstMapa = list(map(lambda x: round(x ** 2, 3), lstNumerosF)) print(lstMapa) # [100.2, 49.421, 4.973] But…
-
7
votes3
answers239
viewsA: Ordering arrays with Bubble Sort
The first thing is to put the entire algorithm within the function. If the 2 loops are part of the algorithm, so both should be there, you shouldn’t depend on a loop external function to function…
-
3
votes5
answers312
viewsA: Make a function that calculates the following sum:
In cases like this, you could have done the table test, that would have easily noticed the problems of your code: you do not exchange the signal to switch between subtraction and sum, and the more…
-
3
votes1
answer95
viewsA: Vector help in Java
First, if you want to create an array with fixed values, you can do so: String[] nomeAlunos = { "Roberto", "Carlos", "Flavia", "Vitor", "Higor", "Luiza", "Joana", "Isaque", "Gabriela", "Antony" };…
-
8
votes2
answers238
viewsA: Python dictionary and functions
Let’s go in pieces. First, let’s see what the / ago. If you declare a function like this: def f(a, b): print(a, b) It is possible to call it several different ways: # passando os parâmetros…
-
13
votes2
answers230
viewsA: What does the / +/g parameter mean in the . split() method?
That’s a regular expression (regex). The bars are the delimiters (they are not part of the expression itself, only serve to indicate that inside it has a regex). The expression itself is a space…
-
2
votes1
answer83
viewsA: formatting values of a dictionary
First, if the dictionary keys are 1, 2, 3, etc., there is not much advantage in using a dictionary. When keys are sequential numbers, this is a strong indication that maybe what you need is a list.…
-
7
votes2
answers584
viewsA: How do I draw random names from each list without repeating them
You can simply shuffle the lists and then scroll through them with zip (which serves to traverse several lists at the same time): from random import shuffle j1 = ['Jose', 'Bruno', 'Lucas',…
-
2
votes2
answers639
viewsA: Generate random numbers in 2d numpy array without repeating Python
You can use numpy.random.Generator.choice: from numpy.random import default_rng rng = default_rng() numbers = rng.choice(range(1, 171), size=(4, 4), replace=False) I used range(1, 171) so that the…
-
0
votes1
answer41
viewsA: Problems with append incremetando until the infinity in Python
Like the append is called within a loop infinite, of course the list will grow indefinitely. If the idea is that it has at most 35 elements, I think the best is to create it now with this size,…
-
0
votes1
answer87
viewsA: Regex in the input frame always valid
Short answer The problem is in the hyphen, in the final stretch (\d{3}\-?\d{4}). Instead of \-, simply put - (that is to say, \d{3}-?\d{4}). Long answer But why it worked in regex101? According to…
-
2
votes2
answers213
viewsA: Generate a list of prime numbers up to 1000. These numbers should form a list, Lprimos. then check in Lprimos whether it is prime or not
To generate the list with all primes up to a certain number, you can use better algorithms, such as the Eratosthene Sieve (it even has a complete implementation in this answer - but how looks like…
-
4
votes1
answer182
viewsA: Algorithm for transiting in the Cartesian plane
Initially I had understood that the rotation is only a change of direction, without leaving the place. But there is also the possibility to rotate relative to the origin, changing the position.…
-
1
votes1
answer46
viewsA: Javascript Difference between dates
You have to use Math.floor to always round down (instead of Math.round, which depending on the case may round up). In addition, it is necessary to deduct the total of days, hours and minutes from…
-
2
votes2
answers172
viewsA: Intersection between lists without repeating elements
An option is, for each element of A, check if it exists in B and does not yet exist in C: A = list(map(int, input('A: ').split())) B = list(map(int, input('B: ').split())) C = [] for x in A: # para…
-
2
votes2
answers297
viewsA: Python calculator does not return function
input returns a string, but you’re comparing its return with an integer (on the line if opcao == 1). To make this comparison the right way you can turn the return into a number (using…
-
0
votes2
answers168
viewsA: Would you like to make a code that spells out the word?
If you want to iterate through the characters of a string and at the same time get the position of each one, just use enumerate. In a nutshell, I would: palavra = input('palavra para soletrar:') for…
-
4
votes1
answer53
viewsA: Calculator with inputs using DOM
getElementsByClassName returns a list of elements (note that the name is getElements, plural). Then you should only take the first one from the list to have the button (as there is only one, it is…
javascriptanswered hkotsubo 55,826 -
3
votes4
answers406
viewsA: Return the missing element from a list of integer numbers
Like looks like be an exercise, "probably want" you do with a loop simple. But just to leave registered, you can resolve using set: def faltante(numeros): # intervalo contendo todos os números…