Evaluating $\lfloor (3 + \sqrt{5})^{34} \rfloor \pmod {100}$

Question: The problem is to evaluate $\lfloor (3 + \sqrt{5})^{34} \rfloor \pmod {100}$
No calculators are allowed.
I think I have to get rid of $\sqrt{5}$ somehow since it is irrational and would make it hard to find the floor without doing it numerically. But I'm not sure how.
Comment: I'm willing to bet there's a way to use the fact that $3+\sqrt{5}=2+2\phi=2(1+\phi)=2\phi^2$, where $\phi$ is the golden ratio.
Comment: $0 < X - \sqrt{Y} < 1 \quad \rightarrow \quad \lfloor (X + \sqrt{Y})^n \rfloor = 2{\begin{bmatrix} X & Y \\ 1 & X \end{bmatrix}^n}_{1,1} - 1$
Comment: See also [this closely realted question.](https://math.stackexchange.com/q/48508/242)
Answer: This answer inspired by the Fibonacci sequence, in that $$F_n = \frac{\varphi^n - \overline{\varphi}\,^n}{\sqrt 5} = \frac{\lfloor{\varphi^n}\rfloor + 1}{\sqrt 5} = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^n\,_{2,1}$$
which gives us an easy way to compute $\lfloor \varphi^n \rfloor$ using the above matrix.  So attempting to find a matrix that allows the computation of $\lfloor\phi^n\rfloor$ for $\phi = 3 + \sqrt 5$ and $\overline \phi = 3 - \sqrt 5$, consider the matrix:
$$M = \begin{bmatrix} A & B \\ C & D \end{bmatrix} \tag 1$$
It has characteristic equation
$$P(x) = x^2 - (D+A)x + AD - BC \tag 2$$
which to have the desired eigenvalues, the characteristic equation must also be:
$$P(x) = (x - \phi)(x - \overline \phi) = x^2 - 6x + 4 \tag 3$$
Eliminating variables $C$ and $D$ between (1) (2) and (3), it leaves:
$$M = \begin{bmatrix} A & B \\ \frac{(6-A)A - 4}{B} & 6 - A \end{bmatrix} \tag 4$$
Now any choice of $A$ and $B$ will work, but to choose something easy, consider $A = 3$ to make the diagonal nice and $B=1$ to make the rest nice:
$$M = \begin{bmatrix} 3 & 1 \\ 5 & 3 \end{bmatrix}$$
with the eigenvalue decomposition being:
$$M =
\begin{bmatrix} 1 & 1 \\ \sqrt{5} & -\sqrt{5}\end{bmatrix}
\begin{bmatrix} 3 + \sqrt{5} & 1 \\ 0 & 3 - \sqrt{5}\end{bmatrix}
\begin{bmatrix} 1 & 1 \\ \sqrt{5} & -\sqrt{5}\end{bmatrix}^{-1}
$$
and it follows:
$$M^n =
\begin{bmatrix} 1 & 1 \\ \sqrt{5} & -\sqrt{5}\end{bmatrix}
\begin{bmatrix} \phi^n & 1 \\ 0 & \overline \phi\,^n\end{bmatrix}
\begin{bmatrix} 1 & 1 \\ \sqrt{5} & -\sqrt{5}\end{bmatrix}^{-1}
=
\begin{bmatrix}
   \frac{\phi^n + \overline \phi\,^n}{2} & \frac{\phi^n + \overline \phi\,^n}{2\sqrt{5}} \\
   \frac{\phi^n + \overline \phi\,^n}{2 / \sqrt{5}} & \frac{\phi^n + \overline \phi\,^n}{2} \\
\end{bmatrix}
$$
So we have $2 M^n\,_{1,1} = \phi^n + \overline \phi\,^n = \lfloor \phi^n \rfloor + 1$, altogether:
$$\lfloor(3 + \sqrt{5})^n\rfloor = 2 {\begin{bmatrix} 3 & 1 \\ 5 & 3 \end{bmatrix}^n}_{1,1} - 1$$
$$\downarrow$$
$$\boxed{\lfloor(3 + \sqrt{5})^n\rfloor \equiv 2 {\begin{bmatrix} 3 & 1 \\ 5 & 3 \end{bmatrix}^n}_{1,1} - 1 \pmod{100}} \tag{!!}$$
Matrix modular exponentiation with integers is fairly easy to do with repeated squaring:
$$
\begin{array} {|c|c|} \hline
M^1                  & \begin{bmatrix} 3 & 1 \\ 5 & 3 \end{bmatrix} \\ \hline
M^2 = (M^1)^2        & \begin{bmatrix} 14 & 6 \\ 30 & 14 \end{bmatrix} \\ \hline
M^4 = (M^2)^2        & \begin{bmatrix} 76 & 68 \\ 40 & 76 \end{bmatrix} \\ \hline
M^8 = (M^4)^2        & \begin{bmatrix} 96 & 36 \\ 80 & 96 \end{bmatrix} \\ \hline
M^{16} = (M^8)^2       & \begin{bmatrix} 96 & 12 \\ 60 & 96 \end{bmatrix} \\ \hline
M^{32} = (M^{16})^2    & \begin{bmatrix} 36 & 4 \\ 30 & 36 \end{bmatrix} \\ \hline
M^{34} = M^{32}M^2     & \begin{bmatrix} 24 & 70 \\ 60 & 24 \end{bmatrix} \\ \hline
\end{array}$$
So $\lfloor(3 + \sqrt{5})^{34}\rfloor \equiv 2 \times 24 - 1 \equiv 47 \pmod{100}$

It seems that using the above style argument, one can say in general that if $0 < X - \sqrt{Y} < 1$, then the follow identity holds:
$$\lfloor (X + \sqrt{Y})^n \rfloor = 2{\begin{bmatrix} X & Y \\ 1 & X \end{bmatrix}^n}_{1,1} - 1$$
I wonder if this is a well known identity?
Answer: Hint: Add $(3 - \sqrt{5})^{34}$ inside of the floor brackets and use the binomial theorem to cancel terms. Keep in mind that $0 < (3 - \sqrt{5})^{34} < 1$.

Solution:
By the binomial theorem and following the hint, we have that $\lfloor (3 + \sqrt{5})^{34} + (3 - \sqrt{5})^{34} \rfloor = \lfloor 2(3^{34} + {34 \choose 2} 3^{32} 5^1 + {34 \choose 4} 3^{30} 5^2 + \dots {34 \choose 4} 3^{4} 5^{15} + {34 \choose 2} 3^2 5^{16} + 5^{17}) \rfloor$.
The inside is an integer, so we can drop the floor brackets.
Claim: The sum of the terms not including the first two is divisible by $100$.
Proof: We find that ${34 \choose 2n}$ is even for $2 \leq n \leq 8$ by hand checking. We get a second factor of two from the $2$ multiplying it on the outside. Then all terms with at least $5^2$ as a factor and an even binomial coefficient are divisible by $100$. This deals with all of the terms except for the last two. We can see that the last two terms (without being multiplied by the external $2$) are both odd, so their sum is even. They both have at least $5^2$ as a factor so when they are multiplied by the external $2$ their sum is divisible by $100$.
So, we examine the first two terms. Note that $3^4 \equiv 81, 3^8 \equiv 61, 3^{12} \equiv 41, 3^{16} \equiv 21, 3^{20} \equiv 1 \pmod {100}$. So $3^{34} \equiv 69 \pmod {100}$ and $3^{32} \equiv 41 \pmod {100}$. So we have $2(69 + 33 * 17 * 41 * 5) \equiv 2(69 + 61 * 5) \equiv 48 \pmod {100}$
So we have that $(3 + \sqrt{5})^{34} + (3 - \sqrt{5})^{34} \equiv 48 \pmod {100}$. Since $(3 - \sqrt{5})^{34}$ is between $0$ and $1$, $(3 + \sqrt{5})^{34}$ is between $47$ and $48 \pmod {100}$, so its floor will be $\boxed{47}$.
This is a bit computation heavy, if someone can find a way that is less so I will be happy.
Comment: Nice solution, but it seems like this approach only works because of the nature numbers involved, in that $5 | 100$ and $(3 + \sqrt 5)^{*} < 0$, fairly improbable conditions for a problem of this type with an arbitrary choice of integers.  I would be interested if there was a more generic approach.
Comment: We can reduce the computation/technical arguments by working with $14+6\sqrt{5}$ instead. I have given the full explanation below.
Comment: $$3^{2n}=(3^2)^n=(10-1)^n$$
$$=(-1)^n+\binom n1(-1)^{n-1}10+\binom n2(-1)^{n-2}10^2+\cdots+10^n$$
$$\equiv (-1)^{n-1}(10n-1)\pmod{100}$$
Answer: We use a slight modification of Michael T's approach which reduces the necessary computation and number of technical arguments . Note that $(3+\sqrt{5})^2=14+6\sqrt{5}$. Now $$\lfloor(3+\sqrt{5})^{34}\rfloor=\lfloor(14+6\sqrt{5})^{17}\rfloor=\lfloor(14+6\sqrt{5})^{17}+(14-6\sqrt{5})^{17}\rfloor-1$$
since $14-6\sqrt{5}<1$.
Now expansion with the binomial gives
$$(14+6\sqrt{5})^{17}+(14-6\sqrt{5})^{17}=2(14^{17}+{17\choose 2}14^{15}6^25+{17\choose 4}14^{13}6^45^2+\ldots+6^{16}5^8)$$
Since $6^45^2$ is a multiple of $100$, we immediately find that only the first $2$ terms matter. Note for the following that $7^4=2401\equiv1\pmod{100}$.
First term: $14^{17}2\equiv7^{17}2^{18}\equiv7^{4(4)+1}2^{9(2)}\equiv7(12)^2\equiv7(44)\equiv308\equiv8\pmod{100}$,
Second term: $2(17)(8)14^{15}6^25=10(17)(8)14^{15}6^2$, so we can get rid of a factor of $10$, and
$17(8)14^{15}6^2\equiv(7)2^37^{15}2^{15}2^23^2\equiv2^{20}7^{16}3^2\equiv4^2(1)(-1)\equiv-6\equiv4\pmod{10}$.
Hence the sum of the first two terms is $8+10*4=48 \pmod{100}$, and we conclude that $\lfloor(3+\sqrt{5})^{34}\rfloor\pmod{100}=48-1=47$.


Suggestion for the use of to_number in Oracle

Question: Below query is giving error.
select sob.set_of_books_id, orginfo.org_information1
from
  gl_sets_of_books sob,
  hr_organization_information orginfo
    where
      sob.set_of_books_id = to_number(orginfo.org_information1);

The reason is set_of_books_id is number column and org_information1 is varchar which is containing string and also numeric string. so it is type mismatch. we have to pick only those values which has numeric string in org_information1
to overcome this we used regexp_like which will pick only record which are numeric.
select sob.set_of_books_id, orginfo.org_information1
from  hr_organization_information orginfo, gl_sets_of_books sob
where sob.set_of_books_id = to_number(orginfo.org_information1)
and REGEXP_LIKE(orginfo.org_information1, '^[[:digit:]]+$');

We just added this line and REGEXP_LIKE(orginfo.org_information1, '^[[:digit:]]+$'); in previous query and it is working properly.
My question is even in the last query, we are using where clause and joining same condition which was failing in first query .and where will definetly run before AND. so why it is not failing? will it fail for some records? or the query is proper?
is there any better way to use the second query.
if I am not using existing where condition then it is giving error. i dont know what is the issue with below query.

We can not use to_char on set_of_books_id because that column contains index.and we cant modify to create func based index.We have to use index in our query
Comment: _cant modify to create func based index_ - why can't you create a function-based index?
Comment: Hi Chris, Because table does not belongs to us. It is an EBS application in which one table is accessed by many application. They refused to create func based index. we are using their table for dashboard.
Answer:
where will definitely run before and

No!
The optimizer is free to rearrange the conditions in your query. There's no guarantee it processes these top-to-bottom. This can lead to surprising effects if the plan changes (e.g. because you added/removes indexes).
Assuming you're on 12.2 or higher, instead of a regex you can use the on conversion error clause to map all the non-numeric values to null:
with rws as (
  select level x,
         case mod ( level, 3 )
           when 0 then chr ( level+64 )
           else to_char ( level )
         end y
  from   dual
  connect by level <= 10
)
  select y from rws
  where  x = to_number ( y default null on conversion error );

Y
1
2
4
5
7
8
10

Comment: Hi Chris, I am using 11g. also i can not use to_char(sob.set_of_books_id) on this because this column contain index on underlying table. so we want to use index.  can we somehow modify above query and make it sure that it wont fail in any circumstances?
Answer: Use TO_CHAR on the number column rather than TO_NUMBER on the string column:
select sob.set_of_books_id,
       orginfo.org_information1
from   hr_organization_information orginfo
       INNER JOIN gl_sets_of_books sob
       ON ( TO_CHAR(sob.set_of_books_id) = orginfo.org_information1 );

Or, from Oracle 12.2, you can use TO_NUMBER(value DEFAULT NULL ON CONVERSION ERROR):
select sob.set_of_books_id,
       orginfo.org_information1
from   hr_organization_information orginfo
       INNER JOIN gl_sets_of_books sob
       ON ( sob.set_of_books_id
            = TO_NUMBER(orginfo.org_information1 DEFAULT NULL ON CONVERSION ERROR)
          );

Comment: _"We can not use to_char on set_of_books_id because that column contains index."_  The index does _not_ prevent you from using the function.  It simply means that if you use the function, the index cannot be used to help resolve the query, probably leading to a full table scan.  But this may not necessarily be a problem, either.  Have you actually _tired_ it?
Comment: We can not use to_char on set_of_books_id because that column contains index.and we cant modify to create func based index
Comment: Hi, Actually earlier condition was to_char(set_of_books_id)=org_information1 . perf team suggested us to change and use to_number so that index can be used. now index can be used but we are getting that conversion issue because org_information1 contains alphanumeric string as well. so converting them into number will throw error.


Flutter camera package permission for the app targeting Android api version 30

Question: I have recently upgraded one of my flutter app's target sdk to 30. And after that, taking photo isn't working. Its showing the error mentioned in this link.
PlatformException(no_available_camera, No cameras available for taking pictures., null).
It's something to do with the package visibility changes in android 11. Now there have a solution in the first link which is adding a permission for querying all packages installed in the device by adding the following line in the manifest file.
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

Which works for me. But the documentation of query_all_packages says the following:
In the vast majority of cases, however, it's possible to fulfill your app's use cases by
interacting with the set of apps that are visible automatically and by declaring the other
apps that your app needs to access in your manifest file. To respect user privacy, your app
should request the smallest amount of package visibility necessary in order for your app to
work.

In an upcoming policy update, look for Google Play to provide guidelines for apps that need
the QUERY_ALL_PACKAGES permission.

That means, just for the camera app we shouldn't ask for the permissions for all installed packages. Now my question is, what should be added in the android manifest for camera package visibility? Thanks in advance.
Answer: If your app is targeting Android 11(API level 30) or higher, the system makes some apps visible to your app automatically, but it hides other apps by default.
The details can be found here,
https://developer.android.com/training/basics/intents/package-visibility
For using the camera to take a photo in API level 30 and higher devices you need to add this below lines to your manifest,
<queries>
  <intent>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
  </intent>
</queries>


Regex expression for the date format

Question: I need the regex expression that will find/match below formats
1st July 2018 , July 2nd 2018 , 4th March ,2018
(with , and without comma also either dd/mmm/yyyy and mm/dd/yyyy)
I have below regular expression , that is working fine here
The regular expression is
(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*?\s+\d{1,2}(?:[a-z]{2})?(?:\s+|,\s*)\d{4}\b

But it is not working with my code , I dont know what I am placing wrong ...
My code is
Regex regex = new Regex(@"(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*?\s+\d{1,2}(?:[a-z]2})?(?:\s+|,\s*)\d{4}\b");
Match match = regex.Match(html);
return match.Value;

Also the above expression is responsible for
January 1st, 2020 But I need also 1st January  2020 ..
Comment: Please remove all spaces in the pattern, and retry.
Comment: @JotSingh why use a regex at all? Why not `DateTime.TryParse` or `DateTime.TryParseExact` ? If you want it to validate DTO values you can specify the type, format using Data annotation attributes
Comment: @WiktorStribiżew tried after removing space but no luck..
Comment: Please edit the question.
Comment: I edited the question and removed spaces , or you need else editing ? @WiktorStribiżew
Comment: Note you tested on a Web site where ignore case is on, you did not use `RegexOptions.IgnoreCase`. [This one](http://regexstorm.net/tester?p=%28%3f%3a%5cd%2b%5ba-z%5d*%5cs*%29%3f%28%3f%3ajan%7cfeb%7cmar%7capr%7cmay%7cjun%7cjul%7caug%7csep%7coct%7cnov%7cdec%29%5ba-z%5d*%5cs*%2c%3f%5cs*%28%3f%3a%28%3f%3a%5cd%2b%5ba-z%5d*%5cs*%29%3f%5cs*%2c%3f%5cs*%29%3f%5cd%7b4%7d%5cb&i=1st+July+2018+%2c+July+2nd+2018+%2c+4th+March+%2c2018+%28with+%2c+and+without+comma+also+either+dd%2fmmm%2fyyyy+and+mm%2fdd%2fyyyy%29+January+1st%2c+2020+But+I+need+also+1st+January+2020&o=i) can work, but not for `dd/mm/yy`.
Comment: @WiktorStribiżew this ignore case is working , It recognizes January 2nd, 2012
I also have to add rule to recognize 2nd January, 2012 . is that feasible ?
Comment: Sir @WiktorStribiżew can we combine these two rules so we can have all matches
Comment: See [this demo](http://regexstorm.net/tester?p=%28%3f%3a%5cd%2b%5ba-z%5d*%5cs*%29%3f%28%3f%3ajan%7cfeb%7cmar%7capr%7cmay%7cjun%7cjul%7caug%7csep%7coct%7cnov%7cdec%29%5ba-z%5d*%5cs*%2c%3f%5cs*%28%3f%3a%28%3f%3a%5cd%2b%5ba-z%5d*%5cs*%29%3f%5cs*%2c%3f%5cs*%29%3f%5cd%7b4%7d%5cb&i=1st+July+2018+%2c+July+2nd+2018+%2c+4th+March+%2c2018+%28with+%2c+and+without+comma+also+either+dd%2fmmm%2fyyyy+and+mm%2fdd%2fyyyy%29+January+1st%2c+2020+But+I+need+also+1st+January+2020%0d%0a2nd+January%2c+2012&o=i).
Comment: @WiktorStribiżew Yes I have gone through this already..  Its not working for the case of JULY 1ST 2018 .. Can we place a condition where we can get both of these date formats
Comment: It does work for [`JULY 1ST 2018`](http://regexstorm.net/tester?p=%28%3f%3a%5cd%2b%5ba-z%5d*%5cs*%29%3f%28%3f%3ajan%7cfeb%7cmar%7capr%7cmay%7cjun%7cjul%7caug%7csep%7coct%7cnov%7cdec%29%5ba-z%5d*%5cs*%2c%3f%5cs*%28%3f%3a%28%3f%3a%5cd%2b%5ba-z%5d*%5cs*%29%3f%5cs*%2c%3f%5cs*%29%3f%5cd%7b4%7d%5cb&i=1st+July+2018+%2c+July+2nd+2018+%2c+4th+March+%2c2018+%28with+%2c+and+without+comma+also+either+dd%2fmmm%2fyyyy+and+mm%2fdd%2fyyyy%29+January+1st%2c+2020+But+I+need+also+1st+January+2020%0d%0a2nd+January%2c+2012%0d%0aJULY+1ST+2018&o=i).
Comment: @WiktorStribiżew and for 1ST JULY 2018 as well ??
Comment: I understand it is not very safe, but [this one](http://regexstorm.net/tester?p=%28%3f%3a%5cd%2b%5ba-z%5d*%5cs*%28%3f%3aof%5cs%2b%29%3f%29%3f%28%3f%3ajan%7cfeb%7cmar%7capr%7cmay%7cjun%7cjul%7caug%7csep%7coct%7cnov%7cdec%29%5ba-z%5d*%5cs*%2c%3f%5cs*%28%3f%3a%28%3f%3a%5cd%2b%5ba-z%5d*%5cs*%29%3f%5cs*%2c%3f%5cs*%29%3f%5cd%7b4%7d%5cb&i=1st+July+2018+%2c+July+2nd+2018+%2c+4th+March+%2c2018++January+1st%2c+2020+But+I+need+also+1st+January+2020%0d%0a2nd+January%2c+2012%0d%0aJULY+1ST+2018%0d%0a1ST+JULY+2018%0d%0a1ST+JULY+2018++1STJULY+%2c2018+1ST+++JULY%2c+2018++1ST+of+JULY%2c2018&o=i) matches them.
Comment: @WiktorStribiżew working very fine thanks sir , Also if I have to change the format of this detected date to specific format 07/01/2018 (in numeric). Is that possible ?
Comment: @PanagiotisKanavos will it search for the date of any format as well from the given string ?
Comment: @JotSingh the regex won't parse French or German or Greek month names anyway. The parsing functions can do so though, if you pass the appropriate CultureInfo. `TryParseExact` at least has an overload that allows you to specify *multiple* formats
Comment: @JotSingh btw that regex can't tell between MM/DD and DD/MM. You *can't* distinguish between April 7th and July 4th just by looking at the text, you *have* to know the culture. Why do you want to use that regex though? Whether for parsing or validation there are better ways. Perhaps you should use a date picker on the client so you only get ISO dates on the server
Comment: @I Have paragraph in which date is of format 1st june 2018 I have to detect it and convert to 06/01/2018 . thats all I need
Comment: This is how you can specify what you expect at the input and then output a date time object: https://msdn.microsoft.com/en-us/library/h9b85w22(v=vs.110).aspx
Comment: @Lukos any example code explained will be helpfull
Comment: There are examples at the bottom of the page I linked!
Answer: Here is C# function which matched and return result true or false for all formats you mention, you can add more date format in formats array also

    public bool checkIsValidDateTime(string date)
        {
            var formats = new[] { "dd MMMM yyyy", "MMMM dd yyyy", "dd MMMM ,yyyy", "MMMM dd, yyyy", "d MMMM yyyy", "MMMM d yyyy", "d MMMM ,yyyy", "MMMM d, yyyy" };

            DateTime dt;

            var replacements = new[]{
             new{Find="st",Replace=""},
             new{Find="nd",Replace=""},
             new{Find="rd",Replace=""},
             new{Find="th",Replace=""}
              };

            foreach (var set in replacements)
            {
                date = date.Replace(set.Find, set.Replace);

            }

            bool isValid = DateTime.TryParseExact(date, formats, null, DateTimeStyles.None, out dt);

            return isValid;
        }

        // which return true for following formats

            string input = "1st July 2018";
            string input2 = "July 2nd 2018";
            string input3 = "3rd March ,2018";
            string input4 = "January 4th, 2020";
            string input5 = "20th January 2020";

     bool isvalid1 = checkIsValidDateTime(input); // result true
     bool isvalid2 = checkIsValidDateTime(input2); // result true
     bool isvalid3 = checkIsValidDateTime(input3); // result true
     bool isvalid4 = checkIsValidDateTime(input4); // result true
     bool isvalid5 = checkIsValidDateTime(input5); // result true
Comment: Dude code works very well , I appreciate the code writing style . But I have to  match the date from paragraph that is main stream

Regex expression for the date format


Enforcing redaction and/or redact updates in mongodb

Question: In Accumulo, you can enforce what records are returned by the Authorizations and/or server side iterators.
What mechanisms are in place to enforce this type of security in MongoDb?  I know that there is the $redact command for aggregation pipelines, but that relies on the user to restrict their own queries.
Also, how could you enforce that someone doesn't do an update on a document they they don't have sufficient privileges to update?
Answer: MongoDB authorization is, as of 2.6, collection-level and controls what operations a user can perform, as opposed to what documents they can see. This means, for example, that if a user has authorization to perform find on a collection, they can see all the documents in the collection and there's no way to hide specific documents from them.
You can enforce update privileges on a collection by withholding the update privileges from the user, for example by giving them just the read role.
There's also a conceptual overview of MongoDB's security model in the security section of the manual.
Comment: If you control simply the database and nothing else, yet are responsible for enforcing document-level restrictions and can't trust or reasonably work with those responsible for the applications where such things would currently be enforced with MongoDB, then the current security model in MongoDB does not seem appropriate for you.
Comment: So basically, the answer is "No, there is no way to control access to data (collection).  It's all or nothing, and at that point you're on your honor to $redact documents you shouldn't see"
Comment: There are absolutely ways to control access to a collection using the role- and privilege-based security model, as I described. If you have read privileges on a collection and are connecting to the database directly, then you will be able to see all parts of all documents in the collection. But, as depicted e.g. [in the `$redact` docs](http://docs.mongodb.org/manual/reference/operator/aggregation/redact/), you can control access to parts of documents using middleware and $redact.
Comment: So like you said - you can't do it *in the DB* (like Accumulo).  You have to have a middleware solution that you have to force onto people.  Example - I have a collection of employee records. I want team leads to be able to see contact info about their subordinates, but I don't want want them to see salary information. HR needs to be able to see everything.  Both would have READ access, but the team leads would be able to see salary info unless I make them go through a proxy.  This doesn't really scale well for generic solutions.
Comment: Both of those groups would access the database through some other application, which could have the appropriate middleware alongside it. It's not a proxy. In terms of scaling, it's just another part of an application built on MongoDB, and scales in the same way. There's nothing especially bad about it.
Comment: Well, sure there is.  I now have to maintain this "some other application" and I need to know the business logic of every application that wants to access my data. If I'm not the one maintaining it, then I'm hoping that they'll do the right thing when fetching documents.  I need to ENFORCE that they are doing the right thing, not just hoping that someone who's maintaining the middleware is properly $redacting the data.  Plus, I have the overhead of the middleware and limiting the API of what the end user can call to whatever the middleware implements.


Family dweller?

Question: I had the weirdest thing; I tried to get a dwellar pregnant but it just wouldn't happen. (both male and female had very high charm). When I zoomed in on the living quarters they kept saying (I'm paraphrasing a bit): "There's nothing like staying at home with family".
I switched the male for a different male and the female was pregnant in under a minute.
Odd, I didn't realize being family was a feature. What purpose does it serve?
Btw: I never bother changing any of the dwellers names, so they had their original generated name, and they weren't the same last names.
Update: The "Can you commit incest" question is sort of similar to mine, but I am still curious as to which purpose it serves, it it moral? It's basically a huge swing fest already anyway.
Comment: Probably to avoid some [weird minded people](http://gaming.stackexchange.com/questions/224031/can-you-commit-incest) to try it. Also it would make more sense that way.
Comment: The children always get their dad's family name, they still might be related if they have a different dad but same mom.
Comment: I see. Do you have any idea why the game bothers tracking the sibling and ancestry?
Comment: The family tracking is very basic - I believe it only applies to direct parents
Comment: I thought children got the name of the parent with the same gender, so daughter's got mother's surname.
Comment: "I am still curious as to which purpose it serves" - that's basically up to the developers. We can't know for sure, so we can't answer this question
Comment: They *usually* get the surname of their father, but *sometimes* of their mother. @JonathanDrapeau
Answer: Well there is no relation between them. Maybe just a bug. My barracks are really breeder rooms. There was one woman and a man at the start. Currently the both named dwellers + 2 of their children are nearly permanently in those room together and are nearly permanently pregnant.
I've had a similar problem for a time, but after closing and restarting (and waiting some minutes) she gets pregnant by her son. ^^
So I'm pretty sure there is no relation between it. On the other hand I've seen this relation too as there was a dweller and someone in the badlands who came together in one room. They said the same thing to each other, even if they are obviously not from the same family.

Family dweller?


Short SQL Server Rank/Grouping Script

Question: I have a simple table of customers.
I need to assign groupIDs to these customers, but the maximum amount of records that can be in a group is @groupMaxVariable.
GroupIds are assigned sequentially. If no customer has over 5 records, then customer groups just get sequential IDs.
Looking for something simple, not some crazy nested cursor.
create table #tempcust
(
    id int,
    name nvarchar(50),
    amount nvarchar(50),
    groupid int
)

insert into #tempcust (id,name,amount)
values
    (1, 'Bob Smith',    '$50.12'),
    (2, 'Bob Smith',    '$12.33' ),
    (3, 'Bob Smith',    '$33.12' ),
    (4, 'Bob Smith',    '$14.86' ),
    (5, 'Bob Smith',    '$6.36' ),
    (6, 'Bob Smith',    '$2.14'),
    (7, 'Bob Smith',    '$10.64'),
    (8, 'Bob Smith',    '$19.14'),
    (9, 'Bob Smith',    '$27.64'),
    (10,    'Bob Smith',    '$36.14'),
    (11,    'Bob Smith',    '$44.64'),
    (12,    'Bob Smith',    '$53.14'),
    (13,    'Jane Doe', '$14.86' ),
    (14,    'Jane Doe', '$6.36'),
    (15,    'Jane Doe', '$2.14'),
    (16,    'Jane Doe', '$10.64'),
    (17,    'Jane Doe', '$19.14'),
    (18,    'Jane Doe','$27.64'),
    (19,    'Jane Doe', '$36.14'),
    (20,    'Kylie Robinson',   '$5.00') ,
    (21,    'Kylie Robinson',   '$6.00' ),
    (22,    'Kylie Robinson',   '$7.00' ),
    (23,    'Kylie Robinson',   '$8.00' ),
    (24,    'Kylie Robinson',   '$9.00' );

The query:
select
    id, name, amount, groupid,
    dense_rank() over (order by i.name) as Rank
from
    #tempcust i

drop table #tempcust

Results:
id          name                                               amount                                             groupid     Rank
----------- -------------------------------------------------- -------------------------------------------------- ----------- --------------------
1           Bob Smith                                          $50.12                                             NULL        1
2           Bob Smith                                          $12.33                                             NULL        1
3           Bob Smith                                          $33.12                                             NULL        1
4           Bob Smith                                          $14.86                                             NULL        1
5           Bob Smith                                          $6.36                                              NULL        1
6           Bob Smith                                          $2.14                                              NULL        1
7           Bob Smith                                          $10.64                                             NULL        1
8           Bob Smith                                          $19.14                                             NULL        1
9           Bob Smith                                          $27.64                                             NULL        1
10          Bob Smith                                          $36.14                                             NULL        1
11          Bob Smith                                          $44.64                                             NULL        1
12          Bob Smith                                          $53.14                                             NULL        1
13          Jane Doe                                           $14.86                                             NULL        2
14          Jane Doe                                           $6.36                                              NULL        2
15          Jane Doe                                           $2.14                                              NULL        2
16          Jane Doe                                           $10.64                                             NULL        2
17          Jane Doe                                           $19.14                                             NULL        2
18          Jane Doe                                           $27.64                                             NULL        2
19          Jane Doe                                           $36.14                                             NULL        2
20          Kylie Robinson                                     $5.00                                              NULL        3
21          Kylie Robinson                                     $6.00                                              NULL        3
22          Kylie Robinson                                     $7.00                                              NULL        3
23          Kylie Robinson                                     $8.00                                              NULL        3
24          Kylie Robinson                                     $9.00                                              NULL        3

(24 row(s) affected)

Expected Result
id  Name    Amount  GroupID
1   Bob Smith   $50.12  1
2   Bob Smith   $12.33  1
3   Bob Smith   $33.12  1
4   Bob Smith   $14.86  1
5   Bob Smith   $6.36   1
6   Bob Smith   ($2.14) 2
7   Bob Smith   ($10.64)    2
8   Bob Smith   ($19.14)    2
9   Bob Smith   ($27.64)    2
10  Bob Smith   ($36.14)    2
11  Bob Smith   ($44.64)    3
12  Bob Smith   ($53.14)    3
13  Jane Doe    $14.86  4
14  Jane Doe    $6.36   4
15  Jane Doe    ($2.14) 4
16  Jane Doe    ($10.64)    4
17  Jane Doe    ($19.14)    4
18  Jane Doe    ($27.64)    5
19  Jane Doe    ($36.14)    5
20  Kylie Robinson  $5.00   6
21  Kylie Robinson  $6.00   6
22  Kylie Robinson  $7.00   6
23  Kylie Robinson  $8.00   6
24  Kylie Robinson  $9.00   6

Comment: Why do the values in your expected results have parentheses around them, but don't in your sample data?
Answer: Within each name, you can readily enumerate the groups using row_number() and some arithmetic.
Then, for each group, find the minimum id and run dense_rank() over the minimum id:
select c.*, dense_rank() over (order by min_grp_id) as groupid
from (select c.*, min(id) over (partition by name, within_name_groupId) as min_grp_id
      from (select c.*,
                   lag(name) over (order by name) as prev_name,
                   (1 + (row_number() over (partition by name order by id) - 1) / 5
                   ) as within_name_groupId
            from tempcust c
           ) c
     ) c;

Here is a db<>fiddle.
Comment: You query give me bad result not what I expected. Please check with online editor of SQL.
Comment: @PrinceKumar . . . I just updated it and it produces your desired results.
Comment: I think you mis-interpret, In expected result I need group ID 1 1 1 1 1 if 5 records with same name and then again start from 2,2 if two same name. Check my expected result in question.
Comment: @PrinceKumar . . . Did you look at the db<>fiddle?  That is what this code does.
Comment: Thanks So much Gordon Linoff, it works like charm. you are the best.

Short SQL Server Rank/Grouping Script


Cannot boot after changing locale

Question: After I changed my locale and rebooted, now I get this error:
[    1.863178] init: mountall main process (223) terminated with status 2
Filesystem check or mount failed.
A maintenance shell will now be started.
CONTROL-D will terminate this shell and continue booting after re-trying
filesystems.  Any further errors will be ignored
Give root password for maintenance
(or type Control-D to continue):

Btw, this is and Amazon AWS EC2 instance, so I don't have access to actually pressing Control-D. I'm stuck here, how can just changing the local result in the instance not being able to boot at all?
Thanks!
Answer: After many hours of debugging the issue, I detached the volume from this instance and attached it as a secondary volume in another instnace I had access to.
It turns out that, by mistake, I entered to quotes at the end of a line in /etc/default/locale file, as follows:
LANG="en_US.UTF-8""

Aparently, this caused the disk not being able to mount. I corrected the file removing the extra quote:
LANG="en_US.UTF-8"

After that, I reattached the volume to the original instance and it booted up!
If anyone could share the technical reason about why a syntax error in the locale file can cause the disk not to mount, it would be much appreciated.
Thanks!
Comment: This article pointed my in the right direction: http://www.samundra.com.np/solved-mount-of-root-filesystem-failed-ubuntu/1239

Cannot boot after changing locale


modifying jquery-ui accordian to display tickers

Question: want to modify the original Accordian from jquery-ui :

to look something like:

but have no idea where to start.....
Answer: At a high level, you have two puzzles.  The first is dynamically changing the content of your accordion header and the second is "ticking ..." that content.
For the first, the first, you can find the heading that contains the accordion text with:
<code>$("#myAccordion").find(".ui-accordion-header")
</code>
You can then dynamically set the text of that element which will change the text of your accordion.
For the ticker, I'd suggest creating a timer and when the timer fires, change the text of the accordion to be your new text.
Comment: thank you. I am needing helpful hints. I am already looking around for some json feeds, like openweather. by the way i do not know who is the guy who is downgrading this Q an A.
Comment: Don't worry about it ... when I vote, I make it a point to always explain my reasoning.  I think if you google around you may find some existing ticker widgets.  I think I saw some on a jQuery plugin exchange.


Importing Existing Sqlite Database in Flutter

Question: I have this Sqlite database file inside the device's 'Documents' folder named backup.db. Now I have written function that should be able to import that database, but I am getting some weird asynchronous errors, not sure what I am doing wrong here, might need to add something. Here's the code for importing the existing file:
    class DbHelper {
  Future initDb() async {
    final dbPath = await ExtStorage.getExternalStoragePublicDirectory(
        ExtStorage.DIRECTORY_DOCUMENTS);
    final path = join(dbPath, '/backup.db');
    final exist = await databaseExists(path);
    if (exist) {
      print('db imported');
      await openDatabase(path);
    } else {
      print('creating copy of database');
      //try {
        //await Directory(dirname(path)).create(recursive: true);
      //} catch (_) {}
      //ByteData data = await rootBundle.load(join(dbPath, '/backup.db'));
      //List<int> bytes =
         // data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
     // await File(path).writeAsBytes(bytes, flush: true);
    }
    await openDatabase(path);
  }
}

As far as I got, it gets the 'documents' directory, merges the path with backup.db file that already exists in Documents, then it should just import it if it exists. Let me know if there is anything I am doing wrong. I am using the ext_storage library to locate the documents folder and sqlite of course to be able to import the database.
And in another screen I call this function like this:
DbHelper dbHelper = new DbHelper();

onTap: () => dbHelper.initDb(),

Here's the stacktrace:
    [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Unable to load asset: /backup.db
E/flutter (22458): #0      PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:225
E/flutter (22458): <asynchronous suspension>
E/flutter (22458): #1      DbHelper.initDb
package:CWCFlutter/db/import_database.dart:21
E/flutter (22458): <asynchronous suspension>
E/flutter (22458): #2      _FoodListState.build.<anonymous closure>
package:CWCFlutter/food_list.dart:919
E/flutter (22458): #3      GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:183
E/flutter (22458): #4      TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:598
E/flutter (22458): #5      BaseTapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:287
E/flutter (22458): #6      BaseTapGestureRecognizer.acceptGesture
package:flutter/…/gestures/tap.dart:259
E/flutter (22458): #7      GestureArenaManager.sweep
package:flutter/…/gestures/arena.dart:157
E/flutter (22458): #8      GestureBinding.handleEvent
package:flutter/…/gestures/binding.dart:362
E/flutter (22458): #9      GestureBinding.dispatchEvent
package:flutter/…/gestures/binding.dart:338
E/flutter (22458): #10     RendererBinding.dispatchEvent
package:flutter/…/rendering/binding.dart:267
E/flutter (22458): #11     GestureBinding._handlePointerEvent
package:flutter/…/gestures/binding.dart:295
E/flutter (22458): #12     GestureBinding._flushPointerEventQueue
package:flutter/…/gestures/binding.dart:240
E/flutter (22458): #13     GestureBinding._handlePointerDataPacket
package:flutter/…/gestures/binding.dart:213
E/flutter (22458): #14     _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter (22458): #15     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (22458): #16     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (22458): #17     _invoke1 (dart:ui/hooks.dart:265:10)
E/flutter (22458): #18     _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5)

updated code
  class DbHelper {
  Future<void> initDB() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String newPath = join(documentsDirectory.path, '/backup.db');
    final exist = await databaseExists(newPath);
    print("This print works.");
    if (!exist) {
      try {
        final dbPath = await ExtStorage.getExternalStoragePublicDirectory(
            ExtStorage.DIRECTORY_DOCUMENTS);
        final path = join(dbPath, '/backup.db');
        File(path).copySync(newPath);
        print("This print doesn't work.");
      } catch (_) {}
    }
  }

  Future<Database> openDB() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, '/backup.db');

    await initDB();
    Database db = await openDatabase(path);

    return db;
  }
}

Comment: Can you add an error to the question?
Comment: I have tried `flutter clean` but still getting the same error, mostly ``asynchronous suspension``
Comment: Post your errors StackTrace
Comment: added StackTrace in original post
Answer: ByteData data = await rootBundle.load(join(dbPath, '/backup.db'));

You are trying to load "/backup.db" from the asset bundle adding dbPath to the key.
You need to add the backup.db to the assets folder if you want to load the database this way.
If you know the location of the .db file and it's not in the assets then why do you need to import it? Just open it.
You can copy the database to another location using File(path).copy(String newPath)
Future<void> initDB() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String newPath = join(documentsDirectory.path, '/backup.db');
    final exist = await databaseExists(newPath);
    if (!exists) {
        try {
            final dbPath = await ExtStorage.getExternalStoragePublicDirectory(
            ExtStorage.DIRECTORY_DOCUMENTS);
            final path = join(dbPath, '/backup.db');
            File(path).copySync(newPath);
        } catch (_) {}
     }
}

Future<Database> openDB() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, '/backup.db');
    await initDB();
    Database db = await openDatabase(path);
    return db;
}

Comment: you should call the function with await -> "await openDB()"
Comment: Yeah, I figured that out. I know the exact path of the file, it is always here: `final dbPath = await ExtStorage.getExternalStoragePublicDirectory(
        ExtStorage.DIRECTORY_DOCUMENTS);
    final path = join(dbPath, '/backup.db');`
Comment: Could you give me the code on how it would work out in my context? I've been trying out to do it this way, but I still get errors, so I'm not sure what I'm doing wrong @Sergey Inshev
Comment: @arbiter I've updated the answer with a code snippet. This code will look for the database within the ApplicationDocumentsDirectory. If not found will copy the database from the ExtStorage.DIRECTORY_DOCUMENTS
Comment: I appreciate your help, let me see how to link it all up and I'll get back to you asap. Just one more question, how would I initialize this function onTap? I suppose I would call openDB()? @Sergey Inshev
Comment: @arbiter call for async openDB() in your onTap. openDB() returns the Future. If you await for openDB(), you'll get a Database object, which you can use to make calls to your database.
Comment: I am still getting `asynchronous` errors when I click the button, not sure what the issue is, but I will figure it out.
Comment: @arbiter please provide a stacktrace
Comment: updated the main code, the thing is, when button is clicked, there is no error, so I tried to debug with print, and only one print fires, and the other one doesn't sort of like the function stops mid way. When clicked, nothing happens.


population - central limit theorem

Question: 27% of all Americans earn 100K+.  In a city of 2000 people, What is the probability that there will be between 500 and 600 earning 100K+.
My solution:
a = (500-2000(.27))/(square root of 2000(.27)(.73)) = -40/square root of 394.2 = -2.01466
b = (600-2000(.27))/(square root of 2000(.27)(.73)) = 60/square root of 394.2 = 3.022
Then I plug in the values into this site:  http://davidmlane.com/hyperstat/z_table.html
and get 0.9768
Is this correct?  Thank you.
Comment: The statement of your question is unclear.  If I interpret your question literally, then exactly 540 people in the group of 2000 earn more than 100K, so the probability that between 500 and 600 people in the group earn more than 100K is exactly 1.  Any probabilistic interpretation of your question requires a rephrasing because I have no indication of whether 2000 represents a randomly selected sample.  If not, then no sample size is indicated.  I also cannot tell if 27% represents a parameter or a sample proportion.
Comment: Thank you for that. I did further clarify my question.
Answer: We may regard the sample as consisting of 2000 independent and identically distributed Bernoulli random variables with individual probability of success $p = 0.27$.  The sum of these is binomial with parameters $n = 2000$ and $p = 0.27$, so let us denote the random number of individuals from the sample that earn more than 100K by $X$. Therefore, the exact probability that $X$ is between $500$ and $600$ is $$\Pr[500 \le X \le 600] = \sum_{x=500}^{600} \binom{2000}{x}(0.27)^x (1-0.27)^{2000-x}.$$  This of course is not an easy sum to calculate without a computer, so we approximate the distribution of $X$ with a suitable normal distribution, say $Y$, whose mean and variance are equal to the mean and variance of $X$, respectively.  Thus $$\begin{align*} {\rm E}[Y] &= np = 2000(0.27) = 540 = \mu, \\ {\rm Var}[Y] &= np(1-p) = 2000(0.27)(1-0.27) = 394.2 = \sigma^2.\end{align*}$$  But since $Y$ is a continuous variable and $X$ is discrete, an appropriate continuity correction is required:  $$\Pr[500 \le X \le 600] \approx \Pr[499.5 \le Y \le 600.5]$$ since the extra $0.5$ on each end will ensure the coverage of the entire probability masses at $X = 500$ and $X = 600$.  Now we standardize $Y$:  $$\Pr[499.5 \le Y \le 600.5] = \Pr\biggl[\frac{499.5 - 540}{\sqrt{394.2}} \le \frac{Y - \mu}{\sigma} \le \frac{600.5 - 540}{\sqrt{394.2}}\biggr].$$  The resulting probability is approximately $0.978162$, which compares favorably to the exact probability, which is $0.97866966408678077689\ldots$.
Note. Your calculation is mathematically consistent, but does not incorporate the continuity correction, which is why the probability you obtained, $0.976774$, is not as good an approximation.
Comment: thank you so much for taking the time.

population - central limit theorem


Getting Error: 'mat-date-range-picker-actions' is not a known element

Question: I want to add action buttons in my material date range picker. So i added this code given in documentation on link below
Documentation link: Date picker actions
<mat-date-range-picker #picker [touchUi]="true"> <mat-date-range-picker-actions> <button mat-button matDateRangePickerCancel>Cancel</button> <button mat-raised-button color="primary" matDateRangePickerApply> Apply </button> </mat-date-range-picker-actions> </mat-date-range-picker>

But when i run application i am getting error: 'mat-date-range-picker-actions' is not a known element
I have also imported MatDatepickerModule and MatNativeDateModule still getting this error. Without action buttons it is working fine,
Comment: Have you tried restarting the server?
Comment: @OwenKelvin Yes. Getting same issue even after restarting server.
Answer: Got it working after updating Angular material version. Since date-picker-action-buttons were added in version 11.1.0 and i was using version 10.2.2
It working after updating material version using these commands
npm install @angular/cdk@latest
npm install @angular/material@latest
npm install @angular/material-moment-adapter@latest

Comment: What is the last line for?


Powershell 2.0 Default Printer Ouput-file Issues

Question: I am trying something that was simple in Powershell 3.0 that I am having a lot of difficulty achieving in Powershell 2.0. I am simply grabbing the default printer of a profile and outputting to a text file in the following format:
default="Microsoft Print to PDF"
In Powershell 3.0 that was done with the following line:
<code>$DefaultPrinter = 'default="' + (Get-WmiObject win32_printer | Where-Object Default -eq $True).Name + '"'
$DefaultPrinter | out-file $File -encoding utf8
</code>
Powershell 2.0, I learned, did not like that at all. So after some trial an error I was able to get something almost like that with:
<code>$DefaultPrinter = Get-WmiObject win32_printer | Where-Object {$_.Default -eq $True} | ft Name
$DefaultPrinter | out-file $File -encoding utf8
</code>
However, the output is now:
Name
----
Microsoft Print to PDF
For the life of me I cannot figure out the right format to get the exact same output as I do in 3.0 (No NAME title and including the "default=" and quotes surrounding the entry). I've tried everything I can think of and found online to no avail. Anyone have any ideas?
Answer: Try this:
<code>$DefaultPrinter = "default=""$((Get-WmiObject win32_printer | Where-Object {$_.Default -eq $True}).Name)"""
</code>
Comment: That did the trick. Thanks! I don't think I would have figured this one out in the timeframe I am working in.


Idiomatic way to return the value of the non-last expression in a sequence of expressions in Clojure

Question: Let's say I have the following function to get numeric values from a byte buffer:
(defn get-from-bytebuffer
  ([^ByteBuffer buffer width endianness]
    (let [buffer-endianness (.order buffer)]
      (.order buffer endianness)
      (cond
        (= width 1) (.get buffer)
        (= width 2) (.getShort buffer)
        (= width 4) (.getInt buffer)
        (= width 8) (.getLong buffer))
      (.order buffer buffer-endianness))))

The user can specify the endianness of the number being read. To keep side effects to a minimum, the function first gets the current byte ordering of the buffer, sets it to the one indicated by the user, then restores the old endianness. The problem with this is that the value of the last expression in the body of the let is the value of the let expression, but I need the value of the cond. More generally, I have some prologue/epilogue code around an expression but I want the result of the expression to be returned (be the value of the enclosing expression.)
The easy workaround that I came up with simply binds the value of the cond in another let expression, then has that as the last expression like so:
(defn get-from-bytebuffer-fix
  ([^ByteBuffer buffer width endianness]
    (let [buffer-endianness (.order buffer)]
      (.order buffer endianness)
      (let [result (cond
                     (= width 1) (.get buffer)
                     (= width 2) (.getShort buffer)
                     (= width 4) (.getInt buffer)
                     (= width 8) (.getLong buffer))]
      (.order buffer buffer-endianness)
      result))))

But this feels kludgy. Does Clojure have an idiomatic/"proper" way of surrounding an expression with some prologue/epilogue code and then returning the value of that expression?
Comment: See also [this](http://stackoverflow.com/questions/17235657/clojure-functions-returning-value-computed-before-the-last-statement) and [this](http://stackoverflow.com/questions/7491360/how-do-you-return-from-a-function-early-in-clojure) previous questions.
Answer: Maybe one of the alternatives below would be acceptable.
(let [buffer-endianness (.order buffer)
      _ (.order buffer endianness)
      result (cond
               (= width 1) (.get buffer)
               (= width 2) (.getShort buffer)
               (= width 4) (.getInt buffer)
               (= width 8) (.getLong buffer))
      _ (.order buffer buffer-endianness)]
  result)

(let [buffer-endianness (.order buffer)]
  (try
    (.order buffer endianness)
    (cond
      (= width 1) (.get buffer)
      (= width 2) (.getShort buffer)
      (= width 4) (.getInt buffer)
      (= width 8) (.getLong buffer))
    (finally (.order buffer buffer-endianness))))

(defn return-nth [n & exprs]
  (nth exprs n))

(return-nth 1
  'exp-0
  'exp-1
  'exp-2)

(nth ['exp-0 'exp-1 'exp-2] 1)

Also note that
(cond
  (= width 1) (.get buffer)
  (= width 2) (.getShort buffer)
  (= width 4) (.getInt buffer)
  (= width 8) (.getLong buffer))

can be written
(case width
  1 (.get buffer)
  2 (.getShort buffer)
  4 (.getInt buffer)
  8 (.getLong buffer))

Answer: You could write it as:
(defn get-from-bytebuffer
  ([^ByteBuffer buffer width endianness]
    (let [buffer-endianness (.order buffer)
          _ (.order buffer endianness)
          result (cond
                   (= width 1) (.get buffer)
                   (= width 2) (.getShort buffer)
                   (= width 4) (.getInt buffer)
                   (= width 8) (.getLong buffer))]
      (.order buffer buffer-endianness)
      result)))

Note the _ in the let form. It simply ignores the return value allowing you to do pretty much anything in that position.
I wouldn't say it's idiomatic and in a way it's 'weird' in that it has side effects in the let binding but I've used this style before in similar scenarios and so far I'm happy with it.
Otherwise I'd have gone the double let approach you've showed.
Hope this helps.
Comment: Not only is this idiomatic, it is the only way I have seen _ used - as a binding for a value that will be ignored. To be clear though, _ is a valid symbol - `(let [_ 1] (inc _))` -> 2.


Produce a sequence $(g_n):g_n(x)\ge 0$ and $\lim g_n(x)\neq 0$ but $\int_{0}^{1} g_n\to 0$

Question:
Produce a sequence $(g_n):g_n(x)\ge 0,\,\forall x\in [0,1],\,\forall n\in\Bbb N$ and $\lim g_n(x)\neq 0,\,\forall x\in [0,1]$ but  $\int_{0}^{1} g_n\to 0$

Im in need to clarify that Im talking of the Riemann integral. I want some hint or example, Im unable to find a sequence like this. My work at this moment:
If the integral converges to zero then:
$$\int_{0}^{1}g_n=\lim_{m\to \infty}U(g_n,P_m)=k_n:(k_n)\to 0$$ I can find $(g_n)$ like these, by example $g_n(x)=x/n$, but Im unable to imagine something that hold at the same time the condition $\lim_{n\to\infty}g_n(x)\neq 0$ for all $x\in[0,1]$.
I tried to thing something related to the Cantor set, the Tomae function or something like this but I dont found something useful yet :/
Comment: @M.T but what about $\lim g_n(x)\neq 0$?
Comment: Hi, by Fatou's lemma, the requirement that $\lim g_n \neq 0 \forall x$ can not be fulfilled.
Comment: Are you working with Riemann integrals or Lebesgue integrals? If Riemann, can't you take a standard example where $g_n\to 0$ and change all the functions at one point? Do you mean *for all $x$, $\lim g_n(x)\ne 0$*? Or do you mean *there exists an $x$ so that $\lim g_n(x)\ne 0$*?
Answer: The "standard" example is the sequence $\{g_n\}$ defined by
$$ g_1=1_{[0,1]},\; g_2=1_{[0,\frac{1}{2}]},\;g_3=1_{[\frac{1}{2},1]},\; g_4=1_{[0,\frac{1}{4}]},\;g_5=1_{[\frac{1}{4},\frac{1}{2}]},\dots$$
where $1_A$ is the indicator function of the set $A$, i.e. $1_A(x)=1$ if $x\in A$ and $1_A(x)=0$ otherwise.
Note that
$$\lim_{n\to\infty}\int_0^1g_n(x)\;dx=0$$
since the lengths of the intervals goes to zero, but $\limsup_{n\to\infty}g_n(x)=1$ for each $x\in[0,1]$.
The sequence $\{g_n\}$ is sometimes called the "typewriter sequence" or the "floating, shrinking interval".
Comment: First we split the interval $[0,1]$ into $1$ piece, then into $2$ pieces, then into $4$, then into $8$, and so on. It's possible to write down a formula for $g_n$, but it's kind of a pain and I think drawing a picture is much more helpful.


How to loop through a JSON object to fit inside a javascript array?

Question: I'm trying to convert a JSON object with google maps markers (which are generated with data from a CMS) in a way that fits inside the GoMap declaration. I'm trying to get multiple markers from my CMS to place them on a map.
I'll try to explain it in detail below:
I loop through the markers and create an array which is encoded to json:
<code>echo "<script>var googlePlaces = " . json_encode($googlePlaces) . ";</script>";
</code>
Then I get the following object:
<code>[Object { title="Est. Avenida de Mayo",  lat="-34.60844234174374",  lon="-58.37414860725403"}, Object { title="Café Tortoni",  lat="-34.60871608361115",  lon="-58.378823697566986"}]
</code>
All I need is to fit it here:
<code>$(document).ready(function() {

    $("#map").goMap({
        latitude: config.location_lat,
        longitude: config.location_lon,
        zoom: 15,
        mapTypeControl: false,
        maptype: 'ROADMAP',
        navigationControl: true,
        navigationControlOptions: {
            position: 'TOP_LEFT',
            style: 'SMALL'
        },
       markers: [{
                    // Multiple markers here!
            latitude: XXXXXX,
            longitude: XXXXXXX,
            html: {
                content: 'XXXXXXX'
            },
        }]
    });
}
}); // end
</code>
I'm kind of stuck here, do I know I would need to loop through the JSON object somehow and place it inside the markers property in the GoMap script. Is this correct? Can anybody point me in the right direction?
Answer: Try Some thing like this to iterate your JSON object element and push it to in an array.
<code> var markersArray = new Array();

 $.each(googlePlaces, function(i, itemPlace) {
     var obj = {
                 latitude: itemPlace.lat,
                 longitude: itemPlace.lon,
                 html: { content: itemPlace.title }
               };

     markesArray.push(obj);

 });
</code>
After that in the gomap code use markersArray like this
<code>$("#map").goMap({
   // your existing stuff
   markers: markersArray
});
</code>
Good Luck !!


Real polynomials forming a basis

Question: How do you show that a sequence of polynomials form a basis? I know that the rref criterion of a basis (no $0$ rows and has a leading 1 in every column) only works for $K^n$. Particularly,
Consider the set Consider the set $V$ of real polynomials $f$ of degree at most
3 which satisfy $f(1) = f(2)$, and $V$ is a vector space over $\mathbb{R}$.
Show that the following polynomials $f_1(x) = 1$, $f_2(x) = x^2-3x$
and $f_3(x) = x^3-7x$ form a basis of V .
Note: I can't use the determinant method to determine independence.
PROVING V is a vector space:
Let $f(1)=f(2)$, $g(1)=g(2)$ and $\alpha, \beta \in \mathbb{R}$
$(\alpha f+ \beta g)(1)$
= $(\alpha f)(1) + (\beta g)(1)$
= $\alpha (f(1)) + \beta (g(1))$
= $\alpha (f(2)) + \beta (g(2))$
= $(\alpha f)(2) + (\beta g)(2)$
= $(\alpha f + \beta g)(2)$
Comment: What is the vector space $V$ in your case? To show that a set of vectors in a finite-dimensional vector space is a basis, you will have to show that it is 1) linearly independent and 2) spanning
Comment: Define the set $V$, and also define what field it is over first. It looks like it might be $\mathbb{R}[x]$, in which case your polynomials would not be a basis because $\mathbb{R}[x]$ is infinite-dimensional (its basis is an infinite set).
Comment: @hardmath Hi there, please see the edits :)
Comment: @JKL I've edited the post. Thanks for pointing it out
Comment: @RyanShesler I've edited the question now. Thanks!
Comment: You want to show $f,g\in V$ implies $\alpha f+\beta g\in V$. What you currently have is not for checking vectorspace conditions.
Comment: @AlvinLepik Can I just chang $\alpha v$ into $\beta v$ to show that?
Comment: @Car Now it is correct.
Comment: How do I show the span? Can I just express the polynomials as vectors and then finding the rref of the matrix with columns $f_1, f_2, f_3$ where $f_1 =(1,0,0,0), f_2= (0.-3,1,0), f_3=(0,-7,0,1)$?
Answer: You need to show:

The dimension of $V$ is $3$
$f_1, f_2$ and $f_3$ are in $V$
$f_1, f_2$ and $f_3$ are linearly independent.
i.e. The only linear combination $a_1f_1 + a_2f_2 + a_3f_3=0$ is the trivial combination.
Comment: @Car The condition is given for $V$. You are not told, however, that $f_i$ are in $V$.
Comment: How do you show the dimension of V is 3 and that $f_1, f_2, f_3$ is in V? Thanks!
Comment: @Car Verify the condition $f_i(1) = f_i(2)$.
Comment: I guess the condition $f(1)=f(2)$ is given. Do i still need to verify this? If so, do you mind giving me some clues?
Comment: The set of polynomials of degree less than or equal to 3 is a 4 dimensional subspace.  The constraint $f(1) = f(2)$ takes away one dimension.  Test that $f_1(1) = f_1(2)$ are indeed in the subspace, and show that they are linearly independent.  An alternate approach is to choose a generic polynomial $f(x) = ax^3 + bx^2 + cx + d$ and apply the constraint $f(1)= f(2)$ then $a + b +c +d = 8a + 4b + 2c + d.$  Or, $7a + 3b + c = 0$
Comment: @Car Note that $V$ is a subset in a vector space. It suffices to check closedness w.r.t vector addition and multiplication with scalar. The zero polynomial is, of course, in $V$.
Comment: @Car You can include that in your first post. There is no reason to congest the comment section with semi-walls of text.
Comment: @AlvinLepik Okay done.
Answer: Assuming your vectors are in $V$. There are two conditions.

Linear independence. Let $a,b,c\in\mathbb R$ be such that
$$\forall x\in\mathbb R,\quad a + b(x^2-3x) + c(x^3-7x) = 0. $$
Does it follow that $a=b=c=0$? Note that this equality holds for any $x$. Fix three different $x$ and get 3 equations.
Spanning. Let $p(x) := ax^3 + bx^2+cx + d$ such that $p(1) = p(2)$. Find $u,v,w$ such that
$$\forall x\in\mathbb R,\quad ax^3+bx^2+cx + d = u + v(x^2-3x) + w(x^3-7x). $$

To check $V$ is a vector subspace, take $f,g\in V$ i.e $f(1) = f(2)$ and $g(1) = g(2)$. Check the condition for $V$:
$$(f+g)(1) \overset{?}= (f+g)(2),\qquad \forall \alpha\in\mathbb R,\quad (\alpha f)(1) = (\alpha f)(2)\,?  $$
or you could do this in one go by verifying
$$ \forall \alpha,\beta\in\mathbb R,\quad (\alpha f+\beta g)(1) = (\alpha f+\beta g)(2). $$
Either option suffices.
Comment: I have checked that V is a subspace in the post, can you confirm if its right?

Real polynomials forming a basis


How to populate a hashmap from an array

Question: i have two arrays (actually one, but i created two for each columns). I want to populate a hashmap with the values for a listview but all elements of the listview is the last element of the arrays:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();

    for (int i=0; i<13; i++)
    {
        map.put("left1", date[i]);
        map.put("right1", name[i]);
        mylist.add(map);
    }

SimpleAdapter  simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
        new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter);

Any ideas?
Thanks
Comment: You should consider declaring your variable using just the interface, as opposed to restricting it to the concrete implementation. 'List> myList' would be better.
Comment: Actually, it should probably be `List> myList` (and `Map map`). There's an argument to be made, though, that every variable should be declared as the most specific type it is known to be. Under that criterion, the declarations are good as they stand because they are local to the function. (As parameters, the same criterion dictates that they should be declared to be the most general type acceptable, since that's all that needs to be assumed about them.) Besides, this way it saves an import or two. :)
Answer: You're adding the same map to every slot of the array. Try this instead:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

for (int i=0; i<13; i++)
{
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("left1", date[i]);
    map.put("right1", name[i]);
    mylist.add(map);
}

Comment: +1: And this is why you have to be careful where you put your variable declarations!
Comment: what do you mean? I tried to declare the arrays both in the oncreate method, both outside it, like  String[] date = new String[13];
Comment: @erdomester: In your original implementation, you have one map already created. Then, you're going through the loop and and putting differing values, but the same key every time (overwriting it, of course), which is why it ends up with the last value in the array. @Ted Hopp's method creates a new map each time the loop runs, and adds that map with the key and value to your array.
Comment: I would have never thought of this. I like and know arrays better :) Thank you!
Comment: Btw how can i remove the items from the listview? I have googled several topics, and there is no clear() or remove() command for the adapter even if a lot of people say so..
Comment: Did you look at the documentation for [SimpleAdapter.remove()](http://developer.android.com/reference/android/widget/ArrayAdapter.html#remove(T))?

How to populate a hashmap from an array


How to get current view state name in grails webflow plugin

Question: How can I get current view state name in grails web flow plug-in, I am trying to follow this post but I can not get it to work in my app I am getting this error message
No such property: flowExecution for class: ni.org.petApp.AppController

thanks for your time
Answer: Using Grails 2.2.4 or 2.3.8 and the grails-webflow plugin version 126.96.36.199, I was able to access the current WebFlow state via the requestContext:
RequestContext requestContext = RequestContextHolder.getRequestContext();
String stateId = requestContext.currentState.id

When using these methods in a WebFlow your controller should look like this:
package webflow.requestcontext

import org.springframework.webflow.execution.RequestContext
import org.springframework.webflow.execution.RequestContextHolder

class TestController {

    def indexFlow = {
        randomNameForStartState {
            action {
                RequestContext requestContext = RequestContextHolder.getRequestContext();
                flow.startStateName = requestContext.currentState.id
            }
            on("success").to "showStartStateName"
        }

        showStartStateName() {
        }
    }
}

You could then print the start state in the showStartStateName.gsp via ${startStateName}
Based on the requestContext, you could also resolve the FlowExecutionContext:
FlowExecutionContext flowExecutionContext = requestContext?.getFlowExecutionContext();

Comment: After trying

RequestContext requestContext = RequestContextHolder.getRequestContext();

I tried import org.springframework.webflow.execution.RequestContextHolder with no success
String stateId = requestContext.currentState.id

i get the following error message: unable to resolve class RequestContext
Comment: Did you import the `RequestContext` as well?
My import is looking like this:
    `import org.springframework.webflow.execution.RequestContext`
    `import org.springframework.webflow.execution.RequestContextHolder`

Btw, which version of Grails are you using?
Comment: i am using grails 2.3.8 i tried import org.springframework.webflow.execution.RequestContext but i got this error No such property: RequestContextHolder
Comment: I just did create a new sample project using Grails 2.3.8 and the latest version of the grails-webflow plugin 126.96.36.199.
The controller did work for me as previously described. You can compare your attempts to the [gist](https://gist.github.com/croeck/609ecce71fc54a9ba09c). If you still can't get it to import / find the matching class please submit some more information, for instance the BuildConfig and the relevant snippet of the controller.
Comment: thanks for sharing the gist it help a lot, i was able to see my error, it is working now, could you please update your answer and add the imports needed for complete the task. To all thanks for your time.


I have 2 new users who have the same privileges, should I assign them the same UID or not and why?

Question: I have 2 new users, Jack and Annie, they have the same privileges, should I assign them the same UID or not and why?
Comment: no each user has a unique UID
Comment: You may add them to the same **group** if they should have the same privileges, and control the permissions on group level - this is how it's usually done.
Answer: User names and UID are unique identifiers for the users registered on the system. Assigning them a same UID is not an option.
Comment: You ***can*** have two users with the _same user id_ by manually editing `/etc/passwd` file, but of course this is not a wise thing to do...


Tub to shower conversion plumbing questions

Question: I'm in the middle of a tub to shower conversion but am a complete amateur when it comes to plumbing issues. The tub is out, the walls are open, we have the stand up shower kit bought. But upon looking at the existing plumbing I have a few questions.
1) Per this link: http://www.dndb.info/the-challenges-in-converting-bathtubs-to-showers/ it looks like we can't re-use the same drain line for the shower as we have in the tub (We're in Indiana not California but I'm not sure if that makes a difference). Would it be better just to cut the drain line back to main drain line and run new shower drain?
2) Since we're converting from tub to shower, we don't need the overflow drain cover anymore (the thing that you can push or pull and it stops up your drain so you can take a bath) can we just cap that section? I'm worried about venting, per this link: https://www.reddit.com/r/DIY/comments/3t63ah/bathtub_to_shower_cut_drain_overflow_pipe/
3) Is there an easy (no soldering) way to convert two faucet handles (hot & cold) into one shower faucet handle?
Thanks for all help in advance.
EDIT: PICTURE TIME

Important things: Green circle- the two knobs we'd like to convert to one for a stand up shower. I heard mention of shark bite fittings and I like that idea as we've already done a lot of work pulling out all of the water damage.
Blue circle- Overflow drain cover (if that's the right term). Can we cut this back and cap it?
Non-important thing: Red circle- this is an older house but I still had hope I could remove the existing drain from the drain pipe but this is not the case, I've tried everything (drain plug wrench, crow bar, dremel) and that drain will not come up, so we'll have to cut that pipe unfortunately.
EDIT 2: After getting quotes from two plumbers, I feel like the job is big enough to require a professional, especially since the drain line is a 1 1/2 inch line and it needs to be a 2 inch line for the shower. Thanks to everyone for their input.
Comment: Have you already removed the tub? The overflow is part of the tub plumbing and  will be removed when the tub is removed. Are you going to tile the bottom or use a plastic shower bottom? I assume that you are going to have the shower drain at the end under the faucets where the tub drain was, right? Changing to a single handle control valve from a two handle (two valve) mixing valve will require redoing the wall and the plumbing inside it.
Comment: Does your existing tub have a shower too?
Answer: This is way too much to answer in a single question, especially without pictures... but here goes:

...it looks like we can't re-use the same drain line for the shower as we have in the tub. Would it be better just to cut the drain line back to main drain line and run new shower drain?

You certainly can use the same drain line but it will need to be altered to exactly align with the drain assembly in your new "stand up shower kit", which may necessitate cutting it back to reconfigure it.

...we don't need the overflow drain cover anymore... can we just cap that section?

The "overflow drain cover" is just a part of an entire bathtub drain unit called a waste and overflow assembly; which should be long gone since you state that "the tub is out". All that should remain after bathtub removal is a trap and drain line (which will likely need to be removed/reconfigured for the shower, see above).

Is there an easy (no soldering) way to convert two faucet handles (hot & cold) into one shower faucet handle?

NO there is nothing "easy" about plumbing when you are a "complete amatuer"; however, it may be a fun learning experience from which you will emerge more confident and competent.
I presume you mean to replace the old two-valve/two-handled tub filler valves with a modern single-handle/knob shower control valve. This might be accomplished with some new-fangled fittings that don't require soldering (I will assume copper since you mention solder) but I would recommend that you solder the connections (which means you get to buy a torch and a bunch of extra fittings and some pipe to practice with until you have a new skill that you are fluent with).
You must fit the new valve and drain to the shower kit to ensure that the pipes in the wall and floor are positioned perfectly, which means that you will probably need to install and uninstall ("dry fit") the assembly multiple times. Follow instructions and have patience.
Comment: Shark bite fittings don't require soldering I know several contractors that use them but I am old school and still prefer soldered copper. But other than that +
Comment: @Ed Beal, I mentioned such in the answer but not by name as I have encountered other non-solder alternatives as well: "This might be accomplished with some new-fangled fittings that don't require soldering". I, like you, believe solder is the best method and a worthy skill for a novice to learn.
Comment: Maybe I missed it the first time and again reading don't see any thing than non solder reference, I did up vote but felt this type of fitting name would be important for a novice that would not know what to ask for.


Using QStyledItemDelegates as custom items in QListView

Question: I want to design a custom ListView widget which has custom items similar to this:
https://i.stack.imgur.com/iTNbN.png
However, the qt documentation and some stackoverflow posts state that one should ideally use a QStyleItemDelegate. I never worked with 'delegates' before but as far as I understood from my research they are called by the ListView for drawing / rendering each item.
I found a delegate example in another project (https://github.com/pyblish/pyblish-lite/blob/master/pyblish_lite/delegate.py) and they draw everything by hand / are essentially rebuilding entire widgets by painting rectangles.
This seems a bit impractical for me as most of the time custom item widgets can be compounds of existing widgets. Take a look at the screenshot above. It essentially contains a Qlabel, QPixmap, and four DoubleSpinBoxes.
Question: How would you use the painting / rendering methods that already exist in them instead of manually painting everything on your own?
That way you can profit from existing member methods and can use layouts for structuring your widget.
For example the first ListViewItem should pass the model data to the delegate so that the text of the self.lightGroupName QLabel can be set to "Light1".
Any help is greatly appreciated, since I have no idea how to go on from here:
from PySide2 import QtCore, QtGui, QtWidgets

class LightDelagate(QtWidgets.QStyledItemDelegate): #custom item view
    def __init__(self, parent=None):
        super(LightDelagate, self).__init__(parent)
        self.setupUI()

    def setupUI(self):
        self.masterWidget = QtWidgets.QWidget()

        #Light Group Header
        self.hlayLightHeader = QtWidgets.QHBoxLayout()
        self.lightGroupName = QtWidgets.QLabel("Checker")
        self.hlayLightHeader.addWidget(self.lightGroupName)

        #Light AOV Preview
        self.lightPreview = QtWidgets.QLabel()
        #set size
        self.aovThumbnail = QtGui.QPixmap(180, 101)
        #self.lightPreview.setPixmap(self.aovThumbnail.scaled(self.lightPreview.width(), self.lightPreview.height(), QtCore.Qt.KeepAspectRatio))

        # #Color Dials
        # self.hlayColorDials = QtWidgets.QHBoxLayout()
        # self.rgbDials = QtWidgets.QHBoxLayout()
        # self.rDial = QtWidgets.QDoubleSpinBox()
        # self.rDial.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
        # self.gDial = QtWidgets.QDoubleSpinBox()
        # self.gDial.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
        # self.bDial = QtWidgets.QDoubleSpinBox()
        # self.bDial.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
        # self.rgbDials.addWidget(self.rDial)
        # self.rgbDials.addWidget(self.gDial)
        # self.rgbDials.addWidget(self.bDial)
        # #Exposure
        # self.hlayExposureDials = QtWidgets.QHBoxLayout()
        # self.exposureDial = QtWidgets.QDoubleSpinBox()
        # self.exposureDial.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)

        # self.hlayExposureDials.addWidget(self.exposureDial)
        # self.hlayColorDials.addLayout(self.rgbDials)
        # self.hlayColorDials.addLayout(self.hlayExposureDials)

        #entire layout
        self.vlayWidget = QtWidgets.QVBoxLayout()
        self.vlayWidget.addLayout(self.hlayLightHeader)
        self.vlayWidget.addWidget(self.lightPreview)
        # self.vlayWidget.addLayout(self.hlayColorDials)
        self.vlayWidget.setContentsMargins(2,2,2,2)
        self.vlayWidget.setSpacing(2)
        self.masterWidget.setLayout(self.vlayWidget)

    def paint(self, painter, option, index):
        rowData = index.model().data(index, QtCore.Qt.DisplayRole)
        self.lightGroupName.setText(rowData[0])
        print (option.rect)
        painter.drawRect(option.rect)
        painter.drawText()
    def sizeHint(self, option, index):
        return QtCore.QSize(200, 150)

class LightListModel(QtCore.QAbstractListModel): #data container for list view
    def __init__(self, lightList= None):
        super(LightListModel, self).__init__()
        self.lightList = lightList or []

    #reimplement
    def rowCount(self, index):
        return len(self.lightList)

    def data(self, index, role):
        if role == QtCore.Qt.DisplayRole:
            lightGroupData = self.lightList[index.row()]
            return lightGroupData

class LightListView(QtWidgets.QListView): #
    def __init__(self):
        super(LightListView, self).__init__()
        self.setFlow(QtWidgets.QListView.LeftToRight)
        self.setItemDelegate(LightDelagate(self))
        self.setMinimumWidth(1880)

lightListTest = [
    ('Light1' , {'lightList' : [], 'lightColor': (0,0,0), 'mod_exposure': 1, 'mod_color' : (0,0,0)}),
    ('Light2' , {'lightList' : [], 'lightColor': (0,0,0), 'mod_exposure': 1, 'mod_color' : (0,0,0)}),
    ('Light3' , {'lightList' : [], 'lightColor': (0,0,0), 'mod_exposure': 1, 'mod_color' : (0,0,0)}),
    ('Light4' , {'lightList' : [], 'lightColor': (0,0,0), 'mod_exposure': 1, 'mod_color' : (0,0,0)})
]

app = QtWidgets.QApplication([])
LLV = LightListView()
model = LightListModel(lightList=lightListTest)
LLV.setModel(model)
LLV.show()
LLV.setSe
app.exec_()

Answer: Instead of QListView, could you use QListWidget and override itemWidget? The idea would be that this lets you return a QWidget (with children as per your screenshot) instead of having to implement a QStyledItemDelegate that calls each child widget's paint method.
Comment: No, unfortunately .setItemWidget() and QListWidget is not an option as I have to use the model / view structure.


Does setting up DKIM and SPF link reputations of the mail servers?

Question: I'd like to set up custom domain authentication using DKIM and SPF for our 3rd party email marketing company (like mail chimp or constant contact).  We also run MS exchange.  Our Exchange guy is convinced that setting up DKIM and SPF for email marketing company will forever tie the reputation of the email marketing company to our exchange server.  Is he correct?  If not, how do I convince him?
Comment: Where did you get the list you're going to send to? If it's a good list of people who opted-in then there's no need to be very concerned about the reputation of your own IP address and domain name. On the other hand, if the list is bad then the SPF and DKIM will have no effect on the reputation it would be the spam traps and other consequences of sending to a bad list that would be more useful to worry about.
Comment: It's a good list.  I believe his concern is ANY sender from mail chimp would affect our exchange server.  which is ludicrous to me.
Answer: I think I have enough info now to make this an answer...
Yes, if this is a permission-based list that you have sent to recently (if it's old that means likely spam traps) then I think you are correct that there's not much risk at all.
One way to convince this person would be to find out what IP address your MailChimp emails originate from (maybe send to a small list with just yourself on it but a real send). And then check out the reputation of this IP address using the tools available such as MX Toolbox and others, then show him the output. I'd be surprised if your Mailchimp assigned IP address was on any blacklists or had reputation issues
When he says exchange server is he talking about your company domain name taking a reputation hit? Or is he worried about the IP address from which you send non-marketing email? If he's worried about a separate IP that you send day-to-day email from then explain to him that your marketing emails will go out from a Mailchimp assigned IP address. If he's worried about the domain two things: 1. Your list is opt-in and you've sent recently so it's not an issue 2. If it was a bad list that would cause your domain to be blacklisted then whether you have DMARC, SPF, and DKIM doesn't matter, the originating IP that sends spam can get blocked for spamming regardless.
So I think you are right here but it's a matter of making the case.


Testing asynchronous capabilities of a django + tornado application

Question: I'm using django-on-tornado to build an application that is similar to the chat applicatoin proposed. All tutorials are focused on how to run a django application over tornado server, but how can I test an asynchronous feature that depends on tornado?
My current test does the following:

Starts a thread that sleeps for some time than sends a chat message
Do a request to ask for messages
When request ends, check that message arrived and that time elapsed is compatible with thread sleep time

When I run the test (with manage.py test), I get an "AttributeError: 'WSGIRequest' object has no attribute '_tornado_handler'", which is expected, since the _tornado_handler property of the request is set in runtornado command.
Is there a way to make this setup so that I can test the asynchronous feature? I use nose with django_nose plugin for tests.
Answer: Actually django-on-tornado does not anyhow change the manage.py test command of Django, so the Tornado is invoked only via runtornado. You will need to add command to manage.py called something like "testtornado" with implementation similar to https://github.com/koblas/django-on-tornado/blob/master/myproject/django_tornado/management/commands/runtornado.py - it should set up _tornado_handler and proceed with launching your test code.


What is the intuition behind conditional expectation in a measure-theoretic treatment of probability?

Question: What is the intuition behind conditional expectation in a measure-theoretic sense, as opposed to a non-measure-theoretic treatment?
You may assume I know:

what a probability space $(\Omega, \mathcal{F}, \mathbb{P})$ refers to
probability without measure theory really well (i.e., discrete and continuous random variables)
what the measure-theoretic definition of a random variable is
that a Lebesgue integral has something to do with linear combinations of step functions, and intuitively, it involves partitioning the $y$-axis (as opposed to the Riemann integral, which partitions the $x$-axis).

I haven't had time to learn measure-theoretic probability lately due to graduate school creeping in as well as other commitments, and conditional expectation is often covered as one of the last topics in every measure-theoretic probability text I've seen.
I have seen notations such as $\mathbb{E}[X \mid \mathcal{F}]$, where I assume $\mathcal{F}$ is some sort of $\sigma$-algebra - but of course, this looks very different from, say, $\mathbb{E}[X \mid Y]$ from what I saw in my non-measure-theoretic treatment of probability, where $X$ and $Y$ are random variables.
I was also surprised to see that one book I have (Essentials of Probability Theory for Statisticians by Proschan and Shaw (2016)), if I recall correctly, explicity states that conditional expectation is defined as a conditional expectation, rather than the conditional expectation, which implies to me that there's more than one possible conditional expectation when given two pairs of random variables. (Unfortunately, I don't have the book on me right now, but I can update this post later).
The Wikipedia article is quite dense, and I see words such as "Radon-Nikodym" which I haven't learned yet, but I would at least like to get an idea of what the intuition of conditional expectation is in a measure-theoretic sense.
Comment: In a Hilbert space you can think of it as a projection similar to what you might have seen in least-squares context like briefly discussed for example in this text http://www.people.ku.edu/~t926s829/math865-2015spring/conditionalexpL2.pdf
Comment: The expression $\mathbb E[X\mid Y]$ is shorthand for $\mathbb E[X\mid \sigma(Y)]$, where
$$
\sigma(Y) = \{Y^{-1}(B) : B\in\mathcal B(\mathbb R)\}
$$
is the $\sigma$-algebra generated by $Y$.
Comment: Also, in a "measure-theoretic sense" a conditional expectation IS a Radon-Nikodym derivative. These notes may lend some intuition however: https://www.ma.utexas.edu/users/gordanz/notes/conditional_expectation.pdf
Comment: On cond exp or $\sigma$-algebras: https://stats.stackexchange.com/questions/192179 https://math.stackexchange.com/questions/2048219 https://quant.stackexchange.com/questions/37497 https://math.stackexchange.com/questions/1273287/ https://math.stackexchange.com/questions/2711361 https://math.stackexchange.com/questions/375994 https://math.stackexchange.com/questions/26733 https://math.stackexchange.com/questions/77757
Comment: @WalterJ 's remark about the $L^2$ setting can be generalized to the general $L^1$ setting by considering $E[X \mid \mathcal{F}]$ to be the $L^1$ limit of $E[X I_{|X| \leq n} \mid \mathcal{F}]$ as $n \to \infty$, where now the approximants can be defined using Hilbert space methodology. Cf. https://math.stackexchange.com/questions/1130804/approximation-of-conditional-expectation
Answer: So, intuitively you can think of $E(X|\mathcal{F})$ as the "best guess" of the value of $X$ given information about the events in $\mathcal{F}$. However, more formally speaking, $E(X|\mathcal{F})$ is a random variable that satisfies the following:

$E(X|\mathcal{F})$ is an $\mathcal{F}-$measurable function
$\int_A E(X|\mathcal{F}) dP = \int_A X dP$ for every $A \in \mathcal{F}$

Now, the theorem that guarantees the existence of $E(X|\mathcal{F})$ is precisely the Radon-Nikodym Theorem (that's why these words appear in this context). And, in order to fully understand this definition of conditional expectation it's really recommended to cement some Measure Theory knowledge. Having said that, with the intuition above we can already "guess" the conditional expectation for some cases:
i) If $X$ itself is a $\mathcal{F}-$measurable function, then $E(X|\mathcal{F}) = X$. That is, by having all the information about $X$, then our best guess is $X$ itself.
ii) If $X$ is independent of $\mathcal{F}$, i.e. for all $A \in \mathcal{F}$ and $B \in \mathcal{B}_\mathbb{R}$ we have $P(\{X \in B\} \cap A) = P(X \in B)P(A)$, then $E(X|\mathcal{F}) = E(X)$. In other words, if we don't have any information about $X$, then our best guess of the value of $X$ is $E(X)$.
Finally, there are other useful intuitions of this conditional expectation. For instance, if you are familiar with Hilbert spaces and $X \in L^2(\mathcal{G})$ (consider the probability space $(\Omega, \mathcal{G},P)$), then $E(X|\mathcal{F})$ with $\mathcal{F} \subset \mathcal{G}$ is a projection in $L^2(\mathcal{F})$. This a commonly used geometric intuition.
Answer: As Oscar suggested, the most common intuition for $\mathbb{E}[X | \mathcal{F}]$ is that it is the best guess of $X$ given the information in $\mathcal{F}$. However, I find that the alternative intuition that it is the orthogonal projection of $X$ onto a subspace makes it clearer why it is defined the way it is.

First, just for clarity's sake, let me set up the orthogonal projection. Lets say you have an inner product space $V$ and $v \in V$. Then for a subspace $W \subseteq V$, we can define the orthogonal projection of $v$ onto $W$ as the unique $p_{w}(v) \in W$ such that
$$ \left< v, w \right> = \left< p_{w}(v), w \right>$$
for all $w \in W$.
This is based on the idea that a vector can be determined entirely by its inner product with other vectors. That is, the projection $p_{w}(v)$ is unique because if there were some other candidate $z \in W$ we would have that $\left< p_{w}(v) - z, w \right> = 0$ for all $w \in W$.

Before we move back to random variables, let's consider a space of functions, specifically the square-integrable functions $L^{2}(\mathbb{R})$. On this space, we have an inner product
$$ \left< f, g \right> = \int_{-\infty}^{\infty} f(x) g(x) \ dx. $$
Now if we consider specifically the indicator function $I_{A}(x) = I[x \in A]$ for a set $A$, we get that
$$ \left< f, I_{A} \right> = \int_{A} f(x) \ dx, $$
assuming that $A$ is appropriately chosen (e.g. a closed interval).
This in particular will be very convenient for re-interpreting the standard definition of the conditional expectation as a projection.
Now, a function $f$ cannot be determined entirely by its integrals with other $g$. Specifically, if $\tilde{f}$ is another function such that $f(x) = \tilde{f}(x)$ except on a very small set of points (a set of measure zero), the inner product of $\tilde{f}$ with other $g$ will be the same as for $f$. We skirt around this by considering two such functions to be equivalent, ie working with the set of square-integrable functions up to almost-everywhere equivalence.

Now, back to random variables. First, like with $L^{2}$, we consider two random variables $X$ and $Y$ the same if they are equal with probability one. In other words, we can have $X(\omega) \not = Y(\omega)$, but only on a small set of outcomes $\omega$ (small meaning probability zero).
The inner product space here is now the set of random variables with respect to our ambient $\sigma$-algebra, say $\Sigma$. Notice that this is actually a space of functions, with the constraint that they must be $\Sigma$-measurable. The inner product is
$$ \left< X, Y \right> = \mathbb{E}[XY] = \int XY dP. $$
The subspace $W_{\mathcal{F}}$ we project onto is the subset of $\mathcal{F}$-measurable random variables. Then for any $X$, the projection $\mathbb{E}[X|\mathcal{F}]$ is defined by

$\mathbb{E}[X|\mathcal{F}] \in W_{\mathcal{F}}$
$\int XY dP = \int \mathbb{E}[X|\mathcal{F}] Y dP$ for all $Y \in W_{\mathcal{F}}$.

Now compare this to the traditional definition of $\mathbb{E}[X|\mathcal{F}]$.

$\mathbb{E}[X|\mathcal{F}]$ is $\mathcal{F}$-measurable
$\int_{A} \mathbb{E}[X|\mathcal{F}] dP = \int_{A} X dP$ for all $A \in \mathcal{F}$.

The first two correspond to each other since $W_{\mathcal{F}}$ is just the $\mathcal{F}$-measurable random variables. The second two seem slightly different. Specifically, the definition of conditional expectation only looks at $Y$ of the form
$$ Y(\omega) = I_{A}(\omega) = I[\omega \in A] $$
where $A \in \mathcal{F}$. However, using the definition of the Lebesgue integral, considering the indicator functions is enough to ensure equality across all $\mathcal{F}$-measurable functions.
In other words, the two definitions are equivalent in this context.

Basically, this shows that $\mathbb{E}[X|\mathcal{F}]$ is the closest approximation of $X$ that is $\mathcal{F}$-measurable. To put it briefly, if we know which event in $\mathcal{F}$ occurred, but not the specific outcome $\omega$, $\mathbb{E}[X|\mathcal{F}]$ allows us to make a specific guess for the value of $X$ using all the information we have.
There is a lot more to be said about this. One great resource for learning more is David Williams' "Probability with Martingales", which I think anyone even slightly interested in understanding the theory of statistics should have. It has a chapter on the conditional expectation that goes into detail. If this were not already a ridiculously long answer, I would also go into the idea that $\mathbb{E}[X|\mathcal{F}]$ can be thought of as a regression estimate of $X$ (least-squares, of course). But hopefully this gives a start.
Comment: $A$ is a set; it doesn't belong to the same vector space as $X$, so your use of the inner product notation is meaningless
Comment: To anyone voting to delete, please leave a comment. I'd be happy to make edits if you think my answer could be better.
Comment: @JoseAvilez clearly, I didn't explain well enough. I will make edits to clarify why it does make sense.


Pycharm can not read some codes. How can I solve this problem?

Question: enter image description hereAs you can see in the image, in the Pycharm, some codes are not light on... lilke line2 or line 5 , etc.
line 2 import sys
line 5 import csv
Do you know the reason?
<code># Need this for language determination
import sys

# Need this for reading csv file
import csv

# Need this for converting all texts into lowercase
import re

# Need this for tokenization
from nltk import wordpunct_tokenize
from nltk.corpus import stopwords
import nltk
</code>
Comment: "some codes can not be read" - what exactly does that mean? What have you tried to resolve your problem?
Comment: Is there any usage of these packages in your code. I guess you are importing them and they are unused in the code
Comment: "some codes can not be read" means some codes are not light on.
Comment: @AnandTripathi It is my first time to use import sys in my Pycharm, but I used it in the JupyterNotebook
Comment: just next to that line type `sys.version` then then it will light on. It is not lighting on because that package is unused in your code
Comment: @AnandTripathi ye!!~~Now light on! Thanks a lot!
Answer: You have to install some packages before you import them. Try with:
<code>pip install os-sys
</code>
And so on with the other python packages.
Comment: Oh! Thanks a lot! However, it worked at the JupyterNote so I thought sys installed already. Is it possible that sys is not working in Pycharm even it is working in JupyterNote?
Comment: If it's installed, it would work anywhere. Try to check your interpreter inside the PyCharm settings. It will show you every package installed for that interpreter.


Properly sync gnome-calendar and the drop-down calendar to a (non-)Google online calendar

Question: I have recently installed Ubuntu 20.04 and so far I'm really liking it. Earlier today I tried to sync my calendar to the gnome-calendar that ships with the OS.
I have linked my Google account in the settings and the regional holidays I have subscribed to with Google calendar show up both in gnome-calendar and in the drop-down menu that pops up when clicking on the date and time on the top of the screen. So far so good.
The calendar that's more important to me, however, is my university timetable which has all the times for lectures etc. That one is also synced to my Google calendar via a URL that I get on my university website. It links to an iCal. So I was hoping it would just show up in gnome-calendar because I'm syncing my Google calendar, but for some reason it didn't. So what I did was go to gnome-calendar -> Manage calendars -> Add calendar -> Paste URL. That did in fact sync the uni calendar with gnome-calendar, but whenever I open gnome-calendar it takes a good 30 seconds before it shows up at all, which is kind of inconvenient. The Google stuff shows up immediately, so I don't know what the problem is. This gif screen recording shows how long it takes:

Notably, my university timetable also doesn't show up in the drop-down menu where you can see all of your synced calendars until it pops up after 30ish seconds. It also doesn't show up in manage calendars. I have tried disabling the sync and re-enabling it for both the Google and the university calendar, but to no avail. Logging out and back in also didn't fix the issue. Worth mentioning: The university calendar events don't show up in the drop-down calendar at all until I had previously opened gnome-calculator in the current session.
So what I want to achieve is have all the events be there immediately. When researching it I came across this answer, but as far as I understand downloading the ical would mean that I'd have to do that every time an event is changed, which is just very annoying. At that point I might as well just always open Google calendar in the browser.
In the drop-down calendar when clicking on the date on top of the screen, the university events show up immediately. However, the orange color I designated to it in the gnome-calendar settings is not there. See:
.
If the color showed up that would also be great. I assumed this is related to the issue of the calendar not syncing immediately, that's why it's not a separate question.
Edit: Today the drop-down calendar was different, my lecture showed up immediately after booting (without having opened gnome-calendar) and now has a calendar symbol next to it. See:

I am relatively new to Ubuntu and Linux in general, so if what I'm trying to achieve is impossible or the way I'm trying to achieve it is completely nonsensical, please let me know. I hope it is clear what my problem is.
Answer: I'm not sure if you have already found a solution but this: https://www.marc-richter.info/fix-missing-google-calendars-in-evolution-caldav/ helped me.
I don't have to manually add "Other calendars" from Google to Ubuntu calendar, it's automatically synced.