问:
How to debug pandas_udfs without having to use Spark?
If I'm using Python Transforms in Palantir Foundry and I'm trying to run an algorithm which uses in-memory/non-spark libraries, and I want it automatically scale and work in Spark (not pandas).  If I'm having a hard time writing the code and want to test and develop it locally, yet use the same code in pyspark later, how do I do this?

For a concrete example, I want to calculate the area of a geojson column which contains a polygon. Since I would need to use some libraries which arn't native to Spark (shapely and pyproj).  I know that the best way (performance wise) is to use a pandas_udf (otherwise known as streaming udfs or vectorized udfs).  But after reading a couple of guides, specifically Introducing Pandas UDF for PySpark, pandas user-defined functions
 and Modeling at Scale with Pandas UDFs w/code examples, it's still challenging to debug and get working, and it seems like I can't use break statements and there isn't a first class way to log/print.

The actual dataframe would have millions of rows (relating to millions of polygons), but for simplicity I wanted to test locally with a simple dataframe, and it scale to larger dataset later:

```<code>df = spark.createDataFrame(
    [
        ("AFG", "{\"type\":\"Polygon\",\"coordinates\":[[[61.210817,35.650072],[62.230651,35.270664],[62.984662,35.404041],[63.193538,35.857166],[63.982896,36.007957],[64.546479,36.312073],[64.746105,37.111818],[65.588948,37.305217],[65.745631,37.661164],[66.217385,37.39379],[66.518607,37.362784],[67.075782,37.356144],[67.83,37.144994],[68.135562,37.023115],[68.859446,37.344336],[69.196273,37.151144],[69.518785,37.608997],[70.116578,37.588223],[70.270574,37.735165],[70.376304,38.138396],[70.806821,38.486282],[71.348131,38.258905],[71.239404,37.953265],[71.541918,37.905774],[71.448693,37.065645],[71.844638,36.738171],[72.193041,36.948288],[72.63689,37.047558],[73.260056,37.495257],[73.948696,37.421566],[74.980002,37.41999],[75.158028,37.133031],[74.575893,37.020841],[74.067552,36.836176],[72.920025,36.720007],[71.846292,36.509942],[71.262348,36.074388],[71.498768,35.650563],[71.613076,35.153203],[71.115019,34.733126],[71.156773,34.348911],[70.881803,33.988856],[69.930543,34.02012],[70.323594,33.358533],[69.687147,33.105499],[69.262522,32.501944],[69.317764,31.901412],[68.926677,31.620189],[68.556932,31.71331],[67.792689,31.58293],[67.683394,31.303154],[66.938891,31.304911],[66.381458,30.738899],[66.346473,29.887943],[65.046862,29.472181],[64.350419,29.560031],[64.148002,29.340819],[63.550261,29.468331],[62.549857,29.318572],[60.874248,29.829239],[61.781222,30.73585],[61.699314,31.379506],[60.941945,31.548075],[60.863655,32.18292],[60.536078,32.981269],[60.9637,33.528832],[60.52843,33.676446],[60.803193,34.404102],[61.210817,35.650072]]]}"),
        ("ALB", "{\"type\":\"Polygon\",\"coordinates\":[[[20.590247,41.855404],[20.463175,41.515089],[20.605182,41.086226],[21.02004,40.842727],[20.99999,40.580004],[20.674997,40.435],[20.615,40.110007],[20.150016,39.624998],[19.98,39.694993],[19.960002,39.915006],[19.406082,40.250773],[19.319059,40.72723],[19.40355,41.409566],[19.540027,41.719986],[19.371769,41.877548],[19.304486,42.195745],[19.738051,42.688247],[19.801613,42.500093],[20.0707,42.58863],[20.283755,42.32026],[20.52295,42.21787],[20.590247,41.855404]]]}"),
    ],# can continue with more countries  from https://raw.githubusercontent.com/johan/world.geo.json/34c96bba9c07d2ceb30696c599bb51a5b939b20f/countries.geo.json
    ["country", "geometry"]
)
</code>```

Given the geometry column which is actually geojson, how can I calculate the area in square-meters using a good GIS approach? For example using the methods outlined in these questions:

Calculate Polygon area in planar units (e.g. square-meters) in Shapely

How do I get the area of a GeoJSON polygon with Python

How to calculate the area of a polygon on the earth's surface using python?

答:
The way you can think about pandas_udfs is that you are writing your logic to be applied to a pandas series. This means that you would be applying an operation and it would automatically apply to every row.

If you want to develop this locally, you can actually take a much smaller sample of your data (like you did), and have it stored in a pandas series, and get it working there:

```<code>from shapely.geometry import Polygon
import json
from pyproj import Geod

#just select the column you want to use the pandas udf
pdf = df.select("geometry").toPandas()

#convert to pandas series
pdf_geom_raw = pdf.ix[:,0]

#how to apply converting string to json/dict
pdf_geom = pdf_geom_raw.apply(json.loads)

# function using non-spark functions
def get_area(shape):
    geod = Geod(ellps="WGS84")
    poly = Polygon(shape["coordinates"][0])
    area = abs(geod.geometry_area_perimeter(poly)[0])
    return area
pdf_geom = pdf_geom.apply(get_area)
</code>```

Here you could just try it locally (without spark) by replacing pdf = df.select("geometry").toPandas() to pdf = pd.read_csv("geo.csv")

Now that you have it working locally, you can copy paste the code in your pandas_udf

```<code>from shapely.geometry import Polygon
import json
from pyproj import Geod
from pyspark.sql.functions import pandas_udf, PandasUDFType

@pandas_udf('double', PandasUDFType.SCALAR)
def geodesic_polygon_area(pdf_geom):
    pdf_geom = pdf_geom.apply(json.loads)
    def get_area(shape):
        geod = Geod(ellps="WGS84")
        poly = Polygon(shape["coordinates"][0])
        area = abs(geod.geometry_area_perimeter(poly)[0])
        return area

    pdf_geom = pdf_geom.apply(get_area)
    return pdf_geom

df =  df.withColumn('area_square_meters', geodesic_polygon_area(df.geometry))
</code>```

When running the code:

```<code>>>> df.show()
+-------+--------------------+--------------------+
|country|            geometry|  area_square_meters|
+-------+--------------------+--------------------+
|    AFG|{"type":"Polygon"...|6.522700837770404E11|
|    ALB|{"type":"Polygon"...|2.969479517410540...|
+-------+--------------------+--------------------+
</code>```

Body:
If I'm using Python Transforms in Palantir Foundry and I'm trying to run an algorithm which uses in-memory/non-spark libraries, and I want it automatically scale and work in Spark (not pandas).  If I'm having a hard time writing the code and want to test and develop it locally, yet use the same code in pyspark later, how do I do this?

For a concrete example, I want to calculate the area of a geojson column which contains a polygon. Since I would need to use some libraries which arn't native to Spark (shapely and pyproj).  I know that the best way (performance wise) is to use a pandas_udf (otherwise known as streaming udfs or vectorized udfs).  But after reading a couple of guides, specifically Introducing Pandas UDF for PySpark, pandas user-defined functions
 and Modeling at Scale with Pandas UDFs w/code examples, it's still challenging to debug and get working, and it seems like I can't use break statements and there isn't a first class way to log/print.

The actual dataframe would have millions of rows (relating to millions of polygons), but for simplicity I wanted to test locally with a simple dataframe, and it scale to larger dataset later:

df = spark.createDataFrame(
    [
        ("AFG", "{\"type\":\"Polygon\",\"coordinates\":[[[61.210817,35.650072],[62.230651,35.270664],[62.984662,35.404041],[63.193538,35.857166],[63.982896,36.007957],[64.546479,36.312073],[64.746105,37.111818],[65.588948,37.305217],[65.745631,37.661164],[66.217385,37.39379],[66.518607,37.362784],[67.075782,37.356144],[67.83,37.144994],[68.135562,37.023115],[68.859446,37.344336],[69.196273,37.151144],[69.518785,37.608997],[70.116578,37.588223],[70.270574,37.735165],[70.376304,38.138396],[70.806821,38.486282],[71.348131,38.258905],[71.239404,37.953265],[71.541918,37.905774],[71.448693,37.065645],[71.844638,36.738171],[72.193041,36.948288],[72.63689,37.047558],[73.260056,37.495257],[73.948696,37.421566],[74.980002,37.41999],[75.158028,37.133031],[74.575893,37.020841],[74.067552,36.836176],[72.920025,36.720007],[71.846292,36.509942],[71.262348,36.074388],[71.498768,35.650563],[71.613076,35.153203],[71.115019,34.733126],[71.156773,34.348911],[70.881803,33.988856],[69.930543,34.02012],[70.323594,33.358533],[69.687147,33.105499],[69.262522,32.501944],[69.317764,31.901412],[68.926677,31.620189],[68.556932,31.71331],[67.792689,31.58293],[67.683394,31.303154],[66.938891,31.304911],[66.381458,30.738899],[66.346473,29.887943],[65.046862,29.472181],[64.350419,29.560031],[64.148002,29.340819],[63.550261,29.468331],[62.549857,29.318572],[60.874248,29.829239],[61.781222,30.73585],[61.699314,31.379506],[60.941945,31.548075],[60.863655,32.18292],[60.536078,32.981269],[60.9637,33.528832],[60.52843,33.676446],[60.803193,34.404102],[61.210817,35.650072]]]}"),
        ("ALB", "{\"type\":\"Polygon\",\"coordinates\":[[[20.590247,41.855404],[20.463175,41.515089],[20.605182,41.086226],[21.02004,40.842727],[20.99999,40.580004],[20.674997,40.435],[20.615,40.110007],[20.150016,39.624998],[19.98,39.694993],[19.960002,39.915006],[19.406082,40.250773],[19.319059,40.72723],[19.40355,41.409566],[19.540027,41.719986],[19.371769,41.877548],[19.304486,42.195745],[19.738051,42.688247],[19.801613,42.500093],[20.0707,42.58863],[20.283755,42.32026],[20.52295,42.21787],[20.590247,41.855404]]]}"),
    ],# can continue with more countries  from https://raw.githubusercontent.com/johan/world.geo.json/34c96bba9c07d2ceb30696c599bb51a5b939b20f/countries.geo.json
    ["country", "geometry"]
)

Given the geometry column which is actually geojson, how can I calculate the area in square-meters using a good GIS approach? For example using the methods outlined in these questions:

Calculate Polygon area in planar units (e.g. square-meters) in Shapely

How do I get the area of a GeoJSON polygon with Python

How to calculate the area of a polygon on the earth's surface using python?

Body:
The way you can think about pandas_udfs is that you are writing your logic to be applied to a pandas series. This means that you would be applying an operation and it would automatically apply to every row.

If you want to develop this locally, you can actually take a much smaller sample of your data (like you did), and have it stored in a pandas series, and get it working there:

from shapely.geometry import Polygon
import json
from pyproj import Geod

#just select the column you want to use the pandas udf
pdf = df.select("geometry").toPandas()

#convert to pandas series
pdf_geom_raw = pdf.ix[:,0]

#how to apply converting string to json/dict
pdf_geom = pdf_geom_raw.apply(json.loads)

# function using non-spark functions
def get_area(shape):
    geod = Geod(ellps="WGS84")
    poly = Polygon(shape["coordinates"][0])
    area = abs(geod.geometry_area_perimeter(poly)[0])
    return area
pdf_geom = pdf_geom.apply(get_area)

Here you could just try it locally (without spark) by replacing pdf = df.select("geometry").toPandas() to pdf = pd.read_csv("geo.csv")

Now that you have it working locally, you can copy paste the code in your pandas_udf

from shapely.geometry import Polygon
import json
from pyproj import Geod
from pyspark.sql.functions import pandas_udf, PandasUDFType

@pandas_udf('double', PandasUDFType.SCALAR)
def geodesic_polygon_area(pdf_geom):
    pdf_geom = pdf_geom.apply(json.loads)
    def get_area(shape):
        geod = Geod(ellps="WGS84")
        poly = Polygon(shape["coordinates"][0])
        area = abs(geod.geometry_area_perimeter(poly)[0])
        return area

    pdf_geom = pdf_geom.apply(get_area)
    return pdf_geom

df =  df.withColumn('area_square_meters', geodesic_polygon_area(df.geometry))

When running the code:

>>> df.show()
+-------+--------------------+--------------------+
|country|            geometry|  area_square_meters|
+-------+--------------------+--------------------+
|    AFG|{"type":"Polygon"...|6.522700837770404E11|
|    ALB|{"type":"Polygon"...|2.969479517410540...|
+-------+--------------------+--------------------+


来源: stackexchange

问:
Why don't we use const char* const for constant strings?
The standard way to store a constant string in C++ is to use const char*.
In the interest of precision and correctness, why don't we write const char* const?

Is this just because people are too lazy to write the extra const, or because it really does have some disadvantage?

EDIT: I guess I should have been more clear, I don't need an explanation for what const does in it's various positions, I know that much already. What I wanted to know is why the convention is to write const char* even for variables where you know you are never going to change the pointer. Is it just a matter of taste and style?

答:
const applies to the thing on its left, unless there is nothing there, then it applies to the thing on its right instead.

In const char * (which can also be written as char const *), the const is on the char, not on the *.  So, this declares a mutable pointer to immutable const char data.  Data cannot be modified through the pointer, but the pointer itself can be freely changed to point at any const char memory, eg:

const char *ptr = "string 1"; // OK
*ptr = 'S'; // illegal
ptr = "string 2"; // OK
```
<p>In <code>const char * const</code> (which can also be written as <code>char const * const</code>), there is a <code>const</code> on the <code>char</code>, and there is a <code>const</code> on the <code>*</code>.  So, this declares an <em>immutable</em> <code>const</code> pointer to <em>immutable</em> <code>const char</code> data.  Data cannot be modified through the pointer, and the pointer itself cannot be changed once it has been initialized, eg:</p>
<pre class="lang-cpp prettyprint-override"><code>const char * const ptr = "string 1"; // OK
*ptr = 'S'; // illegal
ptr = "string 2"; // illegal
</code>```

And just for completeness, let's also consider char * const. The const is on the *, not on the char.  So, this declares an immutable const pointer to mutable char data.  Data can freely be modified through the pointer, but the pointer itself cannot be changed once it has been initialized, eg:

char buffer1[] = "string 1", buffer2[] = "string 2";
char * const ptr = buffer1; // OK
*ptr = 'S'; // OK
ptr = buffer2; // illegal
```

What I wanted to know is why the convention is to write const char* even for variables where you know you are never going to change the pointer. Is it just a matter of taste and style?

Basically, yes.  There is nothing stopping you from writing const char * const if you KNOW you are not going to change the pointer.  It is just not a requirement, so it is very easy to omit the const on the pointer itself.  What is more important is the type of data that the pointer is pointing at.

ContentLicense: CC BY-SA 4.0

Body:
const applies to the thing on its left, unless there is nothing there, then it applies to the thing on its right instead.

In const char * (which can also be written as char const *), the const is on the char, not on the *.  So, this declares a mutable pointer to immutable const char data.  Data cannot be modified through the pointer, but the pointer itself can be freely changed to point at any const char memory, eg:

const char *ptr = "string 1"; // OK
*ptr = 'S'; // illegal
ptr = "string 2"; // OK

In const char * const (which can also be written as char const * const), there is a const on the char, and there is a const on the *.  So, this declares an immutable const pointer to immutable const char data.  Data cannot be modified through the pointer, and the pointer itself cannot be changed once it has been initialized, eg:

const char * const ptr = "string 1"; // OK
*ptr = 'S'; // illegal
ptr = "string 2"; // illegal

And just for completeness, let's also consider char * const. The const is on the *, not on the char.  So, this declares an immutable const pointer to mutable char data.  Data can freely be modified through the pointer, but the pointer itself cannot be changed once it has been initialized, eg:

char buffer1[] = "string 1", buffer2[] = "string 2";
char * const ptr = buffer1; // OK
*ptr = 'S'; // OK
ptr = buffer2; // illegal

What I wanted to know is why the convention is to write const char* even for variables where you know you are never going to change the pointer. Is it just a matter of taste and style?

Basically, yes.  There is nothing stopping you from writing const char * const if you KNOW you are not going to change the pointer.  It is just not a requirement, so it is very easy to omit the const on the pointer itself.  What is more important is the type of data that the pointer is pointing at.


问:
How to avoid OpenSSL error using pyinstaller?
I was able to directly execute the python script in the environment.
But after I compile the package using pyinstaller:
pyinstaller myscript.py --onefile
When executing the execuable binary, there will be an OpenSSL error:

```<code>$ ./dist/myscript
crypto/fips/fips.c:153: OpenSSL internal error: FATAL FIPS SELFTEST FAILURE
Aborted (core dumped)
</code>```

But if I directly run the script with:
python myscript.py
There will be no such error.

How can I avoid such error when compiling with pyinstaller?
Thanks.

答:
This usually happens, if you installed python with FIPS enabled/disabled or if Python is consuming a module/library or something, where python/ or that library/ python pyInstaller may be consuming artifact (that was built on some machine previously, with FIPS Disabled); using such executables on another machine(where FIPS is actually enabled) i.e. where FIPS setting doesn't match where you built the app in python or that library, then, you'll get such errors.

Ex: A package manager ex: yum / zypper installed python without openssl(pre-installed/pre-requisite for python installing it's stuff for ssl stuff) and if python is now installed without it, then later any app/running that app wants openssl processing, Python won't work even if you install openssl later on the machine and spit out bunch of gnarly openssl errors.

Similarly, if something was installed/packaged/built using FIPS disabled (on some machine) anything, either the app or any library that you are consuming... then later, if you try to run the app or app(using a library/package), and at that time, if you current machine has FIPS = 1 (enabled) then you'll see this error.

In my case, cmake was failing for running simple cmake --version and all I did was used a cmake .tar bundle which may have been compiled/set for the target machine where FIPS was enabled and thus, it worked.

You can also use strace or gdb to find more details info behind the root-cause.

Body:
I was able to directly execute the python script in the environment.
But after I compile the package using pyinstaller:
pyinstaller myscript.py --onefile
When executing the execuable binary, there will be an OpenSSL error:

$ ./dist/myscript
crypto/fips/fips.c:153: OpenSSL internal error: FATAL FIPS SELFTEST FAILURE
Aborted (core dumped)

But if I directly run the script with:
python myscript.py
There will be no such error.

How can I avoid such error when compiling with pyinstaller?
Thanks.


问:
How to add values from an array as textContent with JavaScript
I'm having trouble figuring out how to add the values from an array as .textContent of a div.

What I want to happen is if I receive an array (topics) I want the contents of the array to be set as the .textContent of the elements with class="tab".

I feel like this code is correct, but doesn't seem to be working. Any ideas?

const Tabs = (topics) => {
  const tabsTopics = document.createElement("div");
  const tabsOne = document.createElement("div");
  const tabsTwo = document.createElement("div");
  const tabsThree = document.createElement("div");

  tabsTopics.classList.add("topics");
  tabsOne.classList.add("tab");
  tabsTwo.classList.add("tab");
  tabsThree.classList.add("tab");

  tabsTopics.appendChild(tabsOne);
  tabsTopics.appendChild(tabsTwo);
  tabsTopics.appendChild(tabsThree);

  document.querySelectorAll(".tab").forEach((el, i) => {
      el.textContent = topics[i];
    });

  return tabsTopics;
}

const topics = ['1', '2', '3'];

document.getElementById('container').appendChild(Tabs(topics));```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="container"></div></code>```

答:
tabsTopics has not been inserted in the DOM so the document.querySelectorAll(".tab") will not find its children.

You should do

```<code>tabsTopics.querySelectorAll(".tab").forEach((el, i) => {
  el.textContent = topics[i];
});
</code>```

instead.

const Tabs = (topics) => {
  const tabsTopics = document.createElement("div");
  const tabsOne = document.createElement("div");
  const tabsTwo = document.createElement("div");
  const tabsThree = document.createElement("div");

  tabsTopics.classList.add("topics");
  tabsOne.classList.add("tab");
  tabsTwo.classList.add("tab");
  tabsThree.classList.add("tab");

  tabsTopics.appendChild(tabsOne);
  tabsTopics.appendChild(tabsTwo);
  tabsTopics.appendChild(tabsThree);

  tabsTopics.querySelectorAll(".tab").forEach((el, i) => {
      el.textContent = topics[i];
    });

  return tabsTopics;
}

const topics = ['1', '2', '3'];

document.getElementById('container').appendChild(Tabs(topics));```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div id="container"></div></code>```

Body:
tabsTopics has not been inserted in the DOM so the document.querySelectorAll(".tab") will not find its children.

You should do

tabsTopics.querySelectorAll(".tab").forEach((el, i) => {
  el.textContent = topics[i];
});

instead.

const Tabs = (topics) => {
  const tabsTopics = document.createElement("div");
  const tabsOne = document.createElement("div");
  const tabsTwo = document.createElement("div");
  const tabsThree = document.createElement("div");

  tabsTopics.classList.add("topics");
  tabsOne.classList.add("tab");
  tabsTwo.classList.add("tab");
  tabsThree.classList.add("tab");

  tabsTopics.appendChild(tabsOne);
  tabsTopics.appendChild(tabsTwo);
  tabsTopics.appendChild(tabsThree);

  tabsTopics.querySelectorAll(".tab").forEach((el, i) => {
      el.textContent = topics[i];
    });

  return tabsTopics;
}

const topics = ['1', '2', '3'];

document.getElementById('container').appendChild(Tabs(topics));

<div id="container"></div>


问:
RGB565 to Grayscale
I am trying to convert an RGB565 to the Grayscale image.

I used the below code to do the conversion. I have a rgb565 pixel. I am getting R,G and B values and doing the conversion operation. Once the grayscale is obtained, I am reconstructing a 16 bit pixel values wherein R, G and B will have the same grayscale value.

```<code> int16_t pixel = *buffer;
 int16_t red = ((pixel & 0xF800)>>11);
 int16_t green = ((pixel & 0x07E0)>>5);
 int16_t blue = (pixel & 0x001F);
 int16_t grayscale = (0.2126 * red) + (0.7152 * green) + (0.0722 * blue);
 *buffer=(grayscale<<11)+(grayscale<<5)+grayscale;
</code>```

Here, I expected a grayscale image. However, what is happening is ->

Green color has 6 bits of space (has more magnitude). The resulting grayscale value should be 5bits so that, i will fit for R G and B. However, the calculation done provides a 6 bit grayscale because of which I am not getting a proper grayscale image.

Could anyone help me to convert a rgb565 to grayscale and store it as 16 bits.

答:
I think Pibben's answer still has some shift/mask bugs in it, and it uses floating point which, depending on your environment, could range from abysmally slow to not even possible.  I had done this myself some time ago, so here is what I wrote up:

The first thing I wanted is a monochrome viewfinder mode...  I want to work in 16-bit integer space, for speed,
so I need to use standard fixed-point scaled math.  Starting with https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems, which uses coefficients that work with
gamma-corrected values:

Y = 0.2126×R + 0.7152×G + 0.0722×B

Given RMAX = GMAX = BMAX = 63, a scale
factor of 1000 will avoid overflowing a U16.  So, we have to lose the
LSD of the given four-digit coefficients.  To avoid division by 1000
after the summation we can scale all factors up by 1024/1000, then our
division becomes a right-shift by 10 bits.  So for our coefficients:
(2126×1024)÷10000 = 217.7, rounded to 218.  Etc.  Given
these calculated coefficients and RGB565 source format,
YMAX = 64,220, which does not
overflow a U16; check.  Divided by 1024 the maximum result is 62.7,
which can be rounded to 63; Perfect.  (Rounding is simply adding back
the most-significant bit lost in the shift, something that is usually
nearly trivial in assembly language [add 0 with carry] but somewhat
messier in C.)  That gives us a 0..63 6-bit integer luminance value
that is easy to pack back into RGB565.  The final de-colorization code
becomes:

```<code>void
decolorize(U16 *usp, int count)
{
   U16 us, rc, gc, bc, luma;

   while (--count) {
      us = *usp;
      rc = (us >> 10) & 0x3E; /* 6-bit Red Component. */
      gc = (us >> 5) & 0x3F;  /* 6-bit Green Component. */
      bc = (us << 1) & 0x3E;  /* 6-bit Blue Component. */
      luma = (rc * 218) + (gc * 732) + (bc * 74); /* Wx*1024/10000. */
      luma = (luma >> 10) + ((luma >> 9) & 1); /* 6-bit Luminance value. */
     *usp++ = ((luma & 0x3E) << 10) | (luma << 5) | (luma >> 1); /* RGB565 */
   }
}
</code>```

Moreover, if you can afford a 128kB lookup table, initialized by code similar to above, the RGB565 pixels can be translated directly by array lookup, which is pretty fast on most processors.  I don't think you can beat this for speed unless you are using a GPU, or a vector processor.  The LUT is how I ended up actually using this code.

Title: RGB565 to Grayscale

Body:
I am trying to convert an RGB565 to the Grayscale image.

I used the below code to do the conversion. I have a rgb565 pixel. I am getting R,G and B values and doing the conversion operation. Once the grayscale is obtained, I am reconstructing a 16 bit pixel values wherein R, G and B will have the same grayscale value.

 int16_t pixel = *buffer;
 int16_t red = ((pixel & 0xF800)>>11);
 int16_t green = ((pixel & 0x07E0)>>5);
 int16_t blue = (pixel & 0x001F);
 int16_t grayscale = (0.2126 * red) + (0.7152 * green) + (0.0722 * blue);
 *buffer=(grayscale<<11)+(grayscale<<5)+grayscale;

Here, I expected a grayscale image. However, what is happening is ->

Green color has 6 bits of space (has more magnitude). The resulting grayscale value should be 5bits so that, i will fit for R G and B. However, the calculation done provides a 6 bit grayscale because of which I am not getting a proper grayscale image.

Could anyone help me to convert a rgb565 to grayscale and store it as 16 bits.

Body:
I think Pibben's answer still has some shift/mask bugs in it, and it uses floating point which, depending on your environment, could range from abysmally slow to not even possible.  I had done this myself some time ago, so here is what I wrote up:

The first thing I wanted is a monochrome viewfinder mode...  I want to work in 16-bit integer space, for speed,
so I need to use standard fixed-point scaled math.  Starting with https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems, which uses coefficients that work with
gamma-corrected values:

Y = 0.2126×R + 0.7152×G + 0.0722×B

Given RMAX = GMAX = BMAX = 63, a scale
factor of 1000 will avoid overflowing a U16.  So, we have to lose the
LSD of the given four-digit coefficients.  To avoid division by 1000
after the summation we can scale all factors up by 1024/1000, then our
division becomes a right-shift by 10 bits.  So for our coefficients:
(2126×1024)÷10000 = 217.7, rounded to 218.  Etc.  Given
these calculated coefficients and RGB565 source format,
YMAX = 64,220, which does not
overflow a U16; check.  Divided by 1024 the maximum result is 62.7,
which can be rounded to 63; Perfect.  (Rounding is simply adding back
the most-significant bit lost in the shift, something that is usually
nearly trivial in assembly language [add 0 with carry] but somewhat
messier in C.)  That gives us a 0..63 6-bit integer luminance value
that is easy to pack back into RGB565.  The final de-colorization code
becomes:

void
decolorize(U16 *usp, int count)
{
   U16 us, rc, gc, bc, luma;

   while (--count) {
      us = *usp;
      rc = (us >> 10) & 0x3E; /* 6-bit Red Component. */
      gc = (us >> 5) & 0x3F;  /* 6-bit Green Component. */
      bc = (us << 1) & 0x3E;  /* 6-bit Blue Component. */
      luma = (rc * 218) + (gc * 732) + (bc * 74); /* Wx*1024/10000. */
      luma = (luma >> 10) + ((luma >> 9) & 1); /* 6-bit Luminance value. */
     *usp++ = ((luma & 0x3E) << 10) | (luma << 5) | (luma >> 1); /* RGB565 */
   }
}

Moreover, if you can afford a 128kB lookup table, initialized by code similar to above, the RGB565 pixels can be translated directly by array lookup, which is pretty fast on most processors.  I don't think you can beat this for speed unless you are using a GPU, or a vector processor.  The LUT is how I ended up actually using this code.


问:
Normalize a dataframe column with dictionaries inside
i have the following dataframe and i want to normalize the column Ambience, to get one column for touristy, romantic etc. Im probably missing something obvious, so i really need some help here

```<code>                 business_id                                           Ambience
0       6iYb2HFDywm3zjuRg0shjw  {'touristy': False, 'hipster': False, 'romanti...
1       tCbdrRPZA0oiIYSmHG3J0w  {'romantic': False, 'intimate': False, 'touris...
2       bvN78flM8NLprQ1a1y5dRg                                                NaN
3       oaepsyvc0J17qwi8cfrOWg                                                NaN
4       PE9uqAjdw0E4-8mjGl3wVA                                                NaN
...                        ...                                                ...
160580  D2mHoIDXx9N8mS1pGoKV9Q                                                NaN
160581  bQX-kwVTyZgcdZGEPzce6Q                                                NaN
160582  wvFZ06nmPmQ2-IVoPqVYLA                                                NaN
160583  GB75wPibj3IjNauaoCxyGA                                                NaN
160584  ngmLL5Y5OT-bYHKU0kKrYA  {'romantic': False, 'intimate': False, 'classy...
</code>```

I tried multiple solutions on StackOverflow, but they just dont work for me, for example:

```<code>df = df["Ambience"].apply(pd.Series)
</code>```

or

```<code>df["Ambience"] = df["Ambience"].apply(
    literal_eval
)
df2 = pd.json_normalize(df["Ambience"])
</code>```

i tried filling in the NaN beforehand, but that didnt help also:

```<code>df.Ambience = df.Ambience.fillna(
    {i: {} for i in df.index}
)
</code>```

or some things i didnt understand to the fullest:

```<code>pd.concat(list(df.Ambience.map(json_normalize)))
</code>```

at this point im just clueless, every solution either doesnt work and or i get some result like:

```<code>                                            0
0       {'touristy': False, 'hipster': False, 'romanti...
1       {'romantic': False, 'intimate': False, 'touris...
2                                                     NaN
3                                                     NaN
4                                                     NaN
...                                                   ...
160580                                                NaN
160581                                                NaN
160582                                                NaN
160583                                                NaN
160584  {'romantic': False, 'intimate': False, 'classy...
</code>```

whats even more confusing, for another similar dataframe the df.apply(pd.Series) worked fine but a little bit slow:

```<code>   business_id                                              hours
0       6iYb2HFDywm3zjuRg0shjw  {'Monday': '11:0-23:0', 'Tuesday': '11:0-23:0'...
1       tCbdrRPZA0oiIYSmHG3J0w  {'Monday': '5:0-18:0', 'Tuesday': '5:0-17:0', ...
2       bvN78flM8NLprQ1a1y5dRg  {'Thursday': '11:0-18:0', 'Friday': '11:0-18:0...
3       oaepsyvc0J17qwi8cfrOWg                                               None
4       PE9uqAjdw0E4-8mjGl3wVA  {'Monday': '16:0-19:0', 'Tuesday': '16:0-19:0'...
...                        ...                                                ...
160580  D2mHoIDXx9N8mS1pGoKV9Q  {'Monday': '9:0-19:0', 'Tuesday': '9:0-19:0', ...
160581  bQX-kwVTyZgcdZGEPzce6Q  {'Monday': '10:0-18:0', 'Tuesday': '10:0-18:0'...
160582  wvFZ06nmPmQ2-IVoPqVYLA  {'Monday': '0:0-0:0', 'Wednesday': '12:0-17:0'...
160583  GB75wPibj3IjNauaoCxyGA                                               None

df = df["hours"].apply(pd.Series)

           Monday    Tuesday  Wednesday   Thursday     Friday    Saturday      Sunday
0       11:0-23:0  11:0-23:0  11:0-23:0  11:0-23:0  11:0-23:0   11:0-23:0   11:0-23:0
1        5:0-18:0   5:0-17:0   5:0-18:0   5:0-18:0   5:0-18:0    5:0-18:0    5:0-18:0
2             NaN        NaN        NaN  11:0-18:0  11:0-18:0   11:0-18:0   11:0-18:0
3             NaN        NaN        NaN        NaN        NaN         NaN         NaN
4       16:0-19:0  16:0-19:0  16:0-19:0  16:0-19:0  16:0-19:0    9:0-11:0         NaN
...           ...        ...        ...        ...        ...         ...         ...
160580   9:0-19:0   9:0-19:0   9:0-19:0   9:0-19:0   9:0-19:0    9:0-19:0    9:0-19:0
160581  10:0-18:0  10:0-18:0  10:0-18:0  10:0-18:0  10:0-18:0    9:0-17:0         NaN
160582    0:0-0:0        NaN  12:0-17:0  19:0-21:0  19:0-21:0  15:30-21:0   16:0-18:0
160583        NaN        NaN        NaN        NaN        NaN         NaN         NaN
160584        NaN  17:0-21:0  17:0-21:0  17:0-21:0  17:0-21:0  11:30-21:0  12:0-19:30
</code>```

答:
Thanks for the input, actually i found the solution. I never thought of it but some values in the column were floats, some strings etc.

stackoverflow.com/a/58155933/17328150 this solved my problem.

The types of the column were all over the place, so converting them first to strings and then to dicts, solved the problem!

```<code>df.Ambience = df.Ambience.fillna(
    {i: {} for i in df_attributes_series.index}
)

df = df["Ambience"].astype("str")
df = df.apply(lambda x: ast.literal_eval(x))
df = df.apply(pd.Series)
</code>```

Body:
i have the following dataframe and i want to normalize the column Ambience, to get one column for touristy, romantic etc. Im probably missing something obvious, so i really need some help here

                 business_id                                           Ambience
0       6iYb2HFDywm3zjuRg0shjw  {'touristy': False, 'hipster': False, 'romanti...
1       tCbdrRPZA0oiIYSmHG3J0w  {'romantic': False, 'intimate': False, 'touris...
2       bvN78flM8NLprQ1a1y5dRg                                                NaN
3       oaepsyvc0J17qwi8cfrOWg                                                NaN
4       PE9uqAjdw0E4-8mjGl3wVA                                                NaN
...                        ...                                                ...
160580  D2mHoIDXx9N8mS1pGoKV9Q                                                NaN
160581  bQX-kwVTyZgcdZGEPzce6Q                                                NaN
160582  wvFZ06nmPmQ2-IVoPqVYLA                                                NaN
160583  GB75wPibj3IjNauaoCxyGA                                                NaN
160584  ngmLL5Y5OT-bYHKU0kKrYA  {'romantic': False, 'intimate': False, 'classy...

I tried multiple solutions on StackOverflow, but they just dont work for me, for example:

df = df["Ambience"].apply(pd.Series)

or

df["Ambience"] = df["Ambience"].apply(
    literal_eval
)
df2 = pd.json_normalize(df["Ambience"])

i tried filling in the NaN beforehand, but that didnt help also:

df.Ambience = df.Ambience.fillna(
    {i: {} for i in df.index}
)

or some things i didnt understand to the fullest:

pd.concat(list(df.Ambience.map(json_normalize)))

at this point im just clueless, every solution either doesnt work and or i get some result like:

                                            0
0       {'touristy': False, 'hipster': False, 'romanti...
1       {'romantic': False, 'intimate': False, 'touris...
2                                                     NaN
3                                                     NaN
4                                                     NaN
...                                                   ...
160580                                                NaN
160581                                                NaN
160582                                                NaN
160583                                                NaN
160584  {'romantic': False, 'intimate': False, 'classy...

whats even more confusing, for another similar dataframe the df.apply(pd.Series) worked fine but a little bit slow:

   business_id                                              hours
0       6iYb2HFDywm3zjuRg0shjw  {'Monday': '11:0-23:0', 'Tuesday': '11:0-23:0'...
1       tCbdrRPZA0oiIYSmHG3J0w  {'Monday': '5:0-18:0', 'Tuesday': '5:0-17:0', ...
2       bvN78flM8NLprQ1a1y5dRg  {'Thursday': '11:0-18:0', 'Friday': '11:0-18:0...
3       oaepsyvc0J17qwi8cfrOWg                                               None
4       PE9uqAjdw0E4-8mjGl3wVA  {'Monday': '16:0-19:0', 'Tuesday': '16:0-19:0'...
...                        ...                                                ...
160580  D2mHoIDXx9N8mS1pGoKV9Q  {'Monday': '9:0-19:0', 'Tuesday': '9:0-19:0', ...
160581  bQX-kwVTyZgcdZGEPzce6Q  {'Monday': '10:0-18:0', 'Tuesday': '10:0-18:0'...
160582  wvFZ06nmPmQ2-IVoPqVYLA  {'Monday': '0:0-0:0', 'Wednesday': '12:0-17:0'...
160583  GB75wPibj3IjNauaoCxyGA                                               None

df = df["hours"].apply(pd.Series)

           Monday    Tuesday  Wednesday   Thursday     Friday    Saturday      Sunday
0       11:0-23:0  11:0-23:0  11:0-23:0  11:0-23:0  11:0-23:0   11:0-23:0   11:0-23:0
1        5:0-18:0   5:0-17:0   5:0-18:0   5:0-18:0   5:0-18:0    5:0-18:0    5:0-18:0
2             NaN        NaN        NaN  11:0-18:0  11:0-18:0   11:0-18:0   11:0-18:0
3             NaN        NaN        NaN        NaN        NaN         NaN         NaN
4       16:0-19:0  16:0-19:0  16:0-19:0  16:0-19:0  16:0-19:0    9:0-11:0         NaN
...           ...        ...        ...        ...        ...         ...         ...
160580   9:0-19:0   9:0-19:0   9:0-19:0   9:0-19:0   9:0-19:0    9:0-19:0    9:0-19:0
160581  10:0-18:0  10:0-18:0  10:0-18:0  10:0-18:0  10:0-18:0    9:0-17:0         NaN
160582    0:0-0:0        NaN  12:0-17:0  19:0-21:0  19:0-21:0  15:30-21:0   16:0-18:0
160583        NaN        NaN        NaN        NaN        NaN         NaN         NaN
160584        NaN  17:0-21:0  17:0-21:0  17:0-21:0  17:0-21:0  11:30-21:0  12:0-19:30

Body:
Thanks for the input, actually i found the solution. I never thought of it but some values in the column were floats, some strings etc.

stackoverflow.com/a/58155933/17328150 this solved my problem.

The types of the column were all over the place, so converting them first to strings and then to dicts, solved the problem!

df.Ambience = df.Ambience.fillna(
    {i: {} for i in df_attributes_series.index}
)

df = df["Ambience"].astype("str")
df = df.apply(lambda x: ast.literal_eval(x))
df = df.apply(pd.Series)


问:
Custom Sort using Java 8
Consider the dto class below -

```<code>public class RoleDto {

  private String name;

  RoleDto(String name) {
    this.name = name;
  }
}
</code>```

Its instances would contain names like -

```<code>Business Practitioner - Expert
Developer - Expert
Developer - Master
Business Practitioner - Master
Business Practitioner
Developer
Developer - Professional
Business Practitioner - Professional
</code>```

I want to sort the instances such that the order is -

```<code>Business Practitioner - Expert
Business Practitioner - Master
Business Practitioner - Professional
Business Practitioner

Developer - Expert
Developer - Master
Developer - Professional
Developer
</code>```

I have used the code below -

```<code>ArrayList<RoleDto> roles = new ArrayList<>();
roles.add(new RoleDto("Business Practitioner - Expert"));
roles.add(new RoleDto("Developer - Expert"));
roles.add(new RoleDto("Developer - Master"));
roles.add(new RoleDto("Business Practitioner - Master"));
roles.add(new RoleDto("Business Practitioner"));
roles.add(new RoleDto("Developer"));
roles.add(new RoleDto("Developer - Professional"));
roles.add(new RoleDto("Business Practitioner - Professional"));

List<RoleDto> sorted = roles.stream().sorted(comparing(r -> r.getName(),
    comparing((String s) -> !s.contains("-")))).collect(toList());

for(RoleDto r : sorted)
System.out.println(r.getName());
</code>```

However the output is

```<code>Business Practitioner - Expert
Developer - Expert
Developer - Master
Business Practitioner - Master
Developer - Professional
Business Practitioner - Professional
Business Practitioner
Developer
</code>```

Can someone please help me achieve the expected result

答:
You do not actually compare the names, the compare just checks whether or not the names contain "-", which is why you are getting all that do (in random order) before all that don't (in random order). If you can change the Dto, see the answer of @Orr, otherwise you could do the name split/compare in place, like this:

```<code>    final List<RoleDto> sorted =
        roles.stream()
            .sorted(
                comparing(
                    RoleDto::getName,
                    comparing((String s) -> s.split(" - ")[0])
                        .reversed()
                        .thenComparing(s -> s.split(" - ").length)
                        .reversed()
                        .thenComparing(s -> s.split(" - ")[1])))
            .collect(toList());
</code>```

The reversing may seem a little unintuitive, but effectively every reverse reverses all the previous comparisons, so the first comparison get reversed twice. To skip that, you could instead write:

```<code>    final List<RoleDto> sorted =
        roles.stream()
            .sorted(
                comparing(
                    RoleDto::getName,
                    comparing((String s) -> s.split(" - ")[0])
                        .thenComparing(s -> -s.split(" - ").length)
                        .thenComparing(s -> s.split(" - ")[1])))
            .collect(toList());
</code>```

(notice the minus sign in the middle compare)

Title: Custom Sort using Java 8

Body:
Consider the dto class below -

public class RoleDto {

  private String name;

  RoleDto(String name) {
    this.name = name;
  }
}

Its instances would contain names like -

Business Practitioner - Expert
Developer - Expert
Developer - Master
Business Practitioner - Master
Business Practitioner
Developer
Developer - Professional
Business Practitioner - Professional

I want to sort the instances such that the order is -

Business Practitioner - Expert
Business Practitioner - Master
Business Practitioner - Professional
Business Practitioner

Developer - Expert
Developer - Master
Developer - Professional
Developer

I have used the code below -

ArrayList<RoleDto> roles = new ArrayList<>();
roles.add(new RoleDto("Business Practitioner - Expert"));
roles.add(new RoleDto("Developer - Expert"));
roles.add(new RoleDto("Developer - Master"));
roles.add(new RoleDto("Business Practitioner - Master"));
roles.add(new RoleDto("Business Practitioner"));
roles.add(new RoleDto("Developer"));
roles.add(new RoleDto("Developer - Professional"));
roles.add(new RoleDto("Business Practitioner - Professional"));

List<RoleDto> sorted = roles.stream().sorted(comparing(r -> r.getName(),
    comparing((String s) -> !s.contains("-")))).collect(toList());

for(RoleDto r : sorted)
System.out.println(r.getName());

However the output is

Business Practitioner - Expert
Developer - Expert
Developer - Master
Business Practitioner - Master
Developer - Professional
Business Practitioner - Professional
Business Practitioner
Developer

Can someone please help me achieve the expected result

Body:
You do not actually compare the names, the compare just checks whether or not the names contain "-", which is why you are getting all that do (in random order) before all that don't (in random order). If you can change the Dto, see the answer of @Orr, otherwise you could do the name split/compare in place, like this:

    final List<RoleDto> sorted =
        roles.stream()
            .sorted(
                comparing(
                    RoleDto::getName,
                    comparing((String s) -> s.split(" - ")[0])
                        .reversed()
                        .thenComparing(s -> s.split(" - ").length)
                        .reversed()
                        .thenComparing(s -> s.split(" - ")[1])))
            .collect(toList());

The reversing may seem a little unintuitive, but effectively every reverse reverses all the previous comparisons, so the first comparison get reversed twice. To skip that, you could instead write:

    final List<RoleDto> sorted =
        roles.stream()
            .sorted(
                comparing(
                    RoleDto::getName,
                    comparing((String s) -> s.split(" - ")[0])
                        .thenComparing(s -> -s.split(" - ").length)
                        .thenComparing(s -> s.split(" - ")[1])))
            .collect(toList());

(notice the minus sign in the middle compare)


问:
Using multiline regular expressions
I need to separate individual records from:

```<code>Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    Cork
Returns Year    1880
Returns Quarter 4
Returns Volume No   5
Returns Page No 0110
Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    Enniscorthy
Returns Year    1880
Returns Quarter 3
Returns Volume No   4
Returns Page No 276
Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    New Ross
Returns Year    1880
Returns Quarter 3
Returns Volume No   4
Returns Page No 349
Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    Wexford
Returns Year    1880
Returns Quarter 3
Returns Volume No   4
Returns Page No 389
Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    New Ross
Returns Year    1880
Returns Quarter 3
Returns Volume No   4
Returns Page No 343
</code>```

For instance the first record would be from Marriage (1st line) till 0110 (8th line).

How would you do that using regular expression in order to get a list with all the records?

thanks in advance

答:
You can use below regular expression:

```<code>(Marriage).*?(?=Marriage|\Z)
</code>```

Demo https://regex101.com/r/x2HPMK/1

Title: Using multiline regular expressions

Body:
I need to separate individual records from:

Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    Cork
Returns Year    1880
Returns Quarter 4
Returns Volume No   5
Returns Page No 0110
Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    Enniscorthy
Returns Year    1880
Returns Quarter 3
Returns Volume No   4
Returns Page No 276
Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    New Ross
Returns Year    1880
Returns Quarter 3
Returns Volume No   4
Returns Page No 349
Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    Wexford
Returns Year    1880
Returns Quarter 3
Returns Volume No   4
Returns Page No 389
Marriage of MARY ROCHE
in 1880
Group Registration ID   N/R
SR District/Reg Area    New Ross
Returns Year    1880
Returns Quarter 3
Returns Volume No   4
Returns Page No 343

For instance the first record would be from Marriage (1st line) till 0110 (8th line).

How would you do that using regular expression in order to get a list with all the records?

thanks in advance

Body:
You can use below regular expression:

(Marriage).*?(?=Marriage|\Z)

Demo https://regex101.com/r/x2HPMK/1


问:
Array char operation C#
Anyone can help me how to optimize the code that i create or have better way to implement it?
The rule is on the bottom

```<code>
    public string HandleLastNumber(char[] n_array)
    {
    var n_string = new StringBuilder();
    if (n_array.All(x => char.IsDigit(x)))
    {
    return $"{new string(n_array)} -> {int.Parse(n_array) + 1}";
    }
    if (!n_array.Any(x => char.IsDigit(x)))
    {
    return $"{new string(n_array)}->{new string(n_array)}";
    }
    for (int i = n_array.Length - 1; i >= 0; i--)
    {
    if (!char.IsDigit(n_array[i]))
    {
    break;
    }
    n_string.Insert(0, n_array[i]);
    }
    var n_plus = int.Parse(n_string.ToString()) + 1;
    var n_handled = n_plus.ToString()[^n_string.Length..];
    var sub_clone = new string(n_array).Substring(0,n_array.Length-n_handled.Length);
    return $"{new string(n_array)}->{sub_clone += n_handled}";
    }
    }

</code>```

The rule is
Input char array, validate the last number, plus one at last number. then print old and new char array

result example:

00-> 01

99-> 00

ABC123 -> ABC124

ABCDGF -> ABCDGF

AD33GVB99 -> AD33GVB00

答:
There are many ways to implement this.  I have seen many such posts in the past.  I do not like that your method accepts a char[].  I perfer that the input and output be a string.

Implementing the logic becomes more straight forward:

```<code>public static string IncrementLastDigits(string value)
{
    if (string.IsNullOrWhiteSpace(value))
    {
        return value; // consider returning null or ""
    }
    var array = value.ToCharArray();
    for (var i = array.Length - 1; i >= 0; i--)
    {
        var character = array[i];
        if (!char.IsDigit(character))
        {
            break;
        }
        if (character < '9')
        {
            array[i] = ++character;
            break;
        }
        else
        {
            array[i] = '0';
        }
    }
    return new string(array);
}
</code>```

And here's a quick method to test it:

```<code>public static void TestLastDigits()
{
    var input  = new List<string>() { "00", "99", "ABC123", "ABCDGF", "AD33GVB99", "AD33GV" };
    var answer = new List<string>() { "01", "00", "ABC124", "ABCDGF", "AD33GVB00", "AD33GV" };

    for (var i = 0; i < input.Count; i++)
    {
        var output = IncrementLastDigits(input[i]);
        var status = (answer[i] != output) ? $"Correct answer is {answer[i]}" : "";
        Console.WriteLine($"Input {input[i]}, Output {output}  {status}");
    }
}
</code>```

Title: Array char operation C#

Body:
Anyone can help me how to optimize the code that i create or have better way to implement it?
The rule is on the bottom

    public string HandleLastNumber(char[] n_array)
    {
    var n_string = new StringBuilder();
    if (n_array.All(x => char.IsDigit(x)))
    {
    return $"{new string(n_array)} -> {int.Parse(n_array) + 1}";
    }
    if (!n_array.Any(x => char.IsDigit(x)))
    {
    return $"{new string(n_array)}->{new string(n_array)}";
    }
    for (int i = n_array.Length - 1; i >= 0; i--)
    {
    if (!char.IsDigit(n_array[i]))
    {
    break;
    }
    n_string.Insert(0, n_array[i]);
    }
    var n_plus = int.Parse(n_string.ToString()) + 1;
    var n_handled = n_plus.ToString()[^n_string.Length..];
    var sub_clone = new string(n_array).Substring(0,n_array.Length-n_handled.Length);
    return $"{new string(n_array)}->{sub_clone += n_handled}";
    }
    }

The rule is
Input char array, validate the last number, plus one at last number. then print old and new char array

result example:

00-> 01

99-> 00

ABC123 -> ABC124

ABCDGF -> ABCDGF

AD33GVB99 -> AD33GVB00

Body:
There are many ways to implement this.  I have seen many such posts in the past.  I do not like that your method accepts a char[].  I perfer that the input and output be a string.

Implementing the logic becomes more straight forward:

public static string IncrementLastDigits(string value)
{
    if (string.IsNullOrWhiteSpace(value))
    {
        return value; // consider returning null or ""
    }
    var array = value.ToCharArray();
    for (var i = array.Length - 1; i >= 0; i--)
    {
        var character = array[i];
        if (!char.IsDigit(character))
        {
            break;
        }
        if (character < '9')
        {
            array[i] = ++character;
            break;
        }
        else
        {
            array[i] = '0';
        }
    }
    return new string(array);
}

And here's a quick method to test it:

public static void TestLastDigits()
{
    var input  = new List<string>() { "00", "99", "ABC123", "ABCDGF", "AD33GVB99", "AD33GV" };
    var answer = new List<string>() { "01", "00", "ABC124", "ABCDGF", "AD33GVB00", "AD33GV" };

    for (var i = 0; i < input.Count; i++)
    {
        var output = IncrementLastDigits(input[i]);
        var status = (answer[i] != output) ? $"Correct answer is {answer[i]}" : "";
        Console.WriteLine($"Input {input[i]}, Output {output}  {status}");
    }
}


问:
Find the sum of array using multiple threads
This is my first time trying to work with multithreading so help would be appreciated!

Following problem:

The user enters values for the array, as well as desired quantity of threads to the console. The array should be summed using those threads and each thread finds the sum of its own chunk in array. The summed result should be printed for each thread and also overrall sum.

```<code>public class Thread1  extends Thread{

    @Override
    public void run() {
        System.out.println("Enter the required size of the array :: ");
        Scanner s = new Scanner(System.in);
        int size = s.nextInt();
        int[] myArray = new int [size];
        System.out.println("Enter the required quantity of the threads :: ");
        int k = s.nextInt();
        int sum = 0;
        System.out.println("Enter the elements of the array one by one ");

        for(int i=0; i<size; i++) {
            myArray[i] = s.nextInt();

            for(int j = 0; j< k; j++){
                sum = sum + myArray[i];

            }
            System.out.println("Sum for thread: " + this.getName() + " = " + sum);
        }
        System.out.println("Elements of the array are: "+ Arrays.toString(myArray));
        System.out.println("Sum of the elements of the array ::"+sum);
    }

    public static void main(String[] args) {
        Thread1 t = new Thread1();
        t.start();

    }
}

</code>```

答:
As this dates back to a year ago I would mention how I approached this.
Firstly, as using ExecutorService is more encouraged I used this to do multithreading. It's also worth mentioning that I haven't used synchronize or locks as I won't share any value and the sum will be computed at the end instead of each tread adding to one common sum.</p>
```<code>public void arraySum(int[] arr){
        start = System.currentTimeMillis();
        ExecutorService exec = Executors.newSingleThreadExecutor();
        sum = 0L;
        int noThreads = 100;
        int length = arr.length/noThreads;

        List<Long> res = new ArrayList<>();
        for(int i=0;i<noThreads;i++) {//each thread
            int low = length*i;
            int high = length+low;

            res.add(exec.submit(callable(arr, low, high)).get());
        }

        exec.shutdown();
        while(!exec.isTerminated());
        if(exec.isTerminated()) {
            for(long l:res) {
                sum+= l;
            }
        }
        finish = System.currentTimeMillis();
        System.out.println(noThreads+" threads: Sum: "+sum+" Complettion in miliseconds: "+(finish -start));

    }

    Callable<Long> callable(int[] arr,int low, int high){
        return ()->{
            Long res = 0L;
            for(int i=low;i<high;i++) {
                res += arr[i];
            }
            return res;
        };
    }

</code>```

Body:
This is my first time trying to work with multithreading so help would be appreciated!

Following problem:

The user enters values for the array, as well as desired quantity of threads to the console. The array should be summed using those threads and each thread finds the sum of its own chunk in array. The summed result should be printed for each thread and also overrall sum.

public class Thread1  extends Thread{

    @Override
    public void run() {
        System.out.println("Enter the required size of the array :: ");
        Scanner s = new Scanner(System.in);
        int size = s.nextInt();
        int[] myArray = new int [size];
        System.out.println("Enter the required quantity of the threads :: ");
        int k = s.nextInt();
        int sum = 0;
        System.out.println("Enter the elements of the array one by one ");

        for(int i=0; i<size; i++) {
            myArray[i] = s.nextInt();

            for(int j = 0; j< k; j++){
                sum = sum + myArray[i];

            }
            System.out.println("Sum for thread: " + this.getName() + " = " + sum);
        }
        System.out.println("Elements of the array are: "+ Arrays.toString(myArray));
        System.out.println("Sum of the elements of the array ::"+sum);
    }

    public static void main(String[] args) {
        Thread1 t = new Thread1();
        t.start();

    }
}

Body:
As this dates back to a year ago I would mention how I approached this.
Firstly, as using ExecutorService is more encouraged I used this to do multithreading. It's also worth mentioning that I haven't used synchronize or locks as I won't share any value and the sum will be computed at the end instead of each tread adding to one common sum.

public void arraySum(int[] arr){
        start = System.currentTimeMillis();
        ExecutorService exec = Executors.newSingleThreadExecutor();
        sum = 0L;
        int noThreads = 100;
        int length = arr.length/noThreads;

        List<Long> res = new ArrayList<>();
        for(int i=0;i<noThreads;i++) {//each thread
            int low = length*i;
            int high = length+low;

            res.add(exec.submit(callable(arr, low, high)).get());
        }

        exec.shutdown();
        while(!exec.isTerminated());
        if(exec.isTerminated()) {
            for(long l:res) {
                sum+= l;
            }
        }
        finish = System.currentTimeMillis();
        System.out.println(noThreads+" threads: Sum: "+sum+" Complettion in miliseconds: "+(finish -start));

    }

    Callable<Long> callable(int[] arr,int low, int high){
        return ()->{
            Long res = 0L;
            for(int i=low;i<high;i++) {
                res += arr[i];
            }
            return res;
        };
    }


问:
How to find the frequency of characters in a string in Haskell?
How can I count the frequency of characters in a string and then output them in sort of a table?

For example, if I input the word "happy" the result would be

```<code>h 1
a 1
p 2
y 1
</code>```

If this could be ordered in ASCII order too that would be brilliant.

I know I need to use the count function, any other hints would be appreciated.

EDIT: All the answers are brilliant, only I'm such a beginner at Haskell that I don't actually understand what they are doing.

答:
There's probably something shorter, but this works:</p>

```<code>Prelude> import Data.List
Prelude Data.List> map (\x -> (head x, length x)) $ group $ sort "happy"
[('h',1),('a',1),('p',2),('y',1)]
</code>```

ContentLicense: CC BY-SA 3.0

Body:
How can I count the frequency of characters in a string and then output them in sort of a table?

For example, if I input the word "happy" the result would be

h 1
a 1
p 2
y 1

If this could be ordered in ASCII order too that would be brilliant.

I know I need to use the count function, any other hints would be appreciated.

EDIT: All the answers are brilliant, only I'm such a beginner at Haskell that I don't actually understand what they are doing.

Body:
There's probably something shorter, but this works:

Prelude> import Data.List
Prelude Data.List> map (\x -> (head x, length x)) $ group $ sort "happy"
[('h',1),('a',1),('p',2),('y',1)]


问:
2 problems when trying to create a trigger in MySQL
I am trying to understand triggers in MySQL, but am having a few problems.

I'm trying to implement a trigger which on every UPDATE/INSERT in the table Grades it updates the column "gpa" in another table called Student, but cannot do it properly.

Code:

```<code>CREATE TABLE Student
(
    Id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(30),
    age TINYINT,
    gpa NUMERIC(3, 2) DEFAULT 2
);

CREATE TABLE Grades
(
    StudentId INT PRIMARY KEY,
    grade_bg INT,
    grade_math INT,
    grade_subd INT,
    FOREIGN KEY(StudentId) references Student(Id)
);

delimiter |

CREATE TRIGGER update_gpa
    AFTER INSERT
    ON Grades
    FOR EACH ROW
    BEGIN
        UPDATE Student SET gpa = ((grade_bg + grade_math + grade_subd)/3) WHERE StudentId = Id;
    END;
|
</code>```

After this when I try to insert in the table Student I get:
"Error 1054: Unknown column 'StudentId' in where clause".

For example:

```<code>INSERT INTO Student(name, age)
            VALUES ('Joshua', 17);
</code>```

Also when I try writing "AFTER INSERT, UPDATE" I get a syntax error from the MySQL Workbench and don't know how to make the trigger activate on INSERT AND UPDATE in the table Grades.

Any help is appreciated! Thanks in advance!

答:
I don't know much about Triggers but I tried just running the update statement and noticed you hadn't defined the join:-</p>
```<code>UPDATE Student
INNER JOIN Grades ON Grades.StudentId = Student.Id
SET Student.gpa = ((Grades.grade_bg + Grades.grade_math + Grades.grade_subd)/3);
</code>```

Body:
I am trying to understand triggers in MySQL, but am having a few problems.

I'm trying to implement a trigger which on every UPDATE/INSERT in the table Grades it updates the column "gpa" in another table called Student, but cannot do it properly.

Code:

CREATE TABLE Student
(
    Id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(30),
    age TINYINT,
    gpa NUMERIC(3, 2) DEFAULT 2
);

CREATE TABLE Grades
(
    StudentId INT PRIMARY KEY,
    grade_bg INT,
    grade_math INT,
    grade_subd INT,
    FOREIGN KEY(StudentId) references Student(Id)
);

delimiter |

CREATE TRIGGER update_gpa
    AFTER INSERT
    ON Grades
    FOR EACH ROW
    BEGIN
        UPDATE Student SET gpa = ((grade_bg + grade_math + grade_subd)/3) WHERE StudentId = Id;
    END;
|

After this when I try to insert in the table Student I get:
"Error 1054: Unknown column 'StudentId' in where clause".

For example:

INSERT INTO Student(name, age)
            VALUES ('Joshua', 17);

Also when I try writing "AFTER INSERT, UPDATE" I get a syntax error from the MySQL Workbench and don't know how to make the trigger activate on INSERT AND UPDATE in the table Grades.

Any help is appreciated! Thanks in advance!

Body:
I don't know much about Triggers but I tried just running the update statement and noticed you hadn't defined the join:-

UPDATE Student
INNER JOIN Grades ON Grades.StudentId = Student.Id
SET Student.gpa = ((Grades.grade_bg + Grades.grade_math + Grades.grade_subd)/3);


问:
ErrorException Attempt to read property "name" on null (View:
I have 2 relationships that point to the same User model: operador() and profesional().

```<code>class Cita extends Model
{

    public function paciente(){

        return $this->belongsTo('\App\Models\Paciente');

    }

    public function profesional(){

        return $this->belongsTo('\App\Models\User');

    }

    public function operador(){

        return $this->belongsTo('\App\Models\User');

    }

}
</code>```

In the view I call them like this:

```<code>                        @foreach ($comisiones as $comision)
                            <tr>

                            <td>{{ $comision->paciente->name }}</td>

                            <td>{{ $comision->profesional->name }}</td>
                            <td>{{ $comision->operador->name }}</td>

                            <td>{{ number_format($comision->total, 0, '.', '.') }}</td>
                            <td>{{ $comision->estado }}</td>
                            </tr>
                        @endforeach
</code>```

The program crashes on me when it tries to call $commision->operador->name. If I leave it as a comment it works without problems. But it gives me an error when I have the 2 relations at the same time.

Can I have 2 relationships pointing to the same model? And if not, what alternative do I have? Thanks

答:
When you write :

```<code>public function operador(){

    return $this->belongsTo('\App\Models\User');

}
</code>```

Laravel expects that The Migration (The table citas) has a column named operador_id. So Yes you can have multiple relationships to the same model.

Body:
I have 2 relationships that point to the same User model: operador() and profesional().

class Cita extends Model
{

    public function paciente(){

        return $this->belongsTo('\App\Models\Paciente');

    }

    public function profesional(){

        return $this->belongsTo('\App\Models\User');

    }

    public function operador(){

        return $this->belongsTo('\App\Models\User');

    }

}

In the view I call them like this:

                        @foreach ($comisiones as $comision)

                            {{ $comision->paciente->name }}

                            {{ $comision->profesional->name }}
                            {{ $comision->operador->name }}

                            {{ number_format($comision->total, 0, '.', '.') }}
                            {{ $comision->estado }}

                        @endforeach

The program crashes on me when it tries to call $commision->operador->name. If I leave it as a comment it works without problems. But it gives me an error when I have the 2 relations at the same time.

Can I have 2 relationships pointing to the same model? And if not, what alternative do I have? Thanks

Body:
When you write :

public function operador(){

    return $this->belongsTo('\App\Models\User');

}

Laravel expects that The Migration (The table citas) has a column named operador_id. So Yes you can have multiple relationships to the same model.


问:
How do I add text last in a text element in html via js
I want to add text last in a p element, not first, but i only know how to add last using +=

var pEL = document.querySelector("#text");
pEL.innerHTML += "1";```
<pre class="snippet-code-html lang-html prettyprint-override"><code><p id="text">text</p></code>```

now the p element says "text1" but i want "1text", how do i add something first in a text element?

答:
The Element property innerHTML gets or sets the HTML or XML markup contained within the element so set it with concatenating 1 + the previous text (calling pEL.innerHTML without assignment is basiclly using the get to fetch the value)

var pEL = document.querySelector("#text");
pEL.innerHTML = "1" + pEL.innerHTML;```
<pre class="snippet-code-html lang-html prettyprint-override"><code><p id="text">text</p> </code>```

Body:
I want to add text last in a p element, not first, but i only know how to add last using +=

var pEL = document.querySelector("#text");
pEL.innerHTML += "1";

text

now the p element says "text1" but i want "1text", how do i add something first in a text element?

Body:
The Element property innerHTML gets or sets the HTML or XML markup contained within the element so set it with concatenating 1 + the previous text (calling pEL.innerHTML without assignment is basiclly using the get to fetch the value)

var pEL = document.querySelector("#text");
pEL.innerHTML = "1" + pEL.innerHTML;

<p id="text">text</p>


问:
No android device found in unity
When I am building a game for android, I get this message:

No Android device found!
  Make sure USB debugging has been enabled.
  Check your device. In most cases there should be a small icon in the status bar telling you if the USB connection is available.
  If you are sure that your device is attached then it might be your USB driver that's the problem. For details please check the Android SDK setup section in the Unity manual.

The cancel windows showed this error:

No android device found! If you are sure that your device is attached then it might be your USB driver that's the problem. For details please check the Android SDK setup section in the Unity manual.

The same error also appeared in the Console.
How do I fix this error?

答:
This helped me to fix my one.

I close my Unity. At my phone, I click Revoke USB Debugging Authorization and Ok to confirm.

Open Android Studio

Go to Tools | Connection Assistant. (Mine is V3.3.2)

Press Next, and there will be a page with "Restart ADB Server".

Then on my phone there is a dialog box asking to allow access from the PC.

Click Allow and that's it. Give a try to run an app from Android Studio.

If OK, then close Android Studio and reopen Unity and it should able to build and run on phone now.

source @ https://forum.unity.com/threads/android-device-is-not-responding.713705/

Title: No android device found in unity


问:
Store a api response data in firebase collections using flutter
So, I have been making a post request to a REST API and I want to store the response data in the firebase cloud store collection.
What I have done so far:
I have created the model class for the response data and have written a function that will make this post-call.

I am not getting any such error but still, neither the response is getting printed in the console nor the data is being uploaded in the firebase.
Also, I have checked with almost all the StackOverflow questions that relate to my kind of problem.

Herewith I am attaching my code snippets:

Function:
//This function is only not getting called I don't know why.

```<code>final List<KycDetails> _kyc = [];
  Dio dio = Dio();
  TextEditingController aadhar = TextEditingController();
  Future<List<KycDetails>> postData() async {
    const String pathUrl = 'https://jsonplaceholder.typicode.com/posts';
    dynamic data = {'title': aadhar.text, 'body': 'Flutter', 'userId': 1};
    List<KycDetails> details = [];
    var response = await dio.post(pathUrl,
        data: data,
        options: Options(
            headers: {'Content-Type': 'application/json; charset=UTF-8'}));
    if (response.statusCode == 200) {
      print('ok');
      var urjson = jsonDecode(response.data);
      for (var jsondata in urjson) {
        details.add(KycDetails.fromJson(jsondata));
      }
    }
    return details;
  }
</code>```

Widget where I am calling the function and storing the data in firebase

```<code>     InkWell(
                  hoverColor: Colors.red,
                  onTap: () async {
                    print('API CALLING');
                    await postData().then((value) {
                      setState(() {
                        _kyc.addAll(value);
                      });
                      print(value);

                    });
                    Map<String, String> data = {
                      "aadhar": aadhar.text,
                      "title": _kyc[0].title,
                      "userId": _kyc[0].userId.toString(),
                    };
                    FirebaseFirestore.instance.collection('kyc').add(data);
                  },
                  child: const Text('Submit'),
                ),
</code>```

API response data:

```<code>{"title": "resume", "body": "Flutter", "userId": 1, "id": 101}
</code>```

Model Class:

```<code>class KycDetails {
  KycDetails({
    required this.title,
    required this.body,
    required this.userId,
    required this.id,
  });

  String title;
  String body;
  int userId;
  int id;

  factory KycDetails.fromJson(Map<String, dynamic> json) => KycDetails(
        title: json["title"],
        body: json["body"],
        userId: json["userId"],
        id: json["id"],
      );

  Map<String, dynamic> toJson() => {
        "title": title,
        "body": body,
        "userId": userId,
        "id": id,
      };
}
</code>```

I hope I have provided you with the necessary information
Am stuck on this problem for quite a few days Would appreciate it if anyone can solve my problem considering my code.

答:
For starters, when you make a post request the success code you're looking for is 201 indicating that a resource has been successfully created.

So nothing in this code block will run.

```<code>   if (response.statusCode == 200) {
      print('ok');
      var urjson = jsonDecode(response.data);
      for (var jsondata in urjson) {
        details.add(KycDetails.fromJson(jsondata));
      }
    }
</code>```

response.data doesn't need jsonDecode here. It returns in the form of a map so you can cast it as such.

So this

```<code>var urjson = jsonDecode(response.data);
</code>```

can be this

```<code>final urjson = response.data as Map<String, dynamic>;
</code>```

As for this line

```<code>   for (var jsondata in urjson) {
        details.add(KycDetails.fromJson(jsondata));
      }
</code>```

The response is a single map, not a list. That single map is in the form of your KycDetails model so you don't need to loop through anything.

So you can create your object with your fromJson method.

```<code>final kycDetail = KycDetails.fromJson(urjson);
</code>```

Then you can just do this to add a properly initiated KycDetails object to the list.

```<code>details.add(kycDetail);
</code>```

If all you're trying to do is add a single object to Firebase then none of this in your onTap is necessary. Also trying to access the property at index 0 will not be the most recent addition to the list. You'd need to add the index of the last item in the list.

```<code>Map<String, String> data = {
  "aadhar": aadhar.text,
  "title": _kyc[0].title,
  "userId": _kyc[0].userId.toString(),
};

FirebaseFirestore.instance.collection('kyc').add(data);
</code>```

You can just add to Firebase from your postData function.

```<code>  if (response.statusCode == 201) {
    print('ok');
    final urjson = response.data as Map<String, dynamic>;
    final kycDetail = KycDetails.fromJson(urjson);
    details.add(kycDetail);
    FirebaseFirestore.instance.collection('kyc').add(kycDetail.toJson());
  }
</code>```

Body:
So, I have been making a post request to a REST API and I want to store the response data in the firebase cloud store collection.
What I have done so far:
I have created the model class for the response data and have written a function that will make this post-call.

I am not getting any such error but still, neither the response is getting printed in the console nor the data is being uploaded in the firebase.
Also, I have checked with almost all the StackOverflow questions that relate to my kind of problem.

Herewith I am attaching my code snippets:

Function:
//This function is only not getting called I don't know why.

final List<KycDetails> _kyc = [];
  Dio dio = Dio();
  TextEditingController aadhar = TextEditingController();
  Future<List<KycDetails>> postData() async {
    const String pathUrl = 'https://jsonplaceholder.typicode.com/posts';
    dynamic data = {'title': aadhar.text, 'body': 'Flutter', 'userId': 1};
    List<KycDetails> details = [];
    var response = await dio.post(pathUrl,
        data: data,
        options: Options(
            headers: {'Content-Type': 'application/json; charset=UTF-8'}));
    if (response.statusCode == 200) {
      print('ok');
      var urjson = jsonDecode(response.data);
      for (var jsondata in urjson) {
        details.add(KycDetails.fromJson(jsondata));
      }
    }
    return details;
  }

Widget where I am calling the function and storing the data in firebase

     InkWell(
                  hoverColor: Colors.red,
                  onTap: () async {
                    print('API CALLING');
                    await postData().then((value) {
                      setState(() {
                        _kyc.addAll(value);
                      });
                      print(value);

                    });
                    Map<String, String> data = {
                      "aadhar": aadhar.text,
                      "title": _kyc[0].title,
                      "userId": _kyc[0].userId.toString(),
                    };
                    FirebaseFirestore.instance.collection('kyc').add(data);
                  },
                  child: const Text('Submit'),
                ),

API response data:

{"title": "resume", "body": "Flutter", "userId": 1, "id": 101}

Model Class:

class KycDetails {
  KycDetails({
    required this.title,
    required this.body,
    required this.userId,
    required this.id,
  });

  String title;
  String body;
  int userId;
  int id;

  factory KycDetails.fromJson(Map<String, dynamic> json) => KycDetails(
        title: json["title"],
        body: json["body"],
        userId: json["userId"],
        id: json["id"],
      );

  Map<String, dynamic> toJson() => {
        "title": title,
        "body": body,
        "userId": userId,
        "id": id,
      };
}

I hope I have provided you with the necessary information
Am stuck on this problem for quite a few days Would appreciate it if anyone can solve my problem considering my code.

Body:
For starters, when you make a post request the success code you're looking for is 201 indicating that a resource has been successfully created.

So nothing in this code block will run.

   if (response.statusCode == 200) {
      print('ok');
      var urjson = jsonDecode(response.data);
      for (var jsondata in urjson) {
        details.add(KycDetails.fromJson(jsondata));
      }
    }

response.data doesn't need jsonDecode here. It returns in the form of a map so you can cast it as such.

So this

var urjson = jsonDecode(response.data);

can be this

final urjson = response.data as Map<String, dynamic>;

As for this line

   for (var jsondata in urjson) {
        details.add(KycDetails.fromJson(jsondata));
      }

The response is a single map, not a list. That single map is in the form of your KycDetails model so you don't need to loop through anything.

So you can create your object with your fromJson method.

final kycDetail = KycDetails.fromJson(urjson);

Then you can just do this to add a properly initiated KycDetails object to the list.

details.add(kycDetail);

If all you're trying to do is add a single object to Firebase then none of this in your onTap is necessary. Also trying to access the property at index 0 will not be the most recent addition to the list. You'd need to add the index of the last item in the list.

Map<String, String> data = {
  "aadhar": aadhar.text,
  "title": _kyc[0].title,
  "userId": _kyc[0].userId.toString(),
};

FirebaseFirestore.instance.collection('kyc').add(data);

You can just add to Firebase from your postData function.

  if (response.statusCode == 201) {
    print('ok');
    final urjson = response.data as Map<String, dynamic>;
    final kycDetail = KycDetails.fromJson(urjson);
    details.add(kycDetail);
    FirebaseFirestore.instance.collection('kyc').add(kycDetail.toJson());
  }


答:
The simplest solution is to use a Data.Map to store the intermediate mapping from character to frequency. You can then construct the counts easily using fromListWith. Since Data.Map is sorted, you get them in ASCII order for free.

```<code>λ> :m + Data.Map
λ> let input = "happy"
λ> toList $ fromListWith (+) [(c, 1) | c <- input]
[('a',1),('h',1),('p',2),('y',1)]
</code>```

So what's happening here?

The idea is to build a Data.Map (a tree map) using the characters as keys and the frequencies as values.

First, we take the input string and make tuples of each character with a 1 to indicate one occurrence.

```<code>λ> [(c, 1) | c <- input]
[('h',1),('a',1),('p',1),('p',1),('y',1)]
</code>```

Next, we use fromListWith to build a sorted map from these key-value pairs by repeatedly inserting each key-value pair into a map. We also give it a function which will be used when a key was already in the map. In our case, we use (+) so that when a character is seen multiple times, we add the count to the existing sum.

Finally we covert the map back into a list of key-value tuples using toList.

Title: How to find the frequency of characters in a string in Haskell?

Body:
The simplest solution is to use a Data.Map to store the intermediate mapping from character to frequency. You can then construct the counts easily using fromListWith. Since Data.Map is sorted, you get them in ASCII order for free.

λ> :m + Data.Map
λ> let input = "happy"
λ> toList $ fromListWith (+) [(c, 1) | c <- input]
[('a',1),('h',1),('p',2),('y',1)]

So what's happening here?

The idea is to build a Data.Map (a tree map) using the characters as keys and the frequencies as values.

First, we take the input string and make tuples of each character with a 1 to indicate one occurrence.

λ> [(c, 1) | c <- input]
[('h',1),('a',1),('p',1),('p',1),('y',1)]

Next, we use fromListWith to build a sorted map from these key-value pairs by repeatedly inserting each key-value pair into a map. We also give it a function which will be used when a key was already in the map. In our case, we use (+) so that when a character is seen multiple times, we add the count to the existing sum.

Finally we covert the map back into a list of key-value tuples using toList.


问:
SQL Join on value a otherwise value b
I have two tables which I want to join in a certain way.

Table Resource:

id
type

1
1234

2
2345

Table Options:

articleId
typeId
optionKey

1
1234
1

1
2345
5

2
2345
5

What I basically want to achieve is getting all articleId's with optionKey 1. If optionKey 1 doesn't exist it should look for optionKey 5. There may also be situations that neither 1 or 5 exist, resulting in the following desired table:

articleId
typeId
optionKey

1
1234
1

2
2345
5

I'm already using a very specific query in an SQL view for our SAP system. It does work in a way, the only thing is that it still returns articleId's for both 1 and 5 if they both exist.

```<code>select
        resource."articleId",
        COALESCE(imageTypeChoice1."optionKey",imageTypeChoice5."optionKey") as "imageTypeOptionId"
from "resource" as resource
left join "Options" as imageTypeChoice1 on resource."type" = imageTypeChoice1."id" AND imageTypeChoice1."optionKey" = '1'
left join "Options" as imageTypeChoice5 on resource."type" = imageTypeChoice5."id" AND imageTypeChoice5."optionKey" = '5'
where (imageTypeChoice1."optionKey" IS NOT NULL OR imageTypeChoice5."optionKey" IS NOT NULL)
</code>```

Preferably we would like to do this without subquery's.

答:
I think what you need, given just the sample data provided, is to aggregate your options before joining.

```<code>select r.Id articleId, r.type, o.optionKey
from resource r
join (
    select articleid,  Min(optionkey) optionkey
    from options
    group by articleid
)o on o.articleId = r.id
</code>```

Title: SQL Join on value a otherwise value b

Body:
I have two tables which I want to join in a certain way.

Table Resource:

id
type

1
1234

2
2345

Table Options:

articleId
typeId
optionKey

1
1234
1

1
2345
5

2
2345
5

What I basically want to achieve is getting all articleId's with optionKey 1. If optionKey 1 doesn't exist it should look for optionKey 5. There may also be situations that neither 1 or 5 exist, resulting in the following desired table:

articleId
typeId
optionKey

1
1234
1

2
2345
5

I'm already using a very specific query in an SQL view for our SAP system. It does work in a way, the only thing is that it still returns articleId's for both 1 and 5 if they both exist.

select
        resource."articleId",
        COALESCE(imageTypeChoice1."optionKey",imageTypeChoice5."optionKey") as "imageTypeOptionId"
from "resource" as resource
left join "Options" as imageTypeChoice1 on resource."type" = imageTypeChoice1."id" AND imageTypeChoice1."optionKey" = '1'
left join "Options" as imageTypeChoice5 on resource."type" = imageTypeChoice5."id" AND imageTypeChoice5."optionKey" = '5'
where (imageTypeChoice1."optionKey" IS NOT NULL OR imageTypeChoice5."optionKey" IS NOT NULL)

Preferably we would like to do this without subquery's.

Body:
I think what you need, given just the sample data provided, is to aggregate your options before joining.

select r.Id articleId, r.type, o.optionKey
from resource r
join (
    select articleid,  Min(optionkey) optionkey
    from options
    group by articleid
)o on o.articleId = r.id


问:
Most efficient way to merge two large python dictionaries, Results and Relationships to be written to an excel file
I've been working on a webscraper which runs asynchronously to substantially improve speed. Because of the way scraper works, it writes two pieces of information for a url which it scrapes. These are stored in two dictionaries:

```<code># {url1: [parent1, parent2, parent3], url2: [parent1, parent2, parent3]...}
relationships = dict()
# {url1: [page, redirect, content_type...], url2: [page, redirect, content_type...]}
url_results = dict()
</code>```

The first dictionary, relationships, tracks each occurrence of the key url within a parent url
The second dictionary stores the resulting information I need from each url scraped.

This is all necessary for how I'm handling async threading and memorization, and works really nice. The problem lies in writing these results to an excel report. These dictionaries are getting to thousands of keys in size and merging them together is very slow.

The end result is that for each parent value for a url in relationships dict, a line is written in my excel sheet

parent
url
page
redirect
content type
etc

parent1
url1
page
redirect
content_type
etc...

parent1
url2
page
redirect
content_type
etc...

parent1
url3
page
redirect
content_type
etc...

parent2
url1
page
redirect
content_type
etc...

parent2
url2
page
redirect
content_type
etc...

parent2
url3
page
redirect
content_type
etc...

Here's the method I'm currently using to merge these dictionaries together like this:

```<code>partent_result_df = pd.DataFrame(columns=["Parent", "URL", "Page", "Redirect", "Content-type", "Source", "Status"])
location = 1
for i, key in enumerate(RELATIONSHIPS):
    # result_arr =  [Item, Redirect, Content_type, Source, Status]
    try:
        result_arr = URL_RESULTS[key]
    except:
        result_arr = [None, None, None, None, "Not Yet Accessed"]
    parents_urls = RELATIONSHIPS[key]
    for parent in parents_urls:
        partent_result_df.loc[location] = [parent, key, *result_arr]
        location += 1
return partent_result_df
</code>```

I'm looking for a way to make this merge much faster if at all possible. It's currently taking quite a lot of time to write this report, almost as much as the scrape itself, and I think I'm missing something that could help speed up this process.

I'd appreciate any help finding a better way to do this. Sorry for the information overkill, and thank you greatly for any help!

答:
You can make 2 DataFrames from the dictionaries and then use standard .merge():

import pandas as pd

relationships = {
    "url1": ["parent1", "parent2", "parent3"],
    "url2": ["parent1", "parent2", "parent3"],
}
url_results = {
    "url1": ["page1", "redirect1", "content_type1"],
    "url2": ["page2", "redirect2", "content_type2"],
}

df1 = pd.DataFrame(
    [(k, p) for k, v in relationships.items() for p in v],
    columns=["url", "parent"],
)
df2 = pd.DataFrame(
    [(k, *v) for k, v in url_results.items()],
    columns=["url", "page", "redirect", "content_type"],
)

print(df1.merge(df2, on="url"))
```
<p>Prints:</p>
<pre class="lang-none prettyprint-override"><code>    url   parent   page   redirect   content_type
0  url1  parent1  page1  redirect1  content_type1
1  url1  parent2  page1  redirect1  content_type1
2  url1  parent3  page1  redirect1  content_type1
3  url2  parent1  page2  redirect2  content_type2
4  url2  parent2  page2  redirect2  content_type2
5  url2  parent3  page2  redirect2  content_type2
</code>```

Body:
I've been working on a webscraper which runs asynchronously to substantially improve speed. Because of the way scraper works, it writes two pieces of information for a url which it scrapes. These are stored in two dictionaries:

# {url1: [parent1, parent2, parent3], url2: [parent1, parent2, parent3]...}
relationships = dict()
# {url1: [page, redirect, content_type...], url2: [page, redirect, content_type...]}
url_results = dict()

The first dictionary, relationships, tracks each occurrence of the key url within a parent url
The second dictionary stores the resulting information I need from each url scraped.

This is all necessary for how I'm handling async threading and memorization, and works really nice. The problem lies in writing these results to an excel report. These dictionaries are getting to thousands of keys in size and merging them together is very slow.

The end result is that for each parent value for a url in relationships dict, a line is written in my excel sheet

parent
url
page
redirect
content type
etc

parent1
url1
page
redirect
content_type
etc...

parent1
url2
page
redirect
content_type
etc...

parent1
url3
page
redirect
content_type
etc...

parent2
url1
page
redirect
content_type
etc...

parent2
url2
page
redirect
content_type
etc...

parent2
url3
page
redirect
content_type
etc...

Here's the method I'm currently using to merge these dictionaries together like this:

partent_result_df = pd.DataFrame(columns=["Parent", "URL", "Page", "Redirect", "Content-type", "Source", "Status"])
location = 1
for i, key in enumerate(RELATIONSHIPS):
    # result_arr =  [Item, Redirect, Content_type, Source, Status]
    try:
        result_arr = URL_RESULTS[key]
    except:
        result_arr = [None, None, None, None, "Not Yet Accessed"]
    parents_urls = RELATIONSHIPS[key]
    for parent in parents_urls:
        partent_result_df.loc[location] = [parent, key, *result_arr]
        location += 1
return partent_result_df

I'm looking for a way to make this merge much faster if at all possible. It's currently taking quite a lot of time to write this report, almost as much as the scrape itself, and I think I'm missing something that could help speed up this process.

I'd appreciate any help finding a better way to do this. Sorry for the information overkill, and thank you greatly for any help!

Body:
You can make 2 DataFrames from the dictionaries and then use standard .merge():

import pandas as pd

relationships = {
    "url1": ["parent1", "parent2", "parent3"],
    "url2": ["parent1", "parent2", "parent3"],
}
url_results = {
    "url1": ["page1", "redirect1", "content_type1"],
    "url2": ["page2", "redirect2", "content_type2"],
}

df1 = pd.DataFrame(
    [(k, p) for k, v in relationships.items() for p in v],
    columns=["url", "parent"],
)
df2 = pd.DataFrame(
    [(k, *v) for k, v in url_results.items()],
    columns=["url", "page", "redirect", "content_type"],
)

print(df1.merge(df2, on="url"))

Prints:

    url   parent   page   redirect   content_type
0  url1  parent1  page1  redirect1  content_type1
1  url1  parent2  page1  redirect1  content_type1
2  url1  parent3  page1  redirect1  content_type1
3  url2  parent1  page2  redirect2  content_type2
4  url2  parent2  page2  redirect2  content_type2
5  url2  parent3  page2  redirect2  content_type2


问:
Set a single returned result from sql query as a variable
I have a query that is returning a single result.  I would like to set that as a variable value in the query to be used later on.  It shows up often with all the sub querys that I have.
So I would like to

```<code>DECLARE @NEWID AS INT = 55
</code>```

IdNumber

55

答:
There's 2 ways to get a value from a table into a variable.

Set

```<code>DECLARE @NEWID AS INT;

SET @NEWID = (SELECT MAX(ID)+1 FROM SomeTable);
</code>```

But the query may only return a single value.

Select

```<code>DECLARE @NEWID AS INT;

SELECT @NEWID = MAX(ID)+1
FROM SomeTable;
</code>```

The variable will become the last value that would be returned by the query.

Body:
I have a query that is returning a single result.  I would like to set that as a variable value in the query to be used later on.  It shows up often with all the sub querys that I have.
So I would like to

DECLARE @NEWID AS INT = 55

IdNumber

55

Body:
There's 2 ways to get a value from a table into a variable.

Set

DECLARE @NEWID AS INT;

SET @NEWID = (SELECT MAX(ID)+1 FROM SomeTable);

But the query may only return a single value.

Select

DECLARE @NEWID AS INT;

SELECT @NEWID = MAX(ID)+1
FROM SomeTable;

The variable will become the last value that would be returned by the query.


问:
Theme from React Navigation and Paper doesn't work on the screens background
I'm using themes from React Native Navigation and from React Native Paper, after some configuration everything works, titles, text, headers, etc. Except for the background of the screens.

I used Stack Navigator, Bottom Tab Navigator, and Top Tab Navigator, and only in the Top Tab Navigator screens is working the background color theme. In the other screens from Stack and Bottom Navigator the background is always white and the texts black.

I'm wrapping everything with PaperProvider and NavigationContainer, here is some code:

Custom Themes:

```<code>import {
  DefaultTheme as PaperDefaultTheme,
  DarkTheme as PaperDarkTheme
} from 'react-native-paper';
import {
  DefaultTheme,
  DarkTheme
} from '@react-navigation/native';

const CustomDefaultTheme = {
  ...DefaultTheme,
  ...PaperDefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    ...PaperDefaultTheme.colors,
    primary: '#A16E2E'
  }
}

const CustomDarkTheme = {
  ...DarkTheme,
  ...PaperDarkTheme,
  colors: {
    ...DarkTheme.colors,
    ...PaperDarkTheme.colors,
    primary: '#F9CA2E',
    card: '#000',
    background: '#000',
    border: '#FFFFFF',
  }
}
export { CustomDefaultTheme, CustomDarkTheme }
</code>```

Applying themes:

```<code>export default MainApp = () => {

  const [isDarkTheme, setIsDarkTheme] = useState(false)
  const myFunction = useMemo(() => ({
    toggleTheme: () => {
      setIsDarkTheme(isDarkTheme => !isDarkTheme)
    }
  }))

  const theme = isDarkTheme ? CustomDefaultTheme : CustomDarkTheme

  return(
    <PaperProvider theme = {theme}>
      <myContext.Provider value = {myFunction}>
        <NavigationContainer theme = {theme}>
          <Stack_LoginRegister />
        </NavigationContainer>
      </myContext.Provider>
    </PaperProvider>
  )
}
</code>```

NOTE: The problem existed before I added myContext.Provider, so that's not the problem.

Screen with Top Tab Navigator, nested with Stack and Bottom Tab Navigator

Screen with only Stack Navigator

And thats how the background color only applys on screens with TopTab Navigator.
I hoppe someone can help me.
Thank you so much!

答:
Maybe I'm too late here.
I think your problem is that your are not importing the theme from the Navigation. You have use the PaperDarkTheme for the Navigation as well.

```<code>import * as React from 'react';
import { NavigationContainer, DarkTheme } from '@react-navigation/native';
import {
  DarkTheme as PaperDarkTheme,
  Provider as PaperProvider,
} from 'react-native-paper';

export default function Main() {
  return (
    <PaperProvider theme={PaperDarkTheme}>
      <NavigationContainer theme={DarkTheme}>
        {/* content */}
      </NavigationContainer>
    </PaperProvider>
  );
}
</code>```

Source: Reac Navigation

Body:
I'm using themes from React Native Navigation and from React Native Paper, after some configuration everything works, titles, text, headers, etc. Except for the background of the screens.

I used Stack Navigator, Bottom Tab Navigator, and Top Tab Navigator, and only in the Top Tab Navigator screens is working the background color theme. In the other screens from Stack and Bottom Navigator the background is always white and the texts black.

I'm wrapping everything with PaperProvider and NavigationContainer, here is some code:

Custom Themes:

import {
  DefaultTheme as PaperDefaultTheme,
  DarkTheme as PaperDarkTheme
} from 'react-native-paper';
import {
  DefaultTheme,
  DarkTheme
} from '@react-navigation/native';

const CustomDefaultTheme = {
  ...DefaultTheme,
  ...PaperDefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    ...PaperDefaultTheme.colors,
    primary: '#A16E2E'
  }
}

const CustomDarkTheme = {
  ...DarkTheme,
  ...PaperDarkTheme,
  colors: {
    ...DarkTheme.colors,
    ...PaperDarkTheme.colors,
    primary: '#F9CA2E',
    card: '#000',
    background: '#000',
    border: '#FFFFFF',
  }
}
export { CustomDefaultTheme, CustomDarkTheme }

Applying themes:

export default MainApp = () => {

  const [isDarkTheme, setIsDarkTheme] = useState(false)
  const myFunction = useMemo(() => ({
    toggleTheme: () => {
      setIsDarkTheme(isDarkTheme => !isDarkTheme)
    }
  }))

  const theme = isDarkTheme ? CustomDefaultTheme : CustomDarkTheme

  return(
    <PaperProvider theme = {theme}>
      <myContext.Provider value = {myFunction}>
        <NavigationContainer theme = {theme}>
          <Stack_LoginRegister />
        </NavigationContainer>
      </myContext.Provider>
    </PaperProvider>
  )
}

NOTE: The problem existed before I added myContext.Provider, so that's not the problem.

Screen with Top Tab Navigator, nested with Stack and Bottom Tab Navigator

Screen with only Stack Navigator

And thats how the background color only applys on screens with TopTab Navigator.
I hoppe someone can help me.
Thank you so much!

Body:
Maybe I'm too late here.
I think your problem is that your are not importing the theme from the Navigation. You have use the PaperDarkTheme for the Navigation as well.

import * as React from 'react';
import { NavigationContainer, DarkTheme } from '@react-navigation/native';
import {
  DarkTheme as PaperDarkTheme,
  Provider as PaperProvider,
} from 'react-native-paper';

export default function Main() {
  return (
    <PaperProvider theme={PaperDarkTheme}>
      <NavigationContainer theme={DarkTheme}>
        {/* content */}
      </NavigationContainer>
    </PaperProvider>
  );
}

Source: Reac Navigation