Posts by hkotsubo • 55,826 points
1,422 posts
-
3
votes2
answers328
viewsA: Swap the first characters of a String for another
In Java, when you print an array directly (System.out.println(array)), the array elements are not shown. Instead, the array type information and its hashcode are shown, as explained here. For the…
-
5
votes2
answers122
viewsA: How to view formatting numbers in javascript. Currency
The question is much confused. In the title you say you want to format "currency", but in your answer you format the number and put "Kg" in front (ie it is not a monetary value). In addition, you…
javascriptanswered hkotsubo 55,826 -
1
votes1
answer989
viewsA: How to quickly calculate the sum of dividers in Python?
A small optimization is that you don’t need to go up n, just go to the square root of n. And for every splitter you find, you actually found - potentially - two splitters. For example, if the number…
-
4
votes3
answers95
viewsA: Difference between creating a list with each iteration and using a list comprehension
When you do: square_differences = [(x[i] - y[i])**2] You are creating a list containing a single element: the result of (x[i] - y[i])**2. And every iteration of for, you are overwriting the value of…
-
2
votes4
answers747
viewsA: Get the most frequent and least frequent element from a list, plus the largest and smallest
To count the frequency of all elements in a list, just use a Counter. Then, to get the most and least frequent, use the method most_common: import random from collections import Counter # gera 10…
-
7
votes1
answer131
viewsA: What is the advantage of using Perl-compatible regular expression functions or POSIX-compatible ones in PHP? In what context do you use both?
As stated in the documentation, regex POSIX compatible functions (such as ereg and ereg_replace, among others) are deprecated in PHP 5.3, and were removed in PHP 7. So if you’re using version 7, the…
-
10
votes1
answer661
viewsA: How to show the Date object with the Timezone it was created in Javascript?
The Date javascript nay stores Timezone information. Despite the name, the Date is not quite a date (in the sense of representing a single value of day, month, year, hour, minute and second). How…
-
3
votes2
answers63
viewsA: Toggle the text of an element with each click
If you just want to keep changing the text every time a click occurs, you don’t need one loop, let alone register the same functions several times. Just register once, a single function that checks…
-
2
votes4
answers169
viewsA: Python - Return all cities of a.txt file in a percentage range
To compare whether the insulation is larger, smaller or equal to a certain value, you need to isolate it from the line and convert it to number. An alternative is to use split to separate the line…
-
4
votes1
answer177
viewsA: Python List Invert Values
You do not need to swap entries this way. What if the list has more than 2 entries? In this case, just use reversed to invert the lists: lista = [[1,11],[2,22],[3,33],[4,44]] inversos = [] for e in…
-
2
votes1
answer530
viewsA: Reduce the amount of if in Javascript
To update the list of female options, you can create a male mapping for the respective female possibilities, something like this: var mapMachoFemeas = { "albino": [ {"valor": "albino", "texto":…
-
1
votes1
answer218
viewsA: Simplest way to create circular array in Java
There is no circular list in native language types, so one way to solve it is to create a method that takes the list, the index and the offset, and returns the respective element: public <T> T…
-
0
votes1
answer209
viewsA: Function that transposes a matrix, using passage by reference
The matrix you created is equal to: | 0 | 1 | 2 | 3 | 4 | | 1 | 2 | 3 | 4 | 5 | | 2 | 3 | 4 | 5 | 6 | | 3 | 4 | 5 | 6 | 7 | | 4 | 5 | 6 | 7 | 8 | I mean, it’s a symmetrical matrix, which means it is…
-
1
votes2
answers172
viewsA: How to access an attribute in a Java Arraylist?
Your problem is not "access a private attribute of another class". You’re accessing them via getters correctly. The problem is that you are not entering the second while, so the totals are zeroed.…
-
4
votes2
answers193
viewsA: Define criteria for sorting a list of lists
One way is to create an "old-fashioned comparison function" (used in Python 2), which takes 2 elements and returns a negative number if the first is "less", a positive number if the first is…
-
3
votes4
answers669
viewsA: How to access indexes backwards within a list?
If you only want to get 3 specific elements from the list of notes, you don’t need to make one loop throughout the list. Just take the index of the entered element and from it calculate the others,…
-
2
votes2
answers194
viewsA: Condition to continue loop while invalid value
Every integer is less than or equal to 1 or greater than or equal to 2. Zero? It is less than 1 (and therefore <= 1). The 1? It is equal to 1 (and therefore <= 1). The 2? It is equal to 2 (and…
-
1
votes2
answers1468
viewsA: Java Multiple Numbers and Primes
You can simplify your logic, taking into account some things: only makes sense to test if the number is divisible by 4 if it is even (odd numbers are not multiples of 4) with the exception of 2, all…
-
3
votes3
answers116
viewsA: error in Count function
The other answers have already explained your mistake, I would just like to add one detail. Call count several times is not efficient for what you want to do, since each call of count scrolls…
-
3
votes1
answer84
viewsA: Search for entered values in an input
In doing RegExp('g1'), you are creating a regex that searches for the exact text "g1", and not for the value of what was typed in input id="g". Another detail is that you should only perform the…
-
3
votes1
answer292
viewsA: Where can I find the reference for defining dates of the dd/mm/yyyy format?
Use ISO 8601 format It depends a lot on what you want to do with your API, but in general, a specific date format for each location is a problem of presentation. Internally your(s) system(s)…
-
3
votes2
answers91
viewsA: How to generate a 2d matrix with one of the positions chosen separately having a different value?
Since you want all positions to be "B", except one of them (randomly chosen), which should be "8", you may not even need to create the matrix. Just generate random numbers for the row and column…
-
3
votes1
answer528
viewsA: Typeerror: randint() Missing 1 required positional argument: 'b'
According to the documentation, randint receives two parameters: a and b. The first indicates the minimum value and the second indicates the maximum value. In your code you are only passing one…
-
3
votes2
answers282
viewsA: Build list without repeated words from a file
An alternative is to save the words in a set, which is a native structure that does not allow repetitions. So, just read the file, read the lines of it, separate the words of each line and go saving…
-
4
votes3
answers444
viewsA: Different results when using != or > in loop condition
Modifying your program a little, putting the print within the while, we can understand what is happening: a = 80000 b = 200000 tempo = 0 while a != b: a += a * 0.03 b += b * 0.015 tempo += 1…
-
1
votes1
answer47
viewsA: Problem with regex in php
Change the regex to: $pattern = '#\.S\d{2}E\d{2}\.#i'; The markers ^ and $ indicate respectively the beginning and end of the string, so it makes no sense to use this in a split, since the pattern…
-
4
votes2
answers1221
viewsA: Code revision of the CPF class in Java
As it is a theoretical class (without a real requirement), I will take it as a premise that its responsibility is to represent a CPF (and not only validate or format, because then some things could…
-
2
votes1
answer223
viewsA: Import problem when compiling classes in different packages
Compiling files manually can be good learning to understand how things work, but in practice it’s best to use tools that automate this for you, such as Maven, Gradle, amid others. That said, let’s…
-
3
votes1
answer485
viewsA: Sort using tie-breaker criteria
Yes, you can do everything in one sort. If you want to make a specific sort, you can use Collections.sort passing a Comparator as a parameter. The idea of Comparator is to provide a customized…
-
2
votes2
answers122
viewsA: Add two array values and transform into a single Javascript variable
A version well basic calculator could follow this logic: keep a number guard the operation keep the other number if typed =, calculate the operation and update the display the result of the…
javascriptanswered hkotsubo 55,826 -
5
votes2
answers160
viewsA: What strpos() returns when the searched value is not found?
According to the documentation of strpos, the function takes two strings (which there are called "haystack" and "needle") and the return is (in free translation): The position where the needle is,…
-
2
votes2
answers773
viewsA: How to subtract several elements from a list?
As you want to subtract the first element from the others, an alternative is to do the loop from the second element: subt = lista[0] for a in lista[1:]: subt -= a In the case, lista[1:] creates a…
-
5
votes2
answers250
viewsA: Print struct data with read values from a text file in C
There is a problem in using feof this way. For example, if the file has a blank line at the end: 1 2.2 3 The while will read the numbers twice and the output will be something like: 1 2.20 3 32577…
-
4
votes2
answers159
viewsA: Decoding a conversation
You do not need to turn the code into a list, as strings can be indexed by position (the first character is at zero, the second at index 1, etc.). And since the positions of the code coincide with…
-
4
votes1
answer128
viewsA: Add 365 days to a date in an input type="date"
If you want to add with the date that was typed, it makes no sense to use new Date(), which takes the current date. You have to create the date based on the value of the input. The detail is that a…
-
1
votes3
answers140
viewsA: How do I place a conditional within a . foreach() in Javascript?
Just for the record, you don’t necessarily need to forEach or reduce, because you can go through the elements found by querySelectorAll with a for..of: const medicines = []; for (const a of…
-
1
votes1
answer169
viewsA: soma_ll function that receives a list of lists and returns the sum of all numbers
It doesn’t work because there comes a time when the generated sub-list is empty and a IndexError. For example, if the list is [1, 2]: the sum function lista[0] (paragraph 1) with soma_ll(lista[1:])…
-
1
votes1
answer601
viewsA: Convert number to binary
The code is very confusing and I really could not understand how it tries to convert a number to base 2. A factor that contributes to this is the names of the variables, very confusing and that do…
-
4
votes1
answer268
viewsA: Regex to capture unique numbers within a string
\d+ captures one or more digits (+ is a quantifier meaning "one or more occurrences"), so if the string is abc10xyz, he will capture the 10. If the idea is to capture only a single digit, remove the…
-
1
votes2
answers89
viewsA: recursive exchange
First, remember that using recursion nay is the best way to solve this problem. A simple loop by the list would solve. Anyway... One way to solve is to make the function receive the index, as you…
-
1
votes1
answer513
viewsA: Count number of occurrences of a number
You don’t need to create this list L, just read the numbers and see if they match the first number read: x = int(input()) n = int(input()) qtd = 0 for _ in range(n): if int(input()) == x: qtd += 1…
-
1
votes1
answer21
viewsA: How to convert all_timezones to another format
In fact all_timezones is a list of timezone names. If you want to create another list containing dictionaries (which in turn contain a sequential number and the name of the Timezone), just do:…
-
1
votes1
answer119
viewsA: How to get the table name of the first select Oracle?
Actually your regex doesn’t work because regular expressions are case sensitive by default (differentiate between upper and lower case letters). How the query has FROM and the regex from, no match…
-
2
votes1
answer291
viewsA: How to replace two or more hyphens or spaces with just one hyphen
If the idea is to exchange several spaces (from what I understood from replace which already exists) or several hyphens for a single hyphen, just use replace(/[- ]+/g, "-"). To character class [- ]…
-
0
votes2
answers1092
viewsA: How to create a C Calendar?
First, to display the month name with alignment, do not use \t. Enjoy the options that printf gives you: char *months[]= { "Janeiro", "Fevereiro", "Marco", "Abril", "Maio", // <-- removi o \t…
-
2
votes1
answer101
viewsA: How to stop reading from sys.stdin in Python 3 after reaching a bounded number of lines?
The way you described in the comments is a valid alternative yes (the difference is that I only need to test the size of the list if any element is inserted - that is, within the if, because there…
-
2
votes2
answers82
viewsA: List ordering returns "None"
The method sort sorts the list in-place (that is, modifies the list itself instead of returning another). And its return is None, so it makes no sense to assign your result to a variable. So instead…
-
6
votes2
answers349
viewsA: How to calculate the result of an arithmetic expression contained in a tuple respecting the operators' precedence?
Do not use eval (or "use sparingly") Yes, I know that "works", that the solution was very "easy", with a code "simple", as shown in another answer. I’m not saying she’s wrong, I just think it’s…
-
1
votes2
answers125
viewsA: Format string with durations
I’m afraid you’ll have to do the Parsing and the formatting manually. As it was not clear which formats were possible, I tried to do it in a kind of "generic": // método para separar segundos das…
-
2
votes1
answer629
viewsA: Program in python regarding the factor of odd numbers up to the one typed by the user
Having the function that calculates the factorial and the number typed N, just make a for by the odd numbers from 1 to N, and adding up their factorial: def fatorial(x): result = 1 for i in range(2,…