javascript – Dynamic REST Statements

Is there a way to dynamically create a REST call and specifically I am referring to the data: section of the call. If I get the terminology incorrect by all means keep me honest. I tried some things with no success. I looked at the __metadata portion and thought this was a key/value pair and tried some substitution with no luck. Same for the field:value pairs. Been poking around on stack to try to find something with no luck so far.

So, in the following REST example is there a way to “break out” the information following the __metadata in the brackets and also the field:value immediately after the brackets.

    $.ajax({
    type: 'POST',
    url: odataUrl, 
    contentType: "application/json;odata=verbose",
    processData: false,
    headers: {
            "Accept": "application/json;odata=verbose",
            "Content-Type":"application/json;odata=verbose",
            "X-RequestDigest":$("#__REQUESTDIGEST").val(),
            //"If-Match": "*", 
            //"X-HTTP-Method": "MERGE"
    },
              
    data: JSON.stringify(
        {
        __metadata :{
            **"type":"SP.Data.ErrorHandlerListItem"
        },
        Title:"TEST",
        ver:'2.0',
        modname:'no name'**
        

    }),

I tried using the following with no luck as an example setting the variables outside the REST statement and replacing the metadata statement with the following vars.

var typetest={"type":"SP."+TargetListName+"ListItem"};
var savetst={Title:"projname",ver:'test',modname:'browsertest'};
     data: JSON.stringify(
         {
          __metadata :{typetest},savetxt

Thanks for the help.

terminology – Specifying resources through dynamic attribute access: Term for this technique?

In languages like Python where it’s possible for an instance to resolve instance.foo by applying a function to the string "foo", some libraries take advantage of this feature to let the programmer specify a multi-level resource through chained attribute access, without defining the attributes ahead of time.

Some examples:

  • The xmlrpc.client.ServerProxy class in Python’s standard library lets one call an XML-RPC method foo.bar on a client instance by writing client.foo.bar(). The client.foo attribute is not hardcoded into the client; rather, it is created “on demand” when it is accessed, and likewise for client.foo.bar.

  • The agithub and slumber Python HTTP libraries both provide an API in which the URL path and HTTP method for a request are specified via chained attributes. For example, to make a POST request to /foo/bar, one writes client.foo.bar.post(). The client object has no knowledge of the valid URL routes.

Is there a name for this technique or feature?

mysql – Dynamic pivot table filtering and performance

I have this those tables and i’m trying to rotate the subscriber to column table to horzental and filter it’s result based on multiple AND/OR conditions like following

    WHERE  first_name LIKE 'm%' AND email LIKE '%com'

This is the fiddle
http://sqlfiddle.com/#!9/a7211d/1

Those are my 2 tables

Fields Table
+----+------------+
| id |label       |
+----+------------+
|  1 | email      |
|  2 | first_name |
|  3 | last_name  |
+-----------------+

Subscribers Fields Table
+----+--------------+----------+---------------+-------------------+
| id | mail_list_id | field_id | subscriber_id | value             |
+----+--------------+----------+---------------+-------------------+
|  1 |            1 |        1 |             1 | mark@examble.com  |
|  2 |            1 |        2 |             1 | Mark              |
|  3 |            1 |        3 |             1 | Wood              |
|  4 |            1 |        1 |             2 | luan@domain.com   |
|  3 |            1 |        2 |             2 | Luan              |
|  4 |            1 |        3 |             2 | Charles           |
|  5 |            1 |        1 |             3 | marry@domain.com  |
|  6 |            1 |        2 |             3 | Anna              |
|  7 |            1 |        3 |             3 | Marry             |
|  8 |            2 |        1 |             4 | kevin@domain.com  |
|  9 |            2 |        2 |             4 | Kevin             |
| 10 |            2 |        3 |             4 | Faustino          |
| 11 |            2 |        1 |             5 | frank@examble.com |
| 12 |            2 |        2 |             5 | Frank             |
| 13 |            2 |        3 |             5 | Denis             |
| 14 |            2 |        1 |             6 | max@example.com   |
| 15 |            2 |        2 |             6 | Max               |
| 16 |            2 |        3 |             6 | Ryan              |
+----+--------------+----------+---------------+-------------------+

This is what i tried but that caused to issues that the email and first_name return 0 instead of value and also it dont work with AND condition operator

select 
  subscriber_id,
  MAX(case when field_id = '1' then value else 0 end) as email,
  MAX(case when field_id = '2' then value else 0 end) as first_name,
  MAX(case when field_id = '3' then value else 0 end) as last_name
from test_fields_table
WHERE (field_id = 3 AND value LIKE 'm%') OR (field_id = 1 AND value = '%com')
group by subscriber_id limit 100;

However if i removed the Where condition the query works fine with good performance

I also tried to add my query in a subquery give it an alias and then search that generated virtual table using the alias field name instead of the field id but in this case i will have to remove the limit parameter from the subquery in order to be able to search for the full table not just in the first 100 records which cause a very bad performance since this table will be too large 100-500 milion record and i need to get the query result in under 4 seconds.

dynamic programing. divide boxes stacking to floors

Consider the next problem: in a building, there are boxes in storing on $n$ floors for $1leq i leq n$ there are $c_i$ boxes.
workers required to move the boxes to $mleq n$ floors the workers can move boxes from higher floor lower floor.

solution: partition of ${1…n}$ to $P_1 …P_m $ and $l_1 … l_m$ the floors that will contain the boxes

define: the cost of transform boxes from floor $i$ to floor $j $ and $j leq i $ is $c_i(i-j)$

define: the cost of a solution is $Sigma_{i=1}^{m} Sigma_{jin F_i} c_j(j-l_i)$

optimal solution: a legal solution with minimum cost.

for an example: the for an input $A = {4,2,3,5,8}$ and $m=2$ optimal solution is $F_1 ={1,2,3}
,F_2 ={4,5}$
and the floors are: $l_1 =1 ,l_2 =4$ the cost of the solution is 16

Find an optimal structure.

my try:
$$
OPT(n,m) =
begin{cases}
Sigma_{l=2}^{n} c_l(l-1) , m=1\
0, m=n\
min{OPT(n-1,m-1),OPT(n-1,m) + c_n(n-l_m)}
end{cases}
$$

My thought was that if I solve the problem top-bottom than on any floor n I have 2 option or not transform the boxes to another floor or transform the boxes to $l_m$ of the subproblem.
but that optimal structure doesn’t work because in the example

$OPT(4,2)= 7 ,OPT(4,1) = 23$ and according to that optimal structure $OPT(5,2) = 23$ but there is soultuon with cost of 16.

approaching dynamic programing for problem

This problem is easily solved if you:

  • transform the input graph $G=(V,E)$ by either creating $n-1$ “levels”, where each level is a copy of $G$ and edges go from one level to the next; or
  • consider the line graph $G’$ of $G$ instead (so that edges in $G$ become vertices in $G’$, and two vertices in $G’$ are adjacent if the corresponding edges in $G$ share an endpoint); or
  • For each color $c in {red, blue}$, identify the maximal components $C$ of $G$ such that connectivity within $C$ is preserved using only edges of color $c$.

If you insist in using a dynamic programming algorithm, then the problem can be solved in $O(m)$ time using a Dijkstra-like algorithm.
Let’s start with some definitions:

Given a color $c in {red, blue}$, define a $c$-ending path as a simple path $P$ such that either 1) $P$ is empty, or 2) $P$ ends with an edge of color $c$.

Given a path $P$, let $ell(P)$ be the number of color changes encountered when traversing $P$.
We will say a path $P$ from $s$ to $v$ is a shortest $c$-ending path if there exist no other $c$-ending path $P’$ such that $ell(P’) < ell(P)$.

We will call $eta(v,c)$ the value of $ell(P)$, where $P$ is a shortest $c$-ending path from $s$ to $v$.

According to the above definitions $eta(s, red) = eta(s, blue) = 0$.
Moreover, the following suboptimality property holds:

Claim: Let $P$ be any shortest $c$-ending path from $s$ to $v$, let $u in P$.
The subpath $P(s:u)$ of $P$ going from $s$ to $u$ is a shortest $c’$-ending path where $c’$ is the color of the incoming edge in $u$ (if $u=s$ then $c’$ is any color).

Proof:
Assume that $u neq v$, otherwise the claim is trivial.
Let $c”$ be the color of the edge leaving $u$ in $P$.

Suppose towards a contradiction that $P(s:u)$ is not a shortest $c’$-ending path.
Let $P’$ be a shortest $c’$-ending path from $s$ to $u$, and notice that $Q = P’ circ P(u:v)$ is a (not necessarily simple) path from $s$ to $v$.
$Q$ ends with an edge of color $c$ and we have:
$$
ell(P) = ell(P(s:u)) + 1_{c’ neq c”} + ell(P(u:v))
> ell(P’) + 1_{c’ neq c”} + ell(P(u:v))
= ell(Q),
$$

contradicting the fact that $P$ is a shortest $c$-ending path. $square$

For each node $v$ we will maintain two upper bound $tilde{eta}(v, red)$ and $tilde{eta}(v, blue)$ on $eta(v, red)$ and $eta(v, blue)$, respectively. Initially $tilde{eta}(s, red)=tilde{eta}(s, blue)=eta(s, red) = eta(s, blue) = 0$ while, for $v neq s$, $tilde{eta}(s, red)=tilde{eta}(s, blue) = +infty$.

Finally, we will maintain a priority queue $Q$ in which keys are pairs $(v,c)$ where $v$ is an unmarked vertex and $c$ is a color, and the corresponding priority will be $tilde{eta}(v, c)$. Initially all pairs $(v,c)$ are in the queue.

The algorithm proceeds as follows: while $Q$ is not empty, extract $(u,c)$ from $Q$; for each edge $e=(u,v) in E$ let $c’$ be the color of $e$ and set $tilde{eta}(v, c) = min{tilde{eta}(v, c), tilde{eta}(u, c) + 1_{c neq c’} }$ (thus possibly updating the priority of $(v,c)$, if such a pair is in $Q$).

It is clear by construction that all values $tilde{eta}(v, c)$ are upper bounds to $eta(v,c)$ as claimed. We now prove that, when the pair $(v,c)$ is extracted from $Q$, $tilde{eta}(v, c)$ is also a lower bound to $eta(v,c)$, thus proving that $tilde{eta}(v, c) = eta(v,c)$.

Claim: When $i$-th pair $(v_i,c_i)$ is extracted from $Q$, $tilde{eta}(v, c) le eta(v,c)$.

Proof:
Let $i$ be the smallest value such that, when $(v_i, c_i)$ is extracted from $Q$ we have $tilde{eta}(v_i, c_i) > eta(v_i,c_i)$.

Since the values $tilde{eta}(v_i, c_i)$ never decrease during the execution of the algorithm and cannot become negative we know that $v_i neq s$.
Let $P$ be a shortest $c_i$-ending path from $s$ to $v_i$ and consider the last vertex $x$ such that the incoming edge in $x$ in $P$ has color $c_x$ (if $x=s$ let $c_x$ be any color) and $x=v_j$ and $c_x=c_j$ for some $j<i$ (notice such a vertex always exists since the above conditions are satisfied for $x=s$).
Let $y$ be the vertex following $x$ in $P$ and $c_y$ be the color of the incoming edge.

Since $(v_i, c_i)$ was extracted instead of $(y, c_y)$ we must have:
$$
tildeeta(v_i, c_i) le tildeeta(y, c_y)
$$

Moreover, since $(x, c_x)$ was already extracted from $q$ we have:
$$
tildeeta(y, c_y) le tildeeta(x, c_x) + 1_{c_x neq x_y} = eta(x, c_x) + 1_{c_x neq x_y}
$$

And, by suboptimality:
$$
eta(x, c_x) + 1_{c_x neq x_y} =
eta(y, c_y) le eta(v_i,c_i),
$$

which is a contradiction $square$.

The sought quantity is then $min{tildeeta(t,red), tildeeta(t,blue) }$.

Finally, notice that the the priority can only decrease in $Q$, that the priority of the extracted pairs is monotonically increasing, and that the only possible values of the priorities are ${0, 1, dots, n} cup { +infty }$. Therefore it is possible to implement a priority $Q$ that requires $O(n+m) = O(m)$ time to perform all the required $O(n)$ insertions and $O(m)$ priority updates.

sass – Is there a more dynamic way to target siblings in CSS / SCSS?

The code I’m working on looks like this – All the way up to 50… (Not ideal)

There can be anywhere from 1 to 100 anchor/div combinations, but more often than not in single digits so there’s almost always a lot of redundant CSS rules.

I was wondering if there was a better way to write the CSS / SCSS for this rather than code for each possible outcome manually? Or would the only way to do this be by using JS?

.content { display: none; }
.anchor-1:hover ~ .content-1,
.anchor-2:hover ~ .content-2,
.anchor-3:hover ~ .content-3,
.anchor-4:hover ~ .content-4,
.anchor-5:hover ~ .content-5 { display: block; }

I also have the code fiddled here: https://jsfiddle.net/rn437pw8/8/

unity – Check if NavMeshAgent can reach position with dynamic obstacles

Thanks for contributing an answer to Game Development Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

c++ – Code A Primitive Calculator ( x3,x2,+1) Using Dynamic programming

Question -You are given a primitive calculator that can perform the following three operations with the current number 𝑥: multiply 𝑥 by 2, multiply 𝑥 by 3, or add 1 to 𝑥. Your goal is given apositive integer 𝑛, find the minimum number of operations needed to obtain the number 𝑛
starting from the number 1.

ll n;
cin>>n;

vector<ll>vec;
vec.pb(n);

if(n==1)
{
    cout<<0<<endl<<1;
}

else
{
    while(n!=1)
    {
        if(n%3==0)
        n=n/3;
        else if(n%2==0)
        n=n/2;
        else
        n=n-1;
        
        vec.pb(n);
    }
    
    cout<<vec.size()-1<<endl;
    
    for(int i=(vec.size()-1);i>=0;i--)
    cout<<vec(i)<<" ";
}
return 0;

}

for input 96234 iam getting

15

1 2 4 5 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234

but the optimal soln is

14

1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234

i know i am going wrong at the step when 10 is converted into 5 in my code but it should convert it in to 9, Please help me,

forms – How to check if dynamic select query returns zero records and return markup

I have a custom Drupal form that is populated with a dynamic query. How do I return an ‘alert’ if no results are returned? There has to be a better way of doing this than what I have come up with.

This is what I have so far and it throws an error when a record is returned because it continues processing the actual form.

Query:

public function loadByPID($pid = null) {    
  $database = DrupalCoreDatabaseDatabase::getConnection('default', 'newDB');    
  $query = $database->select ('Person');
  $query->condition('personid', $pid);
  $query->fields('Person', ('col1', 'col2', 'col3'));
  $results = $query->execute();
  return $results;
}

Call to query:

$results = $this->repository->loadByPID($pid);

The form and my attempt to check if there are zero records returned.

$rowCount = 0;
foreach ($results as $record) {         
    $rowCount++;
}
if ($rowCount == 0) {
  // alert if no values
  $form('no_values') = (
    '#markup' => $this->t('No entries exist.'),
    '#prefix' => '<div class="alert alert-warning my-5" role="alert">',
    '#suffix' => '</div>',
  );
  return $form;
}else{
  // the actual form
  $form()...
  return $form;
};