Posts by Woss • 73,416 points
1,476 posts
-
4
votes3
answers515
viewsA: Sum of factorial in a specific range
Making a loop that traverses the numbers from 1 to n by calculating the factorial of each separately may not be a good idea (except when n=51), unless you have implemented some layer of memoization,…
-
5
votes2
answers209
viewsA: Find values from a List and Replace in another list - Python
The best way is to go through the list B and for each value check if it is in A; if it is, return the value to be replaced, but return the value itself. With list comprehension it becomes quite…
-
3
votes1
answer39
viewsA: Result - . find() - Different from expected
The method str.find returns the position of the first occurrence of the desired string or -1 when not found. The main problem is that you just did if line.find('Subject:'). When not found, the…
-
5
votes3
answers193
viewsA: Take values less than or equal to 500 from the list - Python
If there is a need to generate a new list (and given the semantics of the problem I think valid), it is best to use list comprehension (or comprehensilist on). numbers = [100,500, 200, 230, 400,…
-
4
votes3
answers158
viewsA: Use of virtualenv python
In a simplified form, the activate will modify the environment variables during the terminal session. [...] VIRTUAL_ENV="/home/user/python/venv" export VIRTUAL_ENV _OLD_VIRTUAL_PATH="$PATH"…
-
5
votes3
answers575
viewsA: Construct a program to read positive integer n and print all primes in the range [2, n]
Since you already have the main part of the code ready, I will put here a pseudo-code of how it could continue, considering that you already have a function called é_primo returning True or False.…
-
1
votes1
answer72
viewsA: How to receive a user value to be calculated in a function using Sympy?
You can use the function sympy.parsing.sympy_parser.parse_expr: from sympy.parsing.sympy_parser import parse_expr expr = input('Qual é a expressão em x? ') x = int(input('E qual é o valor de x? '))…
-
2
votes2
answers154
viewsA: Repeat Structures in Python
First detail is that the values of M and N will be read in the same input, not separately. limits = input() M, N = map(int, limits.split()) You can always build your loop from M to N, but if M is…
-
1
votes1
answer86
viewsA: Matrix input reading in python
As you are already reporting the entire line in one input, you will not need two loops of repeats, only one, running the number of times referring to the number of rows of the matrix: matrix=[] for…
-
1
votes2
answers166
viewsA: AND OR PANDAS CONDITION
You can use the operator | to define the OR operation, or the operator & for operation E. df[df['col1'] == 'sim' | df['col1'] == 'nao'] # Retorna se for "sim" ou "nao" df[df['col1'] == 'sim'…
-
1
votes2
answers961
viewsA: Help with error '>' not supported between instances of 'Nonetype' and 'int'
There are two main errors in your code: what justifies the error message you put and another that you probably didn’t notice since the first error would fill the second. The function malhar that you…
-
3
votes1
answer76
viewsA: How to form unique tuples for a secret friend game?
And why not just randomly draw the names' positions and associate each name with its successor, the latter being associated with the first? function pairs(names) { const _pairs = []; // Copia o…
javascriptanswered Woss 73,416 -
3
votes1
answer65
viewsA: Tests with Phpunit in class that uses $_SERVER variable
This is a clear sign that the construction of your class is mistaken. If it is difficult to test, then your class has a dependency built wrong. In this specific case, the dependency that is getting…
-
1
votes1
answer75
viewsA: f. write (python) writes "None" to the file
About the problem of just writing None in your file is due to the fact that in your code what you have write is the return of the function print, converted to string. f.write(str(print('%s' % q)))…
-
2
votes5
answers115
viewsA: Browse lists of different sizes
How do I make it so that when the shortest list is over it returns to the first element and so on until the longest list is over? If you need the elements of x remain cyclical until the entire list…
-
2
votes1
answer322
viewsA: python emailing with several attachments
In doing pdfName = "caminho/202008/*.pdf" you are not searching all PDF files in your directory, but only a file that has the name *.pdf, that probably doesn’t even exist. To actually search all the…
-
2
votes1
answer57
viewsA: How to use a Python keyword as a parameter?
You can define a dictionary with the desired values: extra = { ... "from": "A", "to": "B" } And pass them as parameter by deconstructing the dictionary: element = ET.Element('vType', **extra) So you…
-
5
votes2
answers5514
viewsA: How to simplify the logical expression (~x Ʌ ~y Ʌ ~z) V (x Ʌ ~y) V (z Ʌ ~y)?
Given the expression and considering the properties of Boolean algebra: = (~x * ~y * ~z) + (x * ~y) + (z * ~y) Simplifying the rating with the operator and as being * and or as being + By the…
-
2
votes1
answer408
viewsA: String Formatting - PYTHON Alignment
Just put the variable also between keys: print(f"{grade:>{n}}") Thus the value of the variable n will be interpolated in the string and will still be considered as a parameter of formatting the…
-
3
votes4
answers77
viewsA: Upload default photo if no photo is registered
If you are using PHP 7, you can use null coalescence operator to check if there is any value in your variable and, if not, display a default value. <img src="fotos/<?= $dados["foto"] ??…
-
3
votes2
answers45
viewsA: What would it be like without Arrow Function?
Would be using a normal function: document.querySelector('.menu .backdrop').addEventListener('click', function (e) { document.querySelector('header .menu').classList.remove('open'); });…
javascriptanswered Woss 73,416 -
4
votes3
answers99
viewsA: Replace all arguments (of all strings) that exist in a Dict with . format
If there is a possibility to change the format, you can use the structure Template library native string python. For example, instead of making the values between keys, {nome_usuario}, you could put…
-
2
votes2
answers112
viewsA: How to create a file within a directory?
From version 3.4 of Python, it is recommended to use the library pathlib to work with directories and files. from pathlib import Path directory = Path('test') if directory.exists(): file = directory…
-
3
votes2
answers118
viewsA: Method to capture trex of a URL with Javascript
Just use the class URL, access the value of path and split into bar: // const url = window.location; const url = new URL('https://meusite.com/q6uzeyln32td/naoquero.html') const segments =…
javascriptanswered Woss 73,416 -
2
votes1
answer117
viewsA: BEM pattern in CSS
Part of philosophy of nomenclature defined by BEM is that a component of the site should not depend on its context. If the display of the component changes according to the context, it will be the…
-
2
votes1
answer74
viewsA: Operating on a list of lists with the conditional
The error is that you are trying to remove what does not exist. By running, for example, [1, 2].remove(3), will give the cited error, because there is no way to remove the number 3 from the list [1,…
-
1
votes1
answer55
viewsA: Copy content from one table to another
It doesn’t take much to realize that the code you wrote doesn’t make sense to what you want to do. UPDATE picture pc INNER JOIN cars AS c ON c.image = pc.ImName SET pc.ImName = c.image; Used the…
-
5
votes3
answers614
viewsA: Accept user input without pressing enter
With the function input this is not possible. It is implemented in C and designed to store in buffer all content typed up to receive the enter. What you need to do is interact directly on the STDIN…
python-3.xanswered Woss 73,416 -
0
votes1
answer257
viewsA: Python/Mysql - Exporting CSV files
See to mysql documentation: The SELECT ... INTO OUTFILE 'file_name' form of SELECT Writes the Selected Rows to a file. The file is created on the server host, so you must have the FILE Privilege to…
-
3
votes2
answers374
viewsA: Breaking string from a character
The code you tried doesn’t make sense. df_movies = df_movies[df_movies["country"].str.split(",")] You seek the string that has comma separated values and separates the values in that character. That…
-
5
votes1
answer269
viewsA: how to use Vue.js interpolation in a Jango template?
In the Vue configuration you have the option to define new delimiters for your template, so you can report something that doesn’t conflict with Django’s templates system. const app = new Vue({…
-
5
votes1
answer224
viewsA: Fetch always returns "Typeerror: Body has already been Consumed." How to fix?
The method response.json will return a new promise, so you need to deal with it correctly. The easiest is to treat it with then chained: var url = "https://jsonplaceholder.typicode.com/users/2";…
-
1
votes2
answers1396
viewsA: Loop inside loop While PYTHON
It’s just a matter of simplifying the problem. You have little code that does a lot, so it will be better to separate the responsibilities. You can create a function that reads the user’s attempt:…
-
4
votes2
answers40
viewsA: Line numbering
Depending on the version of Mysql you are using, just use the window Function together with the function ROW_NUMBER(). SELECT id, ano, ROW_NUMBER() OVER (PARTITION BY ano) AS posicao FROM anos ORDER…
-
9
votes2
answers1109
viewsA: Do if by denying a boolean in Python
Just use the not. condition = False if not condition: print('Entrou no if')
-
4
votes1
answer84
viewsA: Deserialize Object in array after json_encode
JSON does not serialize the object, only represents as string the current state of the same. If you really want to serialize, you will have to use the functions serialize and unserialize, or…
-
2
votes3
answers2923
viewsA: Intl to format date in Javascript?
The method Intl.DateTimeFormat.prototype.format wait as parameter an object of type Date, then you shouldn’t pass a string (if it really is a string that’s going on). const date = new Date(2019, 11,…
-
3
votes2
answers116
viewsA: Show smaller, average and larger until an empty line is typed
Python already has functions ready to identify the smallest and largest value of a list, as well as calculate the average of it. To identify the lowest value: min To identify the highest value: max…
-
7
votes2
answers98
viewsA: What is the difference between a string defined within a text file and another defined as a variable?
The difference is the type of object you are iterating. In the first example your repetition loop is iterating over a string, that when iterated generates as output the characters one by one. This…
-
4
votes4
answers1891
viewsA: Sum of pairs typed interval
Workaround: need not even use a loop to solve the problem. Remember the basic math must have studied arithmetic progressions. Sequence 2, 4, 6 is a PA of three terms starting at 2 with ratio 2. It…
-
6
votes2
answers166
viewsA: What does the double-dash stand for in a Bash command?
The double dash alone has some special meaning? Yes, it indicates the end of the command options, making everything that comes after it to be considered operating. Or is it something specific to the…
-
3
votes3
answers415
viewsA: Interfacing of vectors in python
A trivial way to solve this is by using the structure itertools.chain together with the zip: from itertools import chain a = [4, -9, 78, 0, 25] b = [8, 2, 34, 90, 200] resultado =…
python-3.xanswered Woss 73,416 -
15
votes3
answers10568
viewsA: What is the difference between using += and =+ in Python?
The answer is: depends on. The answers of Maniero and of Luan are valid only when the variables in question are numerical. The first code snippet, x += n, is the equivalent of x = x + n. The result…
-
1
votes2
answers1485
viewsA: Error in php’s Count function. Warning: Count(): Parameter must be an array or an Object that Implements Countable in line 41
Just commenting on an alternative, you can use the functions range and array_chunk to generate the weeks you want: $semanas = array_chunk(range(1, 31), 7); This will generate the array: ( [0] =>…
-
1
votes2
answers50
viewsA: Overwriting the occurrences of a string in a text file. Differences presented with several file opening modes
The problem with your solution is that you have not properly handled the file pointer. The writing and reading processes of a file use the same pointer and if the idea is to overwrite the content…
-
2
votes3
answers127
viewsA: Python Max Value Lists
The very function max Python native allows you to define which will be the rule to define which is the highest value item in your iterable through the parameter key. In your case, just find the item…
-
11
votes1
answer153
viewsA: Any more pythonic way to solve the problem below?
Yeah, it’s got to be more idiomatic. By the statement we can identify two quite different actions: read the user’s number and calculate the sequence sum. Doing this in the same function breaks the…
-
3
votes2
answers783
viewsA: How to implement the cosine function using Taylor series in Python?
It’s not (but almost). The way you did: def cos(x, n): soma = 1 formula = (((-1)**n)*(x**2*n))/math.factorial(2*n) for i in range(n): soma += formula return soma The value of formula shall be…
-
1
votes1
answer48
viewsA: Duplicate records in the database table
I don’t use Codeigniter to have full authority over what I will talk about, but I will start from basic PHP analysis: whereas $this->input->post('resposta[]') return a array with the values in…
-
2
votes3
answers613
viewsA: Show python position of prime only
The logic to solve this problem is simpler than you think: Set an empty vector; Set a repeat loop with 10 iterations; For each iteration read a number; Add the number in the vector; Define a repeat…