Posts by hkotsubo • 55,826 points
1,422 posts
-
3
votes2
answers2416
viewsA: How to get the first word of a text in Python?
The way to do it depends a lot on how your string is and what you consider to be "word". In your specific case, just take the first element of the list returned by split: frase = "Eu como…
-
2
votes1
answer183
viewsA: How to place dots at the beginning and end of the selected word as well as quotation marks
Just go to shortcut key preferences: Preferences → Key Bindings. In preferences default (editor on the left) you will see that there are already options for "Self-epair" for quotes, parentheses,…
-
1
votes2
answers436
viewsA: 1) Read a 10 x 10 matrix and write the location (row and column) of the highest value
Since you want both the largest element and its position, one option is to use enumerate, which allows the elements and their indexes to be iterated at the same time. Then just hold the position…
-
2
votes1
answer58
viewsA: List only the added files and modes in the git cast
One option is to use git diff --summary, for according to the documentation, it shows only some condensed information such as creation, renaming and mode changes. Then it would be: git diff…
-
2
votes2
answers148
viewsA: String handling with Regex
I would like to suggest some improvements to the solution of another answer (and also the one you posted in the comments). In many places you use the quantifier *, which means "zero or more…
-
1
votes2
answers193
viewsA: Print characters to a given letter and include line breaks at the end
First, eliminate the repetitions of your code. There’s no reason to call input before the while and then inside it. Just do it once and use it inside the main loop. In fact, since the program must…
-
1
votes2
answers382
viewsA: Shopping Cart Java
It makes no sense to create two Scanner's read from the same place (in this case from System.in). Use one. Another detail is this semicolon after the if: if ((resposta == 'S') || (resposta == 's'));…
-
1
votes1
answer79
viewsA: How do I pass an array as an argument in PHP and receive all the data in the shell?
Just pass the array elements separated by space: $return = shell_exec("sh teste.sh ". implode(' ', $param)); And in the shell script these values will be in the special variable $@, which can be…
-
1
votes2
answers159
viewsA: How do I repeat an algorithm in Python?
You don’t need a variable to control the loop (as suggested in another answer). You can make a loop and interrupt it with break: while True: valor1 = int(input("Insira o primeiro valor: ")) operação…
-
2
votes6
answers444
viewsA: Counting multiples of an integer is giving very different values than expected
If the sequence starts with a number n1 and it should have only the multiples of that number, just iterate from n1 in n1: count = 0 for _ in range(n1, n2, n1): count += 1 The third parameter of a…
-
3
votes2
answers224
viewsA: Help in college exercise
First start by giving the correct names for the variables. It may seem like a silly detail, but giving better names helps a lot when programming. At no time was mentioned the minimum value, only the…
-
4
votes1
answer140
viewsA: How to make the ":. 0f" format not round my number
Actually you are trying to solve the wrong problem. If you want the result of the division to be whole, use the operator entire-division // (two bars instead of one). So you don’t have to worry…
-
0
votes1
answer21
viewsA: Display array indices whose elements are greater than the average of them
You can print the positions as you iterate over them, or save the positions in another array and print them at the end: // Solicitar 5 números e calcular a média let soma = 0; let arrNum = []; for…
-
1
votes2
answers46
viewsA: Inserting error message to user
You don’t have to use continue. Just put the print in cases of error and only call the break if you do not enter into any of these cases: while True: x = int(input('Digite um número: ')) y =…
-
0
votes2
answers72
viewsA: How to decode a code that modifies vowels
An alternative is to create a Map that maps each character that will be replaced by its respective letter, and use a StringBuilder to make the replacements: String texto = "2n1v5rs0 5 t2d0 0 q25…
-
1
votes2
answers99
viewsA: How to save multiple read values to be used later
Keep the numbers in one list: numeros = [] # lista que vai guardar os números for i in range(1000): n = int(input()) if i == 0 and n == -1: # se o primeiro é -1, sai do loop break numeros.append(n)…
-
1
votes3
answers2369
viewsA: Definition of the day of the week in the Gregoriancalendar
The TimeZone has nothing to do with the definition of the week. This is controlled by Locale. So much so that if you do this: for (String zone : TimeZone.getAvailableIDs()) { TimeZone tz =…
-
0
votes1
answer167
viewsA: Loop the loop after performing functions
To repeat something n times, you can use a for along with range: for _ in range(n): # faz algo range(n) generates a sequence of 0 to n - 1 (which in practice will cause the loop itere per n times).…
-
6
votes1
answer669
viewsQ: Why do formatting options not work with lists, dictionaries, and other objects?
When I want to print a number or string, I can use f-strings (in Python >= 3.6) or str.format, and I can only pass the variable between keys, or use the formatting options. Ex: numero, texto =…
-
6
votes1
answer669
viewsA: Why do formatting options not work with lists, dictionaries, and other objects?
When you pass only the variable without any formatting option (print(f'{variavel}')), internally is being called the method __str__ of the same. That is, in the case of the class Teste, as she…
-
3
votes2
answers515
viewsA: Java 8 stream - performance improvement
Given your description, I don’t think your code is right: check which elements of the list p have value >= the list values q. If available, I need to store the position (index + 1) of each item…
-
0
votes2
answers71
viewsA: I would like to know how to put this form to read only numbers like this [1,70] and [66.2]
To another answer suggested to change the fields to input type="number", that apparently solves the problem. Don’t forget to put the step, to allow decimal values. In the example below, I used…
javascriptanswered hkotsubo 55,826 -
1
votes1
answer80
viewsA: How to pass values from one function to another?
Basically, when you do this: def dados_cliente(): Nome = str (input ('Por gentileza, informe o nome do cliente: ')) The variable Nome is local to the function dados_cliente, and cannot be accessed…
-
2
votes1
answer299
viewsA: How to access a private attribute in Python, I created an object from another class that has private attributes as well
First, understand that there are actually no private fields in Python. This is said in documentation (my emphasis): "Private" instance variables that cannot be accessed except from Inside an Object…
-
2
votes1
answer66
viewsA: Regex only in word without predecessor or successor of dots or bars
An alternative is: (?<![\w.])VERSION01\.5(?![\w.]) The regex uses lookbehind and Lookahead negative, to check something nay exists before and after. Both have the expression [\w.], which is a…
-
3
votes2
answers58
viewsA: Why do you think the higher value?
Notice that x = valor is inside the if. That is to say, x only receives the valor if the valor is greater than x (that is, if valor was greater than the largest number found so far, x becomes that…
-
1
votes2
answers265
viewsA: Split sublist in python
First let’s see how to do with a single list: def split(lista, tamanho): return [lista[i:i + tamanho] for i in range(0, len(lista), tamanho)] lista = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]…
-
1
votes2
answers173
viewsA: Turn a sentence into a list of letters of each word and then calculate the sum of the alphabetic numbers of those letters
You don’t need to create the list with the letters and then turn them into numbers. You can do this whole transformation at once: nome = "joao da silva pinto".upper() lista = [] a = ord("A") for s…
-
2
votes2
answers396
viewsA: Convert date string to ISO 8601 format (with "T" and "Z")
The format you want to convert to (with "T" and "Z") is defined by ISO 8601 standard. And the "Z" at the end indicates that the date and time is in UTC. The problem is that in the string we only…
-
3
votes1
answer29
viewsA: Undefined value in last iteration to traverse nested array
Its function does not return any value (it has none return within it). And in such cases, the "return" of it is undefined. Ex function comReturn() { console.log('eu retorno um valor'); return 1; }…
-
0
votes1
answer641
viewsA: Print in descending order in Java
The method main is the "input point" of a Java program, is where the program starts running. In case, you created a main in each class, which makes them "independent programs": each can perform…
-
1
votes1
answer88
viewsA: Problem checking for an array
From what we can infer, your board is an array of strings (String[][]). And since it’s an old game, then it’s 3x3. That is, it is an array of 3 elements, and each element is an array with 3 strings.…
-
2
votes1
answer1167
viewsA: Creation of python Dict
I didn’t understand what the parameter would be for keys, because it seems to me that you want to generate a new dictionary with all pages. So it would look like this: def novo_dict(self): d = {}…
-
4
votes1
answer89
viewsA: Print even numbers in a list
He prints the numbers because that’s what you had him do (put the print inside the loop - the detail is that your code actually prints the numbers 0, 2, 4, 6 and 8). If you want the amount, then…
-
3
votes2
answers441
viewsA: Check if a string belongs to a list
It is because you are turning the letter into a list. The operator in checks if something belongs to the list. Since the vowel list only has strings, it only makes sense to test if strings belong to…
-
2
votes2
answers256
viewsA: Convert string to date
If you consult the formats described in the documentation, will see that the abbreviated month name should be used %b, and for the year with 2 digits, it is used %y. But there is another detail:…
-
0
votes5
answers7529
viewsA: How to search recursively using grep
Just complementing the other answers, I think it is worth explaining why your command did not work (besides giving another alternative that was not mentioned). First, about the command: find . |…
-
2
votes4
answers1648
viewsA: Count consonants in the sentence
It’s no use just considering space, as done in your answer, because if a character like @, % or !, it will be counted as consonant. For example, if the typed text is "a b c,!&defXYZ @ ABC", your…
-
2
votes1
answer368
viewsA: Obtain the quantity of odd numbers, positions of the pairs and average of the positives, from a tuple of numbers
I do not know if it is a requirement of exercise to do everything in a separate function, and even more recursive functions, whereas there is no none need to use recursion and still can do…
-
3
votes1
answer105
viewsA: Regex to select only a number within certain Strings
An alternative is \[\w+\]\.+(\d+)\s+ms\.. Like the shortcut \w already takes letters, digits and also the character _, you don’t have to do \w+_\w+ (in fact this way forces to have a _, already…
-
2
votes1
answer313
viewsA: Regex to capture spaces, except within quotation marks
First of all, it is worth remembering that \s corresponds to spaces, tabs, line breaks, among other characters. So if you have a space and then a line break, \s{2,} will consider these two…
-
3
votes1
answer48
viewsA: Why can’t I print the values of a chained list?
This is because when printing an object, implicitly it is called the method toString() of the same. As in your case the class Elemento does not possess this method, so it uses which was inherited…
-
2
votes4
answers1041
viewsA: Doubt Exercise Python
Seeing the image, it was not clear whether or not to have a space between the #. In your code you put, but anyway, follows an alternative to both cases (I will not suggest the loops again because…
-
4
votes1
answer217
viewsA: How to check if a string is None?
Technically, there’s no way "a string can be None", because they are different things. String (more precisely str) is a class, while None is a constant built-in which represents the absence of value…
-
1
votes3
answers745
viewsA: Return the positions of the largest number in a matrix
If the largest element can repeat itself, then you need to have a list of all positions. If the number is equal to the largest, you add the position in this list. But if any other higher number is…
-
2
votes1
answer29
viewsA: Why is my function turning some strings into separate characters when sending them to the CSV file?
The method writerow takes as a parameter an iterable object and prints each element of this iterable as a separate column. How strings are eternal, and when iterating over a string each element is…
-
1
votes4
answers1171
viewsA: Help in Portugol (se e senao encadeado)
As other responses have explained, the senao is only connected to the last se. This means that the program tests all se's, and if you fall into one of them, you will print that the date is valid,…
-
2
votes2
answers446
viewsA: Take a string snippet between () and change it in Javascript
You can change your regex by placing more capture groups (i.e., more snippets between parentheses). This creates more capture groups and allows you to make the correct stretch replacement: function…
-
1
votes2
answers236
viewsA: Grab snippet of a string between () in Javascript
Assuming there are no nested parentheses, for example "(5 abc (7 xyz))", and that within parentheses there is only one occurrence of the number (no cases like "(123 abc 456 xyz 789)"), a way of…
-
3
votes1
answer183
viewsA: Python recursive method that returns how many times a given element appears in a vector
First of all, recursion nay is the best way to solve this problem (we’ll see non-recursive options at the end). And binary search only makes sense in ordered lists (even so, binary search serves to…