Posts by hkotsubo • 55,826 points
1,422 posts
-
2
votes2
answers271
viewsA: Allow certain characters in an input text
One solution is to use the attribute pattern of input: const campo = document.querySelector('#campo'); campo.addEventListener('input', () => { campo.setCustomValidity(''); campo.checkValidity();…
-
1
votes1
answer480
viewsA: Problem "List index out of range"
First you have to understand what is happening. Suppose I have this list: redline = [ 0, 1, 2, 3, 4, 5 ] It has 6 elements: the numbers from zero to 5. If I want to get a specific element, for…
-
2
votes3
answers326
viewsA: Convert date string without punctuation in Date Javascript format
Natively, the Date Javascript does not give many options to convert strings to a date (actually, few formats are in fact "official" and many others work differently depending on the browser). The…
-
0
votes3
answers2047
viewsA: average of values typed in python input
The question implies that you want to go through the digits of a number, see which one is bigger and which one is smaller, add them up and calculate the average. So let’s go... In his another…
-
1
votes2
answers59
viewsA: Turn time into number
All methods getXXX of a Date return numbers (see documentation of getHours(), for example), so you don’t need to use Number, value nor any other trick to convert to number, because by calling these…
-
4
votes1
answer594
viewsA: Where do the names "atob" and "btoa" come from?
Researching I found this question from Soen. The text below is an adaptation of several answers that have there (and some more addenda my). One of the answers mentions a tweet brendan Eich…
-
9
votes2
answers2803
viewsA: Power Calculator in Javascript
I’m assuming it’s an exercise (or a code for training), since to calculate power, it’s best to use Math.pow, as suggested in the other answer (inclusive, it treats other cases that your code does…
javascriptanswered hkotsubo 55,826 -
1
votes2
answers226
viewsA: How to compare instances of Localtime?
The class java.time.LocalTime has the methods isBefore and isAfter for you to compare, respectively, whether one time occurs before and after another. That is, instead of doing t1 > t2, you do…
-
9
votes3
answers1161
viewsA: Hanoi Tower - How does this recursive solution work?
First, the function is wrong, there’s a print the plus and the second recursive call changed the order of the towers: def hanoi(disc, ori, dest, aux): if disc == 1: print('Move disc {} from tower {}…
-
2
votes1
answer911
viewsA: Validate password fields using Javascript / Regex
Your regex is a bit confused. For example, the quantifier {0}, which means "zero occurrences". That is, you defined an excerpt that does not occur (or that "occurs zero times", which is the same as…
-
4
votes2
answers873
viewsA: How to know if a number is an integer or a float? And how to assign a type to a value inserted in the input function
In fact you do not need to check whether it is whole or float. Just use float(input(...)), which also accepts strings containing integers, so you convert what was typed to a number. I suggest we…
-
1
votes1
answer226
viewsA: Regex to validate a specific amount of sequence appearances " - "
You didn’t say which language or program you’re using, which would make it easier, because with a programming language it would be much easier to do the split and check that the list has 3 non-empty…
-
3
votes2
answers1177
viewsA: Limit number quantity in an input
I know it’s a "basic program" you’re probably doing to train (or just an exercise you’ve been given), but anyway it’s an opportunity to discuss and delve into some points. Given the description of…
-
1
votes1
answer79
viewsA: How to locate 2 matches that are interspersed with the same regex
I believe that there is no way to do this with a single regex processing the string at once (that is, with a single call to method Match), as the regex will evaluate from left to right, always…
-
0
votes3
answers10205
viewsA: How to format date in full?
From Java 8 it is possible to use the API java.time. One of the differences of this API to Date and Calendar is that now there are several different classes to represent dates and times. In case, as…
-
1
votes2
answers388
viewsA: Calculate working cut-off date
THE JSON1 returned by API is an array (as it is bounded by []), so he’s converted to a list (that is, its variable json_data is a list). And the way to go through the elements of a list is simply to…
-
0
votes2
answers2134
viewsA: Make input accept only positive numbers
Let’s go in pieces: int converts a string to a number, but if it cannot, launches a ValueError. So if the code came in if not n.isdigit():, it is because int(input(...)) worked and converted the…
-
1
votes1
answer210
viewsA: How to show all values of three matrices in a single message without using indexes
I would say that the teacher has already given all the necessary tips: "Use text concatenation in a variable. Then only one showmessage". "Tip: for to concatenate text after a showmessage". Then…
-
2
votes1
answer98
viewsA: Optional group is not being captured
You can use this regex: ^[\*].+?SemprePresente1=(\d{2}\/\d{4}).+?SemprePresente2=(\d{6})(?:(?!Opcional=)[^\*])+(Opcional=(\d+,\d+))?[^\*]+ See here it working in regex101.com. I made some changes to…
-
9
votes2
answers100
viewsA: Validate difference of 5 points for more or less
You can calculate the difference and use Math.abs to obtain the absolute value of this (i.e., the no sign value): let scoreboard_local_team = 22; let scoreboard_visiting_team = 26; let points = 0;…
javascriptanswered hkotsubo 55,826 -
1
votes2
answers310
viewsA: Regular expression to get number between two bars
Depends on how the URL is. If the URL has only an occurrence of /page/número/, an alternative is to use search (of module re): import re url =…
-
0
votes2
answers960
viewsA: Preg_replace for Preg_replace_callback
The function preg_replace not depreciated. What was removed in PHP 7 is the flag e, but since you don’t use it in any of the regex, no problem. Just to complement, it is possible to remove the…
-
4
votes2
answers1326
viewsA: How to work with timezones without using a timestamp?
Actually, none of the Mysql types (DATETIME and TIMESTAMP) store information relating to the time zone. Both have information about the date and time, but according to the documentation, there are…
-
4
votes1
answer139
viewsA: How to use Join in an object list?
Since you already have a list of people, you can use the function map: class Pessoa: def __init__(self, nome, idade): self.nome = nome self.idade = idade p1 = Pessoa('João', 15) p2 = Pessoa('Maria',…
-
1
votes2
answers171
viewsA: How to show values of an Array in a single message?
Why don’t you want to wear one for? Loops are basic structures of the language, and one of its uses is to walk through a list or array, to do something with its elements. You can even use the method…
-
2
votes2
answers115
viewsA: Remove only strings from a list
To know if a variable is a string, use the function isinstance, as already explained in reply from Fernando. Then just scroll through the list and disregard the elements that are strings:…
-
2
votes2
answers4470
viewsA: Convert date dd/mm/yyyy to yyyy-mm-dd’T'HH:mm:ss
From API Level 26 (required minSdkVersion >= 26, it is not enough to have compileSdkVersion >= 26), it is possible to use the bundle java.time. A key difference is that this API has several…
-
2
votes4
answers455
viewsA: What is the difference between parseint() and operator + before a string?
Complementing the other answers: when the string contains an integer (such as '123', '42', etc), there is no difference in the result. But when the string is not exactly an integer, the results may…
-
4
votes4
answers1946
viewsA: Incorrect Date/Time Formatting Nan/Nan/Nan Nan:Nan:Nan
The other answers have already provided solutions to get the date format, but I think it is worth an explanation about what was the error of your code. First you extract the value of timestamp using…
-
5
votes2
answers710
viewsA: Select "maximum" elements given a certain criterion in a "stream"
The initial version of the question asked: Is there any way to do it without using this Map explicit intermediary? Enough to use collectingAndThen: Stream<Elemento> stream =…
-
2
votes5
answers2539
viewsA: Regex to check if string does not end with certain characters
Complementing the other answers, there is another alternative: to check if something nay is something, it is often easier to check if it is, and simply reverse the result. So you could use this…
-
0
votes2
answers111
viewsA: Return initial condition after false
Yes, you need to make a loop (in this case, I will wear a while), and only stop it when a different option than "Sim" (or when the name entered is "Thanos", which is the other condition you said…
-
8
votes1
answer487
viewsA: Javascript bug that subtracts day from -1 and adds time in 2-digit months+days
According to the specification, the format in question ("year-month-day", format defined by ISO 8601 standard) should have the month and day always with 2 digits. Anything other than this will have…
-
2
votes2
answers338
views -
2
votes2
answers451
viewsA: Validate only one comma in input type ="text"
An alternative is to use the attribute pattern, containing a regular expression that validates the format (I removed some attributes from the input in this example, just to be more succinct): /*…
-
2
votes2
answers631
viewsA: How to verify the occurrence of a given string and return part of it?
how do I check via regex if the occurrence xxx rows returned. is there? And if there is, return only the integer value. In this case, you should first check if the string is in the given format (do…
-
1
votes2
answers61
viewsA: Read data, add it to a list and then print it
Within the while you are changing the same instance of Agenda (is not creating a new one, which I understood to be the intention). Moreover, if resp for "Y", he will go into loop infinite, for resp…
-
1
votes1
answer84
viewsA: Problem finding a solution in an array / list
If you want to print out the apartment data of the building, you can do this directly on main, since the class Predio has a getter to get the apartments: // dentro do main for (Apartamento[] andar :…
-
2
votes1
answer62
viewsA: Doubt regarding the Java two-dimensional array
Your modeling is kind of... weird. If the class Predio represents a building, an array of Predio actually represents several buildings (i.e., a street, a block, a condo, or something like that). An…
-
4
votes2
answers2021
viewsA: Sum with dates and strtotime in PHP
You are confusing dates and times with durations. Consider these two sentences: the movie starts at two o'clock in the afternoon the film lasted two hours In the first, "two hours" refers to a time:…
-
2
votes1
answer2479
viewsA: Generate even and odd random numbers with defined quantities
The question leaves some criteria open, so let’s see some possible approaches. It was not said, for example, the value limits. Can it be any valid integer number? In Java, a int can go from…
-
4
votes1
answer54
viewsA: Match between dictonary and array
First of all, your dictionary is wrong, because it repeats the same keys, overwriting the previous values, so in fact it only has this: {'NAME': 'CARLOS', 'AGE': '35', 'CITY': 'BAHIA'} Maybe what…
-
3
votes2
answers69
viewsA: Preg Replace to remove awesome font from the texts
Maybe it’s easier to use one parser HTML, such as the Domdocument, for example: $dom = new DOMDocument; $dom->loadHtml($html); // $html é uma string contendo o HTML $xpath = new DOMXPath($dom);…
-
5
votes2
answers95
viewsA: Remove specific set from the end of the regex array
If the excerpt ":PM" is always at the end of the string, just do: import re array = ['SOLDADO1:PM','SOLDADO2','SOLDADO3:PM','SOLDADO4','SOLDADO5:PM'] regex = re.compile(r':PM$') for texto in array:…
-
2
votes2
answers1383
viewsA: Print current month and the next two months
If you want to add months to one Calendar, use the method add, informing the field you want to add (in case, how I want to add the month, use Calendar.MONTH): Calendar calendar =…
-
1
votes3
answers133
viewsA: Regex capturing it all
Complementing the other answers, there is a corner case in their proposed solution. If HTML has a span commented within the span you want to take: <span class='pages'>1 de <!-- <span>…
-
3
votes3
answers629
viewsA: Python: Indexerror: list index out of range
Usually for problems in the code, it is interesting to put the test cases that gave error (which strings you used to test), so the others do not need to "guess" and can give more accurate answers.…
-
3
votes1
answer363
viewsA: Why when creating a Calendar, do I need to subtract 1 of the month?
According to the documentation of Calendar, the value of the month to be passed to the method set is indexed at zero. That is, January is 0, February is 1, etc. For this reason, 1 is subtracted from…
-
8
votes4
answers428
views -
15
votes1
answer1878
viewsA: How does String.prototype.normalize work in Javascript?
NFC, NFD, NFKC and NFKD normalisation forms are defined by Unicode. So well summarized, some characters have more than one way of being represented. For example, the á (letter a with acute accent),…