Posts by Woss • 73,416 points
1,476 posts
-
3
votes2
answers812
viewsA: How do you capture two integers with space between them in python3?
The function input will always return a string, regardless of the content read, then whether it is necessary to obtain two integers from a string, separated by a comma, the logic will be basically…
-
2
votes3
answers160
viewsA: What is the logic behind this function?
To maniero’s response explained clearly that the problem in the implemented logic was to change the structure being iterated and its examples could not be more didactic, so I would just add a…
-
0
votes1
answer70
viewsA: How to check if there is a repeated string in a list?
Just use the method count of the object list: cores = ['azul', 'amarelo', 'vermelho', 'preto'] print(cores.count('azul')) # 1 cores = ['azul', 'amarelo', 'vermelho', 'azul']…
python-3.xanswered Woss 73,416 -
5
votes4
answers11856
viewsA: Higher and lower value problem with while. (No list!)
Your code: 1 N = int(input("Digite N: ")) 2 i = 0 3 ma = 'maior' 4 me = 'menor' 5 me = x 6 while i < N: 7 x = float(input("Digite um número: ")) 8 ma = x 9 me < ma 10 i = i + 1 11 if x >…
-
2
votes2
answers1513
viewsA: Calculating Series in Python
The first series: S1 = x + (x-1) + (x – 2) + ... + (x – n) Can be simplified so that the calculation is O(1) by grouping the terms in common: S1 = x + (x-1) + (x – 2) + ... + (x – n) = (x + x + x +…
python-2.7answered Woss 73,416 -
1
votes1
answer428
viewsA: Primes in python A-z A-Z letters
Just set a function that returns the number that represents each letter: def get_ord_of_char(char): if 'a' <= char <= 'z': return ord(char) - ord('a') + 1 else: return ord(char) - ord('A') +…
-
4
votes3
answers268
viewsA: Error when calculating a loop value
It is because you mixed the logics in the same code snippet. while count < len(notasAlunos): if notasAlunos[count] < menorNota: menorNota = notasAlunos[count] if notasAlunos[count] >…
-
3
votes1
answer143
viewsA: Regular Expressions in python
You can do this by implementing a generator that makes use of the library re searching each line of the file for all the words that satisfy the pattern defined by the expression. An outline of this…
-
1
votes2
answers355
viewsA: Get CSV list with Python
Like you showed, the problem is that your CSV file is not formatted properly as there is a blank line between the records. Not that it invalidates, in fact, the format, but basically every blank…
-
7
votes2
answers807
viewsA: Primitive type int() in Python
The two forms are perfectly equivalent when considering only the result produced, but the readability of the code differs. Namely, the native function input always returns a type value string, even…
-
0
votes2
answers356
viewsA: Counter in a dictionary value
Python has, in addition to the dictionary structure that can be used, a native structure in the library collections for the solution of this problem: Counter. Just store all the votes in a list:…
-
3
votes3
answers1913
viewsA: Return last index of Array
Given that the array starts at 0, you just have to access the position related to the size of the array minus one: const valores = [1, 2, 3, 4, 5]; console.log("O último valor é:",…
-
3
votes2
answers364
viewsA: Logic to get the lowest user read value
Numbering the lines of your code for ease: 1. def menor(size): 2. size = size 3. vet = [0] * size 4. menor = 0 5. for i in range(size): 6. vet[i] = int(input('Digite os valores: ')) 7. if vet[i]…
-
3
votes2
answers6045
viewsA: How to ask questions for the user in Python, without running constantly?
Although the other answer produces the expected result, I do not think it is a good solution, since if you need to add more options nicknames, you will have to add other conditions in the structure.…
-
4
votes2
answers1691
viewsA: Algorithm to determine prime numbers between 2 and N. What is wrong?
The error is in the position where you have checked the number of splitters. When checking within the loop itself j, any number that has at least two divisors will be considered as prime. For…
-
4
votes1
answer507
viewsA: Calculation of the maximum value for an expression of natural numbers using Python
In fact its solution has a lot of manual work and some language addictions. To facilitate understanding, let’s start by reading the values of m and n: m = int(input('m:')) n = int(input('n:')) In…
-
2
votes1
answer180
viewsA: Slow matplotlib (Python) to plot a 2-D graph?
If we consider that the object tempo is an eternal 3600 values, in the code for k in range(len(tempo)): plt.plot(tempo,tensao, color='black') you would be generating the same graph 3600 times, which…
-
0
votes1
answer2620
viewsA: How to check if file exists (asynchronous)
Just use the method fs.access. fs.access(path, fs.constants.F_OK, (err) => { console.log(err ? 'não existe' : 'existe'); }); But stick to the documentation: Using fs.access() to check for the…
-
3
votes3
answers99
viewsA: Best practices for setting up the IF condition
As discussed in the question Concatenation or data sequencing: which performs best?, This kind of performance concern about language doesn’t make much sense. It is known that PHP was not created to…
-
22
votes3
answers376
viewsA: NSA Challenge - "Thirteen men and a shipment"
You can do this with just a repeat loop. Whereas they have N coins, when it is divided between 13 pirates, 3 coins remain, which means that N-3 is a multiple of 13 and therefore, N-3 % 13 = 0; when…
-
4
votes3
answers1276
viewsA: code to return divisors and compare common ones
We can make the solution a little more general. Let’s imagine that we can define a limitless amount of values to determine the common divisors between them. We can do this through function: def…
-
0
votes1
answer92
viewsA: Is there any way to create different tables in SQLITE3 through the same python function?
Just go back to the basics of programming and use function parameters: def criar_nova_rodada(numero): cursor.execute("CREATE TABLE rodada{} (id, rodada, texto, autor);".format(numero)) Thus:…
-
6
votes1
answer2858
viewsA: Optional parameter in function
Has. You just need to assign, next to the function setting, the value the parameter will receive by default. def root(n, ind = 2): ind2 = 1 / ind print(n ** ind2) So, call yourself root(10, 5), n…
-
2
votes4
answers132
viewsA: Assign a value to a variable when the search return is NULL
Starting with PHP 7, there is the null coalescence operator, which returns the first operand if it exists and is not null, otherwise returns the second operand. In this case, just do: $IdSetor =…
-
1
votes1
answer624
viewsA: Exercise in the ordering of tuples
When to the "equal default zero", I think the word "equal" actually remained there. As for the function cmp, it has been removed from Python 3 and, even if your question cites version 2.7, I see no…
-
1
votes1
answer22
viewsA: Average mysql with multiple conditions
Yes, you can do this only with SQL. You can use the structure marry to define a common value between the records of each interval and, later, to group them together, calculating the amount of…
-
5
votes4
answers320
viewsA: Flatten lists. Is a more concise solution possible?
In the style of antigravity and, in general, more pythonic, you can use the native module itertools to solve the problem, more specifically the function chain, that does exactly what is desired (ie,…
-
2
votes2
answers402
viewsA: Strings in python
If the intention, which was not clear in the question, is in fact to read multiple user texts until the user does not type something, you must make an infinite loop until the stop condition is…
-
1
votes1
answer1324
viewsA: String with larger and smaller number of characters
Just create a list, very similar to what you have already done: textos = [] while True: texto = input("Entre com um texto:") if texto == 'pare': break textos.append(texto) Then, to find the smallest…
-
6
votes3
answers318
viewsA: Indentation in statement "Else"
In fact, the indentation serves to define which structure the else belongs. In the first case, the else belongs to the loop for (yes, that exists in Python), while in the second the else would…
-
4
votes1
answer167
viewsA: "If(a == b):" or "if a==b:" in Python, which is correct?
As commented, one of the principles of the Python language is the readability of the code. Readability Counts. Thus, read if a is much closer to the natural than if (a). Without the separating…
-
1
votes1
answer27
viewsA: : column in front of variable
It’s called Slice. With it you can access a slice of an object that is eternal. For example, do lista[1:5] you would be accessing items 1 to 4 (the 5 represents the first index not included in the…
-
2
votes1
answer45
viewsA: Displaying full Fractions result
The documentation examples display the results directly from the interactive Python terminal and these display the representation of the object, that is, the return of the method __repr__, through…
python-3.xanswered Woss 73,416 -
1
votes2
answers912
viewsA: Python: find product of the highest value of each matrix list
Complementing the Isac response, it is also possible to calculate the product of the largest numbers of each matrix row using a combination of functions max, which obtains the highest value of an…
-
2
votes2
answers231
viewsA: Highlight main words of a text in order of a ready list
The code you made is already very close to what you need, just set the prioritization list and scroll through it by searching the words in the sentence: with open('teste.txt') as stream: for line in…
-
9
votes2
answers99
viewsA: Why split this operation into two cause result changes?
There is a difference. When on the same line, both values will be updated concurrently, while on different lines no. How a starts at zero and b in a, when you do a, b = b, a+b, a will receive the…
-
0
votes2
answers1542
views -
1
votes2
answers4092
viewsA: How to convert a list to string and vice versa?
As commented, the answer presented does not solve the problem presented. If you have an object, you want to convert it to a string and later convert it back, you will need to use some serialization…
-
6
votes1
answer6606
viewsA: How to compare a list of numerical values with another Python variable
If you want to always search for the value closest to the average, you just have to calculate the deviation of each value of the list against the average, that is, the difference between both, and…
-
1
votes2
answers323
viewsA: Function that returns next character in PHP ASCII table
Just convert the value to integer with the function ord(), increment the desired value and convert to string again with the function chr(). function charOffset(string $char, int $offset = 3): string…
-
8
votes2
answers130
viewsA: How to store an operator?
It has an analogous form, using the library operator. For the minor operator, there is the function lt. from operator import lt a = lt if a(4, 5): print('4 é menor que 5') else: print('4 é maior ou…
-
2
votes2
answers59
viewsA: Doubt with mysqli_fetch_assoc
You don’t need to know this inside the loop to get that result, just remember that everything you perform before the loop will always be before the first line, just like what runs after it will be…
-
1
votes1
answer45
viewsA: $_SESSION function does not work
The problem is in your HTML, not in PHP. When doing: value=<?php echo $_SESSION['endereco'] ?> If the session has white space, when HTML is generated, it would be: value=Rua Teste And…
-
4
votes2
answers1545
viewsA: How to increment the value of a variable according to the user’s response?
When you need to request something from the user and validate the information, it is natural that we do not know how many times the user will need to enter a valid information. There may be the user…
-
4
votes1
answer51
viewsA: Operating array elements
If I understand correctly, you want to numerically divide each element of the array by 2, so yes, it is possible to do this without the loop repeating (and it is even recommended to do). The…
-
1
votes1
answer77
viewsA: How to detect specific text in a string
Your code didn’t make much sense, even though it wasn’t meant to be, it was just to try to illustrate. A possible solution, since it is working with files is to use the function glob.glob to get the…
python-3.xanswered Woss 73,416 -
3
votes1
answer43
viewsA: Using array to place multiple classes in a DIV
I didn’t particularly understand why you defined a array associative, the keys being equal to the values. In my view, a array sequential would be enough and would be even simpler. $classes =…
-
0
votes3
answers1659
viewsA: Set a variable in the value of an input
Just assign the new value to the attribute value of the object that represents your field. See an example below: const novoValor = "Stack Overflow em Português"; const campo =…
-
1
votes1
answer390
viewsA: How to modify a list within a Python method?
It doesn’t work because the changes made within the scope of the function are not loaded into the external scope - nor should it, as this could generate side effects and make it very difficult to…
-
1
votes1
answer59
viewsA: List of PHP arrays
If the array is of numerical and sequential indices, just you count the amount of elements and access the last position, which will be the quantity minus one. $quantidade = count($menu);…