Posts by hkotsubo • 55,826 points
1,422 posts
-
0
votes1
answer58
viewsA: How to Backreferencing with Regex in PHP?
At first it would be enough to pass the regex and the substitution string directly, without using array: $Str = preg_replace('/<\b([abi]|strong)\b>(.*?)<\/\1>/', '$2', $Str); I use the…
-
5
votes1
answer835
viewsA: How to add days to a date with Moment.js?
To add a certain amount of days to a date, just use add: onCalcularData(date: any, dias: number): moment.Moment { return moment(date).add(dias, 'days'); } The return will be a Moment. But if you…
-
1
votes2
answers293
viewsA: Replace string chunk between 2 indexes
Python, strings are immutable, then there is no way to replace a piece of it. The way is to create another string. In your case, just take the piece from the beginning of the string to index 13, the…
-
2
votes1
answer93
viewsA: Repetition loop in Java
First: the use of Try-with-Resources is interesting in many cases because it already automatically closes the resources. But in the specific case of System.in, no need to close it (read more about…
-
1
votes3
answers231
viewsA: Problem Solving in Javascript
Arrays begin at zero (the first index is zero, the second is 1, etc). In addition, to check whether two conditions are true, the operator && (and) and not the || (or). That said, your job…
-
3
votes5
answers135
viewsA: Problems with arrays in a Python password generator
First, an important comment: the module documentation random clearly states that this should not be used for security and encryption purposes, and suggests the use of module secrets (available from…
-
2
votes3
answers227
viewsA: Handling of JAVA arrays
Arrays have a fixed size and there is no way to delete a specific position from it. The most you could do is set the position with null, but then you would have to check if the position is null…
-
3
votes1
answer2108
viewsA: Filter array whose elements are between two values
The logic of your function is wrong. You did: p <= valorMenor && p >= valorMaior Supposing valorMenor be 5 and valorMaior be 10, what happens if p for 8? The above condition checks if…
-
3
votes3
answers799
viewsA: Print different data from an array according to the element value
The problem is in your if: if( familia[i] == 'Pedro' ){ console.log(familia[i] + ' Sousa') continue } else if(familia[i] == 'Pedro'){ console.log(familia[i] + ' Macedo') } First you test whether the…
-
2
votes2
answers7986
viewsA: Make a Program that reads three numbers and shows them in descending order. Python
In general, for any number of elements, the simplest is to place the elements in a list and sort them, as suggested by another answer. But in the specific case of 3 numbers, and without using lists,…
-
1
votes1
answer42
viewsA: My regex is not working
The pattern is receiving a string, and within strings the character \ must be escaped and written as \\. Then it would be: Validators.pattern("\\(\\d{2}\\) \\d \\d{4}-\\d{4}") Also, notice that…
-
2
votes2
answers319
viewsA: I made a script that calculates the average of a class or student. But I couldn’t find a way to ask the user if he wants to do another calculation or not
All you have to do is make a loop to keep repeating what you want. And it is interesting to also have some output condition, something like this: while True: # faz o que precisa (pede as notas,…
-
3
votes2
answers128
viewsA: How to display a list always sorted as new items are inserted
If the list should save strings but you want to sort by numerical values, it is interesting to insert values only if they are numbers. For this you can use int to convert the string to number, and…
-
3
votes1
answer234
viewsA: Validate input by accepting specific amount of letters and numbers in javascript "for an RA, ex: p3087748"
Of course it has how to determine specific quantities. If you want a letter followed by 7 numbers, just change the regex to: /^[A-Za-z][0-9]{7}$/ If you want to define the minimum and maximum…
-
4
votes2
answers2087
viewsA: How to draw numbers from a list, randomly and without repetitions?
Just use random.sample, which already guarantees that there will be no repetition. But first a detail. You are asking the user to enter 15 numbers, so you need to save all of them on the list (the…
-
2
votes1
answer102
viewsA: I wonder if it’s possible to decrease my code?
First you could open the file within a block with, that ensures that the file will be closed at the end. Then you’re making one loop for nothing. The for line in texto runs through every line in the…
-
2
votes1
answer76
viewsA: How can I make regex recognize "partial results" in Javascript to mask an input?
In your job you do replace(/[^0-9]/g, ''), which already deletes all characters that are not numbers. This means that in regex you do not need to have \.?, nor \/? much less -?, as characters that…
-
1
votes1
answer176
viewsA: Call a function multiple times and return an array of objects
First, if you want to iterate through the amount of months the user types, you don’t have to make this whole complication of [...Array(etc)].forEach(etc). Just make a for simple, starting from index…
-
3
votes1
answer113
viewsA: Hyperbolic cosine approach - Python
The problem is that in the first iteration i Zero valley, then you end up calling potencia(x, 0). And that’s why the code always falls on else, making multiple recursive calls until the stack pops.…
-
1
votes1
answer88
viewsA: Python script that generates a main diagonal character square
The print() that makes the line break should be outside the else - in fact it must be outside the for internal also, as you should only change line after you have printed all of her characters. In…
-
1
votes3
answers199
viewsA: Cut unnecessary zeros from a string, without toFixed()
There is another option, no regex. First you need to turn the string into number. Unfortunately parseFloat only accepts the point as decimal separator, so there’s not much to escape from a replace…
javascriptanswered hkotsubo 55,826 -
3
votes1
answer75
viewsA: Split String into blocks, considering only letters or text in square brackets
In your regex you used [^)], which is "any character that nay be it )". And the quantifier +, by default, is "greedy" and tries to pick up as many characters as possible (so in case you have…
-
4
votes1
answer87
viewsA: Regex with lookbehind does not work in Firefox
Note: when asked, Firefox did not support lookbehind (therefore the error), but currently supports and the error no longer occurs. First of all, one detail: you don’t have to build the regex this…
-
3
votes2
answers311
viewsA: How to subtract 2 hours string
An alternative is to use the API java.time - see here the configuration and requirements needed to use it on Android (and here has more details about the API) - if you cannot use it, I have left…
-
2
votes1
answer91
viewsA: What’s the difference between SEQ and FOR I IN in shell script
In their specific example, both are similar. But they are not exactly the same thing. seq is used to generate a sequence of numbers, and nothing else. Ex: # apenas o valor final (inicial é 1) seq 5…
-
10
votes3
answers1292
viewsA: How to loop 'for' in 1 line?
Depends much what you want to do, and maybe you don’t even need the for. Do you want to simply print the list elements? So why not just do print(rank)? All right that will print [1, 2, 3, 4] and you…
-
2
votes2
answers49
viewsA: How do I solve the problem of this instantiation?
This is because Double is a class and double is a primitive type (in Java there is this differentiation, in other languages, not necessarily). And each type has a value default which is used when it…
-
2
votes4
answers112
viewsA: Check the amount of li’s inside a Javascript div
The way to count depends a lot on what you mean by "li’s inside a div". In your specific case, just do document.querySelectorAll('#collapse305 li').length, as your own answer indicated. But what if…
javascriptanswered hkotsubo 55,826 -
1
votes4
answers130
viewsA: Treat blank fields in calculation
I would like to suggest another solution, because it does not seem to me that you are doing it in the best way (take it as constructive criticism). If the idea is to have multiple numbers, each…
javascriptanswered hkotsubo 55,826 -
3
votes2
answers652
viewsA: Python 3.8 - How to format the display of the elements of a list in column
In addition to the solution itself (how to properly format the output), there are other details to look at your code. The function that fills the list is entering in the lista that was created…
-
1
votes2
answers157
viewsA: Calculate the number of characters of the largest word in a file
Your code, although "works", it is not ideal because you are browsing the file twice, unnecessarily. If the words are separated by zero, just create a counter for the current word size, and…
-
3
votes1
answer279
viewsA: How to calculate the current and future date and receive the difference in days
If you want the difference in days, just change to moment().diff(date, 'days'). Already about "calculate forward and not backward", you mean that instead of calculating data_atual - date, you want…
-
3
votes2
answers165
viewsA: Answer coming out only true for all elements, or false
The problem is you’re passing dias[a] for the function, which in this case will be a number. And within the function trueoufalse you try to pick up the attribute length of this number, but no…
-
4
votes2
answers505
viewsA: Count in recursive function
To understand recursion, you have to understand recursion :-) In the case of processing a list recursively, the general idea is usually: if the list is empty, there is nothing to do if not empty, I…
-
2
votes3
answers84
viewsA: Find largest element index in the list
Choose the appropriate data structure You created a list of tuples, but if the idea is to map each product with its respective price, a dictionary seems more appropriate. Not only does it make more…
-
9
votes1
answer259
viewsA: Creating regular expressions with a dynamic pattern is problematic? If so, is there a way to avoid the problem?
One way to "solve" is simply by escaping metacharacters, putting a \ before them. Something like this: possiblyUnsafeUserInput = possiblyUnsafeUserInput.replace(/[-\/\\^$*+?.()|[\]{}]/g,…
-
3
votes2
answers131
viewsA: How to restart the loop for the player to play more than once regardless of whether to hit the answer or not
If you want the loop continue, just don’t interrupt it. In your case, you are using break, interrupting the loop, then just don’t use it (or just use it when you really have to quit). It was unclear…
-
1
votes2
answers389
viewsA: How to display user name initials without their connectors?
You gave as an example "josé da silva", so I am assuming that the string will not necessarily have capitalized and lowercase letters correctly (as a name would normally have, for example: "José da…
-
2
votes2
answers69
viewsA: Make sure the entry doesn’t happen again, numerically and within a range?
If you want to check if a string contains a number and then validate if that number is between certain values, you do not need to use isdigit and then convert to int. Of course if the user always…
-
1
votes2
answers209
viewsA: Vowel count
It makes no sense to ask the person to type twice the word (who guarantees that the same word will be typed in both times?). Read the word only once. And if the idea is to exchange all vowels for a…
-
2
votes1
answer98
viewsA: How to improve this code? Simple Question in C
Whenever you have variables like algo1, algo2, algo3, etc, is a strong indication that you probably need to use an array. In your case, it seems to be exactly that, because you want to keep a…
-
3
votes1
answer354
viewsA: Regex in input Pattern is not valid, but the expression is functional
The problem with regular expressions is that they don’t work exactly the same in all languages/Engines/API’s, and a feature that exists in one will not necessarily be supported in another. In the…
-
4
votes2
answers503
viewsA: Validate password confirmation
getElementsByName returns a list (more precisely a NodeList) which may have several elements. So it does not work to catch the value from this list directly. How its elements possess a id, and Ids…
-
2
votes1
answer76
viewsA: Sum of Values within Compound Lists
First, you don’t need this index d. Just read the data and at the end you add everything in lista_total at once. Since the data are of different types (a name that is a string and several notes that…
-
3
votes2
answers502
viewsA: Associating positions with Python elements
To understand what happened in your code, a tip would be to do the table test. But basically, the problem is that you made a loop within another. That is, for each index k, you make another loop by…
-
4
votes2
answers378
viewsA: Prime Numbers with While and For
To see code working step by step, use a Debugger this link has several options, choose one and use (if you are using IDE’s, like Pycharm for example: they usually already come with a Debugger).…
-
2
votes1
answer475
viewsA: Regex to accept accents in first letter and last letter of first and last name
The problem with your regex is shortcut \b. It indicates a word Boundary ("word border"), that is, a position that has an alphanumeric character before and a non-alphanonumeric character after (or…
-
7
votes3
answers1204
viewsA: How to get the system date in Javascript?
You can use Date, but as you mentioned a concern with the time zone, there are some details to note. Contrary to what the another answer, one Date won’t come in the time zone the device is…
-
4
votes1
answer2915
viewsA: 'Nonetype' Object is not subscripteble in Python
According to the documentation, method sort sorts the list in-place (that is, modifies the list itself) and it is also said that "it does not Return the Sorted Sequence" (does not return the ordered…
-
2
votes2
answers135
viewsA: Problems with Nan when getting input values
As they say, the problem is that you carry the values of input before they are filled in, then their value is not a number, and so parseFloat returns NaN. But contrary to what he said the other…
javascriptanswered hkotsubo 55,826