Posts by Woss • 73,416 points
1,476 posts
-
10
votes2
answers320
viewsQ: Why can’t f-strings be used as docstring?
In accordance with the PEP 257, one has: A docstring is a literal string that occurs as the first statement in a module, Function, class, or method Definition. Such a docstring Becomes the __doc__…
-
12
votes4
answers41980
viewsA: Is there any way to comment on multiple lines in Python?
Just as placed in the PEP 257 (That’s PEP), strings which are the first instruction in a module, class or function will be considered as strings special: the docstrings. A docstring is a literal…
-
5
votes1
answer87
viewsA: Form number for PHP value
Just divide by 100: $resultado = "1234589" / 100; // 12345.89 That will be a floating point number, but in case you need it, for whatever reason, let it be a string, just do the cast: $resultado =…
-
2
votes1
answer993
viewsA: Get a certain column from a file with multiple columns
Use the module csv. With it you can define the column separator and even read each row as a dictionary, making it easy to read the code. import csv def get_address_from_file(filename): with…
-
0
votes1
answer30
viewsA: File corrupts while trying to copy it
You did: with open(cf,"rb") as fl: for l in fl.read(): l.rstrip() That is, open the file for binary reading, read all the content with fl.read() and runs byte by byte by performing the rstrip() in…
-
2
votes3
answers108
viewsA: How to write a large amount of values per line in Python file?
You can also use the module csv, setting the delimiter manually for the character \t. It would look like: import csv with open('arquivo.txt', 'w') as stream: writer = csv.writer(stream,…
-
2
votes5
answers1043
viewsA: Matrix Nxm (2D) in Python with Numpy
You will need to read the user input in order to identify what will be the dimension of the matrix. Do this using the function input; a. The return of the function input will always be a string,…
-
3
votes2
answers439
viewsA: Calculate sequence of triangular numbers smaller than 1000
The problem is that you first increase the value of b, to then display it. If you just reverse the order it will work: int main(){ int b, a; for (a = 0, b = 0; b < 1000;a++){ cout << b…
-
6
votes3
answers639
viewsA: How to compress python code 3?
What you can do is reduce and simplify your code with comprehensilist on: i = int(input('Calcular divisores de: ')) divisores = [j for j in range(1, i+1) if i % j == 0] print('Divisores:',…
-
2
votes3
answers2206
viewsA: How to not return a None value in Python?
As Maniero commented, the logic of its function is somewhat obscure, because part of it works as a filter, another as a map. These are conceptually different things that do different things. When…
-
3
votes1
answer98
viewsA: Problems starting methods in a python class?
You have the class: class Inimigo: def __init__(self, vida): self.vida = vida def ataque(self): self.vida -= 2 inimigo1 = Inimigo() In the class initializer, you have a parameter vida, which will be…
-
2
votes1
answer93
viewsA: Using Prototype on JS
The point is that you didn’t export the function _init to the body of prototype, as it did with the function helloWorld; thus, it will exist only in the local scope of the function that defines the…
-
3
votes2
answers558
viewsA: Imports into __init__.py file
The archive __init__.py is the entry point package. When you do, for example: from django import * What you are importing will be the context set in django/__init__.py - limited, of course, by the…
-
2
votes3
answers340
viewsA: How to add values of a json request in python?
If your answer comes from the way: amounts = [{"amount": 0.1}, {"amount": 2}, {"amount": 145}, {"amount": 5.84}] And you need to add up all the values, just do: total = sum(it['amount'] for it in…
-
3
votes2
answers465
viewsA: How to change the JSON serialization format of PHP Datetime?
Change the return of the class itself at runtime DateTime I believe it is not possible - even because it could generate side effects on the application. A trivial solution would be to extend the…
-
2
votes1
answer1093
viewsA: How to read to the end of the file with Javascript - Uri Online Judge
When you select Javascript, the snippet will already appear for you: var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var lines = input.split('\n'); /** * Escreva a sua solução aqui *…
javascriptanswered Woss 73,416 -
2
votes2
answers228
viewsA: How to use this script for a server list? - Python
IS impossible very difficult to ensure that the command runs on all servers at the same time. But since it is an external resource, you can use the module asyncio to manage tasks through coroutines.…
-
8
votes1
answer139
viewsQ: How to avoid conflict of data between two PUT requests on HTTP?
What techniques can we use to avoid the collision between data of two PUT requests so that the changes of the second request do not override those of the first? Let’s imagine the situation: There is…
-
3
votes3
answers1116
viewsA: Increase and Decrease Font, how to apply on a large site?
I think the simplest is for you to work with variables in CSS and define the property font-size of the elements you want to change based on your variable. With Javascript, just change the value of…
-
8
votes2
answers218
viewsA: See how native functions were written - Python
When it is a function written in Python, you can use the module inspect. For example: def somar(a, b): return a + b In doing print(inspect.getsource(somar)) you will have the exit as string: def…
-
8
votes1
answer147
viewsA: Why do these two ways of initializing the same list in Python generate structures of different sizes?
This is an expected behavior in Python regarding the reallocation of resources from a list. Whenever memory relocation is done on a list, Python will allocate more memory than really necessary in…
-
8
votes1
answer147
viewsQ: Why do these two ways of initializing the same list in Python generate structures of different sizes?
It is common to need to initialize a list in Python with a defined amount of elements and we can do this in two ways: 1) multiplying the list with an element by the desired amount; or 2) using the…
-
4
votes2
answers820
viewsA: GET, POST or PUT, which one to use to issue a invoice?
Determining which method to use in each situation is always a delicate discussion. The implementation shall be according to the needs and limitations of the project. Including methods, whether or…
-
1
votes2
answers39
viewsA: Mouse Event on Two Screens
When the page loads, your JS code will be executed: $("#botao").click(function () { $("#tela").append('<div id="tela2"></div>'); }); $("#tela2").click(function () { alert("quero que…
-
5
votes4
answers136
viewsA: How to know which checkbox are selected?
Just use querySelectorAll('input[type=checkbox]:checked') to select all fields marked and use the attribute dataset to access the value of data-id. const checkboxes =…
javascriptanswered Woss 73,416 -
2
votes3
answers92
viewsA: PHP variable in Javascript
If your PHP delivers its own HTML to the client, Javascript doesn’t even need to go into history. What you’ve tried to do, even if you’ve seen it on W3scholls or right here on the site, is…
-
1
votes2
answers86
viewsA: User with more than one profile
If they are static profiles - there is no possibility to create new profiles with the time of application - you can define them through an enumeration with values being powers of 2. enum Perfis {…
-
0
votes1
answer477
viewsA: Know if client is connected to the server
You can define a block Try/catch to capture possible exceptions triggered by the module socket if something goes wrong. For example, if a timeout in connection, the exception socket.timeout will be…
-
3
votes2
answers91
viewsA: Join in array creating elements?
Just combine the join with the map: const result = arr.map(it => `<li>${it}</li>`).join(' ') See working: const arr = ["a", "b", "c", "d"] const result = arr.map(it =>…
-
2
votes1
answer249
viewsA: Laravel - does not write price when value is NULL
You are returning the value '0.00' nowhere and when the entry is void, the field is not modified. Instead of public function setPromocaoAttribute($value) { $value==null ? '0.00' :…
-
0
votes1
answer23
viewsA: Adafruit_bmp in python 3
Modules are not shared between Python versions. By default, in Raspbian the command python points to Python version 2.7. You can confirm this via the second line of your terminal image Python 2.7.13…
-
11
votes2
answers99
viewsA: How does the mini-code finder work?
This technique is called short-circuiting in logical expressions. When you have a logical expression A && B, the result will be true if, and only if, both operands are true, or false…
-
1
votes1
answer70
viewsA: Search in Array with conditions
You can pick up the records that have vehicle equal to 82 and start date less or equal to 2018-02-10 with the function array_filter: $filtrados = array_filter($registros, function ($registro) {…
-
1
votes1
answer30
viewsA: Parent::method does not return connection
The object $con in Conn is only initialized when the method is called Conn::conecta, which in turn is called in the builder of Conn, but the builder of Conn is never called, for the method was…
-
2
votes2
answers24
viewsA: loop to get date
The date format dd/mm/aaaa does not exist, only the mm/dd/aaaa, then, for PHP, you are reporting the 7th day of month 18, which does not exist, so the date goes to year 1970. Replace to 18-07-2018…
-
3
votes2
answers4189
viewsA: How to generate the alphabet with white space between letters?
If it is possible to keep the space at the end, after the character z, you can map the characters by concatenating with the white space and then merge everything into one string with unwords: >…
-
1
votes4
answers1386
viewsA: Take <span> value with PHP
If your problem is PHP, solve it with PHP - I don’t understand why other answers used Javascript. You started well. In fact, the solution will be using the class DOMDocument, however, to search for…
-
12
votes2
answers2682
viewsA: Is it possible to declare Unknowns in Python to calculate constants of a Mathematical function?
Yes, it is possible. Python has its own library for working with symbolic mathematics: sympy. You can create symbols using the function symbols, or the class Symbol: from sympy import Symbol x =…
-
2
votes1
answer906
viewsA: How to perform calculations on top of a CSV file with Python 2.7?
You started well but I have two comments on your code: It does not discard the header line present in CSV, so the content Nome;P1;P2;P3 would be interpreted as a student’s grades; When reading from…
-
4
votes1
answer121
viewsA: What is the uneval function for?
Answer: for nothing practical. Given the content on the Internet, I believe the function uneval has been created by the Mozilla team under some internal demand and ended up leaving the function…
javascriptanswered Woss 73,416 -
1
votes2
answers36
viewsA: I need a php variable to be placed in a href link
Just interpolate the strings: echo "<a href='http://...?phone={$phone}'></a>"; Notice the use of {$phone} within the string, which will be replaced by the variable value $phone. But…
-
2
votes2
answers185
viewsA: What is the purpose of defining the name of a function that is assigned to a variable?
As commented on in the question you quoted - and in the @dvd answer, there is the difference between the scopes. When you do: var a = function bar() { // ... } Within the function there will be the…
javascriptanswered Woss 73,416 -
3
votes2
answers50
viewsA: Get list names that are 4 in size
For future references, you can solve in a row, with an equivalent code, using the so-called list comprehension: def nomes(lista, tamanho): return [nome for nome in lista if len(nome) == tamanho] The…
-
3
votes3
answers5398
viewsA: Count number of times letters repeat in a text
Yes, just use the collections.Counter, who already does it for you: from collections import Counter texto = 'Stack Overflow em Português' counter = Counter(texto) print(counter) The exit is:…
-
4
votes3
answers623
viewsA: What is the Javascript Set object for?
Ministry of Health warns: this response has high concentrations of theory Set The object Set is defined in Ecmascript 2015 as a keyed colletion, along with three other objects: Map, WeakMap and…
-
3
votes2
answers557
viewsA: Stop CSS animation after interaction
Using data-Attributes to define the animation Instead of being stuck in a class with animation, I would do something quite different. I would define an attribute data-animation which defines, in the…
-
5
votes1
answer904
viewsA: What would be the equivalent of find for lists in python?
The method find, of string, returns the smallest index where the searched value is found. In lists, there is the method index: >>> numeros = [1, 2, 3, 4, 5] >>> numeros.index(3) 2…
-
4
votes3
answers78
viewsA: How to check if variables are filled with strings? and not show them when empty
You can only check if the value is valid within a repeat loop: const filtered = objects.map(obj => { let html = ''; for (let attr in obj) { if (obj[attr]) { html +=…
javascriptanswered Woss 73,416 -
6
votes1
answer1111
viewsA: Destroy Session when leaving the page
The identification of the user’s session is by a cookie - by default, the name of the cookie is PHPSESSID. If the user loses the cookie, he’ll miss the session, just like that. So, for the session…
-
0
votes1
answer692
viewsA: Percentage calculation in mysql
You don’t need two queries for this, just you add up the amount of records that have the column origin equal to app and divide by the total. Something like: SELECT 100 * SUM(CASE WHEN origin = 'app'…