text:
Q: pandas DataFrame to_html style setting I want to print out the pandas DataFrame as html table and send it as email. For Gmail, the styling in header will be eliminated, so the only way I can take is inline styling. The method I am using now is just replacing the html code. For example,
styled_html = html_contents.replace('<table ', '<table style="border: 1px solid; text-align:center"')
styled_html = styled_html.replace('<th ', '<th style="background-color:navy; color:white; border:none; padding:5px"')
styled_html = styled_html.replace('<td', '<td style="border:none; padding:5px;"')

I wonder if there is a better way for styling the table?
By the way, I have tried df.style.set_table_styles(), but it requires further change on the system so I tried not to use.


text:
Q: How to create a string array using JNI in Qt I have to write some Android platform specific code in Qt and need to use JNI. I have a problem with how to create an array of some object. In this case I want to construct an array of strings from C++.
In the two code snippets below the first one creates a java string and it works as expected. In the second code snippet I want to create a java string array, but I get the debug message: "Java string array not valid" so I assume the signature and/or parameters passed to the "QAndroidJniObject javaStringArray()" function is not correct.
I have been looking at the documentation, but was not able to find or properly understand how to do this.
I assume I have to send in the size of the java string array object I want to construct as well.
Any help is appreciated!
QAndroidJniObject javaString("java/lang/String");
if (!javaString.isValid()) {
    qDebug() << "Java string not valid";
    return false;
}

QAndroidJniObject javaStringArray("[Ljava/lang/String;");
if (!javaStringArray.isValid()) {
    qDebug() << "Java string array not valid";
    return false;
}

A: The QAndroidJniObject constructor you are using takes a class name, so I'm afraid passing a string array signature won't work. You'll probably have to get your hands dirty and call JNI NewObjectArray(). Try something like:
QAndroidJniEnvironment env;
jobjectArray stringArray = env->NewObjectArray(5, env->FindClass("java/lang/String"), NULL);
QAndroidJniObject jniArray = QAndroidJniObject::fromLocalRef(stringArray);
// ...

This would create an array of 5 null strings, and transfer the ownership to QAndroidJniObject if you prefer, or else you'll have to take care of calling DeleteLocalRef().

redpajama_set_name: RedPajamaStackExchange


text:
Q: I'm new to Python... I'm creating a telegram bot that would mention users on my request, but this error occurs enter image description hereFor some reason this error pops up. I work in Visual Studio Code, as far as I understand this error may be due to the fact that I am working with an outdated version of the library, but no, I checked and I have the newest version installed  .. please help
type import telegram
from telegram.ext import Updater, CommandHandler
# ваш токен бота
TOKEN = '6122304624:AAG9jFVZWBarG5gzG9CSODze7ZY5XzaW5Ws'
# создаем объект бота
bot = telegram.Bot(token=TOKEN)
# обработчик команды /sozvat_vseh
def sozvat_vseh(update, context):
    # получаем список пользователей в чате
    chat_id = update.message.chat_id
    members = bot.get_chat_members(chat_id)
    usernames = [f'@{member.user.username}' for member in members]

    # упоминаем всех пользователей в чате
    mention_text = ' '.join(usernames)
    context.bot.send_message(chat_id=chat_id, text=mention_text)

# создаем объект для получения обновлений от Telegram
updater = Updater(TOKEN, use_context=True)

# обработчик команды /start
def start(update, context):
    context.bot.send_message(chat_id=update.message.chat_id, text="Привет! Я бот!")

# регистрируем обработчики команд
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('sozvat_vseh', sozvat_vseh))

# запускаем бота
updater.start_polling()
updater.idle()
here

Tried updating the version, reinstalling python but none of that worked for me


text:
Q: Remove rows from CSV file containing certain characters I am looking to remove rows from a csv file if they contain specific strings or in their row.
I'd like to be able to create a new output file versus overwriting the original.
I need to remove any rows that contain "py-board" or "coffee"
Example Input:
173.20.1.1,2-base
174.28.2.2,2-game
174.27.3.109,xyz-b13-coffee-2
174.28.32.8,2-play
175.31.4.4,xyz-102-o1-py-board
176.32.3.129,xyz-b2-coffee-1
177.18.2.8,six-jump-walk

Expected Output:
173.20.1.1,2-base
174.28.2.2,2-game
174.28.32.8,2-play
177.18.2.8,six-jump-walk

I tried this
Deleting rows with Python in a CSV file
import csv
with open('input_csv_file.csv', 'rb') as inp, open('purged_csv_file', 'wb') as out:
    writer = csv.writer(out)
    for row in csv.reader(inp):
        if row[1] != "py-board" or if row[1] != "coffee":
            writer.writerow(row)

and I tried this
import csv
with open('input_csv_file.csv', 'rb') as inp, open('purged_csv_file', 'wb') as out:
    writer = csv.writer(out)
    for row in csv.reader(inp):
        if row[1] != "py-board":
            if row[1] != "coffee":
                writer.writerow(row)

and this
        if row[1][-8:] != "py-board":
            if row[1][-8:] != "coffee-1":
                if row[1][-8:] != "coffee-2":

but got this error
  File "C:\testing\syslogyamlclean.py", line 6, in <module>
    for row in csv.reader(inp):
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

A: I would actually not use the csv package for this goal. This can be achieved easily using standard file reading and writing.
Try this code (I have written some comments to make it self-explanatory):
# We open the source file and get its lines
with open('input_csv_file.csv', 'r') as inp:
    lines = inp.readlines()

# We open the target file in write-mode
with open('purged_csv_file.csv', 'w') as out:
    # We go line by line writing in the target file
    # if the original line does not include the
    # strings 'py-board' or 'coffee'
    for line in lines:
        if not 'py-board' in line and not 'coffee' in line:
            out.write(line)

A: # pandas helps to read and manipulate .csv file
import pandas as pd

# read .csv file
df = pd.read_csv('input_csv_file.csv', sep=',', header=None)
df
              0                    1
0    173.20.1.1               2-base
1    174.28.2.2               2-game
2  174.27.3.109     xyz-b13-coffee-2
3   174.28.32.8               2-play
4    175.31.4.4  xyz-102-o1-py-board
5  176.32.3.129      xyz-b2-coffee-1
6    177.18.2.8        six-jump-walk

# filter rows
result = df[np.logical_not(df[1].str.contains('py-board') | df[1].str.contains('coffee'))]
print(result)
             0              1
0   173.20.1.1         2-base
1   174.28.2.2         2-game
3  174.28.32.8         2-play
6   177.18.2.8  six-jump-walk

# save to result.csv file
result.to_csv('result.csv', index=False, header=False)


text:
Q: Single connection string for multiple users in asp.net In asp.net 4.0 c#, i have two databases on different servers 1.Sql server 2.Oracle
I want to develop a web application in which the users will login with their id password and then
perform the allowed tasks.
I want to know that how i can make a single connection string for multiple users?
I dont want to make connection string on every page.
I was looking for this for a whole day but could not found any solution.
currently i am not using web.config file but i have a class where i have made a connection string and passed the textbox values to it
and the storing them in static fields and suppling them to other classes , but i dont think it is good approach
i think most of people are not getting what i really want to ask
kindly let me know if you want any confirmation.
for example there are 40 users each having a different id,password (no of users can be increased or decreased) there are two approaches to have a connection string
1)make a connection string in a class
string conn=@"server=MYSERVER;database=mydb;user id="+userid+";password="+password);
 but how the userid,password fields can be accessed in web.config?  here userid=textbox1.text,,password=textbox2.text;
2)make a connection string in a web.config file
so how?

A: It is very common for all users to use the same connection string to talk to the database server for a web application, and for the application to control what functions the users can and can't do based on their userid and password and security you build into you application.
In this type of setup, the user's id and password are separate (and different) from the userid and password for the database server.
If you must use a separate userid and password for each users db connection, then you wouldn't  store it in a connection string in your config file, but would instead build it dynamically based on the user logging in.
Like this for example:
var userid = "testuser";  //these would come from your login page, not hardcoded
var password = "letmein";

var sqlConn = new SQLConnection("server=MYSERVER;database=mydb;user id="+userid+";password="+password);


text:
Q: Using ng-repeat on a JSON object doesn't work - why? I can't seem to iterate over a JSON object using ng-repeat. I did test it directly by index and it works, but just can't seem to loop and print. What am I doing wrong? JSFiddle link.
Here's my code:

Upcoming Events

{{ event.feed.entry.title.$t }}

    {{ events.feed.entry[0].title.$t }}

The script:
var app = angular.module('myApp', []);
var feedUrl = 'https://www.google.com/calendar/feeds/ogmda.com_89pas0l9jbhpf053atd83hdj30%40group.calendar.google.com/public/basic?alt=json';

app.controller('Ctrl', function($http, $scope) {
$http.get(feedUrl).success(function(data) {
    $scope.events = data;
    });
});

A: That's because you iterate over the whole data returned by the calendar query, and not over the entries themselves. Change your ul to:

{{ entry.title.$t }}


text:
Q: Is it possible to store os_log message to a text file? Or is it possible to use method swizzling to observe the os_log() function call? I have two iOS apps written in Objective-c. For one of the Apps i am using my own logging functionality.I am storing the logs in local Database and sending the log messages to server for debugging and checking performance of app.
In the other App i have used os_log for logging. In most of the places I am using the following line to log events.
os_log(OS_LOG_DEFAULT, "Log message goes here");
Now I want to store these logs to local DB and send it to my server. Is it possible to do this?
I read in some article that we can use method swizzling to log events. Is it possible to use method swizzling here. If not, is there any way to save the os_log messages to local db.

A: You can define a new macro to call os_log and save method
#define my_log(p1, p2) [MyLog logWithParam:p1 p2:p2]

@interface MyLog : NSObject

+(void)logWithParam:(NSString*)p1 p2:(NSString*)p2;

@end

@implementation MyLog

+(void)logWithParam:(NSString*)p1 p2:(NSString*)p2{
    os_log(p1, p2);
    [self saveLogToLocalFile];
}

@end


text:
Q: GridView cells being pushed to the left How do I dynamically set the height of a cell in GridView, and how do I refresh it? Right now, I have my Adapter implement OnGlobalLayoutListener, get the dimensions of my GridView when onGlobalLayout is called, then save the dimensions. In getView(), I call cell.setLayoutParams(mGridViewWidth / 3, mGridViewWidth / 3); My problem is that my entire GridView is pushed too much to the left so only part of the last column is visible. Calling notifyDataChanged() seems to work, but it looks inefficient. I tried mGridView.requestLayout() and mGridView.invalidateViews() but those didn't work.
I confirmed that my GridView takes up the entire screen as it should. It's the elements that are positioned weird. I set my GridView's background color to #0000ff and the screen was blue. There's something wrong with the elements. Is it wrong for me to recycle LayoutParams?

A: Apparently using android:stretchMode="none" destroys the GridView. It works now that I removed that bit of code.


text:
Q: Unable to add dynamic generated html to a specific place On click of the Button , i am trying to add the dynamically generated html  to a specific position .
I am trying to add at Add it to here present inside HTML section .
This is my complete program

                <form>
                    Test Item
                </form>

 1

 Topping

50

50

// Add it to here

<input type="button" id="moreFieldsBtn" value="Add Data To Specific Place" />

But nothing is being added there , could anybody please tell me how to resolve this ??

A: your javascript code should be something like this
$(document).on("click", "#moreFieldsBtn", function() {
   var orderconfirmhtml = '
Complete your order111
';
   $(this).parent().find('.Test-details').append(orderconfirmhtml);
});

or
$(document).on("click", "#moreFieldsBtn", function() {
   var orderconfirmhtml = '
Complete your order
';
   $(this).parent().find('.Test-details').html(orderconfirmhtml);
});

refer this jsfiddle http://jsfiddle.net/WGQv6/1/ or http://jsfiddle.net/WGQv6/2/

A: You can try this:

For a function inside $(document).ready() method, it can run java code before page loaded, and uses jquery selector to find html element. And Jquery can bind event actions.


text:
Q: A die is throw until 5 followed by 3 appears A die is thrown until 5 followed by 3 appears. Like 156234753
What is the expected number of throws?
I've been trying to solve this problem but can't find anything similar.
Any help?

My solution:
Let $\displaystyle X$ be the random variable of the qustion (number of throws until 53).
Let the event $\displaystyle A=$after the first $\displaystyle 5$, you get a $\displaystyle 3$.
From the total law of expectation:
$\displaystyle E[ X] =E[ X|A] \cdotp P( A) +E[ W|\overline{A}] \cdotp P(\overline{A}) =E[ X|A] \cdotp \frac{1}{6} +E[ W|\overline{A}] \cdotp \frac{5}{6}$.
$\displaystyle E[ X|A] :$
We have gotten a $\displaystyle 5$ and then a $\displaystyle 3$. So we stop here. The expected number is the expected until $\displaystyle 5$ plus $\displaystyle 1$ for the $\displaystyle 3$: $\displaystyle 6+1=7$.
$\displaystyle E[ X|\overline{A}]$:
We have gotten a $\displaystyle 5$, but now not a $\displaystyle 3$ (assume we got some $\displaystyle x$).
We don't care about all the outputs before $\displaystyle x$, since they're not relevant to question anymore.
We can "pretend" then that we have just started with the output of $\displaystyle x$, and act as if that was our first result. I will count $\displaystyle 6$ for the results until the $\displaystyle 5$, and continue with same expectation.
$\displaystyle E[ X] =7\cdotp \frac{1}{6} +( 6+E[ X]) \cdotp \frac{5}{6}$
$\displaystyle  \begin{array}{{>{\displaystyle}l}}
E[ X] =\frac{7}{6} +5+\frac{5}{6} E[ X]\\
\frac{1}{6} E[ X] =\frac{37}{6}
\end{array}$
$\displaystyle E[ X] =37$.
I do see something wrong with my answer, the thing is the $\displaystyle x$ could be a $\displaystyle 5$ itself. This means I don't think it's very correct to say $\displaystyle E[ X|\overline{A}] =6+E[ X]$.
Because if the $\displaystyle x$ was a $\displaystyle 5$, then the possibility of ending the game next round is $\displaystyle \frac{1}{6}$ (with a $\displaystyle 3$), which of course doens't happen in $\displaystyle E[ X]$.
I believe that's my mistake, agreed?
Is the fact that my result is close to the actual result a pure casuality? Or does this mean my answer is close? I have given this answer on a test, trying to understand if it's completely wrong, or instead slighly wrong.

A: Let $x$ be the expected additional waiting time if we have not just tossed a $5$. At the beginning, we certainly have not just tossed a $5$, so $x$ is the required expectation. Let $y$ be the expected additional waiting time if we have just tossed a $3$.
If we have not just tossed a $5$, then with probability $5/6$ we toss a non-$5$ (takes us $1$ toss) and our expected additional waiting time is still $x$. With probability $1/6$ we toss a $5$ (takes us $1$ toss) and our expected additional waiting time is $y$. Thus
$$x=1+\frac{5}{6}x+\frac{1}{6}y$$
If we have just tossed a $5$, then with probability $4/6$ we toss a non-$3$ and a non-$5$, and then our expected additional waiting time is $x$, with probability $1/6$, we toss a $5$ again and the additional waiting time is $y$ and with probability $1/6$ the game is over. Thus
$$y=1+\frac{4}{6}x+\frac{1}{6}y$$
We get $x=36$ and $y=30$

A: As already suggested in a comment the system has two states: either we just have thrown a $5$ (and waiting for a 3) or any other number ($X$). The expected number of throws till the consecutive '53' is therefore:
$$\begin{align}
t_x&=1+\frac56t_x+\frac16t_5\\
t_5&=1+\frac46t_x+\frac16t_5
\end{align}$$
Solving the system we find $t_x=36$, $t_5=30$. Since at first throw we can obtain  both states the final result reads:
$$
t=1+\frac56t_x+\frac16t_5=36.
$$

A: It seems as though this question has been answered well by those above me, however I decided to still post an answer as there is a really cool way of doing this problem for anyone who comes to visit this question in the future.
Imagine we have a Casino and each day the Casino rolls one dice. The question you ask is what is the expected value of days until 5,3 is rolled.
Let us now say we have a sequence of gamblers who bet on the outcome of the dice. The first gambler comes in and bets \$1 that the Dice will be a 5, the second gambler comes in on the second day and bets \$1 that the dice will be a 6 and so on.
Assume the casino offers fair bets, that is the payout if correct for the \$1 bet is $6.
Each gambler works as follows: If they loose their bet that's it, they go home never to return. However if they win their bet then they come back tomorrow and stake the $6 they now have on the next days dice being a 3.
Example: Casino rolls: $4,5,5,1,2,3,5,3$ then Gambler 1 looses his money on day 1 , Gambler 2 wins \$6 on day 2 and then looses it on day 3, Gambler 3 wins \$6 on day 3 but looses it on day 4. Gamblers 4,5,6, all loose instantly but gambler 7 leaves with $36
Let $ \{M_{n} \}_{n \geq 1}$ be the Martingale that is the casinos wealth. It is clear that this is a Martingale as they only offer fair ($0$ expected value) bets. Let us assume they start with wealth $0$, that is $M_0 = 0 $
Let $\tau$ be the stopping time of the event that the sequence $5,3$ is tossed for the first time. We are after $\mathbb{E}[\tau]$
Via the Optimal Stopping Theorem as $\tau$ is almost surely finite and the increments of $M_n$ are bounded we have that:
$\mathbb{E}[M_{\tau}] = \mathbb{E}[M_0] = 0 $
However at time $\tau$ we have that the first $\tau -2$ gamblers have all lost there money so the casino has made $\$+1$ of each of them and also that the (lucky) $(\tau-1)^{th}$ gambler has made $35$ dollars of them, the $36$ in return minus the initial stake of \$$1$ And that the $\tau^{th}$ gambler bet on a $5$ but saw a 3 so he also lost.
Hence $M_{\tau} = 35 - (\tau-2) -1  = 36 - \tau$
Hence $0 = \mathbb{E}[M_{\tau}] = \mathbb{E}[36-\tau] \implies \mathbb{E}[\tau] = 36$
This method can be expanded very nicely to any sequence of tosses. But particular care must be made for the winnings of the final few gamblers. For example if the sequence was $535353$ then the $(\tau -5 )^{th}$ gambler wins his $6^6$ the $(\tau -3 )^{th}$ gambler wins his $6^4$ as he correctly predicted $5353$ and the $(\tau - 2)^{th}$ gambler wins $6^2$ as he correctly predicted the last $53$. In this case $\mathbb{E}[\tau] = 6^6 +6^4 +6^2$


text:
Q: Funcão do moodle mod_quiz_get_attempt_data retornando html incompleto Estou desenvolvendo uma aplicação para o moodle que retorna o quiz em html. Fiz o uso do webservice mod_quiz_get_attempt_data mas o problema é que sempre que eu mando o request, o navegador retorna:
Uncaught TypeError: require is not a function
Uncaught ReferenceError: M is not defined
Uncaught ReferenceError: Y is not defined

Eu achei somente um link em toda a internet que era para supostamente resolver meu problema, mas não funcionou. Como devo prosseguir para resolver isso? Eu estou fazendo algo errado?
Abaixo está a resposta JSON que recebo quando faço o request para o webservices:
{
  "attempt": {
    "id": 1,
    "quiz": 1,
    "userid": 2,
    "attempt": 1,
    "uniqueid": 1,
    "layout": "1,2,0",
    "currentpage": 0,
    "preview": 1,
    "state": "inprogress",
    "timestart": 1515194367,
    "timefinish": 0,
    "timemodified": 1515194367,
    "timemodifiedoffline": 1515194367,
    "timecheckstate": null,
    "sumgrades": null
  },
  "messages": [

  ],
  "nextpage": -1,
  "questions": [
    {
      "slot": 1,
      "type": "multichoice",
      "page": 0,
      "html": "

Question 1<\/span><\/h3>
Not yet answered<\/div>
Marked out of 1.00<\/div>
<input type=\"hidden\" name=\"q1:1_:flagged\" value=\"0\" \/><input type=\"checkbox\" id=\"q1:1_:flaggedcheckbox\" name=\"q1:1_:flagged\" value=\"1\" \/><input type=\"hidden\" value=\"qaid=1&qubaid=1&qid=1&slot=1&checksum=01b16b432969d727271ce46f60fbd868&sesskey=X2xjmV3Svi&newstate=\" class=\"questionflagpostdata\" \/><\/label>\n<\/div><\/div>

Question text<\/h4><input type=\"hidden\" name=\"q1:1_:sequencecheck\" value=\"1\" \/>

Um membro pode ser considerado um \u00f3rg\u00e3o ou m\u00fasculo. Isso significa que pizza \u00e9 \u00f3timo para comer \u00e0 noite.<\/p><\/div>

Select one:<\/div>

<input type=\"radio\" name=\"q1:1_answer\" value=\"0\" id=\"q1:1_answer0\" \/>a. <\/span>P\u00e9<\/label> <\/div>\n
<input type=\"radio\" name=\"q1:1_answer\" value=\"1\" id=\"q1:1_answer1\" \/>b. <\/span>Nenhuma das alternativas<\/label> <\/div>\n
<input type=\"radio\" name=\"q1:1_answer\" value=\"2\" id=\"q1:1_answer2\" \/>c. <\/span>Bra\u00e7o<\/label> <\/div>\n
<input type=\"radio\" name=\"q1:1_answer\" value=\"3\" id=\"q1:1_answer3\" \/>d. <\/span>Nariz<\/label> <\/div>\n<\/div><\/div><\/div><\/div><\/div>


text:
Q: Python Overlays : A case for monkey Patching I am trying to wrap/monkey patch modules in python. I am trying to develop a a clean means of implementing this that does not interfere with any existing code.
Problem
Given a script that imports some CLASS from a MODULE
from MODULE import CLASS

I would like to replace MODULE with another _MODULE_. Where _MODULE_ is a patch for the original MODULE. The cleanest interface I can see for this is as follows.
from overlay import MODULE # Switches MODULE for _MODULE
from MODULE import CLASS   # Original import now uses _MODULE_

This is basically monkey patching modules in the same way one would monkey patch classes, functions and methods. I believe if this is done correctly one could consistently patch code in a project specific kind of way.
What is the best way of implementing this ?

A: >>> import wrapt
>>> @wrapt.when_imported('collections')
... def hook(collections):
...     OldOrderedDict = collections.OrderedDict
...     class MyOrderedDict(OldOrderedDict):
...         def monkey(self):
...             print('ook ook')
...     collections.OrderedDict = MyOrderedDict
...
>>> from collections import OrderedDict
>>> OrderedDict().monkey()
ook ook

A: @wim's answer is certainly better but as I have been fiddling with this for a while here is my best bash at this. I assumes the following folder/file structure :
PACKAGE/
  overlay.py
  _decimal_.py
  __main__.py

within _decimal_.py I include the following lines
from decimal import *
__version__ = "X.Y"

within overlay.py I have :
import importlib
import inspect
import builtins
import os
from pathlib import Path

modsep      = '.'

class OverlayImporter(object):

 def __init__(self, *args, path = None, root = None, _import_ = __import__, **kvps):
  super().__init__(*args, **kvps)
  self.mask = "_{}_"
  self.root = Path(root or os.path.dirname(inspect.getmodule(inspect.stack()[1][0]).__file__))
  self.mods = self.modules()
  # Substitutes Import Functionality
  builtins.__import__ = self
  self.imp = _import_
  self.lom = []

 def __call__(self, name, *args) : # (self, *args, *kvps):
  # Hooks the import statement
  if self.mapToTarget(name) in self.mods.keys() :
   if name in self.lom :
    return self.imp(name, *args)
   self.lom.append(name)
   return importlib.import_module(self.mapToTarget(name)) # This is a little black magic as we ignore the args
  return self.imp(name, *args)

 def mapToTarget(self, name) :
  """Maps request to the overlay module"""
  # Converts PACKAGE.MODULE to overlay._PACKAGE_._MODULE_
  return modsep.join([self.mask.format(part) for part in name.split(modsep)])

 def modules(self) :
  """ Lists the overlays implemented within a directory """
  ext = '.py'
  mod = lambda parts, ext : [part[:-len(ext)] if enum + 1 == len(parts) else part for enum, part in enumerate(parts)]
  lst = [(mod(file.relative_to(self.root).parts, ext), file) for file in self.root.rglob('*'+ext)]
  return {modsep.join(item[0][:-1]) if item[0][-1] == "__init__" else modsep.join(item[0]) : item[1] for item in lst}

and within __main_.py I have
from overlay import OverlayImporter
OverlayImporter()
import decimal
print(decimal.__version__)

Commenting the first two lines in the main file toggles between the patched and un-patched versions of decimal.


text:
Q: dataframe save each column in separate CSV files I have this code that saves downloaded yahoo finance into a dataframe:
import bs4 as bs
import requests
import yfinance as yf
import datetime
import pandas

resp = requests.get('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, "html.parser")
table = soup.find('table', {'class': 'wikitable sortable'})
tickers = []
for row in table.findAll('tr')[1:]:
    ticker = row.findAll('td')[0].text
    tickers.append(ticker)

tickers = [s.replace('\n', '') for s in tickers]
start = datetime.datetime(2019,1,1)
end = datetime.datetime(2019,7,17)
data = yf.download(tickers, start=start, end=end)
print(data)

print(type(data))

data.to_csv('stock_data.csv')

I get these results:
            Adj Close                         ...     Volume
                    A        AAL         AAP  ...        ZBH       ZION        ZTS
Date                                          ...
2018-12-31        NaN        NaN         NaN  ...        NaN        NaN        NaN
2019-01-02  65.271561  32.081280  157.739120  ...  1152100.0  2234100.0  2665600.0
2019-01-03  62.866974  29.690985  162.663467  ...  1166100.0  2078400.0  2390900.0
2019-01-04  65.043022  31.646681  158.628098  ...  1580400.0  2370500.0  3383500.0
2019-01-07  66.424164  32.545509  160.955429  ...   900300.0  2459700.0  2360800.0
...               ...        ...         ...  ...        ...        ...        ...
2019-07-10  73.212486  32.718483  156.368958  ...   632600.0  1404700.0  1763700.0
2019-07-11  71.585976  32.807880  156.229019  ...   885000.0  1500800.0  1588000.0
2019-07-12  71.496178  33.552837  158.518127  ...   644100.0  1565400.0  1473400.0
2019-07-15  70.398537  33.383980  158.857986  ...  1188100.0  1415200.0  1255200.0
2019-07-16  69.799820  33.989880  161.696884  ...  1099400.0  1508700.0  1214600.0

How can I save these into separate CSV files (one for "A", one for "AAL", one for "AAP", etc)?

A: You just have to iterate for for every column, for example you should do something like this:
import pandas as pd
df = pd.read_csv('stock_data.csv')
for column in df.columns:
    df[column].to_csv(column + '.csv')

A: Since you have MultiIndex as your index, you want first to get a list of the relevant column names. Then, you can filter on them to get the proper sub-dataframes.
import pandas as pd
df = pd.read_csv('stock_data.csv')
stock_names = df.columns.get_level_values(1)
for stock in stock_names.unique():
    df.loc[:, stock_names == stock].to_csv(stock + '.csv')

If the levels in the index are named, you should use the name in get_level_values, for better readability.
If you want to get rid of the pesky level in the divided dataframe, either drop the levels:
stock_df = df.loc[:, stock_names == stock].copy()
stock_df.columns = stock_df.columns.droplevel(1)

or reorder the levels, so you can access the dataframe using more "naive" access:
df.columns = df.columns.reorder_levels([1,0])
...
df[stock].to_csv(stock + '.csv')

A: Here is what I was looking for:
import bs4 as bs
import requests
import yfinance as yf
import datetime
import pandas

resp = requests.get('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, "html.parser")
table = soup.find('table', {'class': 'wikitable sortable'})
tickers = []
for row in table.findAll('tr')[1:]:
    ticker = row.findAll('td')[0].text
    tickers.append(ticker)

tickers = [s.replace('\n', '') for s in tickers]
start = datetime.datetime(2010,1,1)
end = datetime.datetime(2019,11,18)

i=1
for ticker in tickers:
  print(i, "Ticker is : ", ticker)
  i=i+1
  data = yf.download(ticker, start=start, end=end)
  data.to_csv(ticker+'.csv')


text:
Q: basic authentication for websockets When I create a new websocket using chrome
new WebSocket('ws://gert:passwd@127.0.0.1:8001/dbname')

The nodejs server receives
GET /dbname HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: 127.0.0.1:8001
Origin: http://127.0.0.1:8000
Sec-WebSocket-Key1: '  5 5) 4 1e   a9 9 0 19
Sec-WebSocket-Key2: 3000909100 Q

How can I retrieve gert and passwd?

A: The Authorization header was not mentioned in the WebSocket spec until Hybi-13.  Chrome implements hybi-00 and hybi-10 (depending on the version of Chrome) so it wasn't a requirement of the implementation.
Now that it's mentioned in the protocol, maybe Google will implement it, but I wouldn't guarantee putting "user:pass@..." in the URI would necessarily be the way to do it, it might require a change to the browser's WebSocket API.

A: Chrome was not passing the authorization header as part of upgrade request. That resulted in authentication failure.
The issue seems to be fixed in the latest chromium Here

A: SSL
I found this link on Quora.
I agree you could use SSL and just sent those credentials as first message.
Socket.io
You could also use socket.io which also has websockets transport to do authorizing

A: is seems like chrome does not pass basic auth data in ws headers indeed.
Why not have login/pwd as part of url query string? (and use secure wss)

A: Chrome does send cookies with websocket connections. If you have a session cookie you can use the session cookie to identify the user initiating the websocket connection.


text:
Q: libusb not working with mediatek chipset I have an android application to take videos and pictures from USB camera. When I tested the application in many devices branded Samsung, Motorola, Nexus, Lenovo. The applocation working well and good in most of the devices with variety of cameras. The problem is I am not getting frames from camera in Mediatek chipset devices [Mobile, Tablets]. I found that, libusb is able to talk to the connected camera. It is correctly getting the descriptors, resolutions, formats but giving 0 byts when I try to read frames. I am using low resolutions, say 640 X 480 with 30fps. I would like know whether Mediatek chipset supports getting data from USB cameras.
I have tried with Control trasfers, Bulk trasfer & Isochronous transfers.
I would like to know, what trasfer types does Mediatekchipset supports.
What is the max data that it can transfer from USB port?


text:
Q: How do I perform a calculation with a distinct ID in MySQL? Wasn't sure how to ask this question. Basically I have a table:
create table sales (
bookcode int not null,
storecode int not null,
saledate date not null,
quantity int not null,
price decimal,
foreign key (bookcode) references books(bookcode),
foreign key (storecode) references stores(storecode)
);

and what I'm trying to do here is calculate the revenue for each store code. It will multiply the quantity by the price which is fine but I don't know how to get it so if there are multiple of the same storecode, it groups them together and gives a total amount
For example: if there are two stores in the sales table with the same store code of 1, I want to get the revenue of both and have them show up as a mass total for that storecode, instead of showing them separately. How can I do this?

A: What you are looking for is a GROUP BY clause, e.g.
SELECT storecode, SUM(quantity * price) AS total_sales
    FROM sales
    GROUP BY storecode;

This clause directs the SQL engine to group together rows which have an identical storecode, then apply aggregate functions to combine the results from each set of rows. In this case, the aggregate function used is SUM(); other common aggregate functions include COUNT(), AVG(), MIN(), MAX(), and GROUP_CONCAT().
Keep in mind that, if you're using a GROUP BY clause, every column you select must be either one of the values that's being GROUPed BY, or an aggregate function. For instance, you could not:
SELECT storecode, saledate FROM sales GROUP BY storecode  /* no! */

because saledate is neither a column that's being grouped by, nor an aggregate function. (To put it simply: there's multiple sale dates, which one is the query engine supposed to give you?)

A: I think you need something like this:
SELECT storecode, SUM(price*quantity) as revenue FROM sales GROUP BY storecode;

A: CREATE TABLE IF NOT EXISTS sales (
  bookcode int(6) unsigned NOT NULL,
  storecode int(6) unsigned NOT NULL,
  saledate date NOT NULL,
  quantity int(6) unsigned NOT NULL,
  price decimal
) DEFAULT CHARSET=utf8;
INSERT INTO sales (bookcode, storecode, saledate, quantity, price) VALUES
  ('1', '1', '2017-09-24',3,5),
  ('1', '1', '2017-09-24',5,5),
  ('1', '2', '2017-09-24',10,5),
  ('1', '2', '2017-09-24',10,10);
select storecode,sum(quantity*price)as income from sales group by storecode


text:
Q: Hibernate TransactionException: nested transactions not supported I am getting following exception:
2015-06-23 12:34:30.015 ERROR findRequest:224  - get failed
org.hibernate.TransactionException: nested transactions not supported
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:154)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
at sun.reflect.GeneratedMethodAccessor127.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
at com.sun.proxy.$Proxy5.beginTransaction(Unknown Source)

The method which is showing this exception is:
public List findRequest() {

    try {
        Transaction trans=sessionFactory.getCurrentSession().beginTransaction();
        Query q = sessionFactory.getCurrentSession().createQuery("query");
        q.setString("reqStatus", reqStatus);
        List result = q.list();
        trans.commit();
        return result;
    } catch (RuntimeException re) {
        LOGGER.error("get failed", re);
    }
    return null;
}

I am getting this issue while load testing. Also I recently added some functionality where number of web service calls increases. Also I created one maintenance thread which continue to call DB. So the point is number of DB calls increases 20 times.
I am using PostgreSQL. Also this is my hibernate setting:
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.password">******</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/******</property>
    <property name="hibernate.connection.username">******</property>
    <property name="hibernate.default_schema">******</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.enable_lazy_load_no_trans">true</property>

As you can see from my Hibernate setting I am using default Hibernate connection pool. Is it possible my connection are full and because of that my transition not released or closed.
Also I am not using rollback mechanism.
Also hibernate.current_session_context_class=thread, so every rest call is creating new thread. And the method which is showing this exception is the first one called from rest API. So for one thread how can it throw nested exception.
Thanks in advance.

A: This issue may arise if your previous transaction has not been rolled back. For now, you should restart your server (Glassfish or whatever else you use) and try again.
To avoid this in the future, you may want to manually roll back it in the catch block:
catch (RuntimeException re) {
        trans.rollback();
        LOGGER.error("get failed", re);
    }


text:
Q: Django migrate ProgrammingError: syntax error at or near "" I'm learning django (1.9.2) on a practice web site and a postgresql database with psycopg2.
I defined a model with a certain attribute "preview" and later deleted the attribute entirely. Despite having removed it, django seems to be referencing that old definition perhaps from a cache or something.
The makemigrations command seems to work fine, reflecting every change that I make to the model definition, but once I run the migrate command, then this error pops up.
(env) D:\Web Workspace\Rat Race Website\ratrace>python manage.py migrate
Operations to perform:
  Apply all migrations: contenttypes, news, polls, auth, sessions, admin
Running migrations:
  Applying news.0003_auto_20160212_1209...Traceback (most recent call last):
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: syntax error at or near "9999999999999"
LINE 1: ...ER TABLE "news_news" ADD COLUMN "preview" varchar(9999999999...
                                                             ^

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\core\mana
gement\commands\migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_ini
tial)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\migrat
ions\operations\fields.py", line 62, in database_forwards
    field,
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\base\schema.py", line 396, in add_field
    self.execute(sql, params)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\base\schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\utils.
py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\utils\six
.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\Web Workspace\Rat Race Website\env\lib\site-packages\django\db\backen
ds\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "9999999999999"
LINE 1: ...ER TABLE "news_news" ADD COLUMN "preview" varchar(9999999999...

The database tables are empty, so I suspect that deleting the database and making a new one might fix the issue for now, but I'd like a more concrete solution in case it happens again in the future where deleting the database is not an option.
Here's what the model definition looks like.
from django.db import models

# Create your models here.
class News(models.Model):
    headline = models.CharField(max_length=100)
    content = models.CharField(max_length=100)
    pub_date = models.DateTimeField('date published')
    content_preview = models.CharField(max_length=100, blank=True)
    thumbnail = models.ImageField(upload_to='thumbnails/%Y/%m/%d/', blank=True)
    def __str__(self):
        return self.headline

There's no "preview" attribute anymore, yet django doesn't seem to get that.
Edit:
Here's the definition for the preview field
preview = CharField(max_length=9999999999999)

It produced an error saying it couldn't be null, so I entered a one time default value "news preview". I assume the default value is for existing entries. At the time there were about 5 test entries.
Just read on another post that the quotes I used when entering the one time default value was the likely cause of the problem. Something about psycopg2 not liking quotes on values?... It was a string value, so I thought I needed quotes.
Anyways, so now that I've cleared out the preview field, how do I make django forget it was ever there?

A: Going off the other comments and my own experience . . . when you get this error, go into your migrations folder and delete everything except the __init__.py. You can even delete the __pycache__ folder. Then run makemigrations and migrate again. I think that should fix the problem.

A: This can also happen when making a CharField nullable if you do not specify max_length.
So the solution is to specify max_length in your model, then remove and remake the problematic migration..
This was described as a bug here.

A: This is old, but I will answer anyway.
The problem is that you made a "makemigrations" with a mistake in your model and that was catalogued,  I will assume you corrected the mistake, that catalogue is not deleted, instead it was aggregated when you tried Migration.
What you need to do is delete everything in your migration folder except init, that should clear that up, if it doesn't then you still have a mistake in your Model


text:
Q: yii2 pretty url not working on form submission Here are the rules I am using for the Url Manager.
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'rules' => [
            'post/<arg1>/<arg2>/<arg3>/<arg4>' => 'post/filter',
            'posts' => 'post/index',
            ],
        ],

And my .htaccess
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

The rule seems to be working and urls like
post/filter?arg1=9&arg2=0&arg3=d&arg4=3 is getting turned into,
post/9/0/d/3

However, I have a search form like below
 $form = ActiveForm::begin([
        'action' => Url::to(['post/filter']),
        'method' => 'get'

the fields in the form are named  arg1,arg2,arg3,arg4. Now whenevr I submit the form the url gets back to the format
post/filter?arg1=9&arg2=0&arg3=d&arg4=3

I am not sure if its got something to do with the rules or the way I am submitting the form (I need submit the form by GET method only). Any help? Thanks.

A: Try this rule
'post/filter?<arg1:\w+>=<val1:\d+>&<arg2:\w+>=<val2:\d+><arg3:\w+>=<val3:\d+><arg4:\w+>=<val4:\d+>' => 'post/filter/<val1>/<val2>/<val3>/<val4>'


text:
Q: You are given a file which contains all possible numbers on a 32-bit architecture. 4 numbers are missing from that file. Find the 4 missing numbers This is an interview question that I've run across a few times, and I'm really not sure how to solve it given that four numbers are missing. I'm familiar with algorithms for finding one or two numbers are missing, but I don't see a way to generalize either of them to four.

A: If this were Java, you could use a BitSet.  Well, two of them, because they can't quite hold all 32 bit numbers.  Skeletal code, perhaps buggy:
BitSet bitsetForPositives = new Bitset(2^31);  // obviously not 2^31 but you get the idea
BitSet bitsetForNegatives = new Bitset(2^31);

for (int value: valuesTheyPassInSomehow) {
  if ((value & 0x80000000) == 0)
     bitsetForPositives.set(value );
  else
     bitsetForNegatives.set(value & ~0x80000000);
}

Then use  BitSet.nextClearBit() to find who is missing.
Note added much later:
Note that with this algorithm, it is fairly easy to run the time consuming part in parallel.  Say the original file has been split into four roughly equal parts.  Allocate 4 pairs of BitSets (2GB, still manageable).

*

*Have four threads, in parallel, each process one file into their own pair of BitSets.

*When complete, go back to a single thread, or the Bitsets (trivial time), then call nextClearBit four times (also fairly trivial time).

I'd expect I/O to still be the rate limiting step, but if magically all the numbers were in memory you could really speed things up.

A: This question can be solved using an array of bits (true/false).  This should be the most efficient structure to hold the answers for all the numbers using the index of the array to hold whether that particular number was found.
C#
var bArray = new BitArray(Int32.MaxValue);

//Assume the file has 1 number per line
using (StreamReader sr = File.OpenText(fileName))
{
        string s = String.Empty;
        while ((s = sr.ReadLine()) != null)
        {
            var n = int32.Parse(s);
            bArray[n] = true;
        }
}

Then just iterate through the array and for those values who are still false they are not in the file.
You could break the file into smaller chunks but I was able allocate a full int32 max size array (2147483647) on my 16.0 GB laptop running Windows 7 (64 bit).
Even if I wasn't running 64 bit I could allocate smaller bit arrays. I would pre-process the file creating a set of smaller files each with a range of [0-64000][64001-128000], etc. numbers in it that would be suitable for the available environmental resources.  Go through the big file and write the each number to it's corresponding set file.  Then process each smaller file.  It would take a little longer because of the pre-processing step, but this would get around resource limitations if there was limited resources.

A: As this is an interview question, I'd show the interviewer some understanding about the constraints. Then, what does "all possible numbers" mean? Is it really 0 ... 2<(32-1) as everyone guesses? Usual 32-bit-architectures can work with many more than just 32 bit numbers. It's just a matter of representation, obviously.
Has it to be solved on a 32-bit-system, or is that rather a part of the restriction on numbers? For example, a typical 32-bit system will not be able to load the file into RAM at once. I'd also mention that a 32-bit-system will often not be able to have a file containing all the numbers due to file size limitation. Well, unless it has some clever encoding, like "All numbers except those four", in which case the problem is solved trivially.
But if you really want to understand the question as "Given a file with all numbers from 0 ... 2^(32-1) except a few, give me a missing ones" (and this is a big if!), then there are many ways to solve it.
Trivial but non-feasable: For each possible number, scan the file and see if it's in there.
With 512 MB of RAM and single pass through file: mark every number (= set bit at that index) read from the file, and afterwards pass the RAM once and see the missing ones.

A: Since it's a file, I'm assuming you are allowed to make multiple passes. First create an array of 256 counters, iterate over the file and for each number increment the counter indexed as the number's first byte. When you're done, most of the counters should be at 2^24, but 1 up to 4 counters should have lower values. Each of these indices represents a first byte of one of the missing numbers(if there are less than 4 it's because multiple missing numbers share the same first byte).
For each of these indices, create another array of 256 counters, and make a second pass on the file. This time, if the first byte is one of the values from before, increment a counter in it's array based on the second byte. When you are done, look again for the counters lower than 2^16, and you'll have the second byte of the missing numbers, each matched to it's first byte.
Do it again for the third byte(notice that you need a maximum of 4 arrays in each pass, even though each byte can be followed by up to 4 different bytes) and for the fourth byte, and you have found all the missing numbers.
Time complexity - O(n * log n)
Space complexity - constant!
Edit:
Actually, I considered the n=2^32 to be the parameter, but the number of missing numbers k=4 is also a parameter. Assuming k<<n this means the space complexity is O(k).
Update:
Just for fun(and because I'm currently trying to learn Rust) I implemented it in Rust: https://gist.github.com/idanarye/90a925ebb2ea57de18f03f570f70ea1f. I elected to have a textual representation, since on-one is going to run it with ~2^32 numbers...

A: Whether it's for an interview or actual work, your first priority needs to be a working solution that makes sense to you. That usually means you should offer the first solution you can think of that is simple and easy for you to explain.
For me, that means sort the numbers and scan for gaps. But, I work on business systems and web apps. I don't fiddle with bits, and I don't want my team to!
If you're interviewing for a low-level, closer-to-the-metal job, "sorting" will probably be met with blank stares. They want you to be comfortable thinkings about bits and so forth. Your first answer there should be, "Oh, I'd use a Bitmap." (Or bit array, or bit set.)
And then, either way -- even if you give "wrong" solution, if your interviewer (or boss!) presses for it, you can suggest some improvements or alternatives, focusing on the manager's specific area of concern.

*

*Severely limited RAM? Less than 512MB?Sort it in place, on disk. You can use a mostly-arbitrary amount of RAM to optimize and/or buffer sorted blocks.

*Limited time?Use that RAM! Sorting is already O(n*log(n)). (Or O(n) for a integer-bucket sort!)

*Maintainability?What could be easier than sorting?!

*Doesn't demonstrate knowledge of bit flags/fields? (BitSet/BitMap/BitArray)Well OK ... go ahead and use a BitArray to flag the "found numbers." And then scan for 0's.

*Predictable "real-time" complexity?Use the bitmap solution. It's a single pass over the file and another pass over the BitArray/BitSet (to find the 0's). That's O(n), I think!

Or whatever.
Address the concerns you actually have. Just solve the problem first, using naive solutions if necessary. Don't waste everybody's time addressing concerns that don't exist yet.

A: One approach that is easy to remember and easy to articulate in an interview would be to use the fact that if you look at all the numbers in N bits, each bit will be set in exactly half of those values and not set in the other half.
If you iterate over all the values in the file and keep 32 counts of the values at the end, you will end up with 32 values that are exactly (2^32/2) or slightly less than that value.  The difference that maximum (2^32/2) and the total gives you the total bits set in each position of the missing values.
Once you have that, you can determine all the possible sets of 4 values that could give those totals.  Given that, you can then go through the values in the file again checking for any values that are part of those combinations.  When you find one, combinations containing that value are eliminated as possibilities.  Once you have only one possible combination remaining, you have you answer.
For example using a nibble, you have the following values:
1010
0110
1111
0111
1101
1001
0100
0101
0001
1011
1100
1110

The total bits set in each position is:
7867

Subtracting those from 8 (4^2/2) we get:
1021

Which means there are these following possible sets of 4 values:
1000
0000
0011
0010

1010
0001
0010
0000

(forgive me if I've missed any, I'm just doing this by sight)
And then looking at the original numbers again, we find 1010 right away meaning the first set was the answer.

A: Assuming that the file is sorted by increasing numbers:
Insure that it indeeds contains (2³²-4) numbers.
Now if the file were complete (or if the 4 missing numbers were the last 4 ones),  reading any word in the file at position N would return matching value N.
Use a dichotomy search on positions [0..2³²-4-1) to search to find the first non-expected number X1.
Once found that first missing number, do a dichtotomy search again on positions [X1 .. (2³²-4-1)] to find the second missing, X2: This time, reading a word at position N should return matching value N-1 if there were no more missing numbers (since you passed one missing number).
Iterate likewise for the two remaining numbers. On the third iteration, reading word at position N should return N-2, and on the fourth, it should return N-3.
Caveat: I have not tested this. But I think it should work. :)
Now in real life, I agree with other answers: the first questions would be about the environment. Do we have RAM avail (how much), is the file on a direct access storage device, is this a one-shot operation (no optimization required) or a critical one (each cycle count), do we have an external sort utility available, etc.
Then find a compromise acceptable for the context. This at least shows that you start analyzing the problem before looking for an algorithm.

A: As with all standard questions the solution is to google them before the interview.
This question and variations have a very definite 'correct' answer involving XORing all the numbers. Its supposed to show you understand indexes in databases or something. So zero points for any 'might work but not what it says on the paper' answer im afriad.
On the plus side there is a finite set of these questions a few hours revision will make you look like a genius. Just remember to pretend you are working it out in your head.
Edit. Ahh it seems for 4 there is a different approach than XOR
http://books.google.com/books?id=415loiMd_c0C&lpg=PP1&dq=muthukrishnan%20data%20stream%20algorithms&hl=el&pg=PA1#v=onepage&q=muthukrishnan%20data%20stream%20algorithms&f=false
Edit. Downvoters : This is a published textbook O(n) solution to the exact problem stated in the OP.


text:
Q: Connect to an existing Git repository in IntelliJ IDEA This is NOT a duplicate of Import an existing Git repository into IntelliJ IDEA, because I am trying to connect an existing non-empty project to an existing non-empty repository. The title says it all, any ideas?

A: Try first in command-line, before switching to Git.
cd /path/to/local/repository
git remote add origin https://url/existing/remote/repository
git fetch
git log --decorate --oneline --graph --all --branches

If you see the same history for origin/main and your local main branch, you have the right remote repository.
Then open IntelliJ IDEA, which should detect the newly added remote origin reference.
(Check  VCS -> Git -> Remotes)


text:
Q: Mean and Variance of Stochastic Differential Equation (SDE) What is the mean and the variance of $y_t$, given the following SDE:

$dy_t = -x_t y_t dt + \sigma_1 dW^1_t$
$dx_t = -\sigma_2 y_t dW^2_t$

$W^1$ and $W^2$ are (possibly correlated) Wiener processes.
Especially, is it true that $Var[dy_t] = \sigma_1^2 dt$, or is that not necessarily correct due to the process $x_t$ in the drift of $y_t$?

A: Start from the second equation, by direct integrating, we have
$$
X_t=X_0-\sigma_2\int_0^t Y_sdW_s^2
$$
Plug into the first equation, we have
$$
dY_t=-(X_0-\sigma_2\int_0^t Y_sdW_s^2)Y_tdt+\sigma_1dW_t^1\\
Y_t-Y_0=-X_0\int_0^t Y_sds-\sigma_2\int_0^t\int_0^sY_\tau dW_\tau^2Y_sds+\sigma_1 W_t^1\cdots(*)
$$
Assuming $E(Y_t)=m_t$ which is a function of time. Take expectation on both sides of $(*)$, we have
$$
m_t+X_0\int_0^t m_s ds=Y_0
$$
Here I am using zero expection property of Ito's integral. This is an integral equation and it is not hard to find the solution is
$$
m_t=Y_0\exp(-X_0 t)
$$
Variance can be found by squaring $(*)$ and follow the similar procedure to find $E(Y_t^2)$.


text:
Q: Why isn't this CLS compatible? I have the following Interfaces:
public interface ITemplateItem
{
    int Id { get; set; }
    String Name { get; set; }
    String Text { get; set; }
    int  CategoryId { get; set; }
    int  Typ { get; set; }
}

public interface ITemplateCategory
{
    int Id { get; set; }
    String Name { get; set; }

    List<ITemplateItem> TemplateItems { get; set; }
    void Add(ITemplateItem item);
    void Remove(ITemplateItem item);
    ITemplateItem CreateTemplateItem();
}

My implementation of the ITemplateItem looks like this:
public class MyTemplateItem : ITemplateItem
{
    #region ITemplateItem Member
    public int Id { get; set; }
    public String Name { get; set; }
    public String Text { get; set; }
    public int CategoryId { get; set; }
    public int Typ { get; set; }
    #endregion
}

But for the ITemplateCategory Implementation the compiler tells me that my class is not CLS Compliant.
public class MyTemplateCategory : ITemplateCategory
{
    #region ITemplateCategory Member
    public int Id { get; set; }
    public String Name { get; set; }

    // Warning: type of TemplateItems not CLS-Compliant
    public List<ITemplateItem> TemplateItems { get; set; }

    // Warning: Argument not CLS-Compliant
    public void Add(ITemplateItem item)
    {
        throw new NotImplementedException();
    }

    // Warning: Argument not CLS-Compliant
    public void Remove(ITemplateItem item)
    {
        throw new NotImplementedException();
    }

    // Warning: Return type not CLS-Compliant
    public ITemplateItem CreateTemplateItem()
    {
        throw new NotImplementedException();
    }
    #endregion
}

Ok,
I could just ignore these warnings or turn them off by adding the CLSCompliant(false) attribute to my class.
But I am curious why this happens. Expecially because the compiler does not complain about the inferface itself.
Does this happen for classes that expose Interfaces in general or did I just use a forbidden keyword?

A: Are these in different assemblies by any chance? Is ITemplateItem in an assembly which doesn't claim to be CLSCompliant? I think that would explain it - in which case, just make that assembly CLSCompliant - or possibly even just ITemplateItem.


text:
Q: The concept of an object variable in typoscript In my typoscript 20.special.value and 10.value.typolink.parameter have the same value, which is a page id. I use this value to build a heading and a menu of its subpages within the same COA object. I would like to be able to re-use this value.
How can I call a object property in typoscript?
Here's what I have tried:
  10 = HTML
  10 {
    value.typolink {
      parameter = {$temp.LANDINGPAGEMENU.20.special.value}
    }
  }

UPDATE:
I am re-utilizing my COA object in different parts of the site, and changing only the special.value, so to display a menu I have:
temp.LANDINGPAGEMENU.10.value.typolink.parameter = 2427
temp.LANDINGPAGEMENU.20.special.value = 2427
temp.COLUMN_NOTSURE < temp.LANDINGPAGEMENU

I am after a cleaner way of handling the
temp.LANDINGPAGEMENU.10.value.typolink.parameter = 2427
temp.LANDINGPAGEMENU.20.special.value = 2427

Full LANDINGPAGEMENU typoscript code is http://pastebin.com/p9kPuZEe

A: *

*Use the constants but not in a way you tried. You have to define the constant first.
Constants: my_constant = 2427
Setup: parameter = {$my_constant}
...OR...

*Assign one of the values by reference using the =< operator. However, this would work only when using the whole object. Example:
temp.something = TEXT
temp.something.value = 2427
...parameter.cObject =< temp.something

I suggest you go with the option 1 using the constants as the 2nd option is somewhat cumbersome.

A: You can copy the property but not by reference it.


text:
Q: Creating matrix table grouped by columns I have requirement where I need to list all the projects running inside a specific region. All the project should be added as columns in a table. The number of projects can increase so the table needs to be dynamically created having dynamic columns. Also the table should start with two columns which include information about the category and region which should be repeated for every project. Also we need to group the project by status. So we need column grouping. I don't know whether the below JSON structure is accurate to meet the requirements or do we need to change that
I have JSON similar to this,
[{
  "category": "a",
  "region": "Region 1",
  projects: [{
    "project": "A",
    "resources" : 2,
    "status": green
  }, {
    "project": "B",
    "resources" : 2,
    "status": green
  }, {
    "project": "C",
    "resources" : 2,
    "status": green
  }]
}, {
  "category": "b",
  "region": "Region 2",
  projects: [{
    "project": "F",
    "resources" : 2,
    "status": red
  }, {
    "project": "A",
    "resources" : 4,
    "status": green
  }]
}]

Is it possible to dynamically create a table grouped by status column.
Or should we handle creation of such tables from server side.
The table should look similar to this,

    Category
    Region
    Green
    Red

    A
    B
    C
    D
    E
    F
    G

    A
    Region 1
    2
    2
    2

    B
    Region 2
    4

    2

I want whether this can be achieved dynamically via jQuery using the above JSON structure or do we need to modify the JSON structure.

A: You can do this in JavaScript using a function such as
"use strict";

function groupBy(array, selectKey) {
  return array.reduce((groups, element) => {
      const key = selectKey(element);
      if (groups[key]) {
        groups[key].push(element);
      }
      else {
        groups[key] = [element];
      }

      return groups;
    }, {});
}

This will project an array into an object with properties for each distinct key projected from the objects in the array. The value of each property will be an array of the values that produced that key under whatever selectKey function we passed.
Note: selectKey should return a string or a number and, irregardless, its result will be coerced into a string.
Now, given your array, we can group it.
Lets start by grouping by category:

"use strict";

function groupBy(array, selectKey) {
  return array.reduce((groups, element) => {
      const key = selectKey(element);
      if (groups[key]) {
        groups[key].push(element);
      }
      else {
        groups[key] = [element];
      }

      return groups;
    }, {});
}

const xs = [{
  "category": "a",
  "region": "Region 1",
  projects: [{
    "project": "A",
    "resources": 2,
    "status": 'green'
  }, {
    "project": "B",
    "resources": 2,
    "status": 'green'
  }, {
    "project": "C",
    "resources": 2,
    "status": 'green'
  }]
}, {
  "category": "b",
  "region": "Region 2",
  projects: [{
    "project": "F",
    "resources": 2,
    "status": 'red'
  }, {
    "project": "A",
    "resources": 4,
    "status": 'green'
  }]
}];

const byCategory = groupBy(xs, x => x.category);

console.log(byCategory);

Now, what if we need to group by multiple columns? We could augment groupBy with logic to take a custom comparison function, but we can cheat by using string concatenation.
In the following example, we group by category and by region:

"use strict";

function groupBy(array, selectKey) {
  return array.reduce((groups, element) => {
      const key = selectKey(element);
      if (groups[key]) {
        groups[key].push(element);
      }
      else {
        groups[key] = [element];
      }

      return groups;
    }, {});
}

const xs = [{
  "category": "a",
  "region": "Region 1",
  projects: [{
    "project": "A",
    "resources": 2,
    "status": 'green'
  }, {
    "project": "B",
    "resources": 2,
    "status": 'green'
  }, {
    "project": "C",
    "resources": 2,
    "status": 'green'
  }]
}, {
  "category": "b",
  "region": "Region 2",
  projects: [{
    "project": "F",
    "resources": 2,
    "status": 'red'
  }, {
    "project": "A",
    "resources": 4,
    "status": 'green'
  }]
}];

const byCategoryAndRegion = groupBy(
    xs,
    x => `category: ${x.category}, region: ${x.region}`
  );

console.log(byCategoryAndRegion);

I'm not entirely clear on the structure and orientation of your data, but this should be enough to get you going.


text:
Q: How to perform default double click action on customised NSToolbarItem with label I have created a custom toolbar using storyboard which contain a toolbar item with label and buttons. The default double click action to zoom and zoom out(enlarging and minimising the window size) in toolbar is working except the label area.
How to make it work on label area also.


text:
Q: Query to get all subtasks associated to stories with some labels in Jira I have user stories in Jira with some labels A,B,C for example. But at subtasks level there is no labeling. Now I need to fetch count of all user stories of that label, their tasks and subtasks.
How can I get subtasks which have no labels but linked to labelled user stories?

A: You can use the following JQL if you have scriptrunner;
( issue IN subtasksOf('type=Story and labels in (label1, label2)') or issueFunction in linkedIssuesOf('type=Story and labels in (label1, label2)')  ) and labels is EMPTY

It both finds the subtasks, and linked issues of the story labeled as label1 and label2. It also filters the issues with own labels.

A: issuetype in (Task-Agile, Sub-task) AND issueFunction in subtasksOf("Issuetype = story")


text:
Q: Error with rendering webapp2 Good personal day. I have a problem I can not solve.
It happens that in an app I am creating have 3 files: main, config and home.html
The config file is used to grab the User data and adds them to a dictionary. If the User does not exist in the datastore, he adds.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from google.appengine.api.app_identity import get_default_version_hostname
from google.appengine.ext import ndb

url = get_default_version_hostname()

# BANCO DE DADOS PARA SALVAR OS USUARIOS
class Usuarios(ndb.Model):
    usuario = ndb.StringProperty()
    email = ndb.StringProperty()
    openID = ndb.StringProperty()
    date = ndb.DateTimeProperty(auto_now_add=True)
    tipo = ndb.StringProperty()

    @classmethod
    def get_email(cls, email):
        return cls.query(cls.email == email)

########### FUNÇÃO QUE CHAMA O LOGIN ########
def Redir():
    from google.appengine.api import users
    user = users.get_current_user()
    if users.is_current_user_admin():
        redir = 'http://' + url + '/admin'
    else:
        redir = None

    return redir

def Geral():
    from google.appengine.api import users
    user = users.get_current_user()

    providers = {
        'Google' : 'https://www.google.com/accounts/o8/id',
        'Yahoo'  : 'yahoo.com',
    }

    rt = {
        'titulo'    : 'BETA HCBravin',
        'version'   : '14.11.10',
        'url'       : 'http://' + url,
    }

    # O USUARIO ESTA LOGADO?
    if user:

        # O USUARIO E UM ADMINISTRADOR?
        if users.is_current_user_admin():
            dtipo = 'Admin'
        else:
            dtipo = 'Aluno'

        # O USUARIO EXISTE NO BANCO DE DADOS?
        quser = Usuarios.get_email(user.email())
        buser = quser.get()

        if not buser:
            dbuser = Usuarios(usuario = user.nickname(), email = user.email(), openID = user.user_id(), tipo = dtipo)
            dbuser.put()

        if buser:
            if buser.tipo == 'Professor':
                dtipo = 'Professor'
            else:
                dtipo = 'Aluno'

        rt.update({
            'usuario': user.nickname(),
            'email': user.email(),
            'openID': user.user_id(),
            'logout_url': users.create_logout_url('/'),
            'user_tipo': dtipo,
        })

    else:     # let user choose authenticator
        #login_url = {'usuario': None, 'logout_url': None}
        login_url = {}
        for name, uri in providers.items():
            login_url[name] = users.create_login_url(dest_url = ('/usuario') ,federated_identity=uri)

        rt.update(login_url)

    return rt
########## ############

The main file is the main file of my app that renders the home.html
import os
import webapp2
import sys

from config import *
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

Geral = Geral()

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write(template.render('home.html', Geral))

app = webapp2.WSGIApplication([
    ('/', MainHandler),
], debug=True)

The home.html only has a value that is to get the variable {{ email }} and print it (that's not all, logical. Bad am using the simplest approach)
The problem:
When I login with a User it perfectly shows email. But if I make out and User mute rather than appearing email "new" keeps popping up the old email.
This remains even if I refresh the page.

A: Code outside of your handler's methods only runs when an instance starts up, not once-per-request.
So your call Geral = Geral() isn't being repeated for visitors after the first one.
You should move that line inside your get method (and probably change the name on the left-hand-side to not be the same as the function-name!)


text:
Q: Float required : [Python 2.7] Error:  c = float(math.sqrt(num))
TypeError: a float is required
Code:
def sqrtn(num):
    c = float(math.sqrt(num))
    print "The square root of %s " % c

num = raw_input("Enter the no.")
if choice == 6:
    sqrtn(num)

SOLUTION:
num = float(raw_input("Enter the no."))

A: raw_input returns a string and you passed it to sqrt without converting to int or float.
You can use a while loop in order to ask a number from user until it enters a valid number.
while True:
    try:
        num = int(raw_input("Enter the no."))
    except ValueError:
        print "Enter a valid number"
    else:
        break

if choice == 6:
    sqrtn(num)


text:
Q: Change Background Image using JQuery is successful but how to make one image default I have a jQuery and, using it, I am changing my body background-image but how to make one image default when the page loads:
This is my jQuery under the head tag
var backImage = new Array();

backImage[0] = "images/1.jpg";

backImage[1] = "images/2.jpg";

backImage[2] = "images/3.jpg";

function changeBGImage(whichImage)

{

if (document.body){

document.body.background = backImage[whichImage];

}

links - clicking on this the background image changes:
1
2
3

A: You could launch the function when the page loads using the default image index.
For example
$(window).ready(function(){
    changeBGImage(0);
});

or maybe if you want to set it before hand, you could just add it to the styles of the body tag


text:
Q: TextExpander, Shell Scripts, & Node I'm trying to execute some JS code as a Node shell script, but it doesn't seem to work and I can't tell why. Here's a simple example:
#!/usr/bin/env node

console.log("foo");

The above script should result in TextExpander inserting the text "foo" when triggered, but it doesn't. The equivalent in bash works just as expected:
#!/usr/bin/env bash

echo "foo"

I thought that perhaps the call to console.log() was not producing something where TextExpander was looking (although all the documentation I can find says that should print to stdout). I've even tried a workaround like this with no luck:
#!/usr/bin/env sh

echo `/usr/bin/env node -e "console.log(\"foo\");"`

I get no visible errors or any indication that something went wrong. I just end up with TextExpander inserting an empty string instead of "foo". Any TextExpander experts out that that might see something I'm missing? Running the non-working examples in the shell work fine, and TextExpander is supposed to just capture whatever goes to stdout. I'm not even sure how I could go about debugging this.

A: Make sure the directory where your node executable is installed is listed in the PATH environment variable that TextExpander will use. This is probably not the same PATH value one you get in your terminal. To verify if that's the problem, try doing #!/full/path/to/node as your shebang line instead of running env. My guess is node is installed either in /usr/local/bin/node or somewhere under your home directory and neither of those are in the PATH that TextExpander has while running.

A: You probably should add -e to your header:
#!/usr/bin/env node -e

Or
You probably should add -e to your header:
#!/path/to/node -e


text:
Q: Running react app in browser issues with saving and editing When I run my react app in the browser, everything works fine. But when I edit a file and it gets saved, I can still see the whole app but when I try to interact with it, it doesnt work, I did inspect element and saw that the body that does not contain anything. Yet the content is visible?
Any solutions?

Note , when I reload the page everything is working fine, but its annoying to have to reload the page everytime.

A: Next JS is based on Reactjs and this might be helpful for you:
Limitations

Fast Refresh tries to preserve local React state in the component
you're editing, but only if it's safe to do so. Here's a few reasons
why you might see local state being reset on every edit to a file:
Local state is not preserved for class components (only function components and Hooks preserve state).
The file you're editing might have other exports in addition to a React component.
Sometimes, a file would export the result of calling a higher-order component like HOC(WrappedComponent). If the returned
component is a class, its state will be reset.
Anonymous arrow functions like export default () => ; cause Fast Refresh to not preserve local component state. For large
codebases you can use our name-default-component codemod.
As more of your codebase moves to function components and Hooks, you
can expect state to be preserved in more cases.

You can read more about FastRefresh from the documentation.


text:
Q: Jitsi meet API persistnet room with password I want to create a chat room using the Jitsi meet API (https://jitsi.github.io/handbook/docs/dev-guide/dev-guide-iframe) in advance with the room having a password.
The problem which I encounter is that the pwd is only valid as long as the meeting is active. I cannot be planned in advance. The Jitsi API has no events or options I could use.
Below is the code I use to construct the Jitsi object. But the room is created right away and when the window which started the room is closed all the settings are lost. I am really stuck here. Please advise ....
var callback = function(records) {

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

        if (records[i].get("key") == 'JitsiServerURL') {

            var val = records[i].get("value");
            var api = new JitsiMeetExternalAPI(val, options);
        }
}

me.jitsiObject = api;

if (pwd != "") {

  setTimeout(() => {

    // when local user is trying to enter in a locked room
    api.addEventListener('passwordRequired', () => {
        api.executeCommand('password', pwd);
    });

    // when local user has joined the video conference
    api.addEventListener('videoConferenceJoined', (response) => {
        api.executeCommand('password', pwd);
    });

}, 10);

}

};

A: Before doing these things first you have to setup Jitsi in your own server under your own domain. After that, you have to enable password-based authentication into your Jitsi server. After that, you can control your Jitsi server using the iframe API.


text:
Q: Add legend for every point on ggmap instead of a group variable I've got the following data frame:
df1 <- structure(list(City = c("Ahmadnagar", "Ahmedabad", "Akola", "Amaravati",
"Aurangabad", "Belgaum", "Bellary", "Bengaluru", "Bhavnagar",
"Bhiwandi", "Bhuj", "Bhusaval", "Bidar", "Bijapur", "Chanda",
"Chikka Mandya", "Chirala", "Davangere", "Ghandinagar", "Gulbarga",
"Guntur", "Hindupur", "Hospet", "Hubli", "Kagaznagar", "Kakinada",
"Kalyan", "Khanapur", "Kolar", "Kolhapur", "Kurnool", "Latur",
"Machilipatnam", "Malegaon Camp", "Mangalore", "Mumbai", "Mysore",
"Nanded", "Nandyal", "Nasik", "Navsari", "Nellore", "Ongole",
"Parbhani", "Porbandar", "Proddatur", "Pune", "Raichur", "Rajahmundry",
"Rajkot", "Sangli", "Shimoga", "Surat", "Tirupati", "Vadodara",
"Vishakhapatnam", "Vizianagaram"), State = c("Maharashtra", "Gujarat",
"Maharashtra", "Maharashtra", "Maharashtra", "Karnataka", "Karnataka",
"Karnataka", "Gujarat", "Maharashtra", "Gujarat", "Maharashtra",
"Karnataka", "Karnataka", "Maharashtra", "Karnataka", "Andhra Pradesh",
"Karnataka", "Gujarat", "Karnataka", "Andhra Pradesh", "Andhra Pradesh",
"Karnataka", "Karnataka", "Andhra Pradesh", "Andhra Pradesh",
"Maharashtra", "Maharashtra", "Karnataka", "Maharashtra", "Andhra Pradesh",
"Maharashtra", "Andhra Pradesh", "Maharashtra", "Karnataka",
"Maharashtra", "Karnataka", "Maharashtra", "Andhra Pradesh",
"Maharashtra", "Gujarat", "Andhra Pradesh", "Andhra Pradesh",
"Maharashtra", "Gujarat", "Andhra Pradesh", "Maharashtra", "Karnataka",
"Andhra Pradesh", "Gujarat", "Maharashtra", "Karnataka", "Gujarat",
"Andhra Pradesh", "Gujarat", "Andhra Pradesh", "Andhra Pradesh"
), Lat = c(19.094571, 23.025793, 20.709569, 20.933272, 19.880943,
15.862643, 15.142049, 12.977063, 21.774455, 19.300229, 23.253972,
21.043649, 17.913309, 16.827715, 19.950758, 12.545602, 15.823849,
14.469237, 23.216667, 17.335827, 16.299737, 13.828065, 15.269537,
15.349955, 19.331589, 16.960361, 19.243703, 21.273716, 13.137679,
16.695633, 15.828865, 18.399487, 16.187466, 20.569974, 12.865371,
18.987807, 12.292664, 19.160227, 15.477994, 19.999963, 20.85,
14.449918, 15.503565, 19.268553, 21.641346, 14.7502, 18.513271,
16.205459, 17.005171, 22.291606, 16.856777, 13.932424, 21.195944,
13.635505, 22.299405, 17.704052, 18.11329), Long = c(74.738432,
72.587265, 76.998103, 77.75152, 75.346739, 74.508534, 76.92398,
77.587106, 72.152496, 73.058813, 69.669281, 75.785058, 77.530105,
75.718988, 79.295229, 76.895078, 80.352187, 75.92375, 72.683333,
76.83757, 80.457293, 77.491425, 76.387103, 75.138619, 79.466051,
82.238086, 73.135537, 76.117376, 78.129989, 74.231669, 78.036021,
76.584252, 81.13888, 74.515415, 74.842432, 72.836447, 76.638543,
77.314971, 78.483605, 73.776887, 72.916667, 79.986967, 80.044541,
76.770807, 69.600868, 78.548129, 73.849852, 77.35567, 81.777839,
70.793217, 74.569196, 75.572555, 72.830232, 79.419888, 73.208119,
83.297663, 83.397743)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -57L), groups = structure(list(
    State = c("Andhra Pradesh", "Andhra Pradesh", "Andhra Pradesh",
    "Andhra Pradesh", "Andhra Pradesh", "Andhra Pradesh", "Andhra Pradesh",
    "Andhra Pradesh", "Andhra Pradesh", "Andhra Pradesh", "Andhra Pradesh",
    "Andhra Pradesh", "Andhra Pradesh", "Andhra Pradesh", "Andhra Pradesh",
    "Gujarat", "Gujarat", "Gujarat", "Gujarat", "Gujarat", "Gujarat",
    "Gujarat", "Gujarat", "Gujarat", "Karnataka", "Karnataka",
    "Karnataka", "Karnataka", "Karnataka", "Karnataka", "Karnataka",
    "Karnataka", "Karnataka", "Karnataka", "Karnataka", "Karnataka",
    "Karnataka", "Karnataka", "Karnataka", "Maharashtra", "Maharashtra",
    "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra",
    "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra",
    "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra",
    "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra"
    ), City = c("Chirala", "Guntur", "Hindupur", "Kagaznagar",
    "Kakinada", "Kurnool", "Machilipatnam", "Nandyal", "Nellore",
    "Ongole", "Proddatur", "Rajahmundry", "Tirupati", "Vishakhapatnam",
    "Vizianagaram", "Ahmedabad", "Bhavnagar", "Bhuj", "Ghandinagar",
    "Navsari", "Porbandar", "Rajkot", "Surat", "Vadodara", "Belgaum",
    "Bellary", "Bengaluru", "Bidar", "Bijapur", "Chikka Mandya",
    "Davangere", "Gulbarga", "Hospet", "Hubli", "Kolar", "Mangalore",
    "Mysore", "Raichur", "Shimoga", "Ahmadnagar", "Akola", "Amaravati",
    "Aurangabad", "Bhiwandi", "Bhusaval", "Chanda", "Kalyan",
    "Khanapur", "Kolhapur", "Latur", "Malegaon Camp", "Mumbai",
    "Nanded", "Nasik", "Parbhani", "Pune", "Sangli"), .rows = structure(list(
        17L, 21L, 22L, 25L, 26L, 31L, 33L, 39L, 42L, 43L, 46L,
        49L, 54L, 56L, 57L, 2L, 9L, 11L, 19L, 41L, 45L, 50L,
        53L, 55L, 6L, 7L, 8L, 13L, 14L, 16L, 18L, 20L, 23L, 24L,
        29L, 35L, 37L, 48L, 52L, 1L, 3L, 4L, 5L, 10L, 12L, 15L,
        27L, 28L, 30L, 32L, 34L, 36L, 38L, 40L, 44L, 47L, 51L), ptype = integer(0), class = c("vctrs_list_of",
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -57L), .drop = TRUE))

From which I'm generating a plot like this:

Here's the code I've used:
india_centroid <- c(68.18625, 6, 97.41529, 37)
req_zoom <- calc_zoom(Long, Lat, data = df1)
map_type = get_map(location = india_centroid, zoom = req_zoom, maptype = "roadmap")
p <- ggmap(map_type, extent = "panel", legend = "bottomright", size = c(500, 600))
p + geom_point(aes(x = Long, y = Lat, color = State), data = df1) +
  ggtitle("Cities in India")

So, as you can see the points are color based on the State column. And legend shows the names of the states they belong to. But, I need the legend to show the name of every City instead of the state while the colors are the same as per state.
I know that doesn't make sense because the colors will be repeated for the cities in the same state. But I need to know if it is possible. And if possible, how to achieve it.

A: One option would be to create a named vector of colors which assigns to each City the State color. Doing so you could map City on the color aes and set the colors according to the color vector via scale_color_manual:
library(ggmap)
library(dplyr)

# State colors
col_state <- data.frame(
  State = sort(unique(df1$State)),
  color = scales::hue_pal()(length(unique(df1$State)))
)
# City colors
col_city <- df1 %>%
  ungroup() %>%
  select(City, State) %>%
  distinct() %>%
  left_join(col_state) %>%
  select(-State) %>%
  tibble::deframe()

india_centroid <- c(68.18625, 6, 97.41529, 37)
req_zoom <- calc_zoom(Long, Lat, data = df1)
map_type = get_map(location = india_centroid, zoom = req_zoom, maptype = "roadmap")
p <- ggmap(map_type, extent = "panel", legend = "bottomright", size = c(500, 600))
p + geom_point(aes(x = Long, y = Lat, color = City), data = df1) +
  scale_color_manual(values = col_city) +
  ggtitle("Cities in India")

EDIT To order the legend by State you have to set the breaks accordingly. To this I would suggest to rearrange you dataset by State and make use of forcats::fct_inorder to convert your City column to a factor. Afterwards you could set the breaks of the color scale according to the levels of the City factor:
df1 <- df1 %>%
  arrange(State, City) %>%
  mutate(City = forcats::fct_inorder(City))

p + geom_point(aes(x = Long, y = Lat, color = City), data = df1) +
  scale_color_manual(values = col_city, breaks = levels(df1$City)) +
  ggtitle("Cities in India")


text:
Q: why my ec2 in one region is trying to use private IP of another ec2 instance in another region? Our company has a github setup in an aws EC2 instance. This of course will have private/public ip.
I have setup two ec2 instances in a different region.
However, when I try connection to our company's github from one instance, it will use the public ip of the github instance. In this case, the connection is successful and I can clone and do stuff.
But the other will use private ip of the github instance. In this case, it fails to connect to the github instance.
BTW, the second one(the one that fails) was migrated from the region where github instance is setup, to a different region. Saved the AMI and relaunched it in the new region.
Here is an example describing my symptom.
I will assume:

*

*instanceA: migrated ec2 instance to new region

*instanceB: newly created ec2 instance in new region

*github instance: has domain name "git.blahblah.com". It has private IP of 172.31.xx.xx , public IP of 52.78.xx.xx
instanceA
$ ping git.blahblah.com
PING git.blahblah.com (172.31.xx.xx) 56(84) bytes of data.

instanceB
$ ping git.blahblah.com
PING git.blahblah.com (52.78.xx.xx) 56(84) bytes of data.

What can be causing it? Any hints on tackling this issue?

I have checked DNS settings by `$ cat /etc/resolv.conf
instanceA
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 172.30.0.2
search us-west-2.compute.internal

instanceB
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 172.30.0.2
search us-west-2.compute.internal

the security group of instanceA and instanceB is identical.
VPC ID, subnet ID of instanceA and instanceB is identical.

A: How dumb of me.
The cause of this was that in instanceA(migrated from a different region) had some configurations in /etc/hosts file that forced git.blahblah.com to refer to the private IP of it.
$ cat /etc/hosts
127.0.0.1 localhost
172.31.xx.xx git.blahblah.com

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

After I deleted 172.31.xx.xx git.blahblah.com line from the hosts file, it worked.
Sorry for my stupidity people.

A: Because your instances are in different regions from the github instance then they are not over the same network, and hence they can't connect to the private ip address.
If you want them to connect together over the same ip address then they need to be in the same VPC and to get there they need to be on the same region (but not the same availability zone)

A private IPv4 address is an IP address that's not reachable over the
  Internet. You can use private IPv4 addresses for communication between
  instances in the same network (EC2-Classic or a VPC).

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html#concepts-private-addresses


text:
Q: Is there good method to clockwise rotate M*N 2D matrix in place, assuming matrix space of max(M,N)*max(M,N) Appreciate any advice. Searched internet but no luck
Assuming the original matrix has big enough size for rotation in place
{{1,2},{3,4},{5,6}} to {{5,3,1},{6,4,2}}
or
1 2
3 4
5 6
clockwise rotation to
5 3 1
6 4 2
Thanks.

A: One easy way would be a combination of flipping a matrix left-right or up-down (which can be done trivially by in-place reversing of individual columns or rows) and doing an in-place matrix transposition, see mcdowella's link. See for example the source code of Numpy's rot90 function which does exactly these two steps (the swapaxis is basically a transpose).
You could do everything in one step, but this might require iterating over the matrix in some sort of spiral pattern. This would cost quite some effort to implement right, and, given the availability of efficient algorithms (e.g. optimized for cache access) for swapping and transposing, this might even be slower.


text:
Q: How to apply css to a native ios app? I am working on a app were I need to apply the css style sheet to the background as a theme. Is there a possibility to apply programmatically by dragging the css file into resource folder and calling it from the documents folder.
please help.
Thanks.

A: I'm using a custom CSS style sheet I added to my resources folder to style a UIWebView object. First, I put the HTML code I want to load on an NSString and I specify the style sheet I want to use. Then, I create the UIWebView object and load the HTML string with the method loadHTMLString:baseURL: where the base URL is the path to your app's bundle. Hope this helps.
NSString *bodyHTML = [NSString stringWithFormat:@" \n"
                         " \n"
                         "<link href=\"default.css\" rel=\"stylesheet\" type=\"text/css\" /> \n"
                         " \n"
                         "%@ \n"
                         "", textPost.body];

UIWebView *bodyView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 400.0, 532.0, 10.0)];
bodyView.delegate = self;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[bodyView loadHTMLString:bodyHTML baseURL:baseURL];

A: Have you tried using a UIWebView?
It offers a – loadHTMLString:baseURL: method that will allow you to do what you describe.


text:
Q: play framework file upload I'm trying to add file uploading handling in play framework app to existing form with text field.
 According to documentation, I need to use asMultipartFormData() or asRaw method, however, it can not handle text input fields. Is it possible to use one controller for standart html form and file processing at the same time?

A: Yes, it is possible, just simply handle file and text input separately. For instance:
public Result myControllerMethod(){
  ...
  //handle input text here
  Form<MyModel> myForm = formFactory.form(MyModel.class);
  MyModel myModel = myForm.bindFromRequest().get();
  ...
  //handle file
  MultipartFormData<File> body = request().body().asMultipartFormData();
  ...
}


text:
Q: Microsoft.Graph.ServiceException: Code: ExtensionError Message: Operation: Read; Exception: [A task was canceled.] I am using the following example for Microsoft Graph Webhook
https://github.com/microsoftgraph/aspnet-webhooks-rest-sample
It was working fine till yesterday, but now i am getting the following error:

Microsoft.Graph.ServiceException: Code: ExtensionError  Message: Operation: Read; Exception: [A task was canceled.]    Inner error       at Microsoft.Graph.HttpProvider.d__19.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     at Microsoft.Graph.BaseRequest.d__36.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     at Microsoft.Graph.BaseRequest.d__32`1.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     at Microsoft.Graph.SubscriptionRequest.d__6.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     at ---------------------------------------------------

Any one faced similar kind of problem to get email notification or create subscription using Microsoft.Graph API
Even i reconfigured application in
https://apps.dev.microsoft.com
and give the following permissions in "Microsoft Graph Permissions" section,
Delegated Permissions:
Mail.Read
User.Read
Application Permissions:
Mail.Read (Admin only)
User.Read.All(Admin only)
and even now while creating subscription i am also getting same kind of error:
while creating subscription token is created and webhook listen the request but when i return response back to Microsoft Graph then i am getting following error:

Microsoft.Graph.ServiceException: Code: ExtensionError  Message: Operation: Create; Exception: [A task was canceled.]
token:YTQyYTY5YzctYjg0Ny00Zjg0LWFiMWItMTQ1OGRkYmY2M2Q2

I am using following code to create subscription:
// The notificationUrl endpoint that's registered with the webhook subscription.
[HttpPost]
public async Task<ActionResult> Listen()
{

    // Validate the new subscription by sending the token back to Microsoft Graph.
    // This response is required for each subscription.
    if (Request.QueryString["validationToken"] != null)
    {
        var token = Request.QueryString["validationToken"];
        return Content(token, "plain/text");
    }

    // Parse the received notifications.
    else
    {
        try
        {
            var notifications = new Dictionary<string, Notification>();
            using (var inputStream = new StreamReader(Request.InputStream))
            {
                var jsonObject = JObject.Parse(inputStream.ReadToEnd());
                if (jsonObject != null)
                {

                    // Notifications are sent in a 'value' array. The array might contain multiple notifications for events that are
                    // registered for the same notification endpoint, and that occur within a short timespan.
                    JArray value = JArray.Parse(jsonObject["value"].ToString());
                    foreach (var notification in value)
                    {
                        var current = JsonConvert.DeserializeObject<Notification>(notification.ToString());

                        // Check client state to verify the message is from Microsoft Graph.
                        var subscription = SubscriptionCache.GetSubscriptionCache().GetSubscriptionInfo(current.SubscriptionId);

                        // This sample only works with subscriptions that are still cached.
                        if (subscription != null)
                        {
                            if (current.ClientState == subscription.ClientState)
                            {
                                // Just keep the latest notification for each resource.
                                // No point pulling data more than once.
                                notifications[current.Resource] = current;
                            }
                        }
                    }

                    if (notifications.Count > 0)
                    {

                        // Query for the changed messages.
                        await GetChangedMessagesAsync(notifications.Values);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            // TODO: Handle the exception.
            // Still return a 202 so the service doesn't resend the notification.
        }
        return new HttpStatusCodeResult(202);
    }
}


text:
Q: How to call variables from a list of variable names in R I have of list of variable names.
list = c("smoke", "ptd", "ht", "ui", "racecat", "visit")

I want to make a series of plots like this.
plot(low[somevariable=="0"] ~ age[somevariable=="0"])

I'd like to replace somevariable with each of the variable name in the list. For example,
plot(low[smoke=="0"] ~ age[smoke=="0"])
plot(low[ptd=="0"] ~ age[ptd=="0"])
......
plot(low[visit=="0"] ~ age[visit=="0"])

I've tried to create a for-loop and use a variety of things to replace somevariable, such as
list[i], paste(list[i]), substitute(list[i]), parse(list[i])

But none of them works. I would really appreciate some help. Thanks in advance.

A: You can try:
for(i in list) {
  print(plot(low[get(i) == 0] ~ low[get(i) == 0]))
}

A: Here's a trivial example of what you want to do. You can use get to find the relevant named variable:
set.seed(1)
x <- rnorm(100)
y <- rnorm(100)
a <- rnorm(100)
b <- rnorm(100,10)
l <- c('a','b')
# histogram of one variable:
hist(get(l[1]))
# loop of plots more like what you're going for:
for(i in 1:length(l)) plot(y[get(l[i])<2] ~ x[get(l[i])<2])

A: Find the solution:
eval(parse(text=list[i]))

There is another post addressing the same thing:
Getting strings recognized as variable names in R