Posts by hkotsubo • 55,826 points
1,422 posts
-
14
votes1
answer271
viewsA: Why does "parseint(0.0000005)" return "5" in Javascript?
According to the documentation, parseInt first converts the argument to string, and then evaluates this string. This is also described in language specification, is the first thing that parseInt…
-
1
votes1
answer40
viewsA: Creating matrix with the Scanner class
What happens when you type anything? This thing is put in s, and then you go through the whole matrix and put s in all the elements of it. If you want each element to have a different value, then…
-
3
votes2
answers102
viewsA: My code is only returning half of what it should
The problem is in the condition while len(retorno) < len(lista). Basically, you remove an element from the original list when it is inserted into the result. Then when the list retorno has 5…
-
5
votes1
answer47
viewsA: Javascript recursiveness
"f" returns a parent or mother object No, the father and mother’s objects are passed as arguments for valueFor, whose results are passed as arguments to f. Note the parentheses (I put some extra…
-
3
votes1
answer64
viewsA: Only get snippets between brackets
cannot cut and separate using special characters such as square brackets, asterisks, and mathematical operators Of course you can. But instead of making a split, I find it easier to pick up only the…
-
1
votes5
answers145
viewsA: Check if a string is composed only of '0' and '1'
You said you want "convert binaries to decimal" (and unless it’s for learning purposes, I would use parseInt, which although more direct, has some details to consider - detailed below). Anyway, one…
-
2
votes1
answer200
viewsA: Create a contact list and separate it into two other lists
Your lists contatoNome, contatoIdade and contatoNumero are saving all values, so it’s no use adding them to lista1 and lista2, for then both will have all the values too. In fact I suggest reviewing…
-
1
votes6
answers3314
viewsA: Get the local time in javascript with zeros?
One option is to use method String.prototype.padStart to handle from scratch to left (but for that you need to turn the values into string first): function format(val) { return…
-
0
votes2
answers69
viewsA: How to replace file extensions?
If you want to work with filenames, you could even manipulate the strings directly, as indicates the another answer. But you also have the option to use a specific module for such, such as pathlib.…
-
3
votes2
answers84
viewsA: How to access the position of multiple matchs of a regex in a string?
Just to give another alternative (because what I would really use is finditer, in accordance with another answer already explained). You can use search indicating the position in which the search…
-
3
votes2
answers87
viewsA: What is the relationship between unhashable and mutable?
As seen in question you quoted, what hash tables do is calculate the hash value of each key, so you know which table position the element will be in. In the case of Python, objects can implement the…
-
4
votes1
answer32
viewsA: Return the elements of a randomized list
It is possible to return all array elements randomly? Yes, just use the function shuffle module random: from random import shuffle arrTeste = ["Junior", "Gabriel", "Henrique"] shuffle(arrTeste)…
-
4
votes1
answer50
viewsA: Why does C syntax work to create variable range limit in bash?
If you consult the Bash manual (typing man bash in your terminal, or reading the online version), will see the following: Brace Expansion is performed before any other expansions, and any characters…
-
3
votes2
answers87
viewsA: Regex to catch single word
You used the flag g (this letter "g" after the second bar), which causes all occurrences to be replaced. Then the first thing is to remove it. But that doesn’t solve all the problems, for example:…
-
2
votes2
answers514
viewsA: Subtract extract months between two javascript dates
To another answer already explains how to get the dates of the date Picker whenever one is selected. But I would like to mention one detail in the calculation. Getting the difference in months is…
-
5
votes3
answers68
viewsA: Regular expression to take one or more occurrences that precede and follow a given letter
As pointed out in the comments, the solution below will bring false positives if the phrase has any words other than numbers and has an "and" between them. So see if that’s what you really need...…
-
8
votes2
answers229
viewsA: If Python strings are immutable, how can we change them with the replace() method?
This is an alteration of the original string or not? Not. Let’s follow step by step (in a simplified way) what happens: s = "banana" Here we create an object of the string type (to be more precise,…
-
2
votes3
answers72
viewsA: How to manipulate cells from a vector?
An alternative is to store the sum so far, and use it to update each element: vetor = [2, 1, 20, 5, 17, 19, 14, 4, 18] soma_acumulado = 0 acumulado = [] for n in vetor: soma_parcial = n +…
-
5
votes2
answers53
viewsA: Process command line arguments
Just inform that "age" is a positional argument, removing the hyphens and leaving only the name: parser.add_argument('idade', type=int) # retire o "--" I also included the type, indicating that it…
-
4
votes1
answer103
viewsA: What is the Unicode flag "u" in regular expressions? What is its function?
This flag changes some aspects of how regex treats the string. Interpretation of code points in Surrogate pairs For example, if the string has emoji (PILE OF POO). It is a "character" (in the sense…
-
0
votes1
answer39
viewsA: Error using local time as a condition
You are making an unnecessary turn, converting the date to string and then taking pieces of the string and converting to number. You don’t need any of this, date and time objects already have…
-
1
votes4
answers1308
viewsA: What is the purpose of an empty parenthesis "()" in a regular expression?
I believe you’ve seen this question (at least it is the same regex), and there are also listed some possible uses for this resource (which I explain in more detail below). It is worth remembering…
-
4
votes1
answer67
viewsA: What does an expression between parentheses mean followed by an expression between brackets?
Let’s go in pieces... (2, 0.5) is a tuple containing two numbers: the 2 and the 0.5. In a tuple, we can access its elements separately, through its indices, the first index being zero, the second is…
-
1
votes2
answers47
viewsA: I need the highest and lowest note of the matrix, but when I print out a vector with the three notes what do I do? I’m using min and max
In your case, you can check the biggest and smallest in it loop who reads the notes: maior = float('-inf') menor = float('inf') for l in range(0,5): for c in range(0,3): nota = float(input(f"Aluno…
-
1
votes2
answers134
viewsA: Make a program that calculates the value of H*S, being:
Note that to calculate h and s you are always adding up (or subtracting) 1/i (that is, 1 divided by i). But note the definition that the numerator is not always 1, it also has to change. And in the…
-
2
votes1
answer56
viewsA: the program does not read the file line it should read
The problem is you call readlines afterward of having read the first line (when you do for line in alunos, every iteration of for a line is read). That is, the first line has already been read, and…
-
4
votes2
answers65
viewsA: Print an Array in <li> line by line with javascript
His element i.z is an array, so if you want each item of it to be separate on its own li, make another for to travel it. Don’t forget to put the li's within a ul or ol. And give better names for…
-
6
votes1
answer91
viewsA: What is Regexp Match Indices?
The idea of proposal is to return the initial and final index of the match found, and also the capture groups, when present. Before this proposal, by using RegExp.prototype.exec,…
-
1
votes2
answers85
viewsA: Javascript regex schedules
No need for regex. If the format is this and you have already validated (or if you are "sure" that you always receive a valid string), simply separate the parts with split and then get the first…
-
3
votes1
answer35
viewsA: What is the best way to get values within multiple tags in multiple HTML files
As explained above here (and here, here, and mainly here), do not use regex to read/manipulate/do Parsing html. The ideal is to use libraries specific to HTML/XML. I will give an example with the…
-
5
votes1
answer92
viewsA: Show current date in a particular Timezone
According to the API of Openweather, the field timezone contains the difference from UTC in seconds. Thus, one way to get the local time in the indicated Timezone would be to use toLocaleTimeString,…
-
5
votes1
answer218
viewsA: Check if it is vowel or consonant and if it is lowercase or uppercase without using ready-made functions
You don’t actually need this array of letters, just check if the char is between the letters "a" and "z": void minusculaMaiuscula(char letra) { if ('a' <= letra && letra <= 'z') {…
-
4
votes3
answers871
viewsA: How to separate characters from a string to a Python list?
Complementing the another answer, there are some cases where use only list may not be enough. See this example: print(list('pá')) print(list('pá')) Although it does not seem, the two lines above…
-
3
votes1
answer58
viewsA: How to not write the header to a CSV every time you update the file
An idea is to check if the file already existed (before opening it, since when opening with the option a, it will be created). Then you only write the header if it did not exist: nome_arquivo =…
-
11
votes2
answers399
viewsA: How does Python structural matching (match declaration) work?
The Pattern matching is not the same as a switch. In the example code that is in the question, it may even look like it, but if we go into the details, we will see that it is quite different.…
-
1
votes3
answers144
viewsA: Calculate the value of a multiplication using only summation and subtraction operators
Your code already works (for positive numbers, because if they are negative, you will need an adjustment, explained below). Anyway, we can improve a little... Basically, multiplication is nothing…
-
0
votes2
answers70
viewsA: Problem adding elements to the array
If you really want to use an array, the way is for you to control the position where the next one should be inserted (taking care not to oversize the array). Something like that: public class Teste…
-
3
votes2
answers160
viewsA: Prevent Insertion of Zalgo Text in Inputs
To detect a zalgo text, first we need to understand how it is done. Unicode Combining Characters In the Unicode there is the definition of Combining characters. Basically, in a way well summarized,…
-
6
votes3
answers190
viewsA: How to get the index of the first positive element in a list?
The question asks that the index of the first positive element, but one of the answers is returning the element itself. Therefore, a small modification would suffice: def…
-
2
votes1
answer100
viewsA: How to create specific validations using Regex
Maybe regex is not the best solution. Instead, you could simply separate the data and treat it separately. Assuming that each record is in a row, a "skeleton" of the general idea would be: $dados =…
-
3
votes1
answer154
viewsA: How to hyphenate between string characters
Although the expression is the same, you just noticed a fundamental feature of regex: they don’t work exactly the same in all languages/Engines/tools, because although the "basic" functioning is the…
-
1
votes3
answers58
viewsA: I cannot convert mongodb files to json in python using bson in python
i receive a string object and not a json Remember that JSON is not a "type", it is only a data format. What the API’s do is map this format to structures proper to each language. In the case of…
-
3
votes2
answers115
viewsA: Make a sequence of letter combinations start with a specific letter
To start from the "F", just... put the "F" at the beginning. But then it was not clear what you want... You want it to start from "F" and then go to "Z", excluding all combinations that have the…
-
6
votes1
answer348
viewsQ: How to check if a year is leap in Java?
How to know if the current year - indeed, if any year - is leap? Since I can have only the numerical value of the year, or an object representing a date (i.e. Date, Calendar, LocalDate, etc), the…
-
6
votes1
answer348
viewsA: How to check if a year is leap in Java?
There are several ways to do it, and it varies according to the data you already have and/or the Java version. I have only the numerical value of the year If you already have the numerical value of…
-
7
votes2
answers213
viewsA: Like "clone" an Inputstream?
As it was said that the methods doSomething and doSomethingElse consume all the stream, so I understand that you both need all of her content. Therefore, an alternative is - as stated in another…
-
3
votes2
answers53
viewsA: How I take information from within an Array from the index
You don’t need slice. If you want another array, then... create another one, and add in it the elements you want. In this case, just go through the array of positions and insert the respective…
-
6
votes2
answers107
viewsA: Using do/while in Javascript to request 10 numbers from the user
If you want only the number of numbers over 50, no matter what they are, so you wouldn’t need to store them in an array. Simply increment the counter when the number is greater than 50: var i = 0,…
-
6
votes2
answers455
viewsA: What is the new Javascript Temporal object?
To another answer has already given an overview, I would just like to complement a few points. Obs: as the API is still subject to change, I haven’t gone into the examples much. I’ve only put a few…
-
4
votes9
answers17879
viewsA: How to identify if a string is a palindrome?
Complementing the other answers... Most ended up using the Slice [::-1], which generates another string with the characters in reverse order. This actually works, but if the idea is just to check if…