How to put a Slider widget into the Better Exposed Filters for Ubercart price and other product features?

Question: How to put a Slider widget into the Better exposed filters and product search result view in Drupal 7 for Ubercart price and other product features?

I have developed an ecommerce shop, and need to a add slider widget to search products.
Answer: Jquery UI and most of its widgets are embedded with D7 core, in misc/ui folder. First add these lines to your theme template file (sites/all/themes/YOUR_THEME/template.php) :
/**
 * Implements template_preprocess_views_view().
 */
function YOUR_THEME_preprocess_views_view(&$vars) {//replace YOUR_THEME by your theme name
  $view = $vars['view'];
  switch($view->name) {
    case 'YOUR_VIEW' : //replace YOUR_VIEW by your view name

      drupal_add_library('system','ui.slider');
      drupal_add_css('/misc/ui/jquery.ui.slider.css');
      break;
  }
}

Don't forget to clear cache.
Your filter must be compatible with is between, lower, greater operators. This is fine for  numeric fields. In the filter setup, set it as exposed and choose "is between" operator.
Last, in views advanced settings, in the BEF setup screen, select Jquery ui slider for your filter and adjust other settings like range at convenience.
EDIT
To answer your comments, don't edit Bartik or other core files.
You can easily create a subtheme as following :

Create the folder: /sites/all/themes/vassia
Create the file /sites/all/themes/vassia/vassia.info
put this code in vassia.info
name = vassia
description = my first subtheme
core = 7.x
base theme = bartik

stylesheets[all][] = css/vassia.css
stylesheets[all][] = css/colors.css

regions[header] = Header
regions[help] = Help
regions[page_top] = Page top
regions[page_bottom] = Page bottom
regions[highlighted] = Highlighted

regions[featured] = Featured
regions[content] = Content
regions[sidebar_first] = Sidebar first
regions[sidebar_second] = Sidebar second

regions[triptych_first] = Triptych first
regions[triptych_middle] = Triptych middle
regions[triptych_last] = Triptych last

regions[footer_firstcolumn] = Footer first column
regions[footer_secondcolumn] = Footer second column
regions[footer_thirdcolumn] = Footer third column
regions[footer_fourthcolumn] = Footer fourth column
regions[footer] = Footer

settings[shortcut_module_link] = 0

Create a file named: /sites/all/themes/vassia/css/vassia.css for your future css custom
If you need colors features, copy the file /themes/bartik/css/colors.css to /sites/all/themes/vassia/css/colors.css and copy the folder and its contents /themes/bartik/color/ to /sites/all/themes/vassia/color/
Go to the the Administration > Appearance page to enable your new subtheme called Vassia

Everything is very well explained in Creating a subtheme DO page. Now the slider part

Create a file /sites/all/themes/vassia/template.php
Write exactly this code in your template.php file :
<?php
/**
 * Implements template_preprocess_views_view().
 */
function vassia_preprocess_views_view(&$vars) {
  $view = $vars['view'];
  switch($view->name) {
    case 'productviewwithsliders' :

      drupal_add_library('system','ui.slider');
      drupal_add_css('/misc/ui/jquery.ui.slider.css');
      break;
  }
}

Double check the view name. Clear all caches

Enjoy
Comment: @VassiaAlk I edited the answer. Add it in template.php file as explained. If it's empty, put only opening  `name) {
    case 'ProductViewWithsliders' : //replace YOUR_VIEW by your view name
      //global $theme_path;
      drupal_add_library('system','ui.slider');
      drupal_add_css('/misc/ui/jquery.ui.slider.css');
      break;
  }
} No slider appears...
Comment: You should not edit core theme  files. Don't you have your own theme ? Then please write the code you did put into your question below an edit title, and format it correctly so that I'm able to read it. Last, it seems it's not the system name of your view which you can find in the edit url something like /admin/structure/views/view/your_view_name, with lowercase and underscores instead of spaces. You're not far...
Comment: I use Bartik theme, I tried various names and no slider appears. Did you try similar on your machine? I don`t write themes, started to write modules, learning curve goes forward. Thank you. Is there any other way to put sliders in BEF?
Comment: @VassiaAlk I added more explanations.
Comment: Thank you, Mr. Kojo and Forum, I did exactly as you advised, subtheme works, but no slider appears. Can`t find the reason, changed names in all variants. Will try on on the other computer and Drupal instance and on Drupal 8. Are there any other modules for sliders in BEF or other filters?
Comment: Sorry to ear that!  Another point, try  installing JQUERY UPDATE and choose 1.7 or upper version..Tell me if it work please. you may also declare your slider in js behavior but I think it's not necessary anymore with last version.  No errors messages in dblog ?
Answer: Can you try Slide Field Views Module

The sliderfield module provides a new widget for number fields
  (decimal, float, and integer) and a form element. When creating a
  numeric field, select the slider widget.
This project uses the jQuery UI slider included with core.

Or this Module Select with Style

The Select with Style project download contains two modules containing
  three styleable widgets:
Slide with Style, contains a field widget to enter numbers or list
  values via sliders when creating content and also to filter using this
  widget in Views. This includes double-handled sliders to input
  "in-between" ranges. Select with Style, comprises two widgets to enter
  taxonomy fields, in particular hierarchical taxonomies, when creating
  content and also to filter by these taxonomy fields in Views These are
  light-weight solutions that have no external dependencies. No external
  libraries are required. Only minimum configuration, no permissions, no
  coding. Just style!
Comment: Nagarjuna, don't you think it's would be an overkill to add another module for the slider, since VassiaAlk already configured BEF for his project, which comes with Slider feature ? :)
Comment: Nāgārjuna is the name of a Buddhist monk who founded the Mahāyāna Buddhist school... Is it a nickname or your real name ? Sorry for being curious ;)
Comment: I provided an option if you don't wanna code, few people like that few not. And It is my Name :). So nice of you, you just shared that I dont know
Comment: Nice of you, dear patriarch !
Comment: thank you, people, Select with Style and Slide Views widgets do not apprear in BEF. Only JQ UI appears but on a final view there is not. I am trying to add that code you have published.


Eclipse - Stuck "processing jax-rs changes"

Question: I have installed Eclipse Mars2 and when I try to save changes I got a message saying "Processing JAX-RS changes... (Waiting)" and Eclipse freezes for a couple of minutes. I'm developing a spring-mvc maven based application using jboss as server (I don't know whether this is relevant or not).
In this answer a nullPointer is gotten, but I can't see any errors whatsoever. I can't find the "jax-rs- support" option in my project either.
Any clues?
Thank you very much!
Comment: Do you even need to ask? ;) This probably came from the project creation wizard you used which setup JavaEE basics; nowadays it is pretty common to use JAX-RS when you do a JavaEE project after all. But that still wouldn't explain why eclipse freezes for so long of course, that sounds a little buggy.
Comment: Any particular additional plugins you have installed?
Comment: Aside from the default ones of the JavaEE, subversion, jboss red hat and checkstyle.
Comment: If you open the project properties you should be able to see which builders and projects facets are active. Which facets are active?
Comment: There's a "JAX-RS builder" checked. Should I uncheck this one? As fas as I know we are not using JAX-RS in this project.
Comment: Yup, I shouldn't have asked such an obvious question XDD. I think it's working!! Thank you very much!! Post your answer please, and I'll mark it as solved. And I completely agree with you, sounds weird to me as well. Thank you again for you help.
Answer: The reason for this problem to exist is unclear. A quick Google for the problem doesn't seem to bring up any existing bug report.
Meanwhile, whatever processing features Eclipse has for JAX-RS are quite superfluous and you can easily do without - especially if you do not actually use JAX-RS in your project. So in this case disabling the builder which you will find in the Project properties does the trick to make the problem go away.
Comment: Which is the stable version of eclipse which does not have this issue?
Comment: The latest stable version of Eclipse. There is no proof that this is an actual bug in Eclipse, the only thing documented here is a workaround.
Comment: Still annoying and not very reasonable without any JAX-RS in the project. Eclipse ist losing ground...
Answer: You can uncheck JAX-RS from project->properties->project facets . This helped me


Using partials and projections of entities in Web API

Question: I am using breeze with Web API. I don't have a good grasp on how to "Filter columns" or how to not expose and entire table to my Web API. I am using the Entity Framework as my source, and both of my questions are addressed by John Papa here: http://www.johnpapa.net/spajs04/#comment-113761  and confirmed to be a good solution by Ward Bell below. Can someone please show me how I to use the entity framework to create a partial or projection that is queryable in my webapi and will work with breeze?
Here is my current function in the webapi
[HttpGet]
public IQueryable<Contact> GetContacts()
{
    return _contextProvider.Context.Contact;
}

Here is my current class:
public class Contact
{
    [Key]
    public Guid ID { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string NickName { get; set; }
    public string JobTitle { get; set; }
    public DateTime BirthDate { get; set; }
    public bool Gender { get; set; }
    public string SSN { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateUpdated { get; set; }

    public virtual ICollection<Address> Address { get; set; }
}

I would like to have a queryable webapi function that is my current class WITHOUT the SSN field. A solution that works a "database first" entity and does not involve changing my database or adding "views" would be great.
Answer: Client-side projection is fine when you're trying to reduce the payload. You need something server-side when you must ensure that certain data (e.g., SSN) are truly and safely hidden from the client.
@james suggestion - to use the [NonSerialized] (or JSON.NET's [JsonIgnore] attribute) - is an easy and effective approach when the SSN should never go to the client.
It's too inflexible if the SSN should be visible on the client in authorized circumstances (e.g., a user reviewing her own SSN or an HR person with the right to see the SSN).  The JSON.NET IContractResolver gives you tremendous flexibility in deciding dynamically, based on authorization rules, what properties may cross the service boundary.
Some might consider addressing this problem with the serializer as too much of a hack. They might be satisfied by the server-side projection that you showed, @chris_dotnet. Btw, it still makes sense to return an IQueryable from the projection so that the client can reduce the network payload with a filtering query.
Others will prefer to define a DTO (ContactDTO) and serialize that over the wire.

[HttpGet]
    public IQueryable GetContacts()
    {
      return _contextProvider.Context.Contacts
        .Select(p =>
            new ContactDto
            {
                FirstName = p.FirstName,
                ID = p.ID,
                LastName = p.LastName
            });
    }

This IQueryable is more robust than the projection version because the filtering can take place on the data tier rather than the server tier.
On the client-side you can either define metadata for the ContactDto type or you can use a JsonResultsAdapter to map the ContactDto data into a Contact Breeze entity.
Using the JsonResultsAdapter presupposes that you actually want the Contact type - the type as it is shaped in the business model on the server - to be known on the client.
You may not want the server-side Contact shape to be exposed from your service. Many people feel really strongly about this. If you are one of those people, you're better off defining a "DTO Model" representing the entities as you want them to be seen on the client. That means learning to create metadata for your DTO model and writing mapping logic on the server to move between DTOs and your business model.
You can see how all of this can become a big topic. It's one that I'll be taking up soon in the Breeze documentation. Consider this answer a taste of things to come. The take-away is ... you have good choices for hiding data that users' shouldn't see.
Comment: Thanks for the great explanation! I can totally understand why it is so hard to address this when there are multiple viable options. I am currently going with the DTO option and have created a ContactDto using AutoMapper. Since it is a DTO would it be a bad idea to also put my business logic in the dto? I really don't want to create a third layer for the data on the server.
Answer: Thanks John for this info, I took a look at that and it was helpful for client side filtering. I found that I could do it from client side and/or server side.
Client side with breeze:
var query = EntityQuery.from("Customers")
.select("ID, FirstName, LastName");

Server side:
    [HttpGet]
    public IQueryable<Contact> GetContacts()
    {
        var contactList = _contextProvider.Context.Contacts
        .ToList()
        .Select(p =>
            new Contact
            {
                FirstName = p.FirstName,
                ID = p.ID,
                LastName = p.LastName
            })
        .AsQueryable(); // actually IQueryable is not useful after "ToList()"

        return contactList ;
    }

Answer: Chris - There is an example of this in the course with the speaker and session partials. I return projections from those where i do not return the description field (for sessions) nor the bio field (for speakers). Check that out and use the same style for not showing your SSN field for your Employee class
Answer: Can't you mark your attribute as [NonSerialized]
[NonSerialized]
 public string SSN { get; set; }

Answer: I've just found another solution to ignore columns depending on criterias. Just add a partial class (my model gets generated) belonging to the model of interest and use the ShouldSerialize%PropertyName%:
namespace ProvSys.Models
{
    partial class tblEmployees
    {
        public static bool ShouldSerializeResetPassword()
        {
            // Only hand over ResetPassword Field for admin role
            return (ProvSysRepository.IsAdmin);
        }
    }
}

In this sample the ResetPassword property of the tblEmployees table gets transmitted only if the user is in the admin role.
Comment: imo it's all about conditionally exposing properties of models to the web api. that's exactly what it does.


Concept Clarification: Wilcoxon rank test vs two-samples T-test (UPDATE)

Question: I am currently deciding whether to use the Wilcoxon rank test vs two-samples T-test to assess BMI between two unrelated groups. Using the following code to assess normality with(group1, shapiro.test(BMI)), I got p > 0.05 for group 1 (n = 10) but p < 0.05 for group 2 (n = 50).
I want to know whether this means I should use a non-parametric test for my analysis? I know if both are p < 0.05, then I should definitely use the Wilcoxon rank test BUT I have no clue what to do when only one group is non-normally distributed.
UPDATE: QQ-PLOT FOR GROUP 2
Answer: Following standard logic, the t-test assumes normality in both groups, so if you reject it in one, that should be reason enough to think that there's something wrong with the normality assumption.
Unfortunately, though, it is not quite as simple as that, and "standard logic" is somewhat flawed. In fact, even for certain non-normal distributions, the t-test is better than the Wilcoxon test. Testing whether the data are normal or not solves the wrong problem. Model assumptions are never fulfilled anyway (this holds as well for Wilcoxon, still which assumes i.i.d. at least), and the really relevant issue is whether there is something in the data that may mislead the t-test, such as gross outliers or strong skewness. This can only be diagnosed from looking at the data, a Shapiro-Wilk test won't tell you this.
Comment: Now after having seen the qq-plot, I'd indeed say hands off the t-test.
Comment: The issue with outliers is that the more and the more extreme of them you have, the larger the n needs to be in order for the CLT to kick in. Furthermore, with outliers it's not quite clear anymore whether comparing means is the right thing to do (which the t-test does regardless of whether data are normal and the CLT). So no, I don't think this argument works here to save the t-test.
Comment: These issues have been discussed to the extreme on this site.  Briefly, testing for normality is bad statistical practice (among other things assumes the test for normality as a power of 1.0).  The Wilcoxon test is more powerful than the t-test over the types of distributions seen in practice.  If normality holds, Wilcoxon has 0.95 efficiency still.  The central limit theorem is a **limit** theorem and does not help us with finite sample sizes.
Comment: Thx for this! I updated the qq-plot for the second group. It seems like there are some outliers. Does it mean I should NOT use t-test and should go ahead with the non-parametric test?
Comment: I'd hold that against the t-test and tend to use Wilcoxon, although note that Wilcoxon tests equality of distributions against one being stochastically larger than the other, and if the two distributional shapes are very different, it may be that none of them is clearly stochastically larger than the other (i.e., one may have more probability on low as well as high values, the other one in the middle), despite them being different. This doesn't need to stop you from using Wilcoxon, however should be taken into account for interpretation of the result.
Comment: I thought about the Central Limit Theorem and wonder whether I should still use the t-test since my group 2 has n>30 and according to CTL, we can assume normality? What do you think?
Answer: This is to complement rather than to disagree with @ChristianHenning's Answer and Comments.

It is the residuals of the t-test that need to be
normal. So if you have Sample 1 $X_1, X_2, \dots, X_n$
and Sample 2 $Y_1, Y_2, \dots Y_m,$ then you might
make a normal probability plot of the $m+n$ differences $$X_1 - \bar X, X_2, - \bar X, \dots, X_n - \bar X,\:\; Y_i - \bar Y, \dots Y_m-\bar Y.$$ However, your normal probability plot of the $Y_i$ is sufficiently non-normal (perhaps exponential?) to lead to a non-linear normal probability plot of residuals.

It may not be quite enough for $\bar X - \bar Y$ to be nearly normal. Only for normal data are the sample mean and sample variance stochastically independent. Thus, if data are not normal the t statistic (with information from the means in the numerator and information from
the variances in the denominator) may not have a t distribution, leading to a t test that is misleading.

If we use a nonparametric Wilcoxon Rank Sum test to compare two distributions of remarkably different shapes, and if we reject the null hypothesis, indicating that the
two samples have significantly different 'locations', then it may not be entirely clear what to conclude.
The correct conclusion may have to do with differences in location (medians) or indicate 'stochastic domination' of one sample over the other.

In particularly problematic cases, it may be helpful to do a permutation test of the null hypothesis that the two samples come from populations with the same mean and the alternative that population means differ.

Illustrating permutation tests. Whatever its distribution may be, you may feel that
a two-sample t statistic does a good job of reflecting differences in means. Then you can randomly permute
observations between the x and y samples and
find the value of the t statistic for each such permutation. With enough iterations of such permutations, one can get a good idea of the permutation distribution of the t statistic, and of the P-value of test of the null hypothesis against
the alternative.
For example, consider the following fictitious data
in which the two populations have normal and exponential distributions with the same mean.
set.seed(2021)
x = rnorm(20, 50, 10)  # mean 50
y = rexp(40, 1/50)     # mean 50
z = c(x,y)             # all 60 observations
g = rep(1:2, c(20,40))
pv.obs = t.test(z ~ g)$p.val  # obs p-val Welch
pv.obs
0.8331715

Boxplots illustrate different shapes of the two samples.
hdr = "Boxplots of samples x (bottom) and y"
boxplot(x, y, col="skyblue2", horizontal=T, pch="|",main=hdr)

[In this scenario a Wilcoxon rank sum test does not reject at the 5% level. R code wilcox.test(x, y)$p.val returns $0.105.]$
In the R code below for the permutation test, the
expression sample(g) makes the random assignments of all data among two samples of size2 20 and 40.
set.seed(1222)
pv.prm = replicate(10^5,
          t.test(z~sample(g))$p.val)
mean(pv.prm <= pv.obs)
[1] 0.83924           # aprx P-val of perm test
2*sd(pv.prm <= pv.obs)/sqrt(10^5)
[1] 0.00232308        # aprx 95% margin of sim error

Thus, the simulated P-value of the permutation
test is $0.839 \pm 0023,$ which exceeds 5%, so that
the null hypothesis is not rejected.
By contrast, if the fictitious data are from
populations of different shape with means $\mu_x = 75$ and $\mu_y = 50,$ then a permutation test rejects
the null hypothesis (P-value about $0.015 < 0.05 = 5\%)$ that population means are equal.
set.seed(2021)
x = rnorm(20, 75, 10)  # mean 75
y = rexp(40, 1/50)     # mean 50
z = c(x,y)             # all 60 observations
g = rep(1:2, c(20,40))
pv.obs = t.test(z ~ g)$p.val  # obs p-val Welch
pv.obs
[1] 0.0149717

hdr = "Boxplots of samples x (bottom) and y"
boxplot(x, y, col="skyblue2", horizontal=T, pch="|",main=hdr)

[In this scenario a Wilcoxon rank sum test rejects at the 5% level. R code wilcox.test(x, y)$p.val returns $0.015.$ The interpretation of 'rejection' may be controversial.]
set.seed(1222)
pv.prm = replicate(10^5,
           t.test(z~sample(g))$p.val)
mean(pv.prm <= pv.obs)
[1] 0.01476

In this example the P-value of the permutation test, using the Welch 2-sample t statistic as metric, gives about the same P-value as the Welch test. In both parts of this
example $(H_0$ true and not), $n=50$ observations from the exponential
distribution seems to be enough to rely on the legendary robustness of t tests against departures
from normality.
In reporting the results, we might just say that 50 is enough, and that we verified this via a permutation test. Alternatively, one we might say we were not
confident using the Welch t test with such skewed
data in Sample 2, so we did a permutation test, and
report results of the permutation test.


Converting Text to mp3 or .wav to mp3 in C# (For Captcha Audio)

Question: I am looking to take numbers generated from a captcha and have a control/link to allow the user to play it if they need to hear it. I currently have it using  System.Speech to generate this into a wav file. When attempting to find a method to allow the user to play this (was using a regular a href link) to open up a new page which ran the .aspx page and play the file, we ran into various issues across browsers.
IE(8 and 10) seemed to reset the session (which clears how I am getting the string from the captcha), Chrome worked fine, Firefox tried to infinitely load the page before timing out.
My boss wants to avoid using the google captcha, so my search continued.
We then came across the audio html tag. However, the drawback to this is for some reason IE won't play .wav files in an audio tag.
So what I am seeking is a way to either:

Convert this .wav file into a MP3 which could be played on all browsers.
A way to force IE to play .wav files in the  tag.
Any suggestions as to make all the browsers treat my .wav file the same and play it after a trigger.

Thanks!
Here is my code to create the .wav file.
<code>protected override void OnPreRender(EventArgs e)
{
    Response.ContentType = "audio/wav";
    string captchacode = Session["HologramID"].ToString();
    string[] num = captchacode.Select(c => c.ToString()).ToArray();
    captchacode = string.Join(" ", num);
    MemoryStream mstream = GetAudio(captchacode);
    mstream.Position = 0;
    mstream.CopyTo(Response.OutputStream);
    Response.End();
}

public static MemoryStream GetAudio(string input)
{
    MemoryStream mem = new MemoryStream();

    var t = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
    {
        SpeechSynthesizer synth = new SpeechSynthesizer();

        synth.Rate = -5;
        synth.SetOutputToWaveStream(mem);
        synth.Speak(input);

    }));

    t.Start();
    t.Join();

    return mem;
}
</code>
Comment: Not a solution, but look into [security through obscurity](https://en.wikipedia.org/wiki/Security_through_obscurity), it might give you an idea why rolling your own Captcha is a bad idea.
Answer: You can use html5 audio player to play and httphandler (ashx) to stream mp3. However old browsers don't support html5 player. Instead you can use a flash player solution.
Craeta ashx handler that takes the captcha text and stream mp3 data.


Issue with the results of PCA component values

Question: I am performing PCA on a dataset of (28 features + 1 class label) and 11M rows (samples) using the following simple code:
<code>from sklearn.decomposition import PCA
import pandas as pd

df = pd.read_csv('HIGGS.csv', sep=',', header=None)

df_labels = df[df.columns[0]]
df_features = df.drop(df.columns[0], axis=1)
pca = PCA()
pca.fit(df_features.values)
print(pca.explained_variance_ratio_)
print(pca.explained_variance_ratio_.shape)
transformed_data = pca.transform(df_features.values)
</code>
The pca.explained_variance_ratio_ (or eigenvalues) are the following:
<code>[0.11581302 0.09659324 0.08451179 0.07000956 0.0641502  0.05651781
 0.055588   0.05446682 0.05291956 0.04468113 0.04248516 0.04108151
 0.03885671 0.03775394 0.0255504  0.02181292 0.01979832 0.0185323
 0.0164828  0.01047363 0.00779365 0.00702242 0.00586635 0.00531234
 0.00300572 0.00135565 0.00109707 0.00046801]
</code>
Based on the explained_variance_ratio_, I don't know if there is something wrong here. The highest component is 11%, as opposed to the fact that we should be getting values starting at 99% and so. Does it imply that the dataset needs some preprocessing such as ensuring the data are in a normal distribution?
Answer: There is no rule that you need to have high amount of variance explained in the first principal component.
PCA finds orthogonal linear combinations of your original variables, such that the first principal component has the highest variance, the second the second highest, etc. However, the 'highest' does not mean that it should be a large absolute amount, just that there is no linear combination that has greater variance.
But PCA just maximizes variance, it is not some magical tool to find the most interesting combination of variables. If your data come from a strongly skewed probability distribution, then this variance isn't a very informative measure of variability.

As for how to proceed:

Consider the purpose of your analysis and the data generating process. Is PCA truly the best way to do whatever you are trying to accomplish? (Dimension reduction, orthogonalization, ...)
If the answer to (1) is yes, then you may consider transforming the variables such that their variances are more representative of their variability. You may also conclude that your variables already are approximately normal, in which case 11% is apparently the largest amount of the total variance that can be explained in a single linear combination of your original variables.
Are all variables measured on the same scale? Are they vastly different things on different scales? PCA is not invariant to scaling (variance depends on scale!), and whether you scale or not has important implications for how you can interpret the results.

Have a look at the other PCA related questions on here (e.g. this one). If you better understand what PCA does, you will also better understand the results you get, and more importantly, whether it is the right tool for the job to begin with.
Answer: If the first few eigenvalues don't explain the bulk of the variance in your data, it means one of two things:

The data is just random noise. Try running PCA on a matrix of independent
points sampled from a standard Gaussian. You'd see that the eigenvalues are much more evenly dispersed.
Basic PCA isn't sufficient for dimensionality reduction of your data. If this is the case, you may want to try more advanced methods, like kernel PCA.


Constructor requesting floats when it already should get floats

Question: I am following an online tutorial for a live wallpaper. During this I have encountered a problem which I find peculiar. The problem is that the constructor MyPoint in the code below says that it is getting String, int, int and that it needs String, float, float. However, the x and y in this constructor reference to the local floats x and y. I checked by selecting x or y in the constructor, and the local floats x or y would light up, so it should be getting floats as input. So why does it give this error?
@Override
public void onTouchEvent(MotionEvent event) {
if (touchEnabled) {

  float x = event.getX();
  float y = event.getY();
  SurfaceHolder holder = getSurfaceHolder();
  Canvas canvas = null;
  try {
    canvas = holder.lockCanvas();
    if (canvas != null) {
      canvas.drawColor(Color.BLACK);
      circles.clear();
      circles.add(new MyPoint(String.valueOf(circles.size() + 1), x, y));
      drawCircles(canvas, circles);

    }
  } finally {
    if (canvas != null)
      holder.unlockCanvasAndPost(canvas);
  }
  super.onTouchEvent(event);
}
}

EDIT:
The exact error is: "The constructor MyPoint(String, float, float) is undefined"
With quick fix option: "Change constructor MyPoint(String, int, int) to MyPoint(String, float, float)"
Comment: please also post the code of MyPoint constructor, as it seems your constructor is not compatible
Comment: It says: "The constructor MyPoint(String, float, float) is undefined" with quick fix option: "Change constructor MyPoint(String, int, int) to MyPoint(String, float, float)"
Comment: You try to call a constructor `MyPoint(String, float, float)`, but you haven't defined one. Fix: define one.
Answer: A constructor does not request anything.  It requires values of whatever type you have defined in the constructor signature.  If you do not supply values of the correct type, then the compiler cannot compile your code since you have told it to instantiate an instance of MyPoint via it's constructor receiving String, float, float which does not exist.
So the error is, as a minimum, in your "MyPoint" class.  You might also have an error in the code you've shown if indeed the MyPoint constructor requires ints.  However, looking at your code, it seems that float is more likely.
The error, and it's solution, are straightforward.
In MyPoint, you have
public MyPoint(String name, int x, int y);

You should also have
public MyPoint(String name, float x, float y);

I am assuming that you want both.  If you have no need for a constructor receiving x and y as ints, then simply change it.


LoginToBoggan and user registration password to allow users register with password and email verification at the same time

Question: I am using the LoginToBoggan module to enrich the account functionalities. In its settings there is a Set password field; when it's not checked anonymous users can't choose their passwords in registration form but they can receive verification emails.
I need both functionalities: email verification and set the password field in registration form.
I found the User registration password module which allow users to choose password in the registration form.
The problem is when I create a new account. The user can login with the chosen password, but I need they first verify their email.
So the process must be like this:

Anonymous users register and choose their password in the registration form
They receive a verification email
They click on the link given in the email to activate the account

The problems I have now are the following.

Users can log in after registration, even if they didn't click on the link sent via email
The link in the email is a kind of reset password link and not a link to verify the email link

Is there a possibility to fix this and allow users to choose their password in registration even if they still to verify the email they use for the site?
Answer: You could use https://www.drupal.org/project/registration_role to auto assign a different role (as in other than authenticated) to users who register.
https://www.drupal.org/project/autoassignrole this one offers a little bit more functionality.
Answer: enable logintoboggan_rules module and import following rule
<code>{ "rules_notify_user_when_account_activated" : {
    "LABEL" : "Notify user when account activated",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "logintoboggan_rules" ],
    "ON" : { "logintoboggan_validated" : [] },
    "DO" : [
      { "user_send_account_email" : { "account" : [ "account" ], "email_type" : "status_activated" } }
    ]
  }
}
</code>


Fazer o programa voltar ao inicio

Question: Estou fazendo uma criptografia em cifra de césar. Como que eu faço para o programa voltar ao começo.
Código:
tela=input('Com isso Coloque uma palavra ou frase para ajudar no comprometimentoao meio ambiente')

# Solicitando o texto a ser encriptado ou decriptado:
texto = input('Digite a mensagem a ser encriptada ou decifrada: ')
# Chave a ser utilizada
chave = int(input('Entre com o valor da chave (deslocamento): '))
# Determinar modo de operação (E = encriptar; D = decriptar)
modo = input('Escolha E para encriptar ou D para decriptar o texto: ')
# Conjunto de caracteres válidos no algoritmo
CARACTERES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# Variável para armazenar o texto criptografado (ou decifrado)
convertido = ''
# Converter todo o texto em maiúsculas:
texto = texto.upper()
# Código que será executdo em cada caractere do texto:
for caractere in texto:
  if caractere in CARACTERES:
 # Obter o número criptografado ou decriptado do caractere
    num = CARACTERES.find(caractere)
 # Obter o número do caractere
    if modo == 'E':
      num = num + chave
    elif modo == 'D':
      num = num - chave
 # Manipulando a rotação se o valor de num for maior do que o comprimento de CARACTERES ou menor que 0
  if num >= len(CARACTERES):
    num = num - len(CARACTERES)
  elif num < 0:
    num = num + len(CARACTERES)
 # Adicionar (concatenar) o caractere correspondente a num na variável convertido
    convertido = convertido + CARACTERES[num]
  else:
 # Concatenado o caractere sem efetuar criptografia ou decifragem
    convertido = convertido + caractere
# Mostrar o texto encriptado ou decifrado na tela:
if modo == 'E':
  print('O texto criptografado é ', convertido)
if modo == 'D':
  print('O texto decriptado é ', convertido)

Answer: Utilize o bloco while. Esse bloco de repetição irá repetir o trecho de código sempre que a instrução for verdadeira. Logo você pode fazer o código desta forma:
while True:

    tela=input('Com isso Coloque uma palavra ou frase para ajudar no comprometimentoao meio ambiente')

    # Solicitando o texto a ser encriptado ou decriptado:
    texto = input('Digite a mensagem a ser encriptada ou decifrada: ')

    # Chave a ser utilizada
    chave = int(input('Entre com o valor da chave (deslocamento): '))

    # Determinar modo de operação (E = encriptar; D = decriptar)
    modo = input('Escolha E para encriptar ou D para decriptar o texto: ')

    # Conjunto de caracteres válidos no algoritmo
    CARACTERES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    # Variável para armazenar o texto criptografado (ou decifrado)
    convertido = ''

    # Converter todo o texto em maiúsculas:
    texto = texto.upper()

    # Código que será executdo em cada caractere do texto:
    for caractere in texto:
      if caractere in CARACTERES:
     # Obter o número criptografado ou decriptado do caractere
        num = CARACTERES.find(caractere)
     # Obter o número do caractere
        if modo == 'E':
          num = num + chave
        elif modo == 'D':
          num = num - chave
     # Manipulando a rotação se o valor de num for maior do que o comprimento de CARACTERES ou menor que 0
      if num >= len(CARACTERES):
        num = num - len(CARACTERES)
      elif num < 0:
        num = num + len(CARACTERES)
     # Adicionar (concatenar) o caractere correspondente a num na variável convertido
        convertido = convertido + CARACTERES[num]
      else:
     # Concatenado o caractere sem efetuar criptografia ou decifragem
        convertido = convertido + caractere
    # Mostrar o texto encriptado ou decifrado na tela:
    if modo == 'E':
      print('O texto criptografado é ', convertido)
    if modo == 'D':
      print('O texto decriptado é ', convertido)

Answer: Está resposta é válida, porém você não terá a opção de sair do programa.
Sabendo disto implementei uma opção de parada:
opcao=0
while opcao !=1:
  tela=input('Com isso Coloque uma palavra ou frase para ajudar no comprometimentoao meio ambiente')

  # Solicitando o texto a ser encriptado ou decriptado:
  texto = input('Digite a mensagem a ser encriptada ou decifrada: ')
  # Chave a ser utilizada
  chave = int(input('Entre com o valor da chave (deslocamento): '))
  # Determinar modo de operação (E = encriptar; D = decriptar)
  modo = input('Escolha E para encriptar ou D para decriptar o texto: ').upper()
  # Conjunto de caracteres válidos no algoritmo
  CARACTERES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  # Variável para armazenar o texto criptografado (ou decifrado)
  convertido = ''
  # Converter todo o texto em maiúsculas:
  texto = texto.upper()
  # Código que será executdo em cada caractere do texto:
  for caractere in texto:
    if caractere in CARACTERES:
  # Obter o número criptografado ou decriptado do caractere
      num = CARACTERES.find(caractere)
  # Obter o número do caractere
      if modo == 'E':
        num = num + chave
      elif modo == 'D':
        num = num - chave
  # Manipulando a rotação se o valor de num for maior do que o comprimento de CARACTERES ou menor que 0
    if num >= len(CARACTERES):
      num = num - len(CARACTERES)
    elif num < 0:
      num = num + len(CARACTERES)
  # Adicionar (concatenar) o caractere correspondente a num na variável convertido
      convertido = convertido + CARACTERES[num]
    else:
  # Concatenado o caractere sem efetuar criptografia ou decifragem
      convertido = convertido + caractere
  # Mostrar o texto encriptado ou decifrado na tela:
  if modo == 'E':
    print('O texto criptografado é ', convertido)
  if modo == 'D':
    print('O texto decriptado é ', convertido)
  opcao = int(input('>>>> Digite 1 para sair do programa ou 0 para continuar \n'))

Veja funcionando no repl.it
Tomei a liberdade de fazer uma melhoria no seu código:
Seu input para variável modo estava assim:
modo = input('Escolha E para encriptar ou D para decriptar o texto: ')

Por sua vez, observe que seu if estava assim:
if modo == 'E':

Com a possibilidade de gerar um erro no seu código, já que o usuário poderia inserir letra minuscula na variável modo.
Sendo assim corrigir seu input usando a função upper():
modo = input('Escolha E para encriptar ou D para decriptar o texto: ').upper()

Fazer o programa voltar ao inicio


Reverse derivation of negative log likelihood cost function

Question: If I understand correctly, the negative log likelihood cost function  goes hand-in-hand with the softMax output layer.
But why?
The simplest motivating logic I am aware of goes as follows: softMax outputs (which sum to 1) can be considered as probabilities. Whichever unit corresponds to the actual correct answer, we wish to maximise the probability (or output) for this unit.  Which is the same as minimising the neg-log-prob.
So the probability of the network outputting the correct (Target) answer is:
$$P(T) = y_T = {\exp z_T \over \sum_k \exp z_k}$$
So we wish to minimise $-log(P(T))$, which can be rewritten as $log(\sum_k \exp z_k)-z_T$
Now if we calculate the gradient $\partial C \over \partial z_m$ across the (SoftMax) activation function, it comes out as $y_T - 1$ when $m=T$ and $y_m$ otherwise, so we can write:
$${\partial C \over \partial z_m} = y_m - targ_m$$
Which looks like magic.
My question is: How to go about removing the magic while maintaining clarity?

Michael Nielsen's page here points out that one can derive the cross entropy cost function for sigmoid neurons from the requirement that ${\partial C \over \partial z_k} = y_k - targ_k$.
i.e. The cost function has to exactly counterbalance the gradient across the (sigmoid) activation function.
I've been trying to do the same thing for the negative log likelihood cost function for SoftMax neurons:

SoftMax looks like a sensible formula; $\exp$ will accentuate the
  winner, and maps $R \to R^+$ and the denominator is just a
  normalisation term to make sure outputs sum to 1. So maybe no further
  justification is required for choosing $\exp$... but still this seems
  a little arbitrary/weak.
Now supposing the choice has been made, we have:
$$y_m = {\exp z_m \over \sum_k \exp z_k}$$
So maybe we can start off by
  requiring some cost function that satisfies:
$${\partial C \over \partial z_m} = y_m - targ_m$$
... so it should be possible to derive $C(y_m)$ using integration.

... but I can't see how to do that last step.  Can anyone see it through?
PS Peter's Notes contains a tricky derivation for the negative log likelihood cost function.
Comment: I am tackling the exact same questions. Have you taken a look here? https://www.quora.com/What-is-the-gradient-of-the-log-likelihood-function-in-multinomial-logistic-regression
Answer: I haven't touched integration after leaving college, but for this specific problem, it seems straightforward to get the cost function. Here I am trying to sketch it just FYR.
$$
C = \int{\partial C \over \partial z_m} d z_m = \int(y_m - 1_{m=T})d z_m
$$
With $y_m = {\exp z_m \over \sum_k \exp z_k}$,
$$
\int(y_m - 1_{m=T})d z_m = \int({\exp z_m \over \sum_k \exp z_k} - 1_{m=T})d z_m
$$
Since we have,
$$
\int {\exp z_m \over \sum_k \exp z_k} d z_m = \log ({\sum_k \exp z_k}) + f,
$$
and,
$$
\int 1_{m=T} d z_m = z_T + g,
$$
$f$ and $g$ do not consist of $z_m, m = 0,...,k$, so it is safe to require $f$ and $g$ to have only constants, and here for minimization purpose, we can ignore the constants.
Then the equations above give the cost function,
$$
\log(\sum_k \exp z_k)- z_T = - \log{\exp z_T \over \sum_k \exp z_k} = -\log(y_T)
$$


React-Jest error: node:internal/process/promises:288 triggerUncaughtException(err, true /* fromPromise */); TypeError

Question: The exact error is as follows:
node:internal/process/promises:288
          triggerUncaughtException(err, true /* fromPromise */);
TypeError: (0, _MyApp.getDataInfo) is not a function

This error occurs when I do
npm test

npm version: 8.19.2
node version: v18.12.1
This error started to occur when I upgraded my existing code from node14 to node18.
npm test in package.json: react-scripts test --runInBand --detectOpenHandles
jest version: ^28.1.1
react-script: ^5.0.0
Tried adding async and await in multiple test cases but that didn't work.
Answer: "strict": true,
"forceConsistentCasingInFileNames": true,

enable this two options
Comment: How enabling "forceConsistentCasingInFileNames" will help this issue?
It clearly says it's a type error "triggerUncaughtException".
Comment: I have got the same problem, and I need some help with this. Thanks.
Comment: anyone got a solution to this?
Comment: Hey all, sorry for the late reply but the solution is to mock the occurrence of this function and the set an appropriate return value. It does take time to debug but post that the issue is resolved.


Install a module in Linux

Question: I am looking to download and install a module using the Linux command line.
How can I do this?
Answer:
You can install Drush (see Installing Drush on Ubuntu or How to install Drush server-wide in less than one minute); after, you can install a module with drush:

to download a module
drush dl modulename

to enable the module
drush en modulename

Also, you can use wget to download the module, and make a directory inside the modules directory (in this case the modules directory is /home/youruser/public_html/sites/all/modules/).

    wget http://ftp.drupal.org/files/projects/modulename-6.x.tar.gz
    mkdir -p /home/youruser/public_html/sites/all/modules/modulename
    tar -zxf modulename-6.x.tar.gz -C /home/youruser/public_html/sites/all/modules/modulename

Enable the  module in "Administer > Site building > Modules."
Comment: I liked that you provided both the options with and without drush.
Answer: To install Drupal modules using command line, you have to install Drush Composer on your machine.
So use this link to install Drush using composer : https://github.com/drush-ops/drush
After installing it, open your terminal and go to your project/drupal directory into which you want to download modules.
To download and enable modules using a single command, run following command:
drush en -y your_module_name

Note : your_module_name should be the name present on the url of the module name like https://www.drupal.org/project/your_module_name
Answer: The best way to do this is via the Drush project.  This will allow you to download and enable modules via the command line similar to the code below.
# Download the Views module
drush dl views

# Enable the Views module
drush en views

Answer: If you don't have (or don't want) drush, you can also just do something like this:
$ cd $MY_DRUPAL_SITE_ROOT/sites/all/modules
$ wget http://ftp.drupal.org/files/projects/querypath-6.x-1.6.tar.gz
$ tar -zxvf querypath-6.x-1.6.tar.gz

(the -z flag to the tar command does a "gunzip" of the file before untarring)
From there, you can go to the Site Building -> Modules screen and install the module there.
That said, if you are going to be doing this a lot, Drush is very helpful
Comment: Or you can even combine both commands together  wget http://ftp.drupal.org/files/projects/querypath-6.x-1.6.tar.gz &&
 tar -zxvf *.tar.gz
Comment: What would be the next step to configure these modules?
Answer: Install drush, which is a command line tool for Drupal. Downloading and installing a module would just need you to execute two commands. (The first command download the module, and the other one installs it.)
drush dl <modulename>
drush en <modulename> -y

These commands will take care of module dependencies too.
Comment: @ arpitr -thanks for your response,already my drupal 7 is running .again i want to install drush?
Comment: drush is independent. After installation you just need to run drush commands inside drupal installation directory, there are other cool stuff you can do with drush http://drushcommands.com/drush-7x
Answer: 1-   By using cyber duck tool we can import any thing into linux server from windows.using ip,username,password,port.
http://www.v3.co.uk/v3-uk/download-review/2032496/cyberduck-windows-v401
2-   wget http://ftp.drupal.org/files/projects/modulename-7.x-1.0-rc2.tar.gz
 mkdir -p /home/force_password_change-7.x-1.0-rc2.tar.gz -C /home/www/html/transgenic/sites/all/modules/modulenmae

procedure
-open terminal  and give the download link adrress(above format) it will download save on desktop and follow second command and you can able to see your module,in modules.

Install a module in Linux


Dynamically creating module in sys.modules causes sys to become None

Question: I'm creating a module called Qt (Github link) where I alias another module (for example PyQt4), so that when I import Qt I am in fact importing PyQt4:
from Qt import QtWidgets  # this really imports PyQt4.QtGui

Example Qt module:
# Qt.py
import sys
import PyQt4.Qt

sys.modules["Qt"] = PyQt4
PyQt4.QtWidgets = PyQt4.QtGui

However, If I add print sys at the end of Qt.py, sys equals None:
print sys
>>> None

Why does sys "break" and can I avoid breaking it?
This is in Python 2.7.11 on OS X.

@Lol4t0 – Here's an example where I'm only using standard modules:
# Xyz.py

import os
import sys

sys.modules['Xyz'] = os
print sys  # this will print "None"

So importing this will result in that "None is printed:
$ python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Xyz
None

Comment: Is this behaviour observed only for PyQt modules?
Comment: @Lol4t0 no, this also happens with e.g. `PySide`.
Comment: Then can you make an example, that only use standard modules, that shows the problem, so everyone can run it on their machine and was able to investigate?
Comment: @Lol4t0 ok, added an example "Xyz.py".
Comment: Thanks. I see, you can just reimport sys
Answer: When there are no longer any reference to a module, it will drop all its globals by replacing them with None.
You need to remember the old verion of sys.modules['Xyz'] (i.e. the current module) until you are done executing it.
Comment: Aha, I see now. This happens not only to `sys`... but it actually happens to *any* imported module inside the python file. By changing the filename to not match the module I register, this issue doesn't happen.


How to create a symmetrical Host - VM setup

Question: So I'm in a rather annoying situation where about half of the work I do requires a Linux based environment to work on, and the other half requires a Windows based environment. Unfortunately I need to switch between the environments on a regular basis and the time taken to re-boot into the other environment is quite prohibitive of this.
I have tried running Linux most of the time with a Windows VM to work in when necessary but I have found that to be quite restrictive in terms of performance, the same when running Windows as the host and Linux within a VM.
What I would like to do is set up my work machine to dual boot Windows and Linux alongside each other, but set up VMWare Workstation or a similar product to allow me to boot one partition as a VM while working in the other as a host, and vice versa with the other partition as the host and the first as a VM. Ideally this would allow using each host for it's main tasks and swapping to the VM when needed for quick things.
RAM isn't an issue on the machine, I have 16GB available and can get more if necessary (probably not) and both operating systems will run from an SSD with a HDD for data storage
, or being able to hibernate as the host and restart the machine
Just to re-iterate, what I'm imagining is:

/dev/sda1 - Windows C: drive
/dev/sda2 - Linux / drive
/dev/sdb1 - Linux /home drive
/dev/sdb2 - Windows D: drive

Where both Windows C: and Linux / can be booted as both native host operating systems and as a guest inside the other.
Has anyone done a set up like this before? If so are there any guides you'd recommend?
What problems can people foresee happening?
Is there another possible solution that I'm missing?
I realise that this may not be the ideal solution, I've asked about getting multiple machines and a KVM but our IT administrator shot that idea down.
Answer: It seems you are looking for a solution that may be very specific to your instance.  I would pose the question of why running a VM is not working for you (in either instance of Win/Linux or Linux/Win).
I will give a few ideas just to toss around and see what might be the best solution for you.  Since I do not know what you use each OS for I'm going to just speculate a little bit.
Idea 1:
I would say almost everything in linux can be run from a console.  With that in mind typically I would setup a Windows computer running a VMWare Workstation or any other VM tool to run a instance of Linux on it.  With that I would just SSH into the VM through PuTTY and do the tasks at hand.  You could also go the extra step to setup VNC and X if you really need a full "desktop" environment.  I find running the OS is less of a issue but rather running the graphics/desktop.  Typically a VM software comes with a console view of the desktop but I find using RDP or VNC use less resources.
Idea 2:
You could do the same thing as Idea 1 but instead of running a VM you would run a linux emulator like Cygwin to run linux at the same time without needing to actually install a VM on VM Software.  Since I do not know what you use each OS for it would really change the solution a bit.
Side Note: with both 1 or 2 I believe Windows being the one needing a full Desktop environment it would be the preferred OS to be the Host.
Idea 3:
Run a full KVM type server such as Unraid.  This would require 3 graphic cards + at least 3 HDDs and it would take a bit of configuration but it can run Unraid + Windows (Monitor 0) + Linux (Monitor 1) and each OS would have a dedicated graphics card to point to.  You can probably YouTube how to do this (people make single machine multi gaming rigs with this).
Hope this all helps :)


how can i generate 2 binaries, one in fortran and one in cxx, with cmake

Question: I have a library that must be linked to 2 main codes, one in fortran and on in cxx.
I have this library in src/lib/CMakeLists.txt:
ADD_LIBRARY(mylib a.f90 b.c c.cc)

and in src/main/CMakeLists.txt, I have:
ADD_EXECUTABLE(mymain1 mymain1.f90)
TARGET_LINK_LIBRARIES(mymain1 mylib)

ADD_EXECUTABLE(mymain2 mymain2.cc)
TARGET_LINK_LIBRARIES(mymain2 mylib)

When compiling mymain1, it use the CXX compiler to link instead of the Fortran one.
How can I tell cmake to use Fortran to link mymain1 and CXX to link mymain2?
Answer: You may directly affect on language used for linking with LINKER_LANGUAGE property:
# Use Fortran compiler for link 'mymain1' executable
set_target_properties(mymain1 PROPERTIES LINKER_LANGUAGE Fortran)

Another way could be "teach" CMake to properly choose linker.
Without the library CMake would correctly select Fortran linker for mymain1 as it compiled only from Fortran sources, and C++ linker for mymain2 as it compiled only from C++ sources.
But linking with the library messes CMake: because the library mylib is compiled from sources on several languages, CMake selects linker for it using some "preference scores" for languages (see CMAKE_<LANG>_LINKER_PREFERENCE variable). More likely, C++ "beats" Fortran in your case.
Moreover, when select linker language for mymain1, CMake take into account language for the static library mylib. Because of that C++ wins even for the executable built from Fortran sources only.
You may disable propagating library's language to the executable using variables CMAKE_<LANG>_LINKER_PREFERENCE_PROPAGATES:
# <place this *before* adding executables>
# Do not propagate language of C++ libraries to the executables.
set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES OFF)


Is the cyclic group: {$\ 1, a, a^2, ... a^{n-1}$} even a group?

Question: The cyclic group {$\ 1, a, a^2, ... a^{n-1}$} doesn't seem to be a group to me?
For example if it's something like  {$\ 1,2,4,8,16,32,64,128$}
then $\ 64*32=2048$, which doesn't belong in the group, hence the closure law is violated.
It might be seem silly but I need help.
Comment: $a$ is not a number but an abstract symbol. Multiplication here isn't multiplication of real numbers, but an abstract group operation that satisfies $a^n=1$. You can't treat $a$ as a variable and substitute in any number you like. But you can, for example, take $a$ to be a primitive nth root of unity (and the group operation to be multiplication of complex numbers).
Comment: At the moment you have specified only a set. Even if multiplication is implied as the relevant binary operation, you need the relation $a^n=1$ to make this a cyclic group of order $n$ (Again with the implied condition that no lower positive integer power of $a$ is equal to $1$). If you had $a^2=1$ instead some of the elements in your set would be equal, and you'd have the cyclic group of order $2$.
Comment: It's congruence.
Comment: Every group depend of its binary operation. So, even $\mathbb{Z}/n\mathbb{Z}$ could not be a group if you use the incorrect binary operation. So, remember to be aware of the rules of your context, here is really important to take congruence mod $n$ in the exponents.
Comment: What is the context? What is $a$? Where does it live?
Comment: You have to assume $a^n=1$.  The operation is not ordinary multiplication over the integers.
Comment: Both of the two last comment explain the whole story. I am asking you: If you write that set of powers of $a$ be a cyclic group so what are you looking for then? That set is a cyclic group generated by symbol $a$ when we have a relation like $a^n=1$. What is the operation for your second set? Multiplication?
Comment: @DanielMroz Can you please post that as an answer, not as a comment? I don't know why you posted it as a comment.
Answer: A set is or isn't a group under a given binary operation. The most obvious choice for which the given set is a group is $a^i\otimes a^j:=a^{i+j(\operatorname{mod}\, n)}$. In fact, this is an Abelian group, isomorphic to $\mathbb{Z}\backslash n\mathbb{Z}$.
Answer: Posted my comment as an answer;
$a$ is not a number but an abstract symbol. Multiplication here isn't multiplication of real numbers, but an abstract group operation that satisfies $a^n=1$.
You can't treat $a$ as a variable and substitute in any number you like. But you can, for example, take $a$ to be a primitive nth root of unity (and the group operation to be multiplication of complex numbers).
Comment: Thanks, I now understand.


Why does my train data not fall in confidence interval with scikit-learn Gaussian Process?

Question: I have a dataset with 1-dimensional input and output and I am applying Gaussian Process regression to it using scikit-learn:
from sklearn import gaussian_process

X, y = get_data()
gp = gaussian_process.GaussianProcess(nugget=0.1)
gp.fit(X, y)
test_X = np.arange(0, 24*60*60, 15*60)
pred_y, pred_mse = gp.predict(test_X, eval_MSE=True)
confidence_interval = np.sqrt(pred_mse) * 2.33

Plotting the result:
import matplotlib.pyplot as plt
plt.scatter(X, y, label='Train data')
plt.fill_between(test_X,
                 pred_y - confidence_interval,
                 pred_y + confidence_interval,
                 alpha=.9, label='98% confidence interval', color='#B22222')
plt.plot(test_X, test_y, label='Prediction')

Shows the following:

This shows that the predicted mean roughly follows the shape of the data, but I would expect that the majority of the blue data points should fall within the red interval, which I believe to show the 98% confidence interval.
Am I wrong in expecting this? Do I need to set different parameters? Increasing the nugget, say to 10, does not make the interval wider, which I also would have expected.
Answer: I am not familiar with gaussian_process in scikit-learn so I cannot comment on tool specific aspects. But some general facts about GP might clarify what is going on.
As in all models, the conclusions (here confidence intervals) only hold if the assumptions (here a GP with a certain kernel and parameters) are met. So maybe your training data is from a process (very) different from the one specified. If this is indeed the case there is no reason for the confidence intervals to hold. You can very this by comparing with training data which is indeed generated according to the assumptions of the GP.
One possible reason why the confidence intervals do not change if you change the nugget size is that the predicition intervals are for the latent function itself, not for its noisy observations. This can be verified by looking into the documentation of gp.predict.
Many tools determine kernel parameters including the nugget by maximising likelihood. So maybe your initial value for the nugget size was just used as a start value and modified in the fitting process. Again you can check this with the documentation or from available diagnostic information.
Comment: I now don't think these points apply to the problem I had, but I think they're good points in general anyway, so +1
Answer: As far as I can see, the confidence interval that you are calculating is for the mean, not for the actual values, so the points do not need to fall inside it.
Comment: After reading more on it I think this was indeed my main misunderstanding, aided by my misunderstanding of the meaning of the CI. Reading http://link.springer.com/article/10.3758%2Fs13423-015-0947-8 gave me a much better grasp, and that my misunderstanding is unfortunately not uncommon.
Comment: Interesting -- can a similar process by used for getting CI for binomial predictions (p in range 0..1)
Answer: This will be an answer for scikit-learn 0.18, but I assume something similar was happening in 0.17.
Even though you can get an impression from docs that hyperparameters are optimized automatically, if you keep a default kernel (1.0 * RBF(1.0)), its hyperparameters (=length_scale) are not optimized and neither is noise hyperparameter (=alpha). And since alpha is by default 1e-10, width of the confidence interval will be zero.
To get hyperparameters optimize automatically you have to specify a kernel, e.g. 1.0 * RBF(length_scale=1) + 1.0 * WhiteKernel(). After fitting GP, calling gp.kernel_ returns optimized hyperparameters, e.g. 0.979**2 * RBF(length_scale=1.79) + 0.339**2 * WhiteKernel(noise_level=0.115).
Here's a full example
from sklearn import gaussian_process
from sklearn.gaussian_process.kernels import RBF, WhiteKernel
import matplotlib.pyplot as plt

kernel = 1.0 * RBF(length_scale=1) + 1.0 * WhiteKernel()

X = np.linspace(0, 2 * np.pi, 100)[:, np.newaxis]
y = np.sin(X)[:, 0] + 0.1 * np.random.randn(100)
gp = gaussian_process.GaussianProcessRegressor(kernel=kernel)
gp.fit(X, y)
pred_y, sigma = gp.predict(X, return_std=True)
confidence_interval = sigma * 1.96

plt.scatter(X, y, label='Train data')
plt.fill_between(X[:, 0],
                 pred_y - confidence_interval,
                 pred_y + confidence_interval,
                 alpha=.9, label='98% confidence interval', color='#B22222')
plt.gca().set_title(gp.kernel_)
plt.plot(X, pred_y, label='Prediction')


Problem in the penalty mark on a football field

Question: In a football field, there is a hole in the penalty mark.
Can we change the mark by moving the ball parallel with the goalline from the original mark?
Comment: This question was downvoted and I don't see why. It seems entirely reasonable that the laws could allow a player to move the ball out of the hole (even though that's not the case).
Comment: Could you clarify when this is occurring - during regular play or during a penalty shoot-out ("kicks from the penalty mark")?
Comment: Hi NIMISHAN. There are two answers to this question that both outline why this is not allowed. If you have problems with these answers, please consider leaving comments explaining why. If not, please consider accepting an answer by selecting the tick on the left.
Answer: No, this is not allowed.
From IFAB Laws of the Game, Law 14.1, The Penalty Kick - Procedure:

The ball must be stationary on the penalty mark.

For free kicks, referees may allow players to move the ball out of a small hole, as there is always some uncertainty as to where the offence occurred and hence where the restart location should be. For penalty kicks, this is not possible, as it explicitly states in the Laws where the ball must be placed.
However, it does not state that the centre of the ball has to be placed exactly over the centre of the mark. If part of the ball is overhanging the mark ,that should be sufficient for the referee (this is consistent with principle applied elsewhere in the Laws that the ball is considered on a line even if only part of the ball is on that line). This allows players plenty of latitude to avoid small holes in the penalty mark.
If the hole is so large that a penalty kick cannot be legally taken with the ball out of the hole, it is clearly dangerous and has potential to cause injury to players who may trip over it or roll their ankle in it. The referee should suspend the match until the ground officials (which at a local level, could be the home team) have filled the hole with sand or dirt and have re-marked it.
The referee does not explicitly have the power to suspend the match due to the field being in a state of disrepair, but it is implied in Laws 1 and 5.
Law 1.2, The Field of Play - Field Markings:

The field of play must be rectangular and marked with continuous lines which must not be dangerous.

Law 5.6, The Referee - Liability of Match Officials:

... decisions may include a decision:

that the condition of the field of play or its surrounds or that the weather conditions are such as to allow or not to allow a match to take place

Finally, if this is not a penalty kick during the match, but part of kicks from the penalty mark at the end of a drawn match in in order to determine a winner, the referee may simply move the penalty kicks to the other end of the field.
Law 10.3, Determining the Outcome of a Match - Kicks From the Penalty Mark:

Unless there are other considerations (e.g. ground conditions, safety etc.), the referee tosses a coin to decide the goal at which the kicks will be taken which may only be changed for safety reasons or if the goal or playing surface becomes unusable
Answer: The answer is NO.
To be precise this particular case is not treated but the regulation is clear, and is it often possible to view the striker to arrange the mark area with the shoe to make it easier for the shot
From the FIFA rules of the game about position of the ball (law 14 at page 45) says
The ball:
• must be placed on the penalty mark
The player taking the penalty kick:
•   must be properly identified
The defending goalkeeper:
• must remain on his goal line, facing the kicker, between the goalposts until the ball has been kicked.
The players other than the kicker must be located:
•   inside the field of play
•   outside the penalty area
•   behind the penalty mark
•   at least 9.15 m (10 yds) from the penalty mark
Comment: Please avoid using the old FIFA Laws of the Game. The newer [IFAB Laws of the Game](http://theifab.com/#!/laws) are much easier to reference to and contain changes from the previous years.
Comment: I have no idea exactly what the majority of your first paragraph is trying to say, but it doesn't seem relevant to whether the ball can be moved. Plus as studro mentioned, this is something brought up on meta some months ago.
Comment: Then how to over come such situation? Considering time and  less resources in the field to recover the issue.


Python find indices in an array based on conditions

Question: I have a list/array(can make them arrays, doesn't matter):
array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]

I would like to create a new_array FROM array which looks like this:
new_array = [[1,2],[1],[1,2,3],[1,2],[1,2,3],[1,2,3,4,5]]

The logic I came up with was to start with a=0,b=1 and loop through array, when the value of array[a] < array[b], then a+=1,b+=1, but if the value of array[a]>=array[b], then append the value of b:
Here is what I tried:
index_1 = [] # This will be the list which will have the FIRST index
index_2 = [] # This will be the list which will have the SECOND index
a = 0
b = 1
for i in array:
    if i[a] = i[b]:
        index_2.append(b)
        index_1.append(a)

So index_1 will have the first index and index_2 will have the second index and then, we can create the new_array by:
new_array = [(array[start:end]) for start,end in zip(index_1,index_2)]

Unfortunately, even though my idea is correct, the loop stops in if i[a]<i[b] because of IndexError: invalid index to scalar variable..
Comment: @juanpa.arrivillaga: doesn't matter, I can make them `numpy arrays` or let them be `lists`.
Comment: Those are not arrays.
Comment: You can just try to `print(i)`, what do you get? So actually, what you need is `for _ in range(len(array))`, `array[a]` and `array[b]`. `i` is already an item inside `array`.
Comment: Of course it matters. Not only are the performance characterstics different, you can't really have staggered arrays in `numpy`, as your `new_array` implies
Comment: @Sraw: so you mean I have to modify my `for` loop ? If so how ? Is everything else correct ?
Comment: I think you can simply have a loop that starts over when a 1 is approached.
Answer: We can go through the array directly by the elements (more precisely by a pair of neighboring elements) simultaneously forming the result.
We initialize our result with a sublist of the 1st element of the array and then:

 if the next element is greater than the previous one, we add it to the last sublist
 otherwise -  we add a new sublist with this element to the result.
array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]

new_array = [[array[0]]] # assume the array is not empty
for a,b in zip(array, array[1:]):
    if a<b:
        new_array[-1].append(b)
    else:
        new_array.append([b])

print(new_array)
# [[1, 2], [1], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]

UPD.
Alternatively, we can implement it with indices.
(based on this answer by @fortran)
First, prepare indices to split the array:
idxs_to_split = [idx for idx in range(1, len(array)) if not array[idx-1] < array[idx]]
pairs = zip([0] + idxs_to_split, idxs_to_split + [None])

Second, implement the splitting itself:
new_array = [array[i:j] for i,j in pairs]

print(new_array)
# [[1, 2], [1], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]

Comment: `list[-1]` refers to the last element in a list, in this case it is another list, to which we append
Comment: @ThePredator, `new_array[-1]` is the current last sub-list of the result.
In the above expression, we append `b` to it
Comment: @ThePredator, `a` is needed for the comparison, which determines how exactly we will add `b` to the result
Comment: Can you explain ?
Comment: What does `new_array[-1].append(b)` do? I can understand `new_array.append([b])`
Comment: When we are zipping, where are we using `a` ? I only see that we append `b`.
Answer: Here's a simple approach, just keep track of the previous, and check if it is smaller. If it is, append to sub. If it isnt, append sub to the new list, then create a new sub. Always append sub at the end.
>>> new_array = []
>>> array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]
>>> sub = []
>>> prev = float('-inf')
>>> for x in array:
...     if prev < x:
...         sub.append(x)
...     else:
...         new_array.append(sub)
...         sub = [x]
...     prev = x
...
>>> new_array.append(sub)
>>> new_array
[[1, 2], [1], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]

Answer: def g(l):
    acc = []
    for i in l:
        if not acc:
            acc.append(i)
        else:
            if acc[-1] < i:
                acc.append(i)
            else:
                yield acc
                acc = [i]
    yield acc

Here's a generator version that should work for all iterables. Usage would be something like
list(g(array))

And gives the desired
[[1, 2], [1], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]

Answer: I simply checked for two conditions, 1. If next element == 1  and 2. If j is at last element of the list.
array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]
newList = []

j = 0
k = 0
while j < len(array)-1:
    if array[j+1] ==1:
        newList.append(array[k:j+1])
        k = j+1
    if  j+2==len(array):
        newList.append(array[k:j+2])
        k = j + 1
   j +=1
print newList

Comment: The above was just a minimal example. I have different floating values in my original list. So I cannot use the condition of `==1`. Thanks for your effort anyway.
Comment: Okay. I hope you get the answer. Happy solving :)


Converting a str to a dict

Question: I have a record in a field in the database with this format:
'(1, 7.0), (2, 2.0), (3, 3.0), (4, 4.0), (5, 7.0), (6, 6.0), (7, 8.0), (8, 5.0)'

That comes as a string, I need to convert the 1st position to an int and the 2nd position to a float, then do something like this:
OrderedDict(sorted(d.items(), key=lambda x:x[1], reverse=True))

and sort the results.
Any ideas on how to convert this str value into a dict so the final result looks like this?
OrderedDict([(7: 8.0), (5: 7.0), (1: 7.0), (6: 6.0), (8: 5.0), (4: 4.0), (3: 3.0), (2: 2.0)])

Answer: You can use 'eval function' to evaulate your string data then you can use your tuple data to convert into dictionary data
str_val = '(1, 7.0), (2, 2.0), (3, 3.0), (4, 4.0), (5, 7.0), (6, 6.0), (7, 8.0), (8, 5.0)'
tuple_val = eval(str_val)
mydict = { x[0]:x[1] for x in tuple_val }
Comment: Are you sure? You can try it..
Comment: Ah you're right, just tested, my bad! You can do `mydict = dict(tuple_val)` btw.
Comment: Have a good day :)
Answer: You can use a regular expression to extract values:
import re
from collections import OrderedDict

s = '(1, 7.0), (2, 2.0), (3, 3.0), (4, 4.0), (5, 7.0), (6, 6.0), (7, 8.0), (8, 5.0)'

d = OrderedDict()

result = re.findall(r'[-.0-9]+, [-.0-9]+', s)

for val in result:
    num = val.split(', ')
    first = int(num[0])
    second = float(num[1])
    d[first] = second

print(d) # OrderedDict([(1, 7.0), (2, 2.0), (3, 3.0), (4, 4.0), (5, 7.0), (6, 6.0), (7, 8.0), (8, 5.0)])

Using eval is not safe, when you retrieving data from a database.

Converting a str to a dict


How to add evaluated results to 2d list

Question: I'm trying to make a list of 3d coordinates of a sphere vertices, starting with ((0 0 1) ...) like this:
(defvar spherelatamount 7)
(defvar spherelonamount 8)
(defparameter sphereverticeslist
  (make-list (+ 2 (* (- spherelatamount 2) spherelonamount))))
(setf (elt sphereverticeslist 0) '(0 0 1))

Trying to add next point
(setf (elt sphereverticeslist 1)
      '(0 (sin (/ pi 6)) (cos (/ pi 6))))

this gives result
((0 0 1) (sin (/ pi 6)) (cos (/ pi 6)) ...)

while I need:
((0 0 1) (0 0.5 0.866) ...)

i.e. evaluated sin and cos. How do I achieve that? Thanks.
wrote:
(defvar spherelatamount 7)
(defvar spherelonamount 8)
(defparameter sphereverticeslist
  (make-list (+ 2 (* (- spherelatamount 2) spherelonamount))))
(setf (elt sphereverticeslist 0)
      '(0 0 1))
(setf (elt sphereverticeslist 1)
     '(0 (sin (/ pi 6)) (cos (/ pi 6))))

expecting:
((0 0 1) (0 0.5 0.866) ...)

Comment: If you're going to be indexing the collection numerically, it's generally better to use an array rather than a list.
Answer: Quoting the list prevents evaluation of everything in the list, so you just insert the literal values.
Call list to create a list with evaluated values.
(setf (elt sphereverticeslist 1) (list 0 (sin (/ pi 6)) (cos (/ pi 6))))

If you have a mixture of literal and evaluated values, you can use backquote/comma.
(setf (elt sphereverticeslist 1) `(0 ,(sin (/ pi 6)) ,(cos (/ pi 6))))

Comment: @adabsurdum: I don't think backquoted forms are literals in general.  Certainly the spec does not say they are that I can see.  Obviously `\`(x)` is allowed to be, but I would be astonished if, in `(let ((x (cons 1 1))) \`(,x ,x))` I think it would be very surprising if the value was a literal.  Indeed, if it was the case that backquote always made literals, then any object can be made literal by `\`,`.
Comment: @ignisvolens -- After looking through the CL spec it is clear that you are right about this. I guess I was remembering that _Scheme_ implementations are allowed to transform, e.g., `(let ((x (cons 1 1))) \`(,x ,x))` into `(quote ((1 . 1) (1 . 1)))`.
Comment: @adabsurdum  Can you cite the section of R7RS that allows that? I think it might be allowed if you did `(let ((x '(1 . 1))) ...)` but not if you call `cons`.
Comment: @Barmar -- R7RS and R6RS have almost the same language. From R7RS 4.2.8. Quasiquotation: _"A quasiquote expression may return either newly allocated, mutable objects or literal structure for any structure that is constructed at runtime during the evaluation of the expression."_
Comment: But `x` is not constructed during the evaluation of the quasiquote expression. It's constructed by the let-binding `(x (cons 1 1))`
Comment: @Barmar -- I think that you are right. I guess that Scheme implementations are not allowed to transform `(let ((x (cons 1 1))) \`(,x ,x))` into `(quote ((1 . 1) (1 . 1)))`, but they are allowed to transform `\`(,(cons 1 1) ,(cons 1 1))` this way.
Answer: Instead of using an inefficient 3-element list, consider using a structure to represent the coordinates instead. This stores the values more compactly, gives you O(1) accessors and setters instead of a list's O(N), and lets you use symbolic names for components of the coordinates, leading to more readable code. Something like:
(defstruct sphere (distance 0.0 :type float) ; r
                  (polar-angle 0.0 :type float) ; θ
                  (azimuth 0.0 :type float)) ; φ

;; Make a list of sphere coordinates
(defparameter sphere-vertices-list
  (list (make-sphere :distance 0.0 :polar-angle 0.0 :azimuth 1.0)
        (make-sphere :distance 0.0 :polar-angle (sin (/ pi 6)) :azimuth (cos (/ pi 6)))))

;; Set and get a value
(setf (sphere-distance (first sphere-vertices-list)) 0.5)
(print (sphere-distance (first sphere-vertices-list)))
;; etc.

Answer: You need to evaluate the calls:
(defvar spherelonamount 8)
(defparameter sphereverticeslist
   (make-list (+ 2 (* (- spherelatamount 2) spherelonamount))))
(setf (elt sphereverticeslist 0) '(0 0 1))
(setf (elt sphereverticeslist 1)
      `(0 ,(sin (/ pi 6)) ,(cos (/ pi 6))))

How to add evaluated results to 2d list


Should a rails migration be committed separately from schema.rb?

Question: After generating / writing / running a Rails database migration, should the migration file and db/schema.rb be committed to version control separately?
Answer: Most people I've seen commit them in a single commit.
It makes more sense to me that way. It makes sure that running a rake db:migrate on any revision doesn't modify db/schema.rb.
Comment: Are there any implications in this as far as merge conflicts go?
Answer: Some rake tasks that should be similar may behave differently if schema.rb is not in sync with migrations, for example:
After creating the database, if you run:
rake db:migrate - it'll run the migrations
rake db:schema:load - it'll use the schema.rb
Reseting the database:
rake db:migrate:reset - recreates the database running the migrations one by one.
rake db:reset - recreates the database using current version of schema.rb
In both cases, no matter which task you choose, the result should be the same. Normally, using schema.rb is preferred since it creates the database in one step, instead of passing for every single migration, but if it's not the same version as the migrations the results will be different.


DateTimeFormatterBuilder parsing days below 31 SMART not STRICT if appendValueReduced is used

Question: Playing in groovyConsole with DateTimeFormatter and DateTimeFormatterBuilder
String inputDateString = "31.2.58" // german date format

dtfIn = DateTimeFormatter
        .ofPattern ( "d.M.uu" )
        .withResolverStyle ( ResolverStyle.STRICT )

dtfIn.parse(inputDateString) // ERROR as expected

...but
// with base range 1937-2034
dtfIn = new DateTimeFormatterBuilder()
       .appendPattern("d.M.")
       .appendValueReduced(ChronoField.YEAR, 2, 2, Year.now().getValue() - 80)
       .parseStrict()
       .toFormatter()

dtfIn.parse(inputDateString) // Result: 1958-02-28

So DateTimeFormatterBuilder with .parseStrict() would parse rather SMART, which DateTimeFormatterBuilder shouldn't do at all but either STRICT or LENIENT (?)'
With day numbers over 31 I'll get an error.
The problem seem to be .appendValueReduced(). Without it I'd become an error as expected.
What do I do wrong?
Thanks
Rawi
Answer: DateTimeFormatter from DateTimeFormatterBuilder.toFormatter() is indeed SMART as documented:

The resolver style will be SMART

To obtain STRICT one has to use DateFormatter.withResolverStyle(ResolverStyle)
in this case as follows:
.toFormatter().withResolverStyle(ResolverStyle.STRICT);

Comment: .parseStrict() doesn't work because toFormatter() ignores previous set values and uses his own (SMART).
Comment: Thanks Mikko. Yes, it's working correct this way. Please could you explain, why the method .parseStrict() didn't help? I thought it would tell the DateTimeFormatterBuilder to do just that.


Tem como simplificar minha estrutura? se sim como

Question: Ja consegui resolver essa questao mais queria saber se consigo simplificar ela pois estou no meio de aprendizado e busco diversas formas de fazer a mesma coisa pra poder fixa bem na mente. a questao e a seguinte.
Um estacionamento deseja automatizar a cobrança de mensalistas.
Se o motorista realizou até 20 entradas, ele deve pagar R$ 10,00 por entrada realizada.
Da vigésima primeira entrada em diante, cada entrada custa R$ 5,00 ao cliente.
//array que recebe as placas dos veiculos

var placas = [
    'RXB-2525', 'AKX-3333', 'ORO-7142','RXB-2525', 'AKX-3333', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'ORO-7142', 'ORO-7142',
    'ORO-7142', 'RXB-2525', 'AKX-3333','AKX-3333', 'ORO-7142', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525'
 ]

//aqui calcula quantas vezes essa mesma placa entrou no estacionamento

 function calcularNumeroDeEntradas(placa){

    var entradas = 0

    for(var i = 0;i < placas.length; i++) {

       if (placas[i] == placa){

           entradas++

       }

    }

    return entradas

 }

//aqui calcula o valor que o dono do carro deve pagar, E aqui que queria simplificar o código

 function calcularValorDevido(placa){

   var valorDevido = calcularNumeroDeEntradas(placa)

    if (valorDevido < 21){

       return valorDevido = valorDevido*10

    } else{

       return valorDevido = ((valorDevido-20)*5) + (20*10)

    }

}

 console.log(calcularNumeroDeEntradas("ORO-7142"))

 console.log(calcularValorDevido("ORO-7142"))

Answer: Depende da sua definição de "simplificar", o que é simples para mim pode não ser simples para você, aqui está a minha solução para o seu problema que eu acredito ser "simples".
Você vai precisar de métodos como Math.min que retorna o mínimo entre 2 ou mais elementos passados como parâmetro, que no seu caso caso eu usei com (20, entrada), ou seja se o número de entradas for maior que 20, ele irá retornar 20, se não retorna o número de entradas.
Também usei o método filter que filtra um array em base a função passada como parâmetro e retorna um novo array com todos os elementos que passaram no teste, que neste caso eu usei para criar um novo array com todos os elementos que tem a mesma placa e então eu pego simplesmente o length deste novo array que corresponde ao número de entradas.
Observações:
O array placas pode não existir, então neste caso pode-se adicionar um operador ternario para evitar erros, por exemplo placas.filter vai ter dar erro se placas não existir pois ele irá tentar aplicar o método filter em um valor que não existe, então ficaria assim com o operador placas?.filter neste caso se o array não existir ele irá retornar null e por isso adicionamos um controle ao final da expressão ?? 0 ou seja se o valor a esquerda o filter por algum motivo retornar null, então ele usa 0 como valor default.

var placas = [
    'RXB-2525', 'AKX-3333', 'ORO-7142','RXB-2525', 'AKX-3333', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'ORO-7142', 'ORO-7142',
    'ORO-7142', 'RXB-2525', 'AKX-3333','AKX-3333', 'ORO-7142', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525'
 ];

 function calcDivida(placa){
    // Procura o número de ocorrencias desta mesma placa dentro ao array
    const entrada =  placas?.filter(p => p === placa).length ?? 0;

    // Controlamos se houveram entradas adicionais usando o operador ternario, basicamente um if/else em uma linha como podes ver
    const entradasAdicionais = entrada < 21 ? 0 : entrada - 20;

    // O valor inicial é dado do minimo entre 20 e o número de entradas multiplicado por 10
    const valorInicial = Math.min(20, entrada) * 10;

    // O valor adicional é o valor salvado antes multiplicado por 5
    const valorAdicional = entradasAdicionais * 5;

    // retornamos a soma de ambos
    return valorInicial + valorAdicional;
 }

 console.log(calcDivida('RXB-2525')); // 170
 console.log(calcDivida('AKX-3333')); // 270
Comment: acho que me coloquei errado na questao simplificar, mais era isso mesmo outras formas para fazer a mesma coisa, me ajudou muito tem coisas que preciso me aprofundar mais, e a maneira que você respondeu me mostrou outra maneira de resolver o problema e ate mais bonito de se ver kkkk XD.


WebStorm: no autocomplete for React

Question: On WebStorm website they have screenshot for React autocomplete:

However, it doesn't work on my WebStorm:

I have switched my JavaScript language version to React JSX in Preferences | Languages & Frameworks | JavaScript.
In the file, I right click then selected react-DefinitelyTyped and react-dom-DefinitelyTyped from User JavaScript Library, and it is still not working, neither.
Any advice?
Comment: Try typing `<DiceBut`
Comment: It works!!!!, thanks you!
Answer: Try typing
<Dice

instead of
Dice

The auto-complete is looking for the start of a tag.

WebStorm: no autocomplete for React


jQuery color scaling html table column

Question: I'm working on adding better visualization to data tables that I have so that the highest numbers in that column will have a green CSS background, and the lowest values in that column will have a red CSS background.
I've come pretty far, I am basically down to the last bit. I'm a native PHP dev, so I may be messing up the integer comparison as well as not doing the final jQuery selector correctly. The code selects all the elements in the same column, finds the min and max, calculates the step value, and calculates how many steps above the minimum the current element is. All I need to do now is apply a css class based on the steps. It will be something like values in the 0-5% range will have css group 0, 5-10 will have css group 1, 10-15 group 2, 95-100 group 20. All that css is on the fiddle. I am successfully applying a CSS class, but not to a single cell, it does it for the whole column
<code>$(document).on('click', '#dvData td.color', function() {
    var ndx = $(this).index() + 1;
    //alert('Val of ndx: ' + ndx);
    var thisCol = $('#dvData td:nth-child(' +ndx+ ')');
    var arr = thisCol.slice(1, thisCol.length);

    var columnDataArr = new Array();
    alert("Number of rows: " + arr.length);
        //alert("First Row: " + arr[0].innerHTML);
    for(var i = 0, x = arr.length; i < x; i++){
        columnDataArr[i] = arr[i].innerHTML;
    }
    var colorsArray = ["63BE7B","72C27B","82C77C","91CB7D","A1D07E","B1D47F","C0D980","D0DD81","DFE282","EFE683","FFEB84","FFDE82","FED280","FDC47D","FDB87B","FCAA78","FB9D75","FB9073","FA8370","F9776E","F8696B"];

    var max = Math.max.apply(Math, columnDataArr),
        min = Math.min.apply(Math, columnDataArr),
        range = max - min,
        step_val = range/100;
        alert("Step Value:" + step_val);

    for(var i = 0, x = arr.length; i < x; i++){
        var thisPercentile = parseInt((columnDataArr[i] - min) / step_val);
        alert("Percentile:" + thisPercentile);
        switch ( thisPercentile ) {
            // yes this looks terrible, but i can't seem to get the case to work
            // with: case(thisPercentile) <= 5:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                alert("Below 10th Percentile");
                break;
            case parseInt(90):
                alert("90th Percentile");
                //arr[2].addClass('group10') // doesn't work
                break;
        }
    }
    arr.addClass('group20');
});
</code>
So the two issues are how to deal with passing a Range to a switch statement (or giving up and using ifs and else ifs), and what the correct selector is to target the current table cell. I have my code on jsfiddle.
Answer: Since your value range goes from 0-100 and your group names go from group0-group20, you can do a bit of math and forego the switch/if statement entirely.
If you get the floor of ( value / 5 ), you will end up with 0 for 0-4, 1 for 5-9, ... 19 for 95-99, 20 for 100.
After getting the floor value, you can concatenate the result with the group name and add the result clsas like below:
Note:  You created a vanilla JS array with splice, so you'll need to wrap arr[i] with $( ) to turn it into a jQuery object.
jsfiddle: http://jsfiddle.net/7Luwyyxr/2/
<code>    for(var i = 0, x = arr.length; i < x; i++){
        var thisPercentile = parseInt((columnDataArr[i] - min) / step_val);
        alert("Percentile:" + thisPercentile);

        // added this stuff
        var gnum = Math.floor( thisPercentile/5 );  // returns 0 for 0-4, 1 for 5-9, ...
        //alert("Group Num: " + gnum);
        $(arr[i]).addClass('group'+gnum);  // appends class to array index
    }
</code>
Comment: Beautiful -- yeah, can't believe I missed a $().. actually yes I can. You don't by chance also happen to know how to change this so that it assigns color based on rank, not percent of max?
Comment: Hmm, it should be something similar.  What's the definition of rank?
Comment: For example I might have the dataset 5, 90, 98, 99, 99, I'd want to mark 5 as red, 90 as orange, 98 as yellow, 99's as green -- so 5 would be the bottom or top rank, 99 would be the top or bottom rank, the middle "rank" would be the median
Comment: Would the top rank always get the same color/class?  Also, how many possible values would be in the array?  This should be pretty easy - 1.  sort the array 2.  iteratte through the sorted array and add class.  Done!
Comment: Top rank always means green css. There's always a different number of rows in the column. Yeah that does sound simple when you put it that way
Comment: Cool.  Give it a shot, and if you need help, just update your fiddle and post back.
Answer: This solution will distinguish between 20 different numbers before assigning 2 numbers the same colors. It works with percentile (e.g. the percent of items it is greater than or equal to) to assign a color. The top value would always get the same class, and the lowest would always get the same class. The numbers in between would depend on each other to get a class assigned.
<code>for(var i = 0, x = columnDataArr.length; i < x; i++){
    var greaterThan = 0;
    var curNum = columnDataArr[i];
    for(var j = 0, x = columnDataArr.length; j < x; j++){
        if(curNum <= columnDataArr[j]){
            greaterThan += 1;
        }

    }

    var percentile = Math.round((greaterThan*100)/columnDataArr.length);
    var group = Math.round(percentile/5);
    $(arr[i]).addClass('group'+group);
}
</code>
And heres a fiddle : http://jsfiddle.net/7Luwyyxr/4/

jQuery color scaling html table column


In TCL SCRIPT, how to add dict or array

Question: I am trying to create an output format like keyword start_number last_number on a dictionary or array format.
In my code, I used a dictionary, and the code is:
set keyword_numbers {}
set previous_keyword ""
set last_no 0
foreach item $data {

    regexp {(\w+)\[(\d+)\]} $item -> keyword number
    set new_keyword "$keyword $number"
    puts $keyword
    set key_start_end_data [dict create ]
    if {$keyword == $previous_keyword} {
        if { $number > $last_no } {
            set last_no $number
        } else {
            set last_no $number
        }
        dict lappend key_start_end_data $keyword_data bit_end $last_no

    } else {
        set new_keyword $keyword
        set start_no $number
        set keyword_data [lindex $new_keyword 1]
        dict lappend key_start_end_data $keyword_data bit_start $start_no
    }

puts $key_start_end_data
set previous_keyword $keyword
}

where the input is, or $data consist
BITS_CLK2QDLY[0]
BITS_CLK2QDLY[1]
BITS_DCC_MAIN[0]
BITS_DCC_MAIN[1]
BITS_DCC_MAIN[2]
BITS_DCC_MAIN[3]
BITS_DCC_MAIN[4]
BITS_DCC_MAIN[5]
BITS_DCC_MAIN[6]
BITS_FIXDLY_MAIN[0]
BITS_FIXDLY_MAIN[1]
BITS_FIXDLY_MAIN[2]
BITS_FIXDLY_MAIN[3]
BITS_FIXDLY_MAIN[4]
BITS_FIXDLY_MAIN[5]
BITS_FIXDLY_MAIN[6]
BITS_FIXDLY_MAIN[7]
BITS_NDE_DLY[0]
BITS_NDE_DLY[1]
BITS_NDE_DLY[2]
BITS_NDE_DLY[3]
BITS_NDE_DLY[4]
BITS_NDE_DLY[5]
BITS_NDE_DLY[6]
BITS_NDE_DLY[7]

the expected output is
In dictionary
BITS_DCC_MAIN { bit_end: 0 bit_start: 6 }
BITS_FIXDLY_MAIN { bit_end: 0 bit_start: 7 }
BITS_NDE_DLY{ bit_end: 0 bit_start: 7 }

or in array
BITS_CLK2QDLY [ 0 1 ]
BITS_DCC_MAIN [ 0 6 ]
BITS_FIXDLY_MAIN [0 7 ]
BITS_NDE_DLY[ 0 7 ]

this is what i am getting
{} {bit_start 0}
{} {bit_end 1}
{} {bit_start 0}
{} {bit_end 1}
{} {bit_end 2}
{} {bit_end 3}
{} {bit_end 4}
{} {bit_end 5}
{} {bit_end 6}
{} {bit_start 0}
{} {bit_end 1}
{} {bit_end 2}
{} {bit_end 3}
{} {bit_end 4}
{} {bit_end 5}
{} {bit_end 6}
{} {bit_end 7}
{} {bit_start 0}
{} {bit_end 1}
{} {bit_end 2}
{} {bit_end 3}
{} {bit_end 4}
{} {bit_end 5}
{} {bit_end 6}
{} {bit_end 7}

Can anyone tell me what wrong with this code?
Please mention any doubts related to my question or clarification in the comment section.
Comment: Notice that the question is off topic on ServerFault. I answered it in the hope it will be closed-moved to the StackOverflow where it belongs and where the question and the answer will be valid.
Answer: The first problem is that you print a line of an output for each single line of an input. While, as I understood your problem description, your output will not match the input line-by-line. So, at least the last puts is misplaced; it could be useful for the debug, but not for the output of the final result.
Second, you re-set key_start_end_data every cycle iteration to an empty dict. Probably you wanted to do that only before the cycle or when it switches to a new keyword? Also, before resetting it you have to copy its resulting value to some non-volatile variable, but you didn't used keyword_numbers for anything. Was it intended for collecting the resulting data?
Third, this is wrong use of dict lappend. It is intended to append to a list which is the value of some dict item; you don't have such a structure nowhere and it is not useful for this problem. Probably you wanted to use dict set which replaces the dictionary value for the given key, or creates the key and sets its to the value, if it wasn't exist:
dict set key_start_end_data bit_start $number

Finally, while your code could be debugged, it is too verbose and has too many variables, so it's hard to read and to understand. Compare it with the following:
set keyword_numbers {}

foreach item $data {
    if {[regexp {(\w+)\[(\d+)\]} $item -> keyword number]} {
        if [dict exists $keyword_numbers $keyword] {
            dict set keyword_numbers $keyword bit_start \
                [expr {min([dict get $keyword_numbers $keyword bit_start], $number)}]
            dict set keyword_numbers $keyword bit_end \
                [expr {max([dict get $keyword_numbers $keyword bit_end], $number)}]
        } else {
            dict set keyword_numbers $keyword \
                [dict create bit_start $number bit_end $number]
        }
    }
}

puts $keyword_numbers

if {[regexp ...]} makes it to only process lines that matched the regexp. Just a safeguard. Then, if this keyword already happened before, it replaces its bit_start and bit_end with whatever it should be considering the current line; if the keyword is new, it creates it anew with both subelements set to the number in the current line. This code will successfully process much more relaxed input, for instance, you can shuffle the lines of an input and the result will still be the same. I wrapped long lines; this is not necessary, but just looks better here.
I didn't processed the output to output lines. To do that, instead of last single puts, you can do the following:
dict for {key val} $keyword_numbers {
    puts "$key {$val}"
}

It puts values in TCL's dict format, without colons
Answer: Similar approach to @Nikita, for comparison. Nikita's is better if the numbers may be unordered.
set keyword_numbers {}
foreach item $data {
    if {[regexp {(\w+)\[(\d+)\]} $item -> keyword number]} {
        if {![dict exists $keyword_numbers $keyword]} {
            dict set keyword_numbers $keyword bit_start $number
        }
        dict set keyword_numbers $keyword bit_end $number
    }
}
puts [list $keyword_numbers]

{BITS_CLK2QDLY {bit_start 0 bit_end 1} BITS_DCC_MAIN {bit_start 0 bit_end 6} BITS_FIXDLY_MAIN {bit_start 0 bit_end 7} BITS_NDE_DLY {bit_start 0 bit_end 7}}

In TCL SCRIPT, how to add dict or array