Posts by hkotsubo • 55,826 points
1,422 posts
-
2
votes1
answer43
viewsA: Vector error - Arrayindexoutofboundsexception error?
Arrays are indexed to zero (the first position is 0, the second is 1, and so on). As you stated your arrays with size 30, their positions go from zero to 29, but you’re trying to access position 30…
-
3
votes2
answers352
viewsA: How to delete negative elements from one list and add them to another in python?
As explained here, here and here, remove elements from a list in the same loop that iterates over it can cause problems and does not work in all cases (test the code of the another answer - to first…
-
2
votes2
answers243
viewsA: Transform [hh:mm] into float (ex: 01:30 ==> 1.5) in python
If you want to add up these values, then in fact they are not "hours", but "durations". They are different concepts: one timetable represents a specific time of day. Ex: the meeting will be at two…
-
6
votes1
answer75
viewsA: Function to pick up days of the week
Basically, in that if: if(diasemana == 0) { diasemana = dw[0] } else if(diasemana == 1) { diasemana = dw[1] etc... You are doing "if the day is zero, take the zero index, if it is 1, take the index…
-
5
votes1
answer85
viewsA: How to take system time and add minutes to it in Javascript?
padStart returns a string, and when you "sum" a string with a number, you’re actually concatenating the number into the string: let s = '12'; // "s" é uma string console.log(s + 30); // 1230 So you…
-
1
votes1
answer89
viewsA: Get last day of the week returns date equivalent to one more day
The problem is not the generated date, but - probably - how you are showing/printing it. Using your example and showing the dates in 2 different ways, we have: const primeiroDiaSemana =…
-
1
votes1
answer87
viewsA: Format dates including offset
That value -03:00 is the offset (the difference with respect to UTC - then in this case it would be "3 hours unless UTC"), and according to the documentation, just use the Pattern X to display it.…
-
2
votes1
answer244
viewsA: Error involving Nonetype
The problem is here: def a(lista): return lista.append(str(input('Nome: ')).lower().strip()) The error occurs because append returns None: lista = [] result = lista.append(1) print(result) # None…
-
0
votes2
answers150
viewsA: Regex to remove the comma with a previous space
If the commas you want to delete always have a space before, just include the space in the regex: let texto = 'Cód: mrkk-918278 ,Título: Blusa-02 ,Preço: R$ 60,50 ,Qtd: 1 ,Cód: mrkk-918277 ,Título:…
-
3
votes1
answer60
viewsA: Checking for data inside a list of lists
You have a list of lists (each element of lista is another list): lista = [ # primeiro elemento de "lista" ['2746', '8512.20.21', '2 - Estrangeira - Adquirida no mercado interno, exceto a indicada…
-
6
votes3
answers195
viewsA: List without negative number
No need to create the variable outside the while with an artificial value only to enter it. You can do so: numero = [[], []] while True: valor = int(input('Número: ')) if valor < 0: # número…
-
5
votes2
answers87
viewsA: How to remove all elements from a list equal to a certain value
Don’t do like the another answer (which has been deleted), because removing elements from a list in the same loop that iterates on it does not work in all cases. Ex: array = ['sim', 'sim', 'sim',…
-
5
votes1
answer83
viewsA: Display conditional text snippet with regex
The idea is to first check if the tag has the same value as the array, and make the replacement accordingly. If the value is the same as the array, I remove only the tags and keep the text between…
-
9
votes2
answers186
viewsA: Doubt about Number.isInteger() in JS. Number is an object or function?
Number is an object or function? Both. Technically speaking, Number is a function: console.log(typeof Number); // function But it’s a "construction function", which allows instances to be created…
-
2
votes4
answers361
viewsA: How to count occurrence of strings within a dictionary made up of lists in Python?
Specifically speaking of your answer, len already returns an integer, then do int(len(etc...)) is redundant and unnecessary. Do only count = len(pesquisa[nome]) // 2 would be enough (using the…
-
4
votes2
answers81
viewsA: How do I replace characters from a string, right to left, cumulatively?
In Java, String's are immutable, then methods that "alter" the String in fact return another String containing the changes. Therefore, you should take the return of replace and assign to variable…
-
3
votes1
answer46
viewsA: Use of generators with while results in infinite loop
When you use yield, is creating a Generator Function, and according to the documentation, her return is always an object of the type Generator. We can see that changing your code a little bit:…
-
4
votes3
answers70
viewsA: First decrement in Javascript with post-decrement operator ("number-") differs from "number - 1"
This is because numero-- returns the value of numero before to decrement it: var numero = 10; console.log(numero); // 10 // primeiro retorna o valor atual (10) e depois decrementa…
-
1
votes2
answers60
viewsA: Where are the syntax errors?
The problem is on this line: destruir = destruir_petecas % num_amigos Notice that you are using the function destruir_petecas in the calculation, but the correct is to use the argument num_petecas:…
-
1
votes1
answer57
viewsA: Add the integer value of one list to another. EX: [9,9,9]+[0,0,0,1]=[10,0,0,0]
Without getting into the merit that this is not a good way to add up numbers (I’m assuming it’s just for language learning purposes), one idea is to use reversed to traverse from last to first…
-
1
votes2
answers165
viewsA: How can I get the highest value of a matrix with Lambda function in Python
As his matriz is a list of lists (a list in which each element is another list), use reduce this way you will only go through the first level (ie you will be comparing whether one list is larger…
-
4
votes4
answers140
viewsA: How to print more than a maximum number of a list
While the other answers work, there’s one detail I’d like to comment on. They are calling max several times, no need. To better understand, we will modify only a little the program: def…
-
1
votes3
answers125
viewsA: Check whether an item exists within an array, using a variable
The problem is how to access the attributes of an object. Consider this simple example: let pessoa = { idade: 20 }; console.log(pessoa.idade); // 20 console.log(pessoa['idade']); // 20 The object…
-
0
votes1
answer80
views -
3
votes1
answer266
viewsA: Table in Python
You can greatly simplify the code. For example, when dividing 1 / cotação_dólar, the result will already be a float (because the price is already one float), so you don’t have to force the result to…
-
2
votes1
answer322
viewsA: Extract data from all rows of a file and create a dataframe
If the format is always this, you can use a regex to extract all the data at once, go saving the results in a list and at the end create the dataframe: import pandas as pd import re r =…
-
2
votes1
answer237
viewsA: Error "Exception in thread "main" java.lang.Arithmeticexception: / by zero" in java
The problem is in the function that calculates the factorial. Notice that fatorial starts with zero. If the number is, say, 2, he enters the else, and within the for you multiply fatorial for i. But…
-
3
votes1
answer187
viewsA: How to interrupt the execution of jQuery each?
According to the documentation: You can stop the loop from Within the callback Function by returning false. That is, it is sufficient that the function of callback return false. Below is a…
-
5
votes2
answers108
viewsA: Given an X number, check if there are 2 elements in a list that together result in X
You don’t have to go through the entire list twice. Just go from the current index to the end. For example, I start from the first element and test the sum of it with all the others (from the second…
-
3
votes4
answers326
viewsA: How to change the value of an object array?
You almost right, just needed to put the result of multiplication in the property idade: const usuarios = [ { nome: 'Diego', idade: 23, empresa: 'Rocketseat' }, { nome: 'Gabriel', idade: 15,…
javascriptanswered hkotsubo 55,826 -
0
votes1
answer612
viewsA: Function to calculate the average of two notes in python
You’re calling mediax(i, j), but where are these variables i and j? Nowhere. You are trying to use variables that do not yet exist, hence the error. You have to pass the notes, ie, mediax(nota1,…
-
4
votes2
answers141
views -
4
votes1
answer88
viewsA: How to access a function within a python dictionary?
When you do oi() (with the parentheses) is calling the function (i.e., is running it) and its return is placed as the dictionary value. But since you didn’t put any return in it, then it returns…
-
2
votes1
answer67
viewsA: Tuple FOR loop inside other tuple
The problem is that the dictionary is overwritten with each iteration. In the first iteration, for example, you set the name to "Willian", and in the second iteration the name is set to "Carla",…
-
2
votes2
answers91
viewsA: replace more than one char utitlizando . replace()
Depends on what you need. I need to remove two "x" characters and "%" That means you want to remove the x and the % regardless of where they are, or only if one is after the other (only if it is…
-
5
votes2
answers495
viewsA: Remove the R$ from the tolocalestring
Just do not format as "currency", and set the amount of decimals to 2: let valor = '123456789.947'; console.log(parseFloat(valor).toLocaleString('pt-br', { minimumFractionDigits: 2,…
javascriptanswered hkotsubo 55,826 -
1
votes2
answers74
viewsA: Find a string inside a python list
The problem is in this for: for i in range(worksheet.nrows): #itere sobre os itens da aba lista = (worksheet.row(i)) You iterate through the worksheet lines, but with each iteration of for to lista…
-
1
votes1
answer60
viewsA: create a program that reverses the numbers using while, but my program will always ignore 0 at the end, how can I fix it?
The problem is that you are confusing a number (a numerical value) with its representation (a string/text representing this number). For example, the number 2 is just a "concept", an idea: it…
-
1
votes3
answers75
viewsA: Why is my code "swallowing" string on a list?
As explained here, here and here, remove elements from a list in it loop that iterates on it can cause these problems. To illustrate better, let’s modify a little the loop: letras = ["A", "B", "C",…
-
2
votes1
answer61
viewsA: How do I check if my input is number or text?
According to the documentation, Double.parseDouble makes an exception (a NumberFormatException) if the string does not have a valid number. Then just capture this exception to see if it went wrong:…
-
1
votes1
answer89
viewsA: Formatting a dictionary using regex, based on a large database
First, if you just create a dictionary like this: example_dict = {"host":"146.204.224.152", "user_name":"feest6811", "time":"21/Jun/2019:15:45:24 -0700", "request":"POST /incentivize HTTP/1.1"} It…
-
0
votes1
answer41
viewsA: How to create an object by requesting data from the user and running from a function?
Just make the function return the employee who was created. Then you pass this employee to the function you modify, and inside you change the data you need. Ex: def registrar(): # retorna um novo…
-
0
votes1
answer123
viewsA: Recursive Hanoi Tower with Play Counter
You can make the roles receive the current move number, and return that updated number. And as the total of moves to N disks is always 2N - 1, you can print this value directly: def hanoi(): def…
-
0
votes2
answers65
viewsA: How to convert a separate string into comma and letter "and" at the end to an array from the existing numbers?
You are ignoring the result of the second split and ends up playing his own list[i] in the results. The same applies to flat, which returns another array, but you simply ignore the return. One way…
-
1
votes1
answer219
viewsA: Separate list of names according to regex notes
You don’t need to read the entire contents of the file at once and then iterate through it with finditer. If each student is in a row, you can read one row at a time (for small files it may not make…
-
0
votes1
answer50
viewsA: Multiplication of 3 matrices
To multiply 2 matrices, the number of columns of the first must be equal to the number of rows of the second. That is, if the first matrix is M x N (M rows and N columns), the second must be N x P…
-
1
votes1
answer248
viewsA: ParseException error
The problem is basically what is described here: when mixing calls from nextLine and next with nextInt and nextDouble can cause some unexpected problems. This is because methods that read numbers,…
-
1
votes1
answer98
viewsA: Function returning only one value
The problem begins here: def calcular_salario(dicionario, nome): for nome in dicionario: In the first line, you define that the function has the parameter nome, but in the second line, uses this…
-
2
votes3
answers52
viewsA: How to fill out a list?
If you just want to print the result, you don’t need to save to another list, or modify the lista1. Just go through the lists and print according to the values: lista1 = [0, 3, 7, 10, 15] lista2 =…
-
0
votes2
answers65
viewsA: Check if there is a specific value within a java bi-dimensional array
This logic is wrong (I suggest you do the table test, it will be clear what is wrong). Basically, you go through (I believe) all the elements, and if it is equal to 1, arrow the variable ganhador2…