Lim Sup and Lim Inf relations between the root test and the ratio test

Question: I'm trying to solve the following problem and while looking around I found a couple of notes (Link) by Pete L. Clark where it's discussed but can't really comprehend his proof.
The problem:
Prove that $ \lim\inf\limits_{n\rightarrow \infty} \frac{A_{n+1}}{A_n} \leq \lim\inf\limits_{n\rightarrow \infty} (A_n)^{1/n} \leq \lim\sup\limits_{n\rightarrow \infty} (A_n)^{1/n} \leq \lim\sup\limits_{n\rightarrow \infty} \frac{A_{n+1}}{A_n}$
His proof, as I understood it, is as follows:
Let $r > \lim\sup\limits_{n\rightarrow \infty} \frac{A_{n+1}}{A_n} $, so there's an $n_0$ so that for all $k \geq 1$ then $ \frac{A_{n_0+k}}{A_{n_0 + k - 1}} < r $.
From that we get $ A_{n_0 + k} < r A_{n_0+k-1} $ and that implies $A_{n_0 + k} < r^{k} A_{n_0} $. Rewriting that as $A_{n_0+k} ^{\frac{1}{n_0+k}} < r (\frac{A_{n_0}}{r^n})^{\frac{1}{n_0+k}}$ and by letting $ k \rightarrow \infty$ we see that $\lim\sup\limits_{k\rightarrow \infty} A_{n_0+k} ^{\frac{1}{n_0+k}}$ is at most $r$.
So, we have that $r > \lim\sup\limits_{n\rightarrow \infty} \frac{A_{n+1}}{A_n} $ and $ r \geq \lim\sup\limits_{n\rightarrow \infty} (A_n)^{1/n}$
He says that that implies $\lim\sup\limits_{n\rightarrow \infty} (A_n)^{1/n} \leq
\lim\sup\limits_{n\rightarrow \infty} \frac{A_{n+1}}{A_n}$, but I really don't see how that's the case to be honest. And I'm also not too sure how the analogous argument for $\lim\inf$ would look like.
I'm sure the proof is correct, as I've seen a lot of people recommend that particular set of notes, but I just can't seem to grasp it and it looks so simple.
Any help, both with this particular proof and with any other that applies, would be greatly appreciated.
Answer: Try to prove this:

Suppose that for each $r>b$, we have $a\leqslant  r$. Then $a\leqslant b$.

Equivalently

Suppose that for each $\epsilon >0$, we have $y\leqslant \epsilon$. Then $y \leqslant 0$.

Pete (who is a user here!) is using this with $b=\limsup\limits_{n\to\infty} \dfrac{ A_{n+1}}{A_n}$ and $a=\limsup\limits_{n\to\infty} A_n^{1/n}$.
For the argument using $\liminf$; you start with $\liminf\limits_{n\to\infty}\frac{A_{n+1}}{A_n}$ and choose $r$ smaller than this. The steps are the same but with signs reversed. Then you use

Suppose that for each $r<b$, we have $a\geqslant  r$. Then $a\geqslant b$.
Comment: Thanks! I confess it took me a while longer than what it should have, for some reason.
Answer: The idea is very simple, though understandably, it tends to drown in other detail: You are given numbers $U$ and $V$ so that whenever $r>V$, it is also true that $r>U$. Then $U\le V$. For if $U>V$ then you can pick $r$ with $U>r>V$ to get a contradiction.
In this case, $U=\limsup\limits_{n\to\infty}(A_n)^{1/n}$ and $V=\limsup\limits_{n\to\infty}(A_{n+1}/A_n$.
This sort of technique is very useful because working with strict inequalities gives you a bit of wiggle room. In this case, the wiggle room is used to accomodate the term $A_{n_0+k}^{1/(n_0+k}$.
Comment: Thanks. For some reason I didn't though that was true. That's what I get for doing stuff at 3am.


airflow.exceptions.AirflowException: Scope should be defined when using a key file

Question: Problem : While running dataflow job using airflow ,i am getting below error, is it related to the keypath which we provide in connection. If yes what should be the correct path. Error:
File "/usr/lib/python2.7/site-packages/airflow/models.py", line 1374, in run

result = task_copy.execute(context=context) File "/usr/lib/python2.7/site-packages/airflow/contrib/operators/dataflow_operator.py", line 114, in execute
self.jar = bucket_helper.google_cloud_to_local(self.jar)

File "/usr/lib/python2.7/site-packages/airflow/contrib/operators/dataflow_operator.py", line 229, in google_cloud_to_local
file_size = self._gcs_hook.download(bucket_id, object_id, local_file)
File "/usr/lib/python2.7/site-packages/airflow/contrib/hooks/gcs_hook.py", line 58, in download
service = self.get_conn()
File "/usr/lib/python2.7/site-packages/airflow/contrib/hooks/gcs_hook.py", line 43, in get_conn
http_authorized = self._authorize()
File "/usr/lib/python2.7/site-packages/airflow/contrib/hooks/gcp_api_base_hook.py", line 84, in _authorize
raise AirflowException('Scope should be defined when using a key file.')
AirflowException: Scope should be defined when using a key file.
[2017-06-13 20:05:45,165] {models.py:1433} INFO - Marking task as UP_FOR_RETRY
[2017-06-13 20:05:45,174] {models.py:1462} ERROR - Scope should be defined when using a key file.
Traceback (most recent call last):
File "/usr/bin/airflow", line 28, in <module>
args.func(args)
File "/usr/lib/python2.7/site-packages/airflow/bin/cli.py", line 585, in test
ti.run(ignore_task_deps=True, ignore_ti_state=True, test_mode=True)
File "/usr/lib/python2.7/site-packages/airflow/utils/db.py", line 53, in wrapper
result = func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/airflow/models.py", line 1374, in run
result = task_copy.execute(context=context)
File "/usr/lib/python2.7/site-packages/airflow/contrib/operators/dataflow_operator.py", line 114, in execute
self.jar = bucket_helper.google_cloud_to_local(self.jar)
File "/usr/lib/python2.7/site-packages/airflow/contrib/operators/dataflow_operator.py", line 229, in google_cloud_to_local
file_size = self._gcs_hook.download(bucket_id, object_id, local_file)
File "/usr/lib/python2.7/site-packages/airflow/contrib/hooks/gcs_hook.py", line 58, in download
service = self.get_conn()
File "/usr/lib/python2.7/site-packages/airflow/contrib/hooks/gcs_hook.py", line 43, in get_conn
http_authorized = self._authorize()
File "/usr/lib/python2.7/site-packages/airflow/contrib/hooks/gcp_api_base_hook.py", line 84, in _authorize
raise AirflowException('Scope should be defined when using a key file.')
airflow.exceptions.AirflowException: Scope should be defined when using a key file.

Can anybody please help me with it ?.Thanks in advance
Comment: Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code  necessary to reproduce it **in the question itself**. Questions without a **clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve).
Answer: You should specify the "Scopes" field of the connection in Airflow. In the Web UI, go to Admin > Connections in the top menu and click the edit link for google_cloud_default. For GCS, the list of scopes can be found here. The most general to use is https://www.googleapis.com/auth/cloud-platform.


Add new images to existing nodes

Question: I am using imagefield to make photo albums. Now I want to allow all registered users to upload new images to any photo albums and it should get added to the exisiting album. I do not want to give them permission to edit the node. How can I do this?
Answer: You'll likely have to change what you mean by "edit the node". You could use Field Permissions to restrict what they're able to change when editing the node.
But to truly accomplish this without letting them "edit the node", you'll need a very complex custom solution that would be unreasonably complicated for an answer on this site.
Comment: Does a module like http://drupal.org/project/editablefields would ease the process ?

Add new images to existing nodes


Chmod 640 for uploaded file after SUPEE 7405 patch

Question: After installing the SUPEE 7405 patch, we noticed a problem uploading images from the admin. All file permissions are being set to CHMOD 640 which makes them inaccessible to all users.
Is there a solution that does not involve rewriting the /lib/Varien/File/Uploader.php file?
Comment: Questions on professional server- or networking-related infrastructure administration are off-topic for Stack Overflow unless they directly involve programming or programming tools. You may be able to get help on [Server Fault](http://serverfault.com/about).
Comment: Small note: You can change the chmod with PHP (as long as PHP has the permissions) `chmod($filepath, $new_chmod);` Though the files should be uploaded by the user/group of the webserver.
Comment: The solution below proves that this is a server related issue, partially. The solution doesn't completely solve all of the issues with the CHMOD 640 change of the SUPEE 7405 patch, but it does resolve a few of them.
Answer: A new version of SUPEE-7405 has been released that resolves this issue:
http://magento.com/security/patches/supee-7405

Updated February 23, 2016
Updated versions of this release are now available. The updates add support for PHP 5.3 and address issues with upload file permissions, merging carts, and SOAP APIs experienced with the original release.

Note that even without the revised patch, you can fix the issue by using the recommended file permissions (see below).

Magento expects the webserver to own the site files:
http://devdocs.magento.com/guides/m1x/install/installer-privileges_after.html#privs-after
You can resolve this problem by making the webserver the owner of the files.
chown -R web-server-user-name magento/root/path

The webserver user name is commonly www-data or apache.
If you follow the instructions in the above link, the webserver will have read access to all files, and write access to media files and var files. This should be all you need for typical site operation. If you need to use Magento Connect you'll have to temporarily give the webserver write access to all files.

All file permissions are being set to CHMOD 640 which makes them inaccessible to all users.

Only the webserver user needs access to the files. There is no need to grant any permissions to all users.
You may want to grant access to a specific user if, for example, you need to edit or upload files via FTP. In this case, what I do is set a user who owns the file system and set the files' group to the webserver:
cd magento/root/directory

# Set ownership
# 'username' should be the file system owner username
# 'webserver' should be the webserver username
chown -R username:webserver .

# Give the user read/write access to all files.
# Give the webserver read access to all files
find . -type f -exec chmod 640 {} \;
find . -type d -exec chmod 2750 {} \;

# Give the user and the webserver read/write access to var and media
find var/ -type f -exec chmod 660 {} \;
find media/ -type f -exec chmod 660 {} \;
find var/ -type d -exec chmod 2770 {} \;
find media/ -type d -exec chmod 2770 {} \;
chmod 2770 includes
chmod 660 includes/config.php

The above commands will give your file system owner read/write access to everything and the webserver read access to everything. The webserver will also be able to write to the media and var directories.
Comment: @Jared That sounds like there is still a permissions issue. Does the webserver have read access to the image? Note that Magento can resize images, so the image displayed on the frontend is not necessarily the same image displayed on the backend.
Comment: I never thought to look in this direction. I'll give this a try and see if it works.
Comment: NOTE: I've marked this as a solution, however, I still encountered issues when using this method. For example, we use a javascript image slider on our front page. After adding chown -R to the server, the image appears in the thumbnail view from the admin, but in the front-end (visitor/guest), the user stills recieves a 403 (forbidden) error when trying to view file.
Comment: You might  be right. I tested the solution on three modules: Adding a new image to an existing product failed to display in the admin, but appeared in the front-end. My custom module where the image/file is made available for download worked perfectly in both front-end and back-end. Finally, the third party front-page slider failed both, the front-end and back-end image tests. I've put our dev server in maintenance mode to perform a few more tests and confirm a few things. It does sound like a permissions issue though. Perhaps I missed another patch related change.
Comment: I have a solution, but it's specific to my server's environment and may not help other people out. This is in the right direction so I'm leaving this as the correct answer and will post my answer as well below.
Comment: If your webserver runs as nobody, are they any security implication in changing the group of all your magento files to no-group? Will this invalidate the whole point of the patch?
Comment: @Dom If only the webserver is running as nobody then I don't think there is any difference. My understanding is that `nobody:nogroup` is used for general, unprivileged access. If you have other processes running as nobody then they will have read access to all of the site files, including the local.xml file. This doesn't really have anything to do with the patch. See: APPSEC-1306 https://magento.com/security/patches/supee-7405
Comment: @mtinsley I guess I'm trying to understand what security enhancement the patch makes by making sure the permission for others is zero. I can understand denying executing the files that could be uploaded. If you end up having the files as phpuser:nogroup, then aren't you relaxing the  - others can't view the file - restriction that the patch enforces?
Comment: @Dom Before the patch, the permissions were set to 777 `chmod($destinationFile, 0777);`. Making it 640 addresses the issue. Relaxing it to 644 works as well, but is inconsistent with the [Principle of least privilege](https://en.wikipedia.org/wiki/Principle_of_least_privilege). The patch is mainly meant to address the problem with the file being executable.
Comment: @mtinsley that makes sense now, thanks. I like your solution of using gid and changing the group of all files to the webserver, but I'm concerned that cPanel scripts would not expect this, so for now I've overridden the Varian/File/Uploader.php with 644 permissions. Given the outcry on the Magento forums, do you anticipate Magento will change the core file to 644?
Comment: @Dom Regarding cpanel, you still don't need to grant permissions to all users. With suPHP, PHP files are accessed as the file system owner and non-PHP files are accessed as the webserver. Regarding changing the permissions to 644, I'm not sure if they will or not. I don't think there is any use-case that can't be addressed by correcting the file permissions.
Comment: @mtinsley On my hosting environments, Apache runs as nobody, so for Apache to be able to serve uploaded images, those images either need have o+r permission or have to have no-group. My concern is that cPanel/WHM generates accounts where the webroot is owned and grouped with the account user, and I was wondering if the various cPanel/WHML scripts that run, require or expect this owner:group
Comment: @Dom To be honest, I'm not overly familiar with WHM and I tend to avoid it completely when it comes to Magento. You may want to try asking on server fault.
Answer: We've solved the issue for our environments, however, I'm not sure how much help this will be for everyone else. Even though I'm not a network engineer, I will try to explain it. If enough people find this post helpful, I'll mark it as correct. Also, please note that even though the issue arose from Magento's SUPEE 7405 patch, the solution is Network based, not code based.
I believe the purpose of the chmod alteration in the patch was to prevent hackers from hijacking your images and storing sensitive data within them (the checkout header image hack for example). To prevent this hack, they limit all access to uploaded files/images via chmod 640.
With that said...
The latest patch to Magento 1.X seems to require an environment configuration change. As one of our network engineers said, they assume we are using Apache with mod_php, which reads and writes all files as the Apache user. However, if you are using fcgi or suphp, the files would be written as the domain user. Depending on your environment, you may need to add Apache to your groups and allow it to read the files.
Try the chown -R solution first, and if that doesn't work you may need to contact your host or add Apache to your "groups" so that it has owner access.
Comment: With suphp, I think you can just `chown -R user:user` where 'user' is whatever user the webserver is running as. I think this will be the solution for cpanel as well.
Answer: Please go on this file
lib/Varien/File/Uploader.php

and just change line no 220 and change chmod($destinationFile, 0640) to chmod($destinationFile, 0644)
It's working.
Comment: Shame on Magento! This definitely should be a config option as not everyones setup is the same. As long as it's not, I'll grudgingly apply the core hack :/
Answer: The accepted answer is a good solution.
If you are unable to change the ownership (maybe because you are on a shared server) you can run cron jobs to change file permission on the newly uploaded files.
*/3 * * * * find /path/to/magento/ -type f -perm 640 -exec chmod 644 {} \;

*/3 * * * * find /path/to/magento/ -type d -perm 750 -exec chmod 2755 {} \;

Answer: Look at this: https://community.magento.com/t5/Security-Patches/after-installing-SUPEE-7405-can-no-longer-add-or-change-images/td-p/26785/page/3
Changing Upload.php code worked for all my installtions.
To fix existing uploaded images, you need to change ALL existing uploaded images permissions (chmod) from 0640 to 0644.
To fix it for the future, you would need to edit /lib/Varien/File/Uploader.php and change the line from (after applying the patch)

chmod($destinationFile, 0640);

 to

chmod($destinationFile, 0644);

There is a similar one for directories that you'll need to change from 0750 to 0755.

Comment: Please note that this is not a good solution. It undermines the purpose of the security patch which protects your images from having credit card information stored in them.
Comment: please read the question. Modifying the lib/Varien/File/Uploader.php is not a good solution as it will break future patches. It's better as a temporary band-aid until a real solution is made public.
Comment: There is no need to edit core files, this can be done as an override. Copy /lib/Varien/File/Uploader.php to /app/code/local/Varien/File/Uploader.php and edit the copied file with the suggested permission changes above. This should be the preferred solution as the answer from @mtinsley above doesn't address some common problems with server configuration.
Comment: I try to change Uploader.php but don't work for me. I also try to add the same file to /app/code/local/Varien/File/ . But no change it uploads again image with 0640. Are there more files to fix?


How do I remove the text "webform content" at the top of a block?

Question: I have several Drupal 8 webforms, some of which are embedded as blocks. At the top of each webform, I'm getting the "webform content" text.

How do I safely remove that text?
Comment: It looks like you are including the field label in the display.  If you created the block using a custom block type, look at the Display settings.  Mark the field label as hidden.  Not sure what to do if you are using the built in webform blocks.
Comment: That sounds promising! OK, newbie hat on: Where would I look for the Display settings? Would it be somewhere in the webform itself? Thanks!
Comment: If it was a custom block, it would be under admin->Structure->Block Layout->Types and then select your webform block.  Then it is a tab labeled Manage Display.
Comment: That was it - thank you!!!!
Comment: Thanks to https://drupal.stackexchange.com/users/199/kiamlaluno for the editing recommendations!
Answer: It looks like you are including the field label in the display. If you created the block using a custom block type do the following:

Go to Admin->Structure->Block Layout->Types
Then select your WebForm block type
There will be a tab labeled "Manage Display"
On this page, mark the field label as hidden.

Not sure what to do if you are using the built in Webform blocks
Comment: Thanks again! Should I delete my "answer" below? I entered it before this appeared.
Comment: Technically yes on the delete, but I don't mind if you leave it.... will help you get your reputation points up a bit... and being new every little bit helps.. :)
Comment: Thanks, but I went ahead and deleted it anyway :)


Correlation matrix of rows, correcting for correlation matrix of columns

Question: Given a matrix of observations (rows) x variables (columns), can we compute the correlation matrix of the rows, but corrected by the correlation matrix of the columns?  The goal would be to avoid inflation in the correlation p values, since the t-test for Pearson correlation assumes that the columns/variables are uncorrelated, independent observations, i.e. that the correlation matrix of the columns is the identity matrix.
Intuitively, this could be accomplished with a weighted correlation, where e.g. if a pair of variables are nearly perfectly correlated, they would each be down-weighted by a factor of 2.
Edit: there is an exact null distribution for the correlations in the case where the columns are independent, but there are only two rows and this pair of rows is sampled from a bivariate normal distribution.  I would like a null distribution for the opposite case, where the rows are independent, but the columns are sampled from a multivariate normal distribution.
Comment: Can you explain symbolically the assumption you're referring to (made by the t-test for Pearson correlation)?
Comment: @eric_kernfeld: The assumption is that $t = r \sqrt{\frac{n - 2}{1 - r^2}}$ is t-distributed with $n - 2$ degrees of freedom under the null, where $r$ is the correlation and $n$ is the number of data points.
Comment: That's not what I meant to ask for. When you write "assumes that the columns/variables are independent", can you write out what that means to you and how it differs from the model you have in mind?
Comment: Sorry, that was a bit ambiguous - I edited the question.
Answer: Here's something you can try.
Let $X$ be your data matrix. Normalize $X$ as you choose.
Calculate the $p \times p$ correlation matrix $M$ for the columns of $X$.
Then write the singular value decomposition of $M$ as
$$USV^T = M.$$
Define a new data set $$Y = X U \sqrt{S} .$$
Then calculate the correlation matrix for the rows of $Y$.
This is in some sense what you are after: the "adjusted" correlation matrix for the rows of $X$.
Comment: If I understand correctly, without the $\sqrt{S}$ this would be PCA, but the $\sqrt{S}$ additionally weights by the square root of the percent of variance explained by each component.  One desirable property of a solution is that if you duplicate a column of the data matrix $X$ (say with a bit of noise), the correlation p values should stay the same. Here, I think you'd have the same top principal components, but the PCs most similar to the duplicated column would have larger eigenvalues, which would change the correlation p values.
Comment: Not sure what the question here is.  I provided a real example in the edit.  Play around with it.
Comment: I was saying I think it's not robust because if you duplicate one of the columns, the p values change.  I think all SVD-based solutions have this problem.
Comment: I'm curious whether you could instead use $Y = XU$ (PCA) or $Y = XSU$ (PCA weighted by % variance explained) rather than $Y = X\sqrt{S}U$ (PCA weighted by sqrt of % variance explained)?
Comment: @1'' Hey sorry I had a typo in the answer, the multiplication order should be switched.
Comment: No problem :)  What do you think of the robustness issue I mentioned?  Also, any particular reason why it's a square root?


Is there a formula to assign a 0 to 5 score by comparing your value against the min and the max of a range?

Question: Lets say I have a database of customers with how much they spent
so something like
Jim 100
Bob 300
Tim 14
Sarah 43
Rob 945
Jake 3,000
etc
What I want to do is then assign each customer a value from 0.00 to 5.00 (so to two decimal places) based on how their number compares to the min and the max.
I think this has something to do with percentiles, but what is the formula to do this?
I guess we would start by separating the data into five quintiles. How do I determine the boundary of each quintile?
And then lets say a customer falls into the two to three quintile. How do I specify the exact two point precision of like 2.13 of 2.67 for their position in that quintile.
Answer: If the only thing you are interested in is where the customer falls within the min and the max, compute $$5 * \frac{customer - \min}{\max - \min}$$ to get a number between $0$ and $5$ for each customer. You can then round it to any number of decimal points you like.
For example, if you only have the data you listed, then $\min = 14, \max = 3000$ and so for Bob you would get
$$
m_\text{Bob} = 5 * \frac{300 - 14}{3000 - 14} = \frac{1430}{2986} \approx 0.48.
$$

This, however, takes no account of where the number is with respect to the other customers. If you would like to compute that, then you can compute the actual percentile for each number and multiply by $5$, rounding to $2$ decimal places, if you like.
To compute the percentiles from your data set, first sort it:
Name   Amount
====   ======
Tim       14
Sarah     43
Jim      100
Bob      300
Rob      945
Jake    3000

Since you have $n=6$ data points, divide the $100\%$ range into $6$ boxes, so each box will have $100/n$ (here $100/6 \approx 16.667\%$, and use the maximum (or minimum or middle) block member for each label. Then multiply by $5$ to get your label:
Name   Amount  Percentile  Label
====   ======  ==========  =======
Tim       14     16.667%    0.83
Sarah     43     33.333%    1.67
Jim      100     50.000%    2.50
Bob      300     66.667%    3.33
Rob      945     83.333%    4.17
Jake    3000    100.000%    5.00


Bad sound from amp

Question: I get this really bad feedback sounding noise when I play stuff like this, from the beginning of the Free Bird solo,
shown in tab:
<code>1  ----18---18---18---------
2  -18---18---18---------
</code>
(All notes on second string played with a full bend.)
It's there on any amount of distortion, but not when I play clean. Is it a problem with the amp? If so, what can I do about it?
Comment: Hmm. Can you elaborate a little? What are the settings on the amplifier? Are you plugged straight in? Are you using any pedals?
Comment: Treble: Full. Middle: Full. Bass: Full. Gain: if there's any distortion at all, it makes the sound. No pedals or anything.
Comment: Couple more questions: What model is the amplifier? Any Cabinets? What's the volume settings you are playing at? Finally, it's likely unrelated, but what kind of guitar are you using?
Comment: The amp is a JA-010-G. I hate to sound stupid but I don't know what you mean by Cabinet. Volume is fairly low. The guitar is a First Act electric, so by no means a high-dollar instrument.
Comment: A cabinet (or speaker) is essentially a speaker in a box - the 'cabinet.' They are usually used in conjunction with a Amp Head, which controls the EQ and any built in effects, and sends the signal and power to the Cabinets. A lot of amps (including yours) are combo amps, meaning the head and cabinet are built in together in one unit.
Answer: Looks like you have a little 10 Watt practice amp with a 6.5" speaker. I'm going to be straight honest with you: you may not be able to get rid of this issue. Harmonic feedback as I know it has a lot to do with volume. For example, you could have an amp gained out to obscene levels but at a low volume and never cause any harmonic feedback--so feedback isn't necessarily as much of a relationship with gain as it is with volume and gain. If I were to describe it as a function I would say that the amount of harmonic feedback = f(volume, gain) where volume and gain are variable to some degree but proportional.
So after explaining feedback a little, let's address your problem. I don't necessarily think that you are hearing any harmonic feedback in this case--but it's more likely that you are detecting some dissonance or artifacts in the sound produced from your amp. Practice amplifiers are built to be just that. A 6.5" speaker can get pretty loud mind you--I used to piss off the neighbors with my little Fender Champ--but it's not terribly good at reproducing the frequency range of a guitar; specifically an overdriven guitar. Overdrive results in a lot of higher order harmonics jumping out of the sound, and my guess is your little amp speaker simply cannot properly reproduce the overdrive as it should be. You're likely hearing a bit of dissonance (as AbstractDissonance and Alistair both pointed out).
Since the practice amplifier you named is solid state, I highly doubt any of the components in the tone stack or amp circuitry are on the fritz. Even cheaply built (please take no offense) solid state components can last a pretty long time assuming they are built within manufacturing tolerances.
There are also many, many additional factors that could be potentially causing the problem, but this is my best guess based on the information that I have currently. If you want to help us out further you could post some audio clips of the amplifier on overdriven and clean channels so that we can A/B them. Alternatively, if you're up to it, you could even post a quick video of your gear rundown and produce the problem on the spot. Those pieces would make it a whole lot easier to completely troubleshoot your issue, but right now I've given you my best shot :D.
Comment: Oh, no offense taken. I really appreciate everyone's help. so, is the gist of it "You've got a cheap amp"? if so, what would you recommend for a good amp?
Comment: That depends on what you want to do with it :D. If you're looking for a practice amplifier, you could keep the one you have and deal with the crappy distorted sound--or if you want to one-up a little there's a couple of good entry level gigging tube/solid state amps out there. In any event, I would recommend getting an amplifier with at least a 10" speaker in it. That seems to be about the smallest you can go with for proper harmonic reproduction.
Comment: Cool man. Not sure if you're new to SE or not, but bemember if you like the answer, to up vote it and whichever answer you deem best mark as the answer to your question.
Comment: Yeah I'm brand new here, just started my account this afternoon. I tried to up vote your answers but it said I needed 15 rep.
Comment: My bad, didn't know that restriction existed. Apologies, and welcome to guitars.SE :D
Answer: This could be a number of problems.

Dissonance: This, in basic terms, is when two notes do not sound good together, and clash producing an unpleasant noise. However I have tried this out on my guitar and amp and I do not get any dissonance, so it may not be that.
Amp/Speaker Problems: I have found, particularly with Line 6 Amps and Speakers, that when the bass and mid knobs are turned very high, the speaker cannot cope very well and produces and grinding feedbacky boom.
Tuning problems: It may sound stupid and patronising of me, but are you perfectly in tune? According to both ear and tuner? If so, your guitar's intonation may be out. Any slight fault in an intonation setup causes the guitar to be out of tune with itself, and therefore notes that usually harmonise well may suddenly sound dissonant. This is greatly accentuated at higher frets, where strings will sound even worse if the intonation is out.
See this previous question for help about intonation.

Distortion greatly increases the unpleasant dissonant sound, so that may explain why you do not find you have the problem on a clean setting.
Hope this helps.
Comment: I don't think that is the case with my amp, because turning the Bass and Middle down did not help at all. Also, I'm confidant that the guitar is tuned correct, by tuner and by ear.
Comment: Right, I would thoroughly check your tuning, strings, and intonation to make sure everything is all good. You mentioned that the guitar you used was not particularly expensive. Many lower end guitars are known not to come with the best of factory setups, so it is always worth checking.
Comment: Just did. Intonation and tuning is good. The strings are like new, no rust or anything.
Answer: Is the amp solid state, digital, or tube? Is the volume high or low, does reduce in gain and/or decrease with volume? Can you reproduce the sound without bending(could be an artifact bends not in tune). Is your guitar intonated properly?
Note that the guitar uses equal temperament and not all notes are in tube. m3rd's with a lot of distortion of a specific type can sound pretty bad. With class B tube amps you must turn them up because they have intermodulation distortion which is almost at a constant volume but can be masked with volume. That is, the louder the amp the less you will hear this type of distortion which is very annoying. Solid State can suffer from this too but digital won't.
Distortion adds harshness to a sound and it can be emphasized with certain notes and intervals. Distortion can come from a wide variety of places in an amp. Some good and some bad. Sometimes the "bad" distortion can be good in some context.
Naming the amp you have, if you can reproduce this sound on the clean channel with slight over drive, and a clip of the sound will help(although I think I know exactly what you are getting at as I have a similar issue with my amp ATM and I'm in the process of tweaking it to get a smoother distortion).
Comment: To your first question, I'm sorry, I have no idea. It's a JA-010-G.
Comment: The volume is rather low, though I did try it at a higher volume and the noise was still there. Volume appears to not affect it, and if there is any gain at all the noise will be there. but the noise is not there when I play it clean.
Comment: And also, would you mind telling me how I could tweak my amp?
Comment: Seems the amp is a solid state so you shouldn't have to mess with anything. It could be possible that a transistor is failing OR the design of the amp simply is high distortion or biased improperly. Sometimes amps, if they are old or cheap can have components such as resistors or capacitors that drift off the standard value to such a degree to make the amp not function properly.

First things first though, does the same problem happen when you play in different areas of the neck and/or with different notes? Could it possibly be due to uneven frets?
Comment: I had the frets leveled and polished professionally a little while ago, so I doubt it's that. And it just has that problem up the neck, but yes with different notes, but not if all the notes are on the same string. If you didn't see what I said further up, Thank you for taking the time and effort to answer my question.
Comment: The best I can same is that it's an intonation issue. This is not your fault but guitars use ET-12 tuning which means they are always a little out of tune. Depending on how you play. But because you are bending to the note this may suggest otherwise unless you are blinding bending to the note. Try playing the interval(or others) but see if you can over/under bend the note to make that sound go away(or reduce it). I couldn't tell if that amp had a clean and drive channel but it is possible the drive channel is out.
Comment: I mean that it's got some issues since you say it happens at all volumes. You say it goes away when you play clean but how? When you play really soft or when change to the clean channel or when you reduce the gain so it sounds clean? There seems to be two possibilities: The drive channel is bad producing bad tones when you play or this is just the nature of distortion(I have some issues with it when I play high gain and some intervals too on my 2k tube amp). Since the drive ch seems to work fine except on those intervals I think your just hearing the dissonances created by the distortion.
Comment: Go to your local music store and check out some amps and try to see if you can reproduce the problem. That will be the easiest way. Take your amp and guitar too if you want(of course then they'll try and sell you an amp for sure ;). That way you'll know for sure and won't end up practicing on a bad amp(if that is the case).
Comment: It goes away completely when I either turn overdrive off totally and when I put gain so low that it sounds 100% clean
Comment: Ok, it sounds as if it's simply the result of overdriving the amp. Like I said, I have similar problems with my tube amp. When you play an interval the amp will add more intervals both above and below the notes. These can create harsh dissonances specially if they are not closely harmonically related. This is actually why tube amps sound nice is because they tend to add closely related harmonic intervals while SS tends to add more dissonant intervals.
Comment: Having said that, there comes a point where so much distortion can add so many extra harmonics that it becomes "characteristic". Why do you think they call metal, metal? If you look at the frequency response of metallic things to impulses you get a wide band frequency response.  If this is what kinda sound your betting at you might be in the region where there is not enough overdrive or not enough treble. Try playing a lot around with all the knobs to dial in your sound. You could easily spend a day working on it trying to find just the right combination.
Comment: Ultimately you will be limited by the cheap amp but I have found that some cheap practice amps(my first one for example) can have cool metal like sounds.
Answer: Before "throwing out" your Amplifier I would suggest trying a different guitar! A good guitar with good mics will sound at least resonable through a bad amp, while a bad guitar will sound crappy even through the best amps. I looked at some reviews of your guitar since I am not familiar with that model and it seems like feedback noise is something that many people get with it... A more expensive amp will probably not fix your feedback issues either since the problem lies in the wiring of the mics.
Hope this helps!
"It can't really handle being cranked through an amp too loud because it feeds back and isn't made for anything huge"
http://www.ultimate-guitar.com/reviews/electric_guitars/first_act/me300/index.html
Comment: Ok, thanks. I got it used, but in great condition for $70 so I figured "Oh, why not, I'll learn the electric guitar too." I reckon I'll piddle around on my friend's good equipment. Again, thanks for bringing up a new angle on my question. I didn't think that the problem might be that it's a cheap guitar!
Comment: Have you tested another guitar? Usually all kinds of weird noises/feedbacks can come with cheap mics or bad wiring. The amp would be the last thing I would check in your case.

Bad sound from amp


EndpointNotFoundException in webservice call from website, but not from console application

Question: I'm getting this EndpointNotFoundException on a webservice call from a website, while the same exact call works if I do it from a console application.
Here's the more detailed exception message:
Could not connect to https://******. TCP error code 10060:
A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond ***.***.***.***:443.

I've confirmed that the bindings (in web.config and app.config) are the same, so I can't see why it works in one case and not in the other.
<basicHttpBinding>
<binding name="ServicePortBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
 receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
 bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
 maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
 messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
 useDefaultWebProxy="true">
 <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
 <security mode="Transport">
  <transport clientCredentialType="None" proxyCredentialType="None"
   realm="" />
  <message clientCredentialType="UserName" algorithmSuite="Default" />
 </security>
</binding>
</basicHttpBinding>

Let me know if you need any more details.
EDIT
OK, here's something I left out that might be causing the issue. This is all happening behind the company firewall.
Could it be that said firewall is preventing it from running from the website because it's an HTTP connection spawned from an existing "web process"? Whereas in the console app it's just a single connection.
Comment: Where is the web site?  On the same machine as the console app?
Comment: Yes, they're both running in the same machine. The webservice is not on the same machine, though.
Answer: You need to add a proxy bypass in your web.config
  <system.net>
    <defaultProxy>
      <proxy
      usesystemdefault = "true"
      proxyaddress="http://theproxyaddress:PortNumber"
      bypassonlocal="false"
/>
    </defaultProxy>
  </system.net>

This will allow you to open a connection to the service through your firewalls
Answer: Have you tried using wireshark (or Fiddler if it's HTTP) to see if the outbound calls appear to be equivalent and correct?
Comment: You know what the weirdest thing is? I installed Fiddler and when the program is running, it works! As soon as I turn Fiddler off, I get the timeout again...
Answer: Sounds and smells like a permission issue.
Your web app typically runs as ASPNET or NETWORK SERVICE, while your console app will run as yourself (your account).
Any chance there's a problem there??
Do you do any kind of authentication on the WCF server end that might work for normal user account, but not for ASPNET / NETWORK SERVICE??
Comment: The only thing I could think of was the security mode, but just tried changing it to None and I still get the same error.


fuzzy implementation for capturing specific strings

Question: I am going to develop a web crawler using java to capture hotel room prices from hotel websites.
In this case I want to capture room price with the room type and the meal type, so my algorithm should be intelligent to handle that.
For example:
Room type: Deluxe
Meal type: HalfBoad
price : $20.00

The main problem is room prices can be in different ways in different hotel sites. So my algorithm should be independent from hotel sites.
I am plan to use above room types and meal types as a fuzzy sets and compare the words in  webpage with above fuzzy sets using a suitable membership function.
Anyone experienced with this? or have an idea for my problem?
Answer: There are two ways to approach this problem:

You can customize your crawler to understand the formats used by different Websites; or
You can come up with a general ("fuzzy") solution.

(1) will, by far, be the easiest. Ideally you want to create some tools that make this easier so you can create a filter for any new site in minimal time. IMHO your time will be best spent with this approach.
(2) has lots of problems. Firstly it will be unreliable. You will come across formats you don't understand or (worse) get wrong. Second, it will require a substantial amount of development to get something working. This is the sort of thing you use when you're dealing with thousands or millions of sites.
With hundreds of sites you will get better and more predictable results with (1).
Comment: hey cletus, tnx for ur attention frist,
I also agree with ur 1st option, but this is my  final year project in university and they asked me for a general solution as u mentioned in option 2.
I am still final year under Graduate student and not experienced with these kind of problem before this.
have you any experienced regarding option 2.
any way tnx for your attention agin
:)
Comment: @Kasun: This is why people leave universities thinking they have learned how to write software and are shattered to find out that computer science and software development are all but unrelated.  This is going to be a massive undertaking.  In the real world, you are far better of demonstrating that harnessing human intelligence is a far lower-cost endeavor than creating machine intelligence.  There's no reason why that shouldn't be true in the academic world as well.
Answer: As with all problems, design can let you deliver value adapt to situations you haven't considered much more quickly than the general solution.
Start by writing something that parses the data from one provider - the one with the simplest format to handle.  Find a way to adapt that handler into your crawler.  Be sure to encapsulate construction - you should always do this anyway...
public class RoomTypeExtractor
{
  private RoomTypeExtractor() { }

  public static RoomTypeExtractor GetInstance()
  {
    return new RoomTypeExtractor();
  }

  public string GetRoomType(string content)
  {
    // BEHAVIOR #1
  }
}

The GetInstance() ,ethod lets you promote to a Strategy pattern for practically free.
Then add your second provider type.  Say, for instance, that you have a slightly more complex data format which is a little more prevalent than the first format.  Start by refactoring what was your concrete room type extractor class into an abstraction with a single variation behind it and have the GetInstance() method return an instance of the concrete type:
public abstract class RoomTypeExtractor
{
  public static RoomTypeExtractor GetInstance()
  {
    return SimpleRoomTypeExtractor.GetInstance();
  }

  public abstract string GetRoomType(string content);
}

public final class SimpleRoomTypeExtractor extends RoomTypeExtractor
{
  private SimpleRoomTypeExtractor() { }

  public static SimpleRoomTypeExtractor GetInstance()
  {
    return new SimpleRoomTypeExtractor();
  }

  public string GetRoomType(string content)
  {
    // BEHAVIOR #1
  }
}

Create another variation that implements the Null Object pattern...
public class NullRoomTypeExtractor extends RoomTypeExtractor
{
  private NullRoomTypeExtractor() { }

  public static NullRoomTypeExtractor GetInstance()
  {
    return new NullRoomTypeExtractor();
  }

  public string GetRoomType(string content)
  {
    // whatever "no content" behavior you want... I chose returning null
    return null;
  }
}

Add a base class that will make it easier to work with the Chain of Responsibility pattern that is in this problem:
public abstract class ChainLinkRoomTypeExtractor extends RoomTypeExtractor
{
  private final RoomTypeExtractor next_;

  protected ChainLinkRoomTypeExtractor(RoomTypeExtractor next)
  {
    next_ = next;
  }

  public final string GetRoomType(string content)
  {
    if (CanHandleContent(content))
    {
      return GetRoomTypeFromUnderstoodFormat(content);
    }
    else
    {
      return next_.GetRoomType(content);
    }
  }

  protected abstract bool CanHandleContent(string content);
  protected abstract string GetRoomTypeFromUnderstoodFormat(string content);
}

Now, refactor the original implementation to have a base class that joins it into a Chain of Responsibility...
public final class SimpleRoomTypeExtractor extends ChainLinkRoomTypeExtractor
{
  private SimpleRoomTypeExtractor(RoomTypeExtractor next)
  {
    super(next);
  }

  public static SimpleRoomTypeExtractor GetInstance(RoomTypeExtractor next)
  {
    return new SimpleRoomTypeExtractor(next);
  }

  protected string CanHandleContent(string content)
  {
    // return whether or not content contains the right format
  }

  protected string GetRoomTypeFromUnderstoodFormat(string content)
  {
    // BEHAVIOR #1
  }
}

Be sure to update RoomTypeExtractor.GetInstance():
  public static RoomTypeExtractor GetInstance()
  {
    RoomTypeExtractor extractor = NullRoomTypeExtractor.GetInstance();

    extractor = SimpleRoomTypeExtractor.GetInstance(extractor);

    return extractor;
  }

Once that's done, create a new link for the Chain of Responsibility...
public final class MoreComplexRoomTypeExtractor extends ChainLinkRoomTypeExtractor
{
  private MoreComplexRoomTypeExtractor(RoomTypeExtractor next)
  {
    super(next);
  }

  public static MoreComplexRoomTypeExtractor GetInstance(RoomTypeExtractor next)
  {
    return new MoreComplexRoomTypeExtractor(next);
  }

  protected string CanHandleContent(string content)
  {
    // Check for presence of format #2
  }

  protected string GetRoomTypeFromUnderstoodFormat(string content)
  {
    // BEHAVIOR #2
  }
}

Finally, add the new link to the chain, if this is a more common format, you might want to give it higher priority by putting it higher in the chain (the real forces that govern the order of the chain will become apparent when you do this):
  public static RoomTypeExtractor GetInstance()
  {
    RoomTypeExtractor extractor = NullRoomTypeExtractor.GetInstance();

    extractor = SimpleRoomTypeExtractor.GetInstance(extractor);
    extractor = MoreComplexRoomTypeExtractor.GetInstance(extractor);

    return extractor;
  }

As time passes, you may want to add ways to dynamically add new links to the Chain of Responsibility, as pointed out by Cletus, but the fundamental principle here is Emergent Design.  Start with high quality.  Keep quality high.  Drive with tests.  Do those three things and you will be able to use the fuzzy logic engine between your ears to overcome almost any problem...
EDIT
Translated to Java.  Hope I did that right; I'm a little rusty.
Comment: hi MaxGuernseyIII, Im real happy with ur codes and ur atemps. realy it will use full for my developments, but in this time i hope help for recognizing room type and meal types with price,
(Using an intelligent algorithm)
any wy realy tnx Guernsey,
Comment: @Kasun: The code is meant to demonstrate a process not to be used directly.  At this time, engaging in that kind of process - the process of letting a design emerge - is a far more fruitful activity than attempting to write an intelligent algorithm.


Spring Security, exibir mensagem quando encerrar sessão

Question: Estou controlando a sessão da minha aplicação com o Spring Security, tenho duas regras para encerrar a sessão, max-session
<session-management>
    <concurrency-control max-sessions="1" error-if-maximum-exceeded="true"
            expired-url="/publico/login.jsp" />
</session-management>

E tem o timeout
<session-config>
    <session-timeout>20</session-timeout>
</session-config>

Comment: Solucionou o problema? E quanto a URL `/publico/login.jsp`, não funciona quando expira? E se cria-se um arquivo .jsp quando expira-se com a mensagem quando encerrar a sessão?
Comment: Não solucionei pelo spring, oq eu fiz foi o seguinte, quando da o time out ele corta minha conexão com o banco de dados e acabei tratando diretamente no java com try cat em minha conexão. Não foi a solução mais correta.
Answer: Uma forma de fazer com que essa mensagem apareça dizendo que a session encerrou, é utilizando um parâmetro na URL (expired-url="/publico/login.jsp?sessionExpired=sim") e ter na página login uma condicional em EL, para fazer verificação para quando esse parâmetro aparecer em sua URL, mostrar o corpo da mensagem.
Ex:
${param.sessionExpired}

Utilize "param" para pegar qualquer tipo de parametro, é semelhante ao uso de "requestScope" para pegar qualquer atributo setado em uma Session. ${requestScope.nomeDoAtributo}


popup not active window gets activated

Question: I've a popup window which I show with the SW_SHOWNA flag. If I click parts of this window, I catch the click and then close it, the problem is, there are other parts of the window and I don't want to close the window on click on them, but when I click them the window gets activated and gets the focus, that's bad for me, I want this window to stay not activated, and that other window will remind focused.
any ideas?
thanks,
mike.
Comment: See also http://stackoverflow.com/questions/2271795/not-active-popup-window
Answer: If you handle WM_HITTEST and WM_NCHITTEST, watch for the points you're interested in, and return HT_NOWHERE for everywhere else, then I think the window shouldn't be activated (I haven't tested this theory).

popup not active window gets activated


Not found value spark SBT project

Question: Hi i am trying to set up a small spark application in SBT,
My build.sbt is
import Dependencies._

name := "hello"

version := "1.0"

scalaVersion := "2.11.8"

val sparkVersion = "1.6.1"

libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % sparkVersion,
  "org.apache.spark" %% "spark-streaming" % sparkVersion,
  "org.apache.spark" %% "spark-streaming-twitter" % sparkVersion
)

libraryDependencies += scalaTest % Test

Everything works fine i get all dependencies resolved by SBT, but when i try importing spark in my hello.scala project file i get this error
not found: value spark
my hello.scala file is
package example
import org.apache.spark._
import org.apache.spark.SparkContext._

object Hello extends fileImport with App {
  println(greeting)
  anime.select("*").orderBy($"rating".desc).limit(10).show()
}

trait fileImport {
  lazy val greeting: String = "hello"
  var anime = spark.read.option("header", true).csv("C:/anime.csv")
  var ratings = spark.read.option("header", true).csv("C:/rating.csv")
}

here is error file i get
[info] Compiling 1 Scala source to C:\Users\haftab\Downloads\sbt-0.13.16\sbt\alfutaim\target\scala-2.11\classes...
[error] C:\Users\haftab\Downloads\sbt-0.13.16\sbt\alfutaim\src\main\scala\example\Hello.scala:12: not found: value spark
[error]   var anime = spark.read.option("header", true).csv("C:/anime.csv")
[error]               ^
[error] C:\Users\haftab\Downloads\sbt-0.13.16\sbt\alfutaim\src\main\scala\example\Hello.scala:13: not found: value spark
[error]   var ratings = spark.read.option("header", true).csv("C:/rating.csv")
[error]                 ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 3 s, completed Sep 10, 2017 1:44:47 PM

Answer: spark is initialized in spark-shell only
but for the code you need to initialize the spark variable by yourself
import org.apache.spark.sql.SparkSession
val spark = SparkSession.builder().appName("testings").master("local").getOrCreate

you can change the testings name to your desired name .master option is optional if you want to run the code using spark-submit
Comment: thanks i am done here is new code file

`val spark = SparkSession.builder().master("local").appName("TV Series Analysis").config("spark.sql.warehouse.dir","file:///c:/tmp/spark-warehouse").getOrCreate()
  import spark.implicits._
  var anime = spark.read.option("header", true).csv("C:/anime.csv")
  var ratings = spark.read.option("header", true).csv("C:/rating.csv")
  anime.select("*").orderBy($"rating".desc).limit(10).show()`
Comment: Thanks Ramesh,
One more thing to add here is that SparkSession only works with spark 2.0.0
so my build.sbt is updated now

`import Dependencies._

name := "hello"

version := "1.0"

scalaVersion := "2.11.8"

val sparkVersion = "2.0.0"

libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % sparkVersion,
  "org.apache.spark" %% "spark-streaming" % sparkVersion,
  "org.apache.spark" %% "spark-sql" % sparkVersion
)

libraryDependencies += scalaTest % Test`

[https://stackoverflow.com/questions/37337461/what-is-version-library-spark-supported-sparksession]
Comment: yes thats right . for older versions you need to create sqlContext
Comment: Ramesh there is one more thing i am stuck in right now i have tried using online help,
**value $ is not a member of StringContext**
Comment: `import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.{SparkSession, SQLContext}
import org.apache.spark.sql._

object Hello  {
  val spark = SparkSession.builder().master("local").appName("TV Series Analysis").getOrCreate()
  import spark.sqlContext.implicits._
  import spark.implicits._
  var anime = spark.read.option("header", true).csv("C:/anime.csv")
  anime.select("*").orderBy($"rating".desc).limit(10).show()
}`
Comment: glad to hear that you sorted out the problem you had. yes $ would be identified when you use import sqlContext.implicits._. If the answer helped then please accept and upvote :)

Not found value spark SBT project


Javascript generated ul looks different in internet explorer

Question: jsFiddle
I'm trying to create a playlist widget using javascript and html, it looks great on chrome, but when I test it in IE it looks static and only one row is displayed. I'm trying to figure this out, but I can't.
Please help me out.
p.s. this is the first time I do html and javascript :)
http://animaonline.com/api/yt/pls/v1/
Comment: Trick for beginners: when you ask a question, please post the relevant parts of your code. You could even set up a jsFiddle that reproduces the problem.
Comment: My guess is that the issue is CSS, not JavaScript. Seeing code would help get a better answer though.
Comment: @bažmegakapa good idea, will do
Comment: This is more of a CSS question. Edited.
Answer: You forgot to close your  tags using . I added the  right after the  tag which results in:
http://jsfiddle.net/HJsUM/3/
If you want to enclose the whole thing (the image, title and description) inside the  tag then you should change your  tag to a block element since you cannot enclose block elements (div and ul) inside inline elements. So the updated fiddle looks like this:
http://jsfiddle.net/HJsUM/4/
Answer: First of all - it's not the HTML that is not being displayed incorrectly, but your JavaScript seems to have some flaws here and there.
I think you're making things a bit more complex than necessary. There's a lot of challenges that others have already solved for you. It might be good to take a look at knockoutjs, which allows you to populate templates with an elegant JavaScript solution. Take a look at it, you might like it http://knockoutjs.com/
Good luck!
Comment: "I think you're making things a bit more complex than necessary." Any examples? I'm pretty sure something is wrong with the css.. I believe that using knockoutjs would be an overkill, I don't need mvvm for a rather so simple widget...
Comment: Well - in general, unless there's really no other way, you should try to avoid mixing up presentation elements (such as HTML code) and logics (your JavaScript). It might be interesting to read some articles about design patterns (or the MVC - Model View Controller - pattern in general) and the concepts of N-Tier application development.


Highlight div based on highest number

Question: I am trying to highlight a value in a div that is the lowest value in an array.
When the user completes each input row (design outlet and actual outlet) the outletIndex() function is called which calculates the percentage and pushes the value into an array. This part I have working.
Then what I am trying to do is find the lowest number in that array and highlight the div (proportion). I want this to be dynamic so as the user completes each input row the percentage is calculated and the lowest index value is highlighted progressively.
I am using querySelectorAll() to match the div class value to the lowest index value but Im not sure if I need to parse a nodeList to match the value of the indexArray?
I am also wondering that if a mistake is typed into the fields and added to the Index array and it happens to be the lowest index it wont match the index values that are shown on the HTML and not call the function.

  document.querySelector('#outlet_actual_1').addEventListener('keypress', function(e) {

    if (e.key === 'Enter') {
        let actual = document.getElementById("outlet_actual_1").valueAsNumber || 0;
        let design = document.getElementById("outlet_design_1").valueAsNumber || 0;
        let result1 = outletIndex(actual, design);
         if (!isNaN(result1)) {
            document.getElementById("percentage").textContent = `${result1.toFixed(1)}%`;
        }
    }
});
document.querySelector('#outlet_actual_2').addEventListener('keypress', function(e) {

    if (e.key === 'Enter') {
        let actual = document.getElementById("outlet_actual_2").valueAsNumber || 0;
        let design = document.getElementById("outlet_design_2").valueAsNumber || 0;
        let result1 = outletIndex(actual, design);
         if (!isNaN(result1)) {
            document.getElementById("percentage2").textContent = `${result1.toFixed(1)}%`;
        }
    }
});
document.querySelector('#outlet_actual_3').addEventListener('keypress', function(e) {

    if (e.key === 'Enter') {
        let actual = document.getElementById("outlet_actual_3").valueAsNumber || 0;
        let design = document.getElementById("outlet_design_3").valueAsNumber || 0;
        let result1 = outletIndex(actual, design);
         if (!isNaN(result1)) {
            document.getElementById("percentage3").textContent = `${result1.toFixed(1)}%`;
        }
    }
});
const indexArray = [];
function outletIndex(a, b) {
    let result = a / b * 100;
   if (!isNaN(result)) {
        indexArray.push(+result.toFixed(2));
        indexFindandHighlight();
    }
    console.log(indexArray, result);
    return result;
}

function indexFindandHighlight(){
    const lowestIndex = Math.min(...indexArray);
    const indexCheck = document.querySelectorAll('.proportion').valueAsNumber || 0;
    console.log(lowestIndex);
    if (lowestIndex == indexCheck) {
        $(document).ready(function() {

            $("#outlet_actual_1", "#outlet_actual_2", "#outlet_actual_3").onchange(function(){
               $(".proportion").effect( "highlight", {color:"#669966"}, 3000 );
            });
         });
    }
};

          Outlet Design
            <input
              name = "outlet 1 design"
              class="form-control design_1"
              id="outlet_design_1"
              type="number"
              placeholder="Outlet 1 Design"
              onkeydown="outletIndex();"
            />

          Outlet Actual
            <input
            name="outlet 1 actual"
              class="form-control actual_1"
              id="outlet_actual_1"
              type="number"
              placeholder="Outlet 1 Actual"
              onkeydown="outletIndex();"
            />

          Outlet Balance
            <input
              name="outlet_balance"
              class="form-control"
              input value=""
              id="outlet_balance_1"
              type="text"
              placeholder="Outlet 1 Balance"
            />

          <input
            name="outlet_design"
            class="form-control design_2"
            id="outlet_design_2"
            type="number"
            placeholder="Outlet 2 Design"
            onkeydown="outletIndex();"
          />

          <input
            name="outlet_actual"
            class="form-control actual_2"
            id="outlet_actual_2"
            type="number"
            placeholder="Outlet 2 Actual"
            onkeydown="outletIndex();"
          />

          <input
            name="outlet_balance"
            class="form-control"
            id="outlet_balance_2"
            type="number"
            placeholder="Outlet 2 Balance"
          />

                <input
                  name="outlet_design"
                  class="form-control design_3"
                  id="outlet_design_3"
                  type="number"
                  placeholder="Outlet 3 Design"
                  onkeydown="outletIndex();"
                />

                <input
                  name="outlet_actual"
                  class="form-control actual_3"
                  id="outlet_actual_3"
                  type="number"
                  placeholder="Outlet 3 Actual"
                  onkeydown="outletIndex();"
                />

                <input
                  name="outlet_balance"
                  class="form-control"
                  id="outlet_balance_3"
                  type="number"
                  placeholder="Outlet 3 Balance"
                />

  </fieldset>
</form>

Answer: Multiple issues:

Clean up HTML
Reuse code
Consistency, Either use jquery or javascript
Separate UI changes from data change.
Consistency, Bind event listener either using HTML or javascript file.

Please check below sample

const number = (str) => Number(str) || 0;

const bind = (id) => {
  function onBind(e) {
    let actual = number(document.querySelector(`#outlet_actual_${id}`).value);
    let design = number(document.querySelector(`#outlet_design_${id}`).value);
    let result1 = percentage(actual, design);
    if (!isNaN(result1)) {
      document.querySelector(
        `#percentage_${id}`
      ).textContent = `${result1.toFixed(1)}%`;
    }
    updateIndex(id, result1);
    highlight();
  }
  document
    .querySelector(`#outlet_actual_${id}`)
    .addEventListener("change", onBind);
  document
    .querySelector(`#outlet_design_${id}`)
    .addEventListener("change", onBind);
};

function percentage(a, b) {
  return b === 0 ? 0 : (a / b) * 100;
}
let percentages = [];
function updateIndex(id, value) {
  percentages[id - 1] = value;
}

function highlight() {
  let minIndex = -1;
  let minValue = Number.MAX_SAFE_INTEGER;
  for (let index in percentages) {
    if (percentages[index] < minValue) {
      minIndex = Number(index);
      minValue = percentages[index];
    }
  }
  if (minIndex === -1) return;

  document.querySelector(`#percentage_${minIndex + 1}`).style.color = "red";
  setTimeout(() => {
    document.querySelector(`#percentage_${minIndex + 1}`).style.color = "black";
  }, 3000);
}
bind(1);
bind(2);
bind(3);

          Outlet Design
            <input
              name = "outlet 1 design"
              class="form-control design_1"
              id="outlet_design_1"
              type="number"
              placeholder="Outlet 1 Design"

            />

          Outlet Actual
            <input
            name="outlet 1 actual"
              class="form-control actual_1"
              id="outlet_actual_1"
              type="number"
              placeholder="Outlet 1 Actual"

            />

          Outlet Balance
            <input
              name="outlet_balance"
              class="form-control"
              input value=""
              id="outlet_balance_1"
              type="text"
              placeholder="Outlet 1 Balance"
            />

          <input
            name="outlet_design"
            class="form-control design_2"
            id="outlet_design_2"
            type="number"
            placeholder="Outlet 2 Design"
          />

          <input
            name="outlet_actual"
            class="form-control actual_2"
            id="outlet_actual_2"
            type="number"
            placeholder="Outlet 2 Actual"

          />

          <input
            name="outlet_balance"
            class="form-control"
            id="outlet_balance_2"
            type="number"
            placeholder="Outlet 2 Balance"
          />

                <input
                  name="outlet_design"
                  class="form-control design_3"
                  id="outlet_design_3"
                  type="number"
                  placeholder="Outlet 3 Design"

                />

                <input
                  name="outlet_actual"
                  class="form-control actual_3"
                  id="outlet_actual_3"
                  type="number"
                  placeholder="Outlet 3 Actual"

                />

                <input
                  name="outlet_balance"
                  class="form-control"
                  id="outlet_balance_3"
                  type="number"
                  placeholder="Outlet 3 Balance"
                />

  </fieldset>
</form>

Comment: I agree I was hoping to only use vanilla JS as I am still learning how to write JS and HTML. Thanks for your help. The code is working as I wanted, I didnt expect anyone to write it for me...I also didnt think I was that far off lol but obviously I was.

Highlight div based on highest number


Convert and filter array of objects to one object

Question: I have next array of objects:
const fields = [
    { givenName: 'firstName' },
    { familyName: 'lastName' },
    { 'custom:data': 'blabla' },
    { 'custom:data2': '' },
    { 'custom:data3': null },
  ];

What I need is to filter out elements which is empty, null or undefined and convert it to one object pameters:
{
   givenName: 'firstName',
   familyName: 'lastName',
   'custom:data': 'blabla'
}

Comment: You have explicitly said to exclude `null`, `undefined` and `''` (empty) meaning you want to keep everything else. Most answers including the one you have accepted don't do that. Most rely on truthy/falsy checks excluding values such as `0`, `false` and `NaN`. (FYI)
Comment: nope, only one.
Comment: May the objects have more than one keys?
Answer: You could filter the array by looking to the values. This approach assumes, that only one key/value pair is available.

const
    fields = [{ givenName: 'firstName' }, { familyName: 'lastName' }, { 'custom:data': 'blabla' }, { 'custom:data2': '' }, { 'custom:data3': null }],
    result = Object.assign({}, ...fields.filter(o => {
        const [v] = Object.values(o);
        return v || v === 0 || v === false;
    }));

console.log(result);
Answer: How to check whether a value is empty?
Most people would go about this with a truthy check:
const empty = x => x ? false : true;

empty(null);      //=> true
empty(undefined); //=> true
empty('');        //=> true

But that's always going to exclude things you perhaps didn't intend to exclude:
empty(0);         //=> true
empty(false);     //=> true
empty(NaN);       //=> true

Admittedly NaN could be seen as the "empty" value of its type but for the sake of your question and educational purpose we'll say it's not.
The "workaround" is often something like that:
const empty = x => (x || x === 0 || x === false || Number.isNaN(x)) ? false : true;

However this doesn't need to be more complicated than this:
const empty = x => x == null || x === '' ? true : false;

Checking for either undefined or null is one example where not using triple equality makes sense:
null == undefined;
// true
null === undefined;
// false

See Google JavaScript Style Guide.
If you need to exclude null, undefined and '' please don't rely on clever shorthand tricks and just be explicit about it. Type checking should be a straightforward job (YMMV) and not a show-off contest. Future you and your team mates will thank you.
As for your question, I'd suggest this:
Merge everything you've got with Object.assign:
Object.assign({}, {a:1}, {b:2}, {c:2});
// {a: 1, b: 2, c: 3}

Then deconstruct it into pairs, exclude those whose value is empty, then reconstruct the object from what's left:

const merge = xs =>
  Object.fromEntries(
    Object.entries(
      Object.assign({}, ...xs))
        .filter(([_, v]) =>
          v != null && v !== ''));

console.log(merge(fields));

Answer: You could use reduce and forEach and check if value of each property is falsy or not.

const fields = [
  { givenName: 'firstName' },
  { familyName: 'lastName' },
  { 'custom:data': 'blabla' },
  { 'custom:data2': '' },
  { 'custom:data3': null },
];

const result = fields.reduce((r, e) => {
  Object.entries(e).forEach(([k, v]) => {
    if (v || [false, 0].includes(v)) r[k] = v
  })

  return r
}, {})

console.log(result)
Answer:

const fields = [
    { givenName: 'firstName' },
    { familyName: 'lastName' },
    { 'custom:data': 'blabla' },
    { 'custom:data2': '' },
    { 'custom:data3': null },
  ];

 res = fields.reduce((acc, cur) => {
    if (cur[Object.keys(cur)[0]]) {
       acc = { ...acc, ...cur }
    }
    return acc
 }, {})

 console.log(res)
Comment: This excludes valid values such as `0` and `false`
Answer:

const fields = [
  { givenName: 'firstName' },
  { familyName: 'lastName' },
  { 'custom:data': 'blabla' },
  { 'custom:data2': '' },
  { 'custom:data3': null },
];

const result = fields.reduce( ( acc, field ) => {
  Object.keys( field ).forEach( ( key ) => {
    if( field[key] ) {
      acc[key] = field[key];
    }
  } )
  return acc;
}, {} )

console.log(result)
Answer: Use Array.prototype.filter() method to filter out the empty, null or undefined objects. Then using Array.prototype.map() make a key-value pair array. At last, use Object.fromEntries() method to transform it to a single object.

const fields = [
  { givenName: 'firstName' },
  { familyName: 'lastName' },
  { 'custom:data': 'blabla' },
  { 'custom:data2': '' },
  { 'custom:data3': null },
];
const ret = Object.fromEntries(
  fields
    .filter((x) => {
      const value = Object.values(x)[0];
      return value || value === false || value === 0 || Object.is(value, NaN);
    })
    .map((x) => [Object.keys(x)[0], Object.values(x)[0]])
);
console.log(ret);
Comment: @customcommander that's actually a good point he explicitly wrote that. I think nobody took this into concideration.
Comment: @customcommander Btw, almost all other posted answers suffer from this problem...
Comment: Today I learned something: I knew `NaN === NaN` is false but I didn't know that `Object.is(NaN, NaN)` is true. Thanks.
Answer: It might help you.

const fields = [
    { givenName: 'firstName' },
    { familyName: 'lastName' },
    { 'custom:data': 'blabla' },
    { 'custom:data2': '' },
    { 'custom:data3': null },
  ];

 let item = {};

for ( let i = 0; i < fields.length; i++ ){
  for (const [key, value] of Object.entries(fields[i])) {
   if ( value !== null && value !== '' )
      item [key] = value;
  }

}

 console.log(item);
Comment: You will let `undefined` through
Comment: when you run the code in snippet it seems working.
Answer: Works with one key, simple modification can work on n keys.

const fields = [
    { givenName: "firstName" },
    { familyName: "lastName" },
    { "custom:data": "blabla" },
    { "custom:data2": "" },
    { "custom:data3": null },
];

const reduced = fields
    .filter((f) => {
        const key = Object.keys(f)[0];
        return f[key] === "" || f[key] === null || f[key] === undefined
            ? false
            : true;
    })
    .reduce((acc, curr) => {
        const key = Object.keys(curr)[0];
        acc[key] = curr[key];
        return acc;
    }, {});

console.log(reduced);


Point projection on curve

Question: Point projection on Bézier curves can be easily accomplished using Newton Iteration to try to minimize the dot product between the vector connecting the point P and its projection on curve C and the curve tangent vector C' at the same parameter. When the dot product is zero we have a perpendicular projection.
Am I right saying that this approach cannot work for points on the curve itself? And that if the point P is on the curve I need to try to minimize the distance between the two points P and C?
Is there any other approach that works in all cases, both for points on the curve and away from the curve without involving a distance check? In my case it's impossible to estimate a tolerance for the distance a priori.
Thanks.
Answer: I think your approach should work fine also for points on the curve. Since the zero comes not from the tangent vector but from the difference between the point and the projection, you're effectively just finding the zero of that difference, which should get you to the point. Why do you expect problems?
Comment: I am following the approach of page 231 of this book: http://books.google.it/books?hl=it&id=7dqY5dyAwWkC&q=point+projection#v=snippet&q=point%20projection&f=false. By the way, even my code fails for example if the curve is straight. Do you find it reasonable?
Comment: @devdept: You said you were looking for the dot product to be zero; the book uses the cosine -- that's more likely to cause problems when the point is on the curve. I'd use the dot product; if you want to use the cosine, you probably can't use Newton's method, since the cosine is a step function when the point is on the curve, or a steep sigmoid when it's close, and you can't find the zeros of those using Newton's method. Regarding your code failing for straight curves -- is this with the point on or off the line?
Comment: @ joriki: it's on the curve but what you say makes sense. Do you know an article explaining how to do point projection using dot product? I'm still wandering why this famous book explains this approach. As mentioned above, what I am really looking for is to get rid of point distance checks.
Comment: @devdept: I don't know an article, but I'd just do it the way you proposed in the question, finding the zeros of the dot product using Newton's method. Have you tried that? (BTW, the author of a post automatically gets notified of comments; no need to ping in that case.)
Comment: @joriky: I will try, thanks. (BTW I don't even know what ping means...)
Comment: @devdept: It refers to the at-username that you're putting in front of your comments. For instance, in this case I got notified even though you misspelled my name :-)

Point projection on curve


In what function space, does the calculus of variations hold?

Question: I only know that the calculus of variations holds for continuous function spaces. But does it work for non-smooth functions, like the functions with jumps or with some points having the property similar to the Dirac-delta function? Thanks a lot!
Comment: There is no simple answer: note that an (absolutely) continuous function space is *not* enough in general, due to the Lavrentiev phenomenon; see eg https://www.uam.es/becarios/aferrier/Tesi.pdf
Comment: @user7530 Thanks very much for your comments, and I have downloaded this thesis, and learned a lot through reading it :-)
Answer: Something works everywhere. Not everything works everywhere.
The basic idea of comparing the functional values $F(u)$ and $F(u+\varphi)$, where $\varphi $ is a smooth compactly supported functions, makes sense whenever $u+\varphi$ makes sense. That is, in any linear space of functions that contains $C_c^\infty$. Those can be spaces of rough functions, like $L^p$, or even spaces of distributions.
However, the larger the space becomes, the smaller is the supply of functionals $F$ that can be defined on that space. Usually one has a particular functional $F$ in mind, and looks for a reasonably nice space on which $F$ can be naturally defined. Having the largest possible set of functions is not the goal here.
Interesting functionals tend to involve derivatives of $u$, so the convenient function spaces to use are those where we have a notion of derivative that can be integrated, Sobolev spaces.
If we don't have derivatives, a larger space such as a Lebesgue spaces can work. A random example: maximize
$$
F(u)=\int_{-1}^1 \operatorname{sign}(x) u(x) - \int _{-1}^1 u(x)^2\,dx
$$
where $u\in L^2([-1,1])$.  Assuming $u$ is extremal, consider
$$
F(u+t\varphi) - F(u) = t\int_{-1}^1 (\operatorname{sign}(x) -2u(x))\varphi(x)  - t^2\int_{-1}^1 \varphi(x)^2\,dx
$$
(Here any $\varphi\in L^2([-1,1])$ will do.) Since this quadratic polynomial is maximized at $t=0$, the coefficient of $t$ must be zero. And since
$$\int_0^1 (\operatorname{sign}(x) -2u(x))\varphi(x)=0$$
for every $\varphi\in L^2$, it follows that $u(x) = \frac12 \operatorname{sign}(x)$ almost everywhere.
In the above example, variation is used in a space with a lot of discontinuous functions, and even the extremal function itself is discontinuous.
Comment: Thanks very much for your great answer! [+1] Actually, Lebesgue spaces are large enough, since we are considering some optimizations on probability density functions which are in ${\mathcal L}^1$ space. Thanks again!


Obter a ultima "palavra" de um PATH com diferentes formatos de URL

Question: Estou criando uma função que obtenha a ultima "palavra" de uma url solicitada em php, sem considerar parâmetros e considerando a raiz como index.
Exemplos:

URL http://www.teste.com.br/ EXPECTATIVA index
URL www.teste.com.br/ EXPECTATIVA index
URL teste.com EXPECTATIVA index
URL teste.com/ EXPECTATIVA index
URL www.teste.com.br/teste EXPECTATIVA teste
URL http://www.teste.com.br/teste EXPECTATIVA teste
URL http://teste.com/teste EXPECTATIVA teste
URL https://www.teste.com/teste EXPECTATIVA teste
URL https://teste.com/teste EXPECTATIVA teste
URL teste.com/teste/dois EXPECTATIVA dois
URL teste.com/teste/dois/ EXPECTATIVA dois
URL teste.com/teste/dois/?variavel=teste EXPECTATIVA dois
URL teste.com/teste/dois?variavel=teste EXPECTATIVA dois
URL teste.com/teste/dois/?variavel=teste EXPECTATIVA dois
URL teste.com/teste?var1=t&var2=t EXPECTATIVA teste
URL teste.com/teste/tres#ola EXPECTATIVA tres
URL teste.com/teste?var1=t&var2=t#ola EXPECTATIVA teste

Usando a função basename e trabalhando com substr e preg_match obtenho uma certa taxa de sucesso:
$arr = array(
  array("name"=>"http://www.teste.com.br/","possibleValues"=>array("index")),
  array("name"=>"www.teste.com.br/","possibleValues"=>array("index")),
  array("name"=>"teste.com","possibleValues"=>array("index")),
  array("name"=>"teste.com/","possibleValues"=>array("index")),
  array("name"=>"www.teste.com.br/teste","possibleValues"=>array("teste")),
  array("name"=>"http://www.teste.com.br/teste","possibleValues"=>array("teste")),
  array("name"=>"http://teste.com/teste","possibleValues"=>array("teste")),
  array("name"=>"https://www.teste.com/teste","possibleValues"=>array("teste")),
  array("name"=>"https://teste.com/teste","possibleValues"=>array("teste")),
  array("name"=>"teste.com/teste/dois","possibleValues"=>array("dois")),
  array("name"=>"teste.com/teste/dois/","possibleValues"=>array("dois")),
  array("name"=>"teste.com/teste/dois/?variavel=teste","possibleValues"=>array("dois")),
  array("name"=>"teste.com/teste/dois?variavel=teste","possibleValues"=>array("dois")),
  array("name"=>"teste.com/teste/dois/?variavel=teste","possibleValues"=>array("dois")),
  array("name"=>"teste.com/teste?var1=t&var2=t","possibleValues"=>array("teste")),
  array("name"=>"teste.com/teste/tres#ola","possibleValues"=>array("tres")),
  array("name"=>"teste.com/teste?var1=t&var2=t#ola","possibleValues"=>array("teste"))
);

foreach($arr as $value){
  echo "URL ".$value["name"]."\n";
  echo ( array_search( basename( returnLastWord( $value["name"] ) ), $value["possibleValues"] ) === false ? "FALHOU" : "PASSOU" )." -> expected: ".json_encode( $value["possibleValues"] )." get '".basename( returnLastWord( $value["name"] ) )."'\n\n";
}

function returnLastWord($var){
  preg_match('/[?#]/', $var, $matches, PREG_OFFSET_CAPTURE);
  $after = ( empty( $matches[0][1] ) ? NULL : $matches[0][1] );
  if($after){
    return substr($var, 0, $after);
  }else{
    // echo "aqui\n";
    return $var;
  }
}

https://repl.it/JJYI/7

URL http://www.teste.com.br/
FALHOU -> expected: ["index"] get 'www.teste.com.br'

URL www.teste.com.br/
FALHOU -> expected: ["index"] get 'www.teste.com.br'

URL teste.com
FALHOU -> expected: ["index"] get 'teste.com'

URL teste.com/
FALHOU -> expected: ["index"] get 'teste.com'

URL www.teste.com.br/teste
PASSOU -> expected: ["teste"] get 'teste'

URL http://www.teste.com.br/teste
PASSOU -> expected: ["teste"] get 'teste'

URL http://teste.com/teste
PASSOU -> expected: ["teste"] get 'teste'

URL https://www.teste.com/teste
PASSOU -> expected: ["teste"] get 'teste'

URL https://teste.com/teste
PASSOU -> expected: ["teste"] get 'teste'

URL teste.com/teste/dois
PASSOU -> expected: ["dois"] get 'dois'

URL teste.com/teste/dois/
PASSOU -> expected: ["dois"] get 'dois'

URL teste.com/teste/dois/?variavel=teste
PASSOU -> expected: ["dois"] get 'dois'

URL teste.com/teste/dois?variavel=teste
PASSOU -> expected: ["dois"] get 'dois'

URL teste.com/teste/dois/?variavel=teste
PASSOU -> expected: ["dois"] get 'dois'

URL teste.com/teste?var1=t&var2=t
PASSOU -> expected: ["teste"] get 'teste'

URL teste.com/teste/tres#ola
PASSOU -> expected: ["tres"] get 'tres'

URL teste.com/teste?var1=t&var2=t#ola
PASSOU -> expected: ["teste"] get 'teste'

Estou com problema principalmente nos 4 primeiros exemplos, onde teoricamente seria a raiz do projeto, ou seja, deveria obter o index
Answer: Não encontrei maneira simples de fazer, ainda mais com as variações dos tipos de urls de entrada, o que consegui foi isto:
function returnLastWord($var){

    //Remove o protocolo
    $var = preg_replace('~^[^:]+[:][/]{2,}~', '', $var);

    /*
    Pega qualquer coisa que seja um PATH em URLs
    pega o que esta entre o parenteses neste exemplo:
    `site.com/(foo/bar/baz)?querystring=ignorada#hashignorada`
    */
    if (preg_match('~/([^#?]{1,})~', $var, $matches)) {

        //Remove o / do final em urls como `foo/bar/`, para evitar pegar em branco
        $result = rtrim($matches[1], '/');

        //Pega qualquer coisa que estiver no final
        if (preg_match('~[^/]+$~', $result, $matches)) {
          return $matches[0];
        }
    }

    //Se qualquer coisa anterior falhou é porque provavelmente é "index"
    return 'index';
}

Exemplo no Ideone

É provável que eu revise isto para deixar mais performático ou simples.
Answer: O PHP já possui uma função nativa para trabalhar com URL, porém como nem todas estão no formato definido na RFC-3986, a função acaba analisando de forma errada as que não estão padronizadas. Nada crítico. O que acontece é que a função considera o que deveria ser host como parte do path, então uma verificação se há o caractere . no path é necessária, pois, se houver, o elemento em questão é o host, não o path, retornando, assim, index.
function returnLastWord ($url) {

    // Analisa a URL:
    $url = parse_url($url);

    // Divide o path nas ocorrências de /:
    $parts = explode('/', trim($url["path"], '/'));

    // Busca o último elemento:
    $last = end($parts);

    // Se não estiver vazio e não possuir o caractere ., retorna o valor, senão retorna index:
    return $last && false === strpos($last, '.') ? $last : "index";

}

Veja funcionando no Ideone.
Answer: Usando a função parse_url() pode facilitar o trabalho.
<?php

$arr = array(
    'http://localhost/',
    'https://localhost/',
    'http://localhost',
    'https://localhost',
    'http://sub.localhost/',
    'http://sub.localhost',
    'http://localhost/foo',
    'http://localhost/foo/bar',
    'http://localhost/?p=1&b=1',
    'localhost/foo',
    'localhost'
);

echo '

URL	Word
';
foreach ($arr as $v) {
    $url_original = null;
    // Normalizing the given URL
    if (preg_match('#^https?://#i', $v) !== 1) {
        $url_original = $v;
        $v = 'http://'.$v;
    }

    $url = parse_url($v);
    echo PHP_EOL.'
'.$v.(!empty($url_original)? '
('.$url_original.')': '').'	';
    if (
        isset($url['path'])
        && !empty($url['path'])
        && $url['path'] != '/'
    ) {
        // Path found
        $p = strrpos($url['path'], '/');
        if ($p !== false) {
            echo substr($url['path'], $p+1);
        }
    } else {
        // Empty, no path
        echo 'index';
    }
    echo '
';
}
echo '
';

Normalização
Para que funcione mesmo com URLs sem o schema (http ou https), é feito uma normalização onde o "http://" é prefixado a string antes de passar para a função parse_url().

Caracteres multibyte
O trecho que invoca a função substr() pode apresentar falha quando a URL possuir caracteres multibyte. Caso queira prover suporte a caracteres multibyte, consulte as funções mbstring.


How to detect if an android app is a game?

Question: In an app I am developing I need to iterate through the installed apps and detect which ones are games. Is there any way to do this?
I was thinking to a Play Store API that can search for package name and returns its category even if it's only limited to apps on the store. Does something similar exist? Would it be possible?
Is there any alternative way to do it?
Answer: This answer is deprecated!
Correct and backwards compatible way to do this is here!
Since Android API version 21, there's finally a way to check if an application is a game.
<code>PackageManager pm = mContext.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(mPackageName,0);
if((ai.flags & ApplicationInfo.FLAG_IS_GAME) == ApplicationInfo.FLAG_IS_GAME)
    return true;
return false;
</code>
Comment: There is no requirement for a game to have this flag set.
Comment: But there are several good reasons for a developer to put this flag into his/her application manifest, one of them is proper indexing in play store and android tv launcher.
Answer: There is no automatical way to detect if an app is a game. You just could compaire the package name of the common part of the package name. My solution was to index the google store pages and hash the package names.
I could optimize my hashes by building common prefixes. I handled the package name as a domain and grep the public suffix. I use the list from http://publicsuffix.org/.

A "public suffix" is one under which Internet users can directly register names. Some examples of public suffixes are .com, .co.uk and pvt.k12.ma.us. The Public Suffix List is a list of all known public suffixes.
The Public Suffix List is an initiative of Mozilla, but is maintained as a community resource. It is available for use in any software, but was originally created to meet the needs of browser manufacturers.

With this list you can detect part of a packagename is a common prefix.
Comment: Ahgs you are right I missed that. However there is no official API to get the data. But you could parse the html.
Comment: it's not mine, if it works I'd be interested in some more directions.
Comment: But doing so I am supposed to prior have a list of the most important game companies. wouldn't it be better to get category info from the play store page of the application?
Comment: I tried that too but I found no way to get the categorie from an app. I see no way to detect that the package [com.rovio.angrybirds](https://play.google.com/store/apps/details?id=com.rovio.angrybirds) is a game. Do you see that?
Comment: Ok, so I'll do it with the dirty way.. :-)
Answer: For me the above answer didn't work, the ApplicationInfo.FLAG_IS_GAME is now deprecated, with API 28+ (in my case), you can do something like this:
<code>_pm = _context.PackageManager;
List<string> packageList = new List<string>();
Intent intent = new Intent(Intent.ActionMain);
intent.AddCategory(Intent.CategoryLeanbackLauncher); // or add any category you want
var list = _pm.QueryIntentActivities(intent, PackageInfoFlags.MetaData);
foreach (var app in list)
{
    ApplicationInfo ai = _pm.GetApplicationInfo(app.ActivityInfo.PackageName, 0);
    var allFlags = ai.Flags;

    if (allFlags.HasFlag(ApplicationInfoFlags.IsGame))
    {
        packageList.Add(app.ActivityInfo.PackageName);
    }
}
</code>


How to get changes in database done by someone else?

Question: We are working on features in team. My team-mate have done some changes & synced the cloud database.
I also have some database changes locally, now I want those changes in database which he synced.
I can't use drush sql-sync becauses it asks to drop all the tables. So my local changes will gone.
what should I do?
Answer: Unlike code Database changes cannot be synced easily.
Try using Features Module This module exports your database settings to code so that they can be added to Version Control took like Git.
Tip : Create one feature for every independent functionality, so that it will be easy to manage. There are other supporting module for Features, which can reduce your efforts.


weaker sufficient condition (than proper and free) for the quotient of a Lie group action to be a manifold?

Question: $G$ a (finite dimensional) Lie group. $M$ smooth manifold. $A:M\times G\to M$ a Lie group action (left or right). $M/G$ the quotient space with quotient topology.
A sufficient condition for $M/G$ to possess a unique smooth manifold structure such that $\pi:M\to M/G$ is a smooth surjective submersion is when the following holds true for the action:

smooth
free
proper
effective (faithful)

How can these conditions be weakened?
Effectiveness is not needed. Freeness is not needed, but the stabilizer groups should be all isomorphic, so that there is just one orbit type and the stratification by orbit types gives just one stratum. But properness and smoothness are hard to relax, right? Smoothness is of course necessary, but properness doesn't seem to be.
Consider the action of $\mathbb R$ on $S^1$ by $(\theta,t)\mapsto (t+\theta) \,\text{mod} \,2\pi$. This is effective, smooth, transitive, not free and not proper, but the quotient is just a point, so a perfectly valid manifold. The stabiliser/isotropy group of the orbit is $\mathbb Z$, that's why it's not free and since $\mathbb Z$ is non-compact the transitive action is not proper. So is there a weaker condition than properness? Or is my argument not valid?
Answer: I can provide provide only a partial answer:
Let $A$ be a (right) Lie group action which is smooth and free, and there is some topology and smooth structure on $M / G$ making the quotient  $\pi: M \rightarrow M / G$ into a smooth surjective submersion, then $A$ is proper.
To see this, one has to know two things:
1) Let $P$ and $N$ be any smooth manifolds. Let $\pi: P \rightarrow N$ be a smooth surjective submersion. Assume that there is a smooth (right) action $A: P \times G \rightarrow P$ which is free, and its orbits are precisely the fibers of $\pi$.
Then there is a unique (up to an equivalence) local trivialization making $\pi: P \rightarrow M$ into a principal $G$-bundle.
Hint of the proof: Find local sections of $\pi$ and use them to define local trivialization maps.
2) Let $\pi: P \rightarrow N$ be a principal $G$-bundle with a right action $A: P \times G \rightarrow P$. Then $A$ is proper.
Hint of the proof: Group action $A$ locally looks as a right multiplication in the second component of the Cartesian product $U \times G$. This action is easily seen to be proper.
As the quotient map $\pi: M \rightarrow M/G$ satisfies the assumptions of 1), we can then use 2) to show that the action $A$ has to be proper.