Posts by hkotsubo • 55,826 points
1,422 posts
-
5
votes2
answers96
viewsA: Why does readlines display an empty list even with the file having data?
This is because readlines reads the entire contents of the file, and when you call it again, there is nothing left to read. If you want to save lines to use later, just use readlines only once and…
-
3
votes3
answers183
viewsA: Remove repeated elements using two lists
Here you say you want to remove from the list of people all who were invited. So a very simple way (assuming there are no repeat people) is: pessoas = ["Matheus", "Maria", "Felipe", "Tulio",…
-
5
votes3
answers224
viewsA: Variable becomes string without reason
Consider the code below: lancar = 'abc' def faz_algo(lancar): lancar = 1 faz_algo(lancar) print(lancar) # aqui vai imprimir o que? In his mind, he should print 1. After all, I created the variable…
-
3
votes1
answer101
viewsA: Data munging with python
If you want to work with dates, don’t use regex, use a date API. Dates are more complex than they appear and have rules that are difficult to validate only with a regular expression. For example,…
-
4
votes2
answers679
viewsA: Regular expression to validate a password with Python
For the first case, use \w does not serve, because this shortcut also considers digits and character _, and how do you want the _ be one of the separators, so he can not be part of the word.…
-
0
votes1
answer52
viewsA: Check if two elements of a list are numbers
The syntax of and follows the same idea of the other commands of Lisp, ie instead of doing: (condicao1 and condicao2) The correct is: (and condicao1 condicao2) Then in your case it would be: (defun…
-
1
votes1
answer165
viewsA: Choose between two numbers in Java
Of course you can use Random. If there are only two possible values to choose from, one option would be: public static Random rand = new Random(); public int escolher() { int n = rand.nextInt(2);…
-
3
votes1
answer94
viewsA: What is the "y" flag in regular expressions? What is its function?
It is used to indicate that the search must start from the position indicated by lastIndex, and it only gives match if found exactly at that position (different from a regex non-sticky, which checks…
-
2
votes1
answer67
viewsA: Regex to pick up everything in parentheses in certain situations
What you want is a parser, and not a regex. Research a specific one for the language in question and use it, it will take less work than a regex (and even if it takes "more" work, it will still pay…
-
0
votes2
answers64
viewsA: How to select methods in a function and add results in a list?
How do you do for j in FEATURE_METHODS, then I assume FEATURE_METHODS is a list (or any other durable object) containing the values "LA", "RF", etc. And within the loop you do: if FEATURE_METHODS ==…
-
0
votes1
answer87
viewsA: Function that returns first element of a list
Makes much time I don’t deal with Lisp, but from what little I remember: (defun pr(NomeDaCidade X Y) Here you define a function that takes 3 parameters (not a list). Therefore, to get only the first…
-
0
votes1
answer511
viewsA: I cannot increment a global variable in function parameter
In fact it’s wrong, because counter++ increases the value of counter in 1. But you should not increase by 1, but use the amount that was reported in the function. And in function resetCounter you…
-
3
votes1
answer96
viewsA: Nonstatic nested interfaces can be used independent of instance of the enclosing class?
According to the language specification, a nested interface is static for default. That is, the two forms below are equivalent: public class Foo { public interface Bar { void fazAlgo(); } } // ou…
-
3
votes2
answers201
viewsA: How to remove repeated elements from an object array by preserving the object that has a different id property than null
Although "cool", it is not always easier to think in a "functional" way, so before trying to do with reduce, why not use a loop simple? let userPermissions = [ { id: 1, description: "Cadastro de…
javascriptanswered hkotsubo 55,826 -
2
votes2
answers3288
viewsA: Adding values from an array
If you just want to keep numbers, why did you create a ArrayList of String? If you create a list of String, to add you will have to convert the String's in numbers, and if any is not (for example,…
-
2
votes1
answer126
viewsA: echo output with % and %
Those are just two of the many ways to manipulate strings in Bash. In this case, both serve to remove a snippet from the end of a string. The difference is that % removes as little as possible,…
-
4
votes1
answer98
viewsA: Why when ordering a list with Sort(), returns None?
According to the documentation, list.sort changes the list in-place (that is, the list elements are rearranged internally, rather than returning another list). There it is also said that "it does…
-
2
votes1
answer194
viewsA: How many times has the highest number been read?
Just use count, which returns the amount of times the element occurs in the list: lista_num = ... maior = max(lista_num) print(f'O maior valor é {maior} e ele ocorre {lista_num.count(maior)} vezes')…
-
2
votes2
answers181
viewsA: Reading and processing XML file from a CVE (Common Vulnerabilities and Exposures) database with Python
First you load the CVE list from the file lista.txt: # carregar lista de CVE with open('lista.txt') as arq: # remover as quebras de linha do final das linhas cve_list = [ linha.rstrip() for linha in…
-
1
votes2
answers61
viewsA: Get the number of files contained in a directory
According to the documentation of os.system, the return is the Exit status of the command (and not the output of the command itself), and zero generally indicates that the command ran normally with…
-
2
votes2
answers860
viewsA: Check if number is equal to the sum of squares of 4 consecutive prime numbers
If you make a site search will find several different algorithms to determine if a number is prime (if you search in Google then you will find many others, and you will see that your algorithm is…
-
6
votes4
answers1030
viewsA: Sum the n odd terms ,using Loop for ,without using list, allowed functions:input,int,print and range
A detail: in the title it is said that you want to add the "n odd terms", and in the code is the message "Digite o número de termos". Does that mean that n is the amount of odd numbers, right? For…
-
2
votes2
answers228
viewsA: How to make a program of tables with command structures restriction?
There are some errors in this code. The first is the semicolon right after the while and for: while(valor <= 10);{ ^ aqui for (int i=0; i<=10; i++);{ ^ aqui When you put that ; shortly after…
-
1
votes1
answer252
viewsA: How to change the attribute of an element in javascript?
getElementsByTagName returns a list of several elements (to be more precise, returns a HTMLCollection), then you need to go through this list, and for each element, change the style of the same: for…
javascriptanswered hkotsubo 55,826 -
3
votes1
answer51
viewsA: How would I let this expression of mine beyond the character - (less) be accepted the + (more) as well?
Just add the + inside the brackets: <input type="text" placeholder="Não são permitidos caracteres especiais" pattern="^[+A-Za-zÀ-ú0-9., -]{5,}" required /> ^ aqui Another detail, the way it…
-
7
votes3
answers303
viewsA: When I compare two strings to the "bigger" and "smaller" operators, what am I comparing?
According to the documentation: Strings are Compared based on standard lexicographical Ordering, using Unicode values. That is, the lexicographic comparison is made, taking into account the Unicode…
-
1
votes1
answer620
viewsA: Implement queue (FIFO) using an array
If we look at the language specification, we will see that an array of int is initialized with all its values equal to zero. So if I have this: public class Fifo { private int lista[]; public Fifo()…
-
7
votes1
answer85
viewsA: How does this loop work to check duplicates?
According to the documentation, indexOf returns the first index that has the given element. Ex: let array = [ 1, 2, 3, 1, 4]; console.log(array.indexOf(1)); // 0 The number 1 is in two positions of…
javascriptanswered hkotsubo 55,826 -
5
votes3
answers167
viewsA: Why when rounding the sum of two numbers, the result is Nan?
As the other answers have said, the problem is because toFixed returns a string, then numero1 and numero2 will be strings. And when you use the operator + string, they are concatenated and the…
javascriptanswered hkotsubo 55,826 -
8
votes2
answers294
viewsA: Regex does not take all span tag strings
Do not use regex for Parsing html Since the question has the tag beautifulsoup, why not use that library, which is made just to make Parsing and manipulation of HTML instead of regex? If HTML is…
-
1
votes1
answer57
viewsA: I’m not able to create array with keys in Java
The creation of Java arrays is a bit boring. This way you tried only works if it is in the variable’s own declaration. Ex: String[] nomes = {"fulano", "ciclano"}; But if I do it in another line,…
-
4
votes1
answer237
viewsA: Regex for positive, negative, sum and subtraction number tokens
The problem is that the shortcut \D corresponds to a character (any character that is not \d) and therefore this character will also be part of the match. So he picks up the character before the -…
-
0
votes3
answers347
viewsA: Create list with the sum of consecutive equal numbers
One mistake is in the first if. How do you do the for i in range(1, len(lista)), what happens when i arrives in len(lista) - 1? For example, for the list [ 1, 2, 2, 2, 3, 4, 4 ], which has 7…
-
2
votes2
answers1637
viewsA: How to print 4 triangles patterns next to each other in Python?
You can enjoy existing options for formatting, since at bottom the problem is to align texts to the right or left, and the language already has it ready. For example, in column "a", we have strings…
-
2
votes2
answers1231
viewsA: How to add text to a specific line in Python txt file?
To another answer suggests that you upload the entire file to memory (as that is what readlines() do), and then write all the lines again in the same file. It may even work and give no problem in…
-
4
votes2
answers142
viewsA: Problems for player play order in Tic-tac-toe
The problem can be verified by making a simple table test in function players. Basically, it starts with n = 0, I mean, every time you call players(), the first thing she will do is set the value…
-
2
votes3
answers238
viewsA: Update label every time you click a button
That function soma seems unnecessary to me, as you just update the contents of the button directly: def bt_click(): lb['text'] = int(lb['text']) + 1 Since the button text is a string, I use int to…
-
6
votes1
answer1155
viewsA: Get the smallest positive number that is divisible by all numbers from 1 to 20
Basically what you want is to calculate the MMC (common minimum multiple) between these numbers. So it’s more of a mathematical question than pythonic. Suffice it to consider that: for 3 numbers x,…
-
2
votes2
answers205
viewsA: Store read names in a list and randomly choose one of them
According to the documentation, random.choice takes a sequence and returns one of the elements of this sequence (chosen randomly). In case, you are passing the variable aluno, which is a string, and…
-
5
votes1
answer92
viewsA: What’s the " operator" on Bash?
^ is the operator "or exclusive" (bitwise XOR). Basically, it takes the binary representation of the numbers involved and applies the following rules, bit by bit: if both bits are equal (either 0 or…
-
5
votes1
answer177
viewsA: How to reduce the number of loops to calculate the smallest difference between all the numbers in a list?
An alternative is to first sort the list, and then go through it only once, calculating the difference between each element and the next. As the list will be ordered, it is not necessary to…
-
2
votes3
answers129
viewsA: Recursion - add numbers to an array
After you call countup(n - 1), the lines below are executed yes, but only after this call returns. To make it simple, let’s see what happens when you call countup(2): countup(2): n is equal to 2, so…
-
4
votes3
answers516
viewsA: How to compare two JSON objects with the same elements in Python
If you print the return of the API’s, ie if you put a print(url.text) within the functions flights and flightPlans, will see that their return are JSON arrays: [{'AircraftID': 'f11ed126-bce8 etc...…
-
13
votes2
answers185
viewsA: Add 1 day to a date
In your case, you do not need to assign the return of setDate to the variable: let data = new Date(); data.setDate(data.getDate() + 1); console.log(data); console.log(data.getDate()); According to…
-
3
votes4
answers21920
viewsA: How to transform string into character array?
It depends on what you mean by "character". The other answers work very well on "ascii world", but nowadays the definition of character became so complicated that I think is worth exploring some…
-
0
votes4
answers333
viewsA: Bring only the values of the Python keys
Based on your comment: I had to change the keys because if not, the update that is the merge would not replace the current one. Is there any other way? Yes, and the other answers indicated a way,…
-
4
votes2
answers339
viewsA: How to remove an object from a list during an iteration over the list itself
To understand what happens, let’s modify your loop: for i, x in enumerate(lista): print(f'Pegando elemento {i} = {x.ent}, {x.sai}') if x.sai == 4: lista.remove(x) x.ent = x.ent * 10 I use enumerate…
-
11
votes2
answers583
viewsA: A recursive function can replace while and for?
First I’ll answer the title ("A recursive function can replace while and for?") in general, we will then talk about the specific code of the question. A recursive function can replace while and for?…
-
1
votes5
answers6866
viewsA: Indicate whether a character is a vowel or a consonant
Just complementing, and being a little pedantic about the title ("Indicate whether a character is a vowel or a consonant"): I understand that the function should return True if you receive a vowel,…
-
4
votes1
answer71
viewsA: Is there a better way to group "Ors" in Python without having to type the variable all the time?
You can use the operator in: letra = input('Digite uma letra: ') if letra in ('a', 'e', 'i', 'o', 'u'): print('Vogal') Of course if you just want to do the check and then you won’t use the letter…