Posts by hkotsubo • 55,826 points
1,422 posts
-
1
votes1
answer149
viewsA: Regular expression case insensitive in a dataframe
According to the documentation, it is possible to pass a second parameter to extract, containing flags which alter the behaviour of regular expression. In this case, just use the flag re.I, which…
-
1
votes1
answer56
viewsA: Python recursive function printar correctly
If the size of the sub-lists is fixed, then the value of b should not change. But you were adding 1 in the recursive call, which increases the size to be considered. In fact what should change in…
-
2
votes2
answers169
viewsA: Read multiple values and average
The problem is that when it is typed -1, you add this value to the average, then in your test (typing 10, 10 and -1) the value of the variable media is 19. So the average ends up being 9.5. You…
-
4
votes1
answer186
viewsA: How to filter multiple items within an array
If typed "PCP DIST2", you want to Filter the elements you have both tags "PCP" and "DIST2", or may be the elements that have at least one of them? Regardless of the criteria, one way to solve is to…
-
3
votes2
answers77
viewsA: Using integers as Java Locks
What happens to the Integer is the same as with any object you pass to the statement synchronized. According to the language specification, when you do synchronized(expressão), happens the…
-
2
votes2
answers852
viewsA: Insert elements in the correct position in an ordered list
One of the problems is in numeros.insert(menor < num < maior, num). The expression menor < num < maior is a comparison (checks whether num is among menor and maior), whose result will be…
-
1
votes2
answers1698
viewsA: Check which array only has even numbers
The problem is you’re coming out of the second loop once you find an even number. That is, if you have an even number and you have some odd number afterwards, the odd number doesn’t even have a…
-
6
votes2
answers1699
viewsA: How to compare each character of a Java String?
Just for the record, to check that a String contains some character, do not need to traverse all characters in one loop. Just use the method contains: if (texto.contains("?")) {…
-
8
votes2
answers1239
viewsA: How does the localCompare() method work?
Basically, what localeCompare and Intl.Collator offer is a way to compare strings taking into account specific rules other than "standard comparison". It is often said that both serve to consider…
javascriptanswered hkotsubo 55,826 -
3
votes1
answer90
viewsA: Invert string containing numbers
It happens because you’re dealing with text, and not with numbers. Although the text contains digits from 0 to 9, they are still characters, not numerical values. Only that the char, despite the…
-
5
votes4
answers1335
viewsA: How to use . replace() to remove Python strings
Looking at the documentation of the method replace, it is said that: Return a copy of the string with all occurrences of substring old replaced by new I mean, if you do string.replace('alguma…
-
3
votes2
answers88
viewsA: Problem with Python print formatting
When you pass several parameters to the print, by default it puts a space between these parameters. But you can change this behavior by passing the parameter sep: for i, impares in enumerate(impar):…
-
3
votes2
answers398
viewsA: Play again, not "clean" board - Old Python game
Can greatly simplify code and improve/correct various details: Choose the appropriate data structure For the board, you have created a dictionary with the keys "pos1", "pos2", etc. Whenever you have…
-
2
votes2
answers168
viewsA: String.split() function with separator containing brackets and asterisk
The method split receives a regular expression as a parameter (regex). And some characters have special meaning in regex. The brackets create a character class, and the asterisk is a quantifier…
-
9
votes2
answers332
viewsA: How does the computer "know" the alphabetical order when comparing two chars?
How the program knows that the alphabet is from "a" to "z", without a definition? Of course there is a definition. Like a college professor used to say, computers are "dumb" machines because they…
-
2
votes2
answers99
viewsA: Why is my code giving Undefined?
Running your code, I did not get the error reported. Either way, you’re complicating the code for nothing. One of the errors was to put all the classifications in the same array, and with that you…
javascriptanswered hkotsubo 55,826 -
1
votes2
answers822
viewsA: Regex to pick sequence of letters or numbers
Your regex was only taking one number ([0-9]) followed by one or more letters. With the detail that [a-Z] (with capital "Z") gives error, see. Anyway, if you want it to have letters or numbers, just…
-
5
votes2
answers295
viewsA: How to create a simple markdown with PHP?
The regex does not accept __foo_bar__ because of the character class denied [^_], which corresponds to "any character that nay be it _". And how foo_bar has a _, no one is found match. To accept…
-
3
votes2
answers40
viewsA: How to use "for" to work with data in an array whose amount of varied data is unknown?
The value of the key products is also an array, as it is bounded by [ and ]: "products": [ ... etc ] So just walk it with foreach also: foreach ($array as $value) { foreach ($value->products as…
-
4
votes2
answers286
viewsA: Remove characters that are before another
How is an exercise, probably the teacher wants you to use a loop, and then you can use the code of another answer. But only to record an alternative: public String elimina(String frase) { return…
-
3
votes4
answers2686
viewsA: Create multiple lists containing random numbers
To generate a list of random numbers, you do not need to make a loop and add the numbers one by one. The module random already has two functions ready for this: choices and sample. For example, to…
-
5
votes5
answers427
viewsA: How to run faster a code that calculates the number of factorial digits of a number?
The problem is that the factorial grows very fast, and in a short time you will be multiplying huge numbers, which is quite time consuming. Just to give you an idea, the factor of 108 (which is the…
-
2
votes2
answers112
viewsA: function does not execute regex equal words
First, your regex does not just search for words that end in "mind", but any word that has at least one character before "mind": let s = 'seus dementes, a mente engana frequentemente, plante…
-
1
votes1
answer173
viewsA: Bucket Sort in Python: The array is only partially ordered!
Is there any reason to use the numpy? Unless there’s a specific reason, I’m going to show you a real list solution. The same question applies to the class BucketSort, needs to be a class? Anyway,…
-
1
votes2
answers1060
viewsA: Make a program that inverts an integer number with two digits
No need to string numbers You started well, using math to get the digits of the number separately. Just keep using math to get the final result: # ATENÇÃO: o código só funciona para números com 2…
-
1
votes1
answer74
viewsA: How to do this Regex in Delphi
First, {0,1} means "at least zero and at most 1", which means that the first digit is also optional. But since it seems to be mandatory, then remove it from there. The second part after the point:…
-
5
votes1
answer1109
viewsA: Regular expression to validate car plate
It’s not quite right because [^0-9] accepted whichever character other than a digit from 0 to 9. That is, it can accept space, punctuation marks, accented letters and other alphabets, emojis, etc…
-
1
votes1
answer80
viewsA: Attribute mutator generate Typeerror if it does not receive a number
In doing novo_telefone == int you are comparing the value of the new phone with the class itself int (and will never be equal). To know the type of the variable, one option is to use isinstance: def…
-
0
votes2
answers181
viewsA: Validate password confirmation field, why does confirming field not work?
To another answer explained the problem of using only length to compare passwords, but would like to suggest some more details. The if's to test the various password conditions can be improved. I…
-
1
votes2
answers98
viewsA: How to create a program that checks the entry of a variable and only proceeds until it is valid (Type - Value)? Python
I would do different. Instead of just testing if it is a number, the function could already ask you to type again, and return the value only if it is valid. The solution of another answer, although…
-
1
votes1
answer54
viewsA: How to reduce the number of elifs to find the letter that is most used at the beginning of the names?
Specifically for counting letters, use a dictionary instead of a list: cont_letras = {} for c in range(qtn): nome = str(input('Nome: ')).strip().lower() cont_letras[nome[0]] =…
-
1
votes1
answer97
viewsA: How do I make a program to invert a password by ignoring the # and _?
You didn’t put the class code PilhaInt, but let’s assume that it is complete and has a method pop, which pops the top element and returns it, and which also has a method to check if it is empty. So…
-
1
votes2
answers81
viewsA: Compare dates - Text Mining
What’s in row[83]? If it is exactly the date in the "dd/mm/yy" format and nothing else, so you don’t even need to regex, you can do the Parsing directly: from datetime import datetime data =…
-
1
votes3
answers162
viewsA: Another alternative to not repeat this "function" three times?
I would use a list of lists instead of variables n1, n2, etc (such as probably is an exercise, and exercises tend to have artificial limitations like "cannot use [useful native resource that makes…
-
5
votes3
answers1145
viewsA: How to remove spaces from a string in Python without also removing line breaks?
If you want to replace two or more spaces by just one, an alternative is to use regular expressions (regex), through the module re: import re s = " minha \n string do python " sem_espacos_a_mais =…
-
5
votes1
answer331
viewsA: Top 3 the biggest numbers in the list and their indexes
An alternative is: lista = [5, 7, 2] indices = sorted(range(len(lista)), key=lambda i: lista[i], reverse=True) print(indices) # [1, 0, 2] range(len(lista)) creates a sequence of all indexes in the…
-
1
votes3
answers54
viewsA: error in percentage?
The other answers explained the problem (use || instead of &&), but in fact you don’t need two conditions in if's. For example, in this if: if (salario < 600.00) { printf("\nIsento da…
-
2
votes4
answers422
viewsA: What is the best way to convert an int array to String in Java?
Just to complement the other answers, from Java 8 you can use String.join. But as this method should receive one or more instances of CharSequence and int's are not CharSequence's, we still have to…
-
3
votes1
answer43
viewsA: How to compare datatime to a date
If you’re using DateTime, there’s no reason to mix with strtotime. There is also no reason to transform the DateTime string (using format), just to pass this string to strtotime. Just turn the…
-
5
votes3
answers264
viewsA: Receive a string and put keys in it
Within a f-string, the characters { and } serve to indicate that you will use the value of the expression that is between them. If you want the characters themselves { and } be printed, you should…
-
4
votes3
answers170
viewsA: Remove the brackets from the Sorted() function
sorted returns a list, and when you print a list, the elements are shown in square brackets, so it’s not that "sorted returns the brackets", is that the brackets are part of the textual…
-
10
votes2
answers123
viewsA: Accentuation using regex
According to the documentation, the shortcut \w does not consider accented letters. One simple way to solve is to include accented characters in regex: var palavras =…
-
3
votes1
answer179
viewsA: Printing tabular form lists with generators
His second attempt generated several None because the comprehensilist on is using the return of print(num), and print always returns None. Anyway, from what I understand, you want to have the first…
-
2
votes2
answers79
viewsA: Using functional programming features to remove a word list from another list
map serves to transform the list elements into something else and in your case you have nothing to transform, because you just want to filter. Since you want all the elements of one list that are…
-
0
votes1
answer51
viewsA: I cannot capture regex from the end of the string with the Java replace method
According to the documentation, the method replace nay works with regular expressions. What you want is the method replaceAll. This yes works with regex: String s = "gabriel";…
-
14
votes1
answer209
viewsA: Why, when an iterator is not used, is an emoji "broken" into two parts?
You probably already know what a Unicode code point is (if you don’t know, read here). But in summary, every character (including emojis) has an associated numeric value, which is called code point.…
-
2
votes2
answers399
viewsA: Extracting words from a long text and creating statistics on them. What’s wrong?
An initial idea is to do the split not only by spaces, but by any character that is not part of a word: from collections import Counter import re r = re.compile(r'\W+') c = Counter() with…
-
2
votes1
answer352
viewsA: How to separate the name of packages in the Intellij IDEA directory tree?
Testing in 2019.3.4 Community Edition. Option 1 In the Project View, click on the gear and uncheck the option "Compact Middle Packages": Thus, packages will no longer be shown "together", but…
-
4
votes2
answers5692
viewsA: How to print all the contents of a list in Python
A simple way to do this is: postagens = [1, 0, 5, 9] print('Índice:', *range(len(postagens))) print('Likes: ', *postagens) The asterisk serves to make the unpacking, that is, when making…
-
2
votes1
answer27
viewsA: Record an index-specific value and treat the values
By "neighbor", I am assuming that it is the elements that are immediately next to it. For example, if the list is [5, 6, 9, 10], the neighbors of 6 are the 5 and the 9, the neighbors of 9 are the 6…