Posts by mgibsonbr • 80,631 points
861 posts
-
4
votes1
answer153
viewsA: Javascript problem when receiving variable with date
If your variable $dta_inicial contains a string representing a date (eg.: 17/12/2015) and you make a echo of this variable, then its contents will be inserted as such in the output. That is, it:…
-
4
votes3
answers273
viewsQ: Incorrectly applied width media queries on mobile devices
I’m trying to use media queries on a website thinking about mobile devices, but I’m facing a problem: although the rules specify what to do when the width is small - and the same apply correctly…
-
20
votes3
answers9258
viewsA: What good is while(0) and while(1) in C?
A loop continues (executes the next step) if its condition is true, and does not continue if it is false. In C, zero is considered "false" and everything else is considered true. So: while(0) means…
-
4
votes2
answers435
viewsA: Multiple regular expressions in a bar string
Your mistake was putting the + in the wrong place: URL: /testando-minha-url/ REGEX: ~^/[a-z-]/+$~ SAÍDA: false That’s saying marry a bar, then a single character which is letters or dash, followed…
-
9
votes4
answers332
viewsA: In relation to object orientation, should queries have a class of their own or can they stay in a specific class?
If you are really representing queries by classes[1], consider that "entering a user into the database" is a concept, but the realization of this concept depends on which DBMS is being used. This…
-
3
votes1
answer649
viewsA: Difference between Father. __init(self) and super(Father, self). __init__()
The problem is that the super should not be called in the parent class, but in the parent class itself: class B(A): def __init__(self): super(B, self).__init__() self.b = 'b' For the idea of super…
-
2
votes3
answers16117
viewsA: How to transform a string composed of numbers into an integer list?
To answer from Miguel already explains the problem and gives a solution proposal, I would just like to propose an alternative solution if your list can contain numbers with more than one digit (ex.:…
-
5
votes1
answer126
viewsA: Tilting of an Opengl Crane
You seem to be applying the rotation before translating to the point around which the spin should occur. I have no experience with Opengl, but I noticed in your code that the only operations that…
-
8
votes3
answers11601
viewsA: How to calculate the Euclidean distance
Your problem is on that line: distancia = sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)); In C++, the operator ^ is not the exponentiation, but the "or exclusive" (xor). You are making the xor of the…
-
18
votes2
answers8854
viewsA: Differences between Python versions?
These differences are really significant ? From the point of view of syntax, learning, etc., they are not. You can perfectly learn the "way of Python 2" and the "way of Python 3" simultaneously, and…
-
6
votes2
answers1691
viewsA: What is the advantage of using languages you compile for other languages?
The biggest advantages are the reuse of code, interoperability and the possibility of using a language in an environment that normally would not support it. The design of a programming language,…
-
7
votes2
answers13617
viewsA: Python - 'int' Object is not iterable
Your problem is here: cod(ord(i)) You are calling the function cod recursively passing the result of ord - and therefore a whole - as argument. But as the first thing that cod makes is iterate on…
-
2
votes1
answer335
viewsA: Extend the list to another list
You can correlate list elements lista1 and lista2 through function zip. Also, you can concatenate lists using the operator +. So if you create a new list - using list comprehensions - in which each…
-
15
votes2
answers5051
viewsA: What is a deterministic, nondeterministic algorithm?
A deterministic algorithm is one that behaves the same way in different runs, given the same inputs and the same internal state of the machine (if relevant). A non-deterministic would be one that…
-
1
votes1
answer626
viewsA: Total and subtotal in Django template using predefined lists
Although its current code is suitable for a table that crosses products with stores (such as in your question that originated this whole code), this new structure of yours would benefit more from a…
-
2
votes1
answer851
viewsA: Total and subtotal in Django template using lists
I did not understand whether the subtotal refers to the rows, columns, or both, so I will reply to "both" (although in your particular case this makes no sense). To calculate the subtotal of the…
-
2
votes2
answers1159
viewsA: How to limit the number of houses before the comma in a regular expression?
The same way you did to limit later: "([0-9][0-9]?[0-9]?[0-9]?[0-9]?(\\.)?([0-9]?[0-9]?))" That is, the first is mandatory, and the following 4 are optional. However, I have some suggestions to…
-
2
votes1
answer155
viewsA: Returning the minimum value for comparison in Django template
You can’t make one zip in the template (i.e. iterate over two or more lists simultaneously), so I suggest doing this also in the view before sending it there: minimos = [] # Em vez de mapear, usa o…
-
2
votes2
answers2143
viewsA: What is the difference between . save() and . update() in Django?
The method save saves changes to a single model object (i.e., a single BD line), while the update can change multiple objects at the same time. The differences are as follows: objeto.save() It is…
-
1
votes1
answer51
viewsA: Jframes Editing
A GridLayout simply divides the available space into the specified number (in this case, 1 row and 3 columns) and makes each element occupy that entire space. In your case this gives two problems,…
-
2
votes1
answer91
viewsA: Display tree format directories
If the problem is that whenever you find a file you "reset" the number of spaces to zero, which causes the next folder in the queue to be printed as if it were on the root. It is not necessary to do…
-
9
votes3
answers880
viewsA: What are the security impacts of a site that has an invalid certificate?
If a website is intended for the general public, the only way to guarantee the authenticity of communication without manual intervention is through a valid certificate. Using an invalid certificate…
-
2
votes2
answers3176
viewsA: base64_encode in javascript
Javascript does have functions that convert from and to Base64, see the answer of Guilherme Nascimento. However, this does not change the fact that you will have to convert your image from BLOB to…
-
12
votes2
answers596
viewsQ: How to animate the display of dynamically sized elements with CSS only?
I’m building a site that has some state transitions using CSS only. In one of them the user clicks the header and the div below displays or hides. In the other there is a sequence of images and when…
-
29
votes3
answers880
viewsQ: What are the security impacts of a site that has an invalid certificate?
Many Brazilian government websites often do not have valid safety certificates. Examples: https://www.ibama.gov.br/ (expired) https://www.ibge.gov.br/ (auto-signed, invalid URL)…
-
2
votes3
answers1189
viewsA: Python NLTK method that returns a syntactic tree
I don’t know about "in Portuguese" - or even in any other natural language, like English - but from what I understood the parsed_sents returns a list of already "parsed" sentences, without…
-
9
votes2
answers98
viewsA: No need to escape the "." in a field [...] in REGEX?
No, because it would make no sense to allow "any character" in square brackets. Consider the example given. If the dot inside the bracket was special, the expression: [a-zA-Z0-9.] would be…
-
1
votes1
answer54
viewsA: Problems animating sprites with javas canvas
You need to clear the canvas between one drawing and another: this.draw = function (x, y) { canvas.context.clearRect(0, 0, largura, altura); ... Example in jsFiddle. I don’t quite understand your…
-
2
votes1
answer55
viewsA: Problem Returning sh: 0: -c requires an argument
You are iterating over the command line arguments, so each of them will be run individually by os.system: if len(sys.argv) <= 3: # arv = ['./comando.py', 'ls', '-la'] for comando in sys.argv[1:]:…
python-2.7answered mgibsonbr 80,631 -
0
votes2
answers282
viewsA: I can’t divide with a vector element
I also tried to write how: VIT = ESTAT[2]/ESTAT[1]*100; DER = ESTAT[3]/ESTAT[1]*100; There are two problems here: first, in C the arrays are indexed from zero, not from one, so that the first…
-
7
votes5
answers1382
viewsA: No return on method?
From what I understand, the intent of your code is to read the contents of the entire file, as text, and return it, is that right? If it is, I have three observations: Start with the empty string,…
-
5
votes1
answer234
viewsA: What is the algorithm for distributing the paragraphs?
Just accumulate the values and, when you exceed the target (the average times the number of days travelled), check which alternative has the smallest prediction error (i.e. the difference between…
-
7
votes2
answers350
viewsQ: How to get the set of all Python arguments?
Python has both positional and named parameters and operators * and ** which allow receiving an arbitrary number of additional arguments: def foo(a, b, c=1, *args, **kwargs): print(a, b, c, args,…
-
3
votes1
answer161
viewsA: Refactoring Python code
If the problem is the input of arguments, just use the operator **, without tampering with your code: h = { "title":"foo", "year":2015, ... "imdbID":12345, } self.save(**h) # é o mesmo que…
-
5
votes1
answer522
viewsA: Reflection in Opengl in C?
The way you described it is in fact the simplest. When rendering a scene, you can use two lighting models: the local and global. Local model In the local model each object is rendered independently…
-
6
votes1
answer2733
viewsA: Calculating a magic square
I will answer based on your first attempt, with matrix, because the logic is simpler First, your logic for verifying whether or not a matrix satisfies the condition of the magic square is correct.…
-
4
votes2
answers1939
viewsA: How do I convert an RGB image to Python grayscale?
This can be done with the library Pillow: from PIL import Image img = Image.open('image.png').convert('L') img.save('greyscale.png') Source (adapted). Method documentation convert. (the above code…
-
1
votes1
answer69
viewsA: How do serial numbers work in a system?
There is no standard. Each company does it one way, with higher or lower level of sophistication/quality. Rarely is the algorithm published, since ignorance of it is the main factor that seeks (but…
-
4
votes1
answer102
viewsA: How to model a set of 1 to 4 predetermined values
Unless you’re experiencing performance issues, I would go with the relationship table, otherwise it would complicate your darlings. However, if the number of documents is too large in relation to…
-
4
votes3
answers2079
viewsA: Array value with highest occurrence
1. "Naive" method - O(n2) For each element of the array, scroll through the entire array counting how many times that element appears. If the number is larger than the highest found so far, upgrade,…
javascriptanswered mgibsonbr 80,631 -
13
votes1
answer3146
viewsA: Can the MD5 hash repeat for different passwords causing collision?
There are not only examples but also a website that creates collisions for you: the two blocks d131dd02c5e6eec4693d9a0698aff95c 2fcab58712467eab4004583eb8fb7f89 55ad340609f4b30283e488832571415a…
-
8
votes1
answer737
viewsA: How to create a system to generate hashes and break without the original string?
The SHA-256, like any well-made hash system, is resistant to pre-image ("single-hand function"). This means that, given the value of a hash, it is impractical to discover (with high probability) any…
cryptographyanswered mgibsonbr 80,631 -
4
votes2
answers102
viewsA: Doubts related to recursion
Just call the recursive method twice. For the height of the tree will be one plus the height of its largest subtree, so it is necessary to evaluate both sides before determining the height. An…
-
0
votes1
answer1526
viewsA: How to validate a signature with a private key?
There is a difference between signature via asymmetric cryptography (the one using public and private keys, usually called "digital signature") and signature via symmetric cryptography. In this one,…
-
14
votes4
answers2951
viewsA: What ways to measure the performance of an algorithm?
First, an algorithm is not "efficient" or "inefficient", it has only a number of steps proportional to the size of its inputs. A implementation of this algorithm is going to have an efficiency,…
-
0
votes1
answer56
viewsA: Error "Can not update Identity column 'order'"
That question in Soen the author has found a temporary solution, which is to remove the problematic fields of query insertion/updating. Adapted to your code: class Video(models.Model): id =…
-
9
votes3
answers527
viewsA: Precedence of operators
Your mistake was to assume that the sub-expression ++x would be evaluated before of sub-expression x * 4. At the time this expression is evaluated, x still worth 2, so that the correct resolution…
-
2
votes2
answers577
viewsA: Instance in Django model with problem
Your error message is an indication of circular dependency. You wouldn’t happen to have, in emails/models.py, a line making: from departamentos.models import Departamento ? That code worked before,…
-
9
votes2
answers138
viewsA: How long does an x++ increment take?
It does not make much sense to speak in the time it takes a specific instruction to execute, outside the context of the program as a whole, due to the pipeline processor. Not every instruction…
-
0
votes2
answers625
viewsA: Phonegap: How to save an image taken and transformed into Base64 in the Bank?
In his job onPhotoDataSuccess you save the Base64 data on src of the element smallImage: smallImage.src = "data:img/jpeg;base64," + imageData; But in function enviar you’re catching the innerHTML of…