问:
Duplicate item returned from collection
Blog Schema:

{
        body: { type: String, required: true },
        title: { type: String, required: true },
        published: { type: String, default: false },
        date: { type: Date, default: Date.now },
        user: { type: Schema.Types.ObjectId, ref: 'BlogUser' },
        comments: [{ type: Schema.Types.ObjectId, ref: 'Comments' }],
        likes:[{user:{ type: Schema.Types.ObjectId, ref: 'BlogUser' }}]
    }
```

<p>Like Route for adding a like:</p>

```exports.likeBlog = async (req, res) => {
  const blog_id = req.params.blog_id;
  const user_id = req.body.user_id;
  await Blog.findByIdAndUpdate(
    blog_id,
    {
      $push: {
        likes: {
          user: user_id,
        },
      },
    },
    { new: true },
    (err, newBlog) => {
      if (err) res.status(422).json(err);
      console.log(newBlog);
      res.json(newBlog);
    }
  );
};
```

<p>Blog Route for reciveing a blog:</p>

<pre class="lang-js prettyprint-override"><code>exports.getBlogByID = async (req, res) => {
  const blog_id = req.params.blog_id;
  try {
    const blog = await Blog.findById(blog_id)
      .populate("comments")
      .populate("user");
    console.log(blog);
    res.json(blog);
  } catch (error) {
    res.status(401).json(error);
  }
};
</code>```

When I add a like by calling Like route from client, I get a blog with correct amount of likes i.e only 1. But when I request blog from  Blog Route it returns me with two objects inside "likes" array, with both same as each other(same id too). Why am I getting such result? Mind you that I call 'Blog Route' after calling 'Like Route'.

答:
It worked fine after I changed "like route" to this:

exports.likeBlog = async (req, res) => {
  const blog_id = req.params.blog_id;
  const user_id = req.body.user_id;
  const blog = await Blog.findById(blog_id);
  blog.likes.unshift({ user: user_id });
  await blog.save();
  Blog.findById(blog_id)
    .then((result) => {
      res.json(result);
    })
    .catch((error) => {
      res.status(501).json({ error });
    });
};
```

I still don't know what's the difference between the two though.

Title: Duplicate item returned from collection

Body:
Blog Schema:

{
        body: { type: String, required: true },
        title: { type: String, required: true },
        published: { type: String, default: false },
        date: { type: Date, default: Date.now },
        user: { type: Schema.Types.ObjectId, ref: 'BlogUser' },
        comments: [{ type: Schema.Types.ObjectId, ref: 'Comments' }],
        likes:[{user:{ type: Schema.Types.ObjectId, ref: 'BlogUser' }}]
    }

Like Route for adding a like:

exports.likeBlog = async (req, res) => {
  const blog_id = req.params.blog_id;
  const user_id = req.body.user_id;
  await Blog.findByIdAndUpdate(
    blog_id,
    {
      $push: {
        likes: {
          user: user_id,
        },
      },
    },
    { new: true },
    (err, newBlog) => {
      if (err) res.status(422).json(err);
      console.log(newBlog);
      res.json(newBlog);
    }
  );
};

Blog Route for reciveing a blog:

exports.getBlogByID = async (req, res) => {
  const blog_id = req.params.blog_id;
  try {
    const blog = await Blog.findById(blog_id)
      .populate("comments")
      .populate("user");
    console.log(blog);
    res.json(blog);
  } catch (error) {
    res.status(401).json(error);
  }
};

When I add a like by calling Like route from client, I get a blog with correct amount of likes i.e only 1. But when I request blog from  Blog Route it returns me with two objects inside "likes" array, with both same as each other(same id too). Why am I getting such result? Mind you that I call 'Blog Route' after calling 'Like Route'.

Body:
It worked fine after I changed "like route" to this:

exports.likeBlog = async (req, res) => {
  const blog_id = req.params.blog_id;
  const user_id = req.body.user_id;
  const blog = await Blog.findById(blog_id);
  blog.likes.unshift({ user: user_id });
  await blog.save();
  Blog.findById(blog_id)
    .then((result) => {
      res.json(result);
    })
    .catch((error) => {
      res.status(501).json({ error });
    });
};

I still don't know what's the difference between the two though.


来源: stackexchange

问:
My c++ function is not running and I am not able to decode it
Hey guys I am new with c++. I have the following function in c++ but it always gives me an error by the "double overlap" function and I do not know why. Can somebody tell me, what I am doing wrong?</p>

```<code>double overlap(vector<double> &R1, vector<double> &R2) {
    // overlap in the x dimension
    double xmin = max(R1[0], R2[0]);
    double xmax = min(R1[2], R2[2]);
    if (xmin >= xmax) return 0;

    // overlap in the y dimension
    double ymin = max(R1[1], R2[1]);
    double ymax = min(R1[3], R2[3]);
    if (ymin >= ymax) return 0;

    double overlap_area = (xmax-xmin)*(ymax-ymin)

    return overlap_area;
}

int main() {
    vector<int> R1;
    vector<int> R2;

    // Coordinates of the rectangles
    // with the .push_back() function we are adding the rectangles to our vector to give it to the algorithm.
    // x: x-coordinate, y: y-coordinate, w: weidth, h: height - (w,h: distance from left/botton to right/top side of the rectangle)
    int x1 = 0, y1 = 0, w1 = 6, h1 = 9;
    R1.push_back(x1);
    R1.push_back(y1);
    R1.push_back(w1);
    R1.push_back(h1);

    int x2 = 3, y2 = 4, w2 = 3, h2 = 3;
    R2.push_back(x2);
    R2.push_back(y2);
    R2.push_back(w2);
    R2.push_back(h2);

    cout << "Overlap = " << overlap(R1, R2) << endl;
}
</code>```

答:
There are 2 errors in your program:

The statement double overlap_area = (xmax-xmin)*(ymax-ymin) should be double overlap_area = (xmax-xmin)*(ymax-ymin);

You are passing an std::vector<int> and accepting it as a reference in the function as std::vector<double> which is rightly pointed out by the error

In function 'int main()':
43:43: error: invalid initialization of reference of type 'std::vector&' from expression of type 'std::vector'
6:8: note: in passing argument 1 of 'double overlap(std::vector&, std::vector&)'

You need to change the R1 and R2 in your main to double type vector

ContentLicense: CC BY-SA 4.0

Body:
Hey guys I am new with c++. I have the following function in c++ but it always gives me an error by the "double overlap" function and I do not know why. Can somebody tell me, what I am doing wrong?

double overlap(vector<double> &R1, vector<double> &R2) {
    // overlap in the x dimension
    double xmin = max(R1[0], R2[0]);
    double xmax = min(R1[2], R2[2]);
    if (xmin >= xmax) return 0;

    // overlap in the y dimension
    double ymin = max(R1[1], R2[1]);
    double ymax = min(R1[3], R2[3]);
    if (ymin >= ymax) return 0;

    double overlap_area = (xmax-xmin)*(ymax-ymin)

    return overlap_area;
}

int main() {
    vector<int> R1;
    vector<int> R2;

    // Coordinates of the rectangles
    // with the .push_back() function we are adding the rectangles to our vector to give it to the algorithm.
    // x: x-coordinate, y: y-coordinate, w: weidth, h: height - (w,h: distance from left/botton to right/top side of the rectangle)
    int x1 = 0, y1 = 0, w1 = 6, h1 = 9;
    R1.push_back(x1);
    R1.push_back(y1);
    R1.push_back(w1);
    R1.push_back(h1);

    int x2 = 3, y2 = 4, w2 = 3, h2 = 3;
    R2.push_back(x2);
    R2.push_back(y2);
    R2.push_back(w2);
    R2.push_back(h2);

    cout << "Overlap = " << overlap(R1, R2) << endl;
}


问:
IntelliJ IDEA: Breakpoint not being hit, and is shown without the tick, just a red dot
I have a gradle project, and when I put a breakpoint it is displayed as a red dot, with a tick inside, and it just works fine.

The problem is that in some classes (in the same project, not a dependency class), the red dot doesn't have a tick inside, and it doesn't tell why it wouldn't work. And it just doesn't work..:

What have I tried so far:

Gradle clean & build

Rebuild project

Restart debug

Invalidate caches and restart IntelliJ

Update IntelliJ to the latest version (2017.2.5)

Mute & Unmute breakpoints

This question is similar, but clearly not the same: Intellij IDEA: Breakpoint not being hit, and is shown greyed out

答: I made a n00b mistake on the IDE. Instead of hitting the debug button, I thought it would work the same as visual studio or eclipse in that I'd have to use the run button, yet when I hit the debug button (That looks like an actual bug) my break-point hit just fine.</p>


问:
How to read a value from API in flutter but it is not a json
My API returns just "pending" as the output when called.

I use the future function to call the API.

```<code> Future getOtp(rollNumber, phoneNumber, schoolName) async {

    Response response = await post(genOtpUrl, body: {
      "rollNumber" : rollNumber,
      "phNumber": phoneNumber,
      "schoolName" : schoolName
    });
    print(response.body);
    var result = jsonDecode(response.body);
    return result;

  }
</code>```

Here it prints 'pending to the console' but the return statement just return Future I want to access the data but it is not a JSON how to do it.

Thanks in advance.

答:
"pending' is a String so you have to return a String.

```<code>Future<String> getOtp(rollNumber, phoneNumber, schoolName) async {
    Response response = await post(genOtpUrl, body: {
      "rollNumber" : rollNumber,
      "phNumber": phoneNumber,
      "schoolName" : schoolName
    });
    print(response.body);
    var result = response.body;
    return result;
  }
</code>```

To access the string you can do the following

```<code>callOtp() async{
   String otpResult = await getOtp(rollNumber, phoneNumber, schoolName);
   print(otpResult);
}
</code>```

Body:
My API returns just "pending" as the output when called.

I use the future function to call the API.

 Future getOtp(rollNumber, phoneNumber, schoolName) async {

    Response response = await post(genOtpUrl, body: {
      "rollNumber" : rollNumber,
      "phNumber": phoneNumber,
      "schoolName" : schoolName
    });
    print(response.body);
    var result = jsonDecode(response.body);
    return result;

  }

Here it prints 'pending to the console' but the return statement just return Future I want to access the data but it is not a JSON how to do it.

Thanks in advance.

Body:
"pending' is a String so you have to return a String.

Future<String> getOtp(rollNumber, phoneNumber, schoolName) async {
    Response response = await post(genOtpUrl, body: {
      "rollNumber" : rollNumber,
      "phNumber": phoneNumber,
      "schoolName" : schoolName
    });
    print(response.body);
    var result = response.body;
    return result;
  }

To access the string you can do the following

callOtp() async{
   String otpResult = await getOtp(rollNumber, phoneNumber, schoolName);
   print(otpResult);
}


问:
Spring security, cors error when enable Oauth2
I'm getting an error while querying my oauth/token endpoint.

I've configured cors enable for my resource / also tried to allow all resources but nothing worked.

XMLHttpRequest cannot load http://localhost:8080/oauth/token. Response
  to preflight request doesn't pass access control check: No
  'Access-Control-Allow-Origin' header is present on the requested
  resource. Origin 'http://localhost:1111' is therefore not allowed
  access. The response had HTTP status code 401.

```<code>vendor.js:1837 ERROR SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at CatchSubscriber.selector (app.js:7000)
    at CatchSubscriber.error (vendor.js:36672)
    at MapSubscriber.Subscriber._error (vendor.js:282)
    at MapSubscriber.Subscriber.error (vendor.js:256)
    at XMLHttpRequest.onError (vendor.js:25571)
    at ZoneDelegate.invokeTask (polyfills.js:15307)
    at Object.onInvokeTask (vendor.js:4893)
    at ZoneDelegate.invokeTask (polyfills.js:15306)
    at Zone.runTask (polyfills.js:15074)
defaultErrorLogger @ vendor.js:1837
ErrorHandler.handleError @ vendor.js:1897
next @ vendor.js:5531
schedulerFn @ vendor.js:4604
SafeSubscriber.__tryOrUnsub @ vendor.js:392
SafeSubscriber.next @ vendor.js:339
Subscriber._next @ vendor.js:279
Subscriber.next @ vendor.js:243
Subject.next @ vendor.js:14989
EventEmitter.emit @ vendor.js:4590
NgZone.triggerError @ vendor.js:4962
onHandleError @ vendor.js:4923
ZoneDelegate.handleError @ polyfills.js:15278
Zone.runTask @ polyfills.js:15077
ZoneTask.invoke @ polyfills.js:15369
</code>```

With Postman everything works perfect.

My cors security configuration:

```<code>@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }
}
</code>```

also tried to add http://localhost:1111 in allowed origins

Code in Postman:

```<code>require 'uri'
require 'net/http'

url = URI("http://localhost:8080/oauth/token")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/x-www-form-urlencoded'
request["authorization"] = 'Basic Y2hhdHRpbzpzZWNyZXRzZWNyZXQ='
request["cache-control"] = 'no-cache'
request["postman-token"] = 'daf213da-e231-a074-02dc-795a149a3bb2'
request.body = "grant_type=password&username=yevhen%40gmail.com&password=qwerty"

response = http.request(request)
puts response.read_body
</code>```

答:
This worked for me</p>

```<code>@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception
      {
        security.tokenKeyAccess("permitAll()")
        .checkTokenAccess("isAuthenticated()");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.applyPermitDefaultValues();

        // add allow-origin to the headers
        config.addAllowedHeader("access-control-allow-origin");

        source.registerCorsConfiguration("/oauth/token", config);
        CorsFilter filter = new CorsFilter(source);
        security.addTokenEndpointAuthenticationFilter(filter);

    }

}
</code>```

Body:
I'm getting an error while querying my oauth/token endpoint.

I've configured cors enable for my resource / also tried to allow all resources but nothing worked.

XMLHttpRequest cannot load http://localhost:8080/oauth/token. Response
  to preflight request doesn't pass access control check: No
  'Access-Control-Allow-Origin' header is present on the requested
  resource. Origin 'http://localhost:1111' is therefore not allowed
  access. The response had HTTP status code 401.

vendor.js:1837 ERROR SyntaxError: Unexpected token u in JSON at position 0
    at JSON.parse (<anonymous>)
    at CatchSubscriber.selector (app.js:7000)
    at CatchSubscriber.error (vendor.js:36672)
    at MapSubscriber.Subscriber._error (vendor.js:282)
    at MapSubscriber.Subscriber.error (vendor.js:256)
    at XMLHttpRequest.onError (vendor.js:25571)
    at ZoneDelegate.invokeTask (polyfills.js:15307)
    at Object.onInvokeTask (vendor.js:4893)
    at ZoneDelegate.invokeTask (polyfills.js:15306)
    at Zone.runTask (polyfills.js:15074)
defaultErrorLogger @ vendor.js:1837
ErrorHandler.handleError @ vendor.js:1897
next @ vendor.js:5531
schedulerFn @ vendor.js:4604
SafeSubscriber.__tryOrUnsub @ vendor.js:392
SafeSubscriber.next @ vendor.js:339
Subscriber._next @ vendor.js:279
Subscriber.next @ vendor.js:243
Subject.next @ vendor.js:14989
EventEmitter.emit @ vendor.js:4590
NgZone.triggerError @ vendor.js:4962
onHandleError @ vendor.js:4923
ZoneDelegate.handleError @ polyfills.js:15278
Zone.runTask @ polyfills.js:15077
ZoneTask.invoke @ polyfills.js:15369

With Postman everything works perfect.

My cors security configuration:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true);
    }
}

also tried to add http://localhost:1111 in allowed origins

Code in Postman:

require 'uri'
require 'net/http'

url = URI("http://localhost:8080/oauth/token")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/x-www-form-urlencoded'
request["authorization"] = 'Basic Y2hhdHRpbzpzZWNyZXRzZWNyZXQ='
request["cache-control"] = 'no-cache'
request["postman-token"] = 'daf213da-e231-a074-02dc-795a149a3bb2'
request.body = "grant_type=password&username=yevhen%40gmail.com&password=qwerty"

response = http.request(request)
puts response.read_body

Body:
This worked for me

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception
      {
        security.tokenKeyAccess("permitAll()")
        .checkTokenAccess("isAuthenticated()");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.applyPermitDefaultValues();

        // add allow-origin to the headers
        config.addAllowedHeader("access-control-allow-origin");

        source.registerCorsConfiguration("/oauth/token", config);
        CorsFilter filter = new CorsFilter(source);
        security.addTokenEndpointAuthenticationFilter(filter);

    }

}


问:
What does pipe `|` operator do in case expression of elm-lang?
I have this following code snippet in my Elm code:

```<code>type alias Model =
  { content : String
  }

update : Msg -> Model -> Model
update msg model =
  case msg of
    Change newContent ->
      { model | content = newContent }
</code>```

What does { model | content = newContent } do?
Does it assign (bind) the value of newContent to model as well as content? Is that why the |  operator is placed there?

答:
The pipe is not a part of the case expression. It's record update syntax, as described here: https://elm-lang.org/docs/records#updating-records.

```<code>{ model | content = newContent }
</code>```

assigns the value of newContent to the content field in the model record.

Body:
I have this following code snippet in my Elm code:

type alias Model =
  { content : String
  }

update : Msg -> Model -> Model
update msg model =
  case msg of
    Change newContent ->
      { model | content = newContent }

What does { model | content = newContent } do?
Does it assign (bind) the value of newContent to model as well as content? Is that why the |  operator is placed there?

Body:
The pipe is not a part of the case expression. It's record update syntax, as described here: https://elm-lang.org/docs/records#updating-records.

{ model | content = newContent }

assigns the value of newContent to the content field in the model record.


问:
Unable to load variable from service in Angular 9
I am trying to create a web-app for a game that involves starting and joining rooms. I am using Angular and Socket.io for the project. I have components named Startpage and Lobby. Startpage contains text boxes for player name and Room ID. It also has buttons for "Start Game" and "Join Game". Clicking on the "Start Game" button triggers the startThisGame() function.

```<code>//startpage.component.ts

import { Component, OnInit, Output, NgZone } from '@angular/core';
import { Router } from '@angular/router';
import { SocketioService } from '../socketio.service';

@Component({
  selector: 'app-startpage',
  templateUrl: './startpage.component.html',
  styleUrls: ['./startpage.component.css']
})

export class StartpageComponent implements OnInit {

  constructor( private router: Router, public zone: NgZone, private socketService: SocketioService ) {}

  ngOnInit() {
    this.socketService.setupSocketConnection();
  }
  testRoomId = 'xxxx'

  startThisGame() {
    var playerName = (document.getElementById("nameInput") as HTMLInputElement).value;
    if (playerName) {
      this.testRoomId = this.socketService.startGame(playerName);
      alert("In component: " + this.testRoomId)
      this.zone.run(() => {this.router.navigate(['/lobby']); });
    }
    else{
      alert("Enter a player name!")
    }
  }
}
</code>```

The code inside socketService goes as follows.

```<code>import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class SocketioService {
  socket;
  public roomId: string;
  public playerName: string;
  constructor() {}

  setupSocketConnection() {
    this.socket = io(environment.SOCKET_ENDPOINT)
    alert("Connection set-up")
  }

  startGame(inputPlayerName) {
    this.socket.emit('startGame', {name: inputPlayerName})
    this.playerName = inputPlayerName
    this.socket.on('recieveRoomId', (data: any) => {
      this.roomId = data.code
      alert("In service startGame: " + this.roomId)
    })
    return this.roomId
  }

  getRoomId() {
    alert("In service getRoomId: " + this.roomId)
    return this.roomId;
  }
}
</code>```

The server code creates a unique room ID and passes it to the roomId variable in socketService. My first question starts here. If you notice, I have left a trail of alert messages along the way. I would expect the order of alert messages to be alert("In service startGame: " + this.roomId) and then alert("In component: " + this.testRoomId). However, I get the alert messages in the opposite order and the roomId's are undefined even though the server generates and emits a unique roomId. I do not seem to understand why this is happening.

Secondly, you can see that the startThisGame() function causes socketService to store the generated roomId and playerName inside the class and then reroutes the app to the lobby component. The lobby code goes as follows.

```<code>import { Component, OnInit, OnChanges, AfterContentInit, SimpleChanges, Input } from '@angular/core';
import { SocketioService } from '../socketio.service';

@Component({
  selector: 'app-lobby',
  templateUrl: './lobby.component.html',
  styleUrls: ['./lobby.component.css']
})
export class LobbyComponent implements OnInit, AfterContentInit {
  constructor(private socketService: SocketioService) { }
  @Input()  roomId: string;

  ngOnInit(): void {
    // alert("On init fetched")
    this.socketService.setupSocketConnection()
    this.roomId = this.socketService.getRoomId()
    if (this.roomId) {
      alert("Value obtained in init: " + this.roomId)
    }
    else {
      alert("No value obtained inside init")
    }
    document.getElementById("generated_code").innerHTML = this.roomId
  }
}
</code>```

Here, OnInit, the socketService.getRoomId() function returns an undefined value. If I press back, go to the startpage again, enter a playerName and start a new game again, the previously generated roomId is being rendered. What am I missing here? How do I load the roomId and display the same when I am rerouted to the lobby?

答:
What you are missing is the concept of asynchronous calls and observable.

Try:

service.ts

```<code>export class SocketioService {
  socket;
  public roomId: string;
  public playerName: string;
  private roomData$ = new BehaviorSubject<string>(null);
  constructor() {}

  setupSocketConnection() {
    this.socket = io(environment.SOCKET_ENDPOINT)
    alert("Connection set-up")
  }

  startGame(inputPlayerName) : Observable<string>{
    this.socket.emit('startGame', {name: inputPlayerName})
    this.playerName = inputPlayerName
    this.socket.on('recieveRoomId', (data: any) => {
      this.roomId = data.code;
      this.roomData$.next(this.roomId); // <=== emitting event when the roomId is set.
      alert("In service startGame: " + this.roomId)
    })
    return this.roomData$.asObservable(); // <== returns observable
  }

  getRoomId(){
    alert("In service getRoomId: " + this.roomId)
    return this.roomData$.asObservable();  // <== returns observable
  }
}
</code>```

StartpageComponent.ts

```<code>  startThisGame() {
    // not sure why you are getting value in this way. I can't comment because
    // I dont have access to HTML code
    var playerName = (document.getElementById("nameInput") as HTMLInputElement).value;
    if (playerName) {
      // subscribe to the observable and get the values when prepared.
      // make sure to unsubscribe it in "ngOnDestroy" to avoid memory leaks
      this.socketService.startGame(playerName).subscribe(data => {
           this.testRoomId = data;
           alert("In component: " + this.testRoomId)
           // again, not sure why would you need "this.zone.run()",
           // Handle input values as Angular way and you wont need these patches.
           this.zone.run(() => {this.router.navigate(['/lobby']); });
       });

    }
    else{
      alert("Enter a player name!")
    }
  }
</code>```

In LobbyComponent.ts

```<code>// You can also use RouteParam to pass RoomId rather than using Observable as I have done.
ngOnInit(): void {
    this.socketService.setupSocketConnection()
    // make sure to unsubscribe in ngOnDestroy
    this.socketService.getRoomId().subscribe(data => {
         this.roomId =  data;
         document.getElementById("generated_code").innerHTML = this.roomId;
    })
  }
</code>```

I have added lot of comments to explain you why I have done those changes. I would recommend you to read about Observables and Promises  in Javascript to have better understanding of async calls.

You need to wait for the values to come before you use that value. That is why your code is not running in sequence as you are expecting.

Happy learning :)

Body:
I am trying to create a web-app for a game that involves starting and joining rooms. I am using Angular and Socket.io for the project. I have components named Startpage and Lobby. Startpage contains text boxes for player name and Room ID. It also has buttons for "Start Game" and "Join Game". Clicking on the "Start Game" button triggers the startThisGame() function.

//startpage.component.ts

import { Component, OnInit, Output, NgZone } from '@angular/core';
import { Router } from '@angular/router';
import { SocketioService } from '../socketio.service';

@Component({
  selector: 'app-startpage',
  templateUrl: './startpage.component.html',
  styleUrls: ['./startpage.component.css']
})

export class StartpageComponent implements OnInit {

  constructor( private router: Router, public zone: NgZone, private socketService: SocketioService ) {}

  ngOnInit() {
    this.socketService.setupSocketConnection();
  }
  testRoomId = 'xxxx'

  startThisGame() {
    var playerName = (document.getElementById("nameInput") as HTMLInputElement).value;
    if (playerName) {
      this.testRoomId = this.socketService.startGame(playerName);
      alert("In component: " + this.testRoomId)
      this.zone.run(() => {this.router.navigate(['/lobby']); });
    }
    else{
      alert("Enter a player name!")
    }
  }
}

The code inside socketService goes as follows.

import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import { environment } from 'src/environments/environment';

@Injectable({
  providedIn: 'root'
})
export class SocketioService {
  socket;
  public roomId: string;
  public playerName: string;
  constructor() {}

  setupSocketConnection() {
    this.socket = io(environment.SOCKET_ENDPOINT)
    alert("Connection set-up")
  }

  startGame(inputPlayerName) {
    this.socket.emit('startGame', {name: inputPlayerName})
    this.playerName = inputPlayerName
    this.socket.on('recieveRoomId', (data: any) => {
      this.roomId = data.code
      alert("In service startGame: " + this.roomId)
    })
    return this.roomId
  }

  getRoomId() {
    alert("In service getRoomId: " + this.roomId)
    return this.roomId;
  }
}

The server code creates a unique room ID and passes it to the roomId variable in socketService. My first question starts here. If you notice, I have left a trail of alert messages along the way. I would expect the order of alert messages to be alert("In service startGame: " + this.roomId) and then alert("In component: " + this.testRoomId). However, I get the alert messages in the opposite order and the roomId's are undefined even though the server generates and emits a unique roomId. I do not seem to understand why this is happening.

Secondly, you can see that the startThisGame() function causes socketService to store the generated roomId and playerName inside the class and then reroutes the app to the lobby component. The lobby code goes as follows.

import { Component, OnInit, OnChanges, AfterContentInit, SimpleChanges, Input } from '@angular/core';
import { SocketioService } from '../socketio.service';

@Component({
  selector: 'app-lobby',
  templateUrl: './lobby.component.html',
  styleUrls: ['./lobby.component.css']
})
export class LobbyComponent implements OnInit, AfterContentInit {
  constructor(private socketService: SocketioService) { }
  @Input()  roomId: string;

  ngOnInit(): void {
    // alert("On init fetched")
    this.socketService.setupSocketConnection()
    this.roomId = this.socketService.getRoomId()
    if (this.roomId) {
      alert("Value obtained in init: " + this.roomId)
    }
    else {
      alert("No value obtained inside init")
    }
    document.getElementById("generated_code").innerHTML = this.roomId
  }
}

Here, OnInit, the socketService.getRoomId() function returns an undefined value. If I press back, go to the startpage again, enter a playerName and start a new game again, the previously generated roomId is being rendered. What am I missing here? How do I load the roomId and display the same when I am rerouted to the lobby?

Body:
What you are missing is the concept of asynchronous calls and observable.

Try:

service.ts

export class SocketioService {
  socket;
  public roomId: string;
  public playerName: string;
  private roomData$ = new BehaviorSubject<string>(null);
  constructor() {}

  setupSocketConnection() {
    this.socket = io(environment.SOCKET_ENDPOINT)
    alert("Connection set-up")
  }

  startGame(inputPlayerName) : Observable<string>{
    this.socket.emit('startGame', {name: inputPlayerName})
    this.playerName = inputPlayerName
    this.socket.on('recieveRoomId', (data: any) => {
      this.roomId = data.code;
      this.roomData$.next(this.roomId); // <=== emitting event when the roomId is set.
      alert("In service startGame: " + this.roomId)
    })
    return this.roomData$.asObservable(); // <== returns observable
  }

  getRoomId(){
    alert("In service getRoomId: " + this.roomId)
    return this.roomData$.asObservable();  // <== returns observable
  }
}

StartpageComponent.ts

  startThisGame() {
    // not sure why you are getting value in this way. I can't comment because
    // I dont have access to HTML code
    var playerName = (document.getElementById("nameInput") as HTMLInputElement).value;
    if (playerName) {
      // subscribe to the observable and get the values when prepared.
      // make sure to unsubscribe it in "ngOnDestroy" to avoid memory leaks
      this.socketService.startGame(playerName).subscribe(data => {
           this.testRoomId = data;
           alert("In component: " + this.testRoomId)
           // again, not sure why would you need "this.zone.run()",
           // Handle input values as Angular way and you wont need these patches.
           this.zone.run(() => {this.router.navigate(['/lobby']); });
       });

    }
    else{
      alert("Enter a player name!")
    }
  }

In LobbyComponent.ts

// You can also use RouteParam to pass RoomId rather than using Observable as I have done.
ngOnInit(): void {
    this.socketService.setupSocketConnection()
    // make sure to unsubscribe in ngOnDestroy
    this.socketService.getRoomId().subscribe(data => {
         this.roomId =  data;
         document.getElementById("generated_code").innerHTML = this.roomId;
    })
  }

I have added lot of comments to explain you why I have done those changes. I would recommend you to read about Observables and Promises  in Javascript to have better understanding of async calls.

You need to wait for the values to come before you use that value. That is why your code is not running in sequence as you are expecting.

Happy learning :)


问:
Load entire html page in python
I need to store in a str variable an entire html page.
I'm doing this:

```<code>import requests
from bs4 import BeautifulSoup

url = my_url
response = requests.get(url)
page = str(BeautifulSoup(response.content))
</code>```

This works but the page in my_url is not "complete". It is a website in which going to the end, new things will load, and i need all the page, not only the main visible part.

Is there a way to load the entire page and then store it?

I also tried to load the page manually and then looking at the source code, but the final part of the page is still not visible.

Alternatively, all I want from my_url page are all the links inside it, and all of them are like:

```<code>my_url/something/first-post
my_url/something/second-post
</code>```

Is there a way to find all the links in another way? So, all the possible url that starts with "my_url/something/"

Thanks in advance

答:
I think you should use Selenium and then scroll down with it to get entire the page.

as I know requests can't handle dynamic pages.

Title: Load entire html page in python

Body:
I need to store in a str variable an entire html page.
I'm doing this:

import requests
from bs4 import BeautifulSoup

url = my_url
response = requests.get(url)
page = str(BeautifulSoup(response.content))

This works but the page in my_url is not "complete". It is a website in which going to the end, new things will load, and i need all the page, not only the main visible part.

Is there a way to load the entire page and then store it?

I also tried to load the page manually and then looking at the source code, but the final part of the page is still not visible.

Alternatively, all I want from my_url page are all the links inside it, and all of them are like:

my_url/something/first-post
my_url/something/second-post

Is there a way to find all the links in another way? So, all the possible url that starts with "my_url/something/"

Thanks in advance


问:
How to install pyttsx in Anaconda3?
How to install pyttsx in Anaconda3 (Python3.7) windows 10 machine? I tried to install but getting the error

ERROR: Could not find a version that satisfies the requirement pyttsx3 (from versions: none)
ERROR: No matching distribution found for pyttsx3

C:\Users\Ramesh\Anaconda3\Scripts>pip install pyttsx3
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting pyttsx3
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyttsx3/
  WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyttsx3/
  WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyttsx3/
  WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyttsx3/
  WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pyttsx3/
  Could not fetch URL https://pypi.org/simple/pyttsx3/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pyttsx3/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
  ERROR: Could not find a version that satisfies the requirement pyttsx3 (from versions: none)
ERROR: No matching distribution found for pyttsx3
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
```

答:
I was facing the same problem.The steps I followed to solve it are:

Go to Anaconda prompt and type conda install pip . This will install pip in the current conda environment.

2.After step 1, type pip install pyttsx3  .

Some additional links for help,see under Installing non-conda packages-Managing Conda packages

Title: How to install pyttsx in Anaconda3?


问:
Lookup data from 1 table to other table in R
I have a databse 1 in which I have columns

country      region   area     houseno
bangladesh   south    block1    C1
bangladesh   south    block1    C2
bangladesh   north    block12    C2

Another Database in which i have columns
Area     Tax  muncipality
block1   50   F1
block2   30   F2
block3   40   F1

The key to do the lookup is block

I want to move column B2 from database 2 to matching cases in database 1
how can i do this in R

答:
Not sure if this is what you want without a reproducible example, but you could do a left join by block1 like this:

```<code>library(dplyr)
database1 %>%
  left_join(other_database %>% select(block1, B2), by = c("block1"))
</code>```

Body:
Not sure if this is what you want without a reproducible example, but you could do a left join by block1 like this:

library(dplyr)
database1 %>%
  left_join(other_database %>% select(block1, B2), by = c("block1"))


答:
Get the string.

Cut it from the beginning to (including) the search-string.

extract the number.

```<code>@echo off
setlocal enabledelayedexpansion
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in (test.txt) do set "string=%%a"
set "string=!string:*%search_string%=!"
for /f "delims==&" %%a in ("!string!") do set "result=%%a"
echo %search_string%=%result%
</code>```

EDIT (slightly adapted to take care of multiple entries in the text file):

```<code>@echo off
setlocal enabledelayedexpansion
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%t in (test.txt) do (
  set "string=%%t"
  set "string=!string:*%search_string%=!"
  for /f "delims==&" %%a in ("!string!") do echo result=%%a
)
</code>```

Batch is horrible to process such a string. A more reliable solution is possible with dbenham's utility jrepl.bat, a hybrid JScript/batch script with full REGEX support, which makes this task quite easy:

```<code>@echo off
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in ('type test.txt ^| jrepl ".*tag_for_child_directed_treatment=([0-9]*).*" "$1" /XSEQ') do set "result=%%a"
echo %search_string%=%result%
</code>```

EDIT (slightly adapted to take care of multiple entries in the text file):

```<code>@echo off
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in ('type test.txt ^| jrepl ".*tag_for_child_directed_treatment=([0-9]*).*" "$1" /XSEQ') do echo result=%%a
</code>```

Title: Find a string and only print the required keyword

Body:
Get the string.

Cut it from the beginning to (including) the search-string.

extract the number.

@echo off
setlocal enabledelayedexpansion
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in (test.txt) do set "string=%%a"
set "string=!string:*%search_string%=!"
for /f "delims==&" %%a in ("!string!") do set "result=%%a"
echo %search_string%=%result%

EDIT (slightly adapted to take care of multiple entries in the text file):

@echo off
setlocal enabledelayedexpansion
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%t in (test.txt) do (
  set "string=%%t"
  set "string=!string:*%search_string%=!"
  for /f "delims==&" %%a in ("!string!") do echo result=%%a
)

Batch is horrible to process such a string. A more reliable solution is possible with dbenham's utility jrepl.bat, a hybrid JScript/batch script with full REGEX support, which makes this task quite easy:

@echo off
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in ('type test.txt ^| jrepl ".*tag_for_child_directed_treatment=([0-9]*).*" "$1" /XSEQ') do set "result=%%a"
echo %search_string%=%result%

EDIT (slightly adapted to take care of multiple entries in the text file):

@echo off
set "search_string=tag_for_child_directed_treatment"
for /f "delims=" %%a in ('type test.txt ^| jrepl ".*tag_for_child_directed_treatment=([0-9]*).*" "$1" /XSEQ') do echo result=%%a


问:
What's the best way to save user login and password in flex?
What's the best way to save user credentials in flex? Local storage doesn't seem like good place for storing confidential data, because it saves information as a plain text.</p>

答:
if your service don't support credential, then the only think you can do is save user login state in SharedObject.

You can save hash value of UserName + Random Token to SharedObject and save a copy of UserName too in SharedObject, then when application created creationComplete check wheather the hash value match with the saved user name.

the good thing about this trick is:

Password never persisted locally.

Harder to fake login because need to
match username with the hash value.

a bit hard to explain here you can check it here, source code is available for download.

ContentLicense: CC BY-SA 2.5

ContentLicense: CC BY-SA 3.0


问:
When should you really use the visitor pattern
Ok before marking this as a duplicate let me clarify myself.
I'm reading about the visitor pattern and its applicable uses.

I've stumbled upon this post:
When should I use the Visitor Design Pattern?

and the user who wrote the first answer says as follow :

Now we want to add a new operation to the hierarchy, namely we want
  each animal to make its sound. As far as the hierarchy is this simple,
  you can do it with straight polymorphism:

...

  But proceeding in this
  way, each time you want to add an operation you must modify the
  interface to every single class of the hierarchy.

Now, I mostly see why it's needed from his perspective, it's basically a way to cut compilation time by making it so not every time you want to add a new polymorphic method to a class hierarchy, the whole hierarchy will get recompiled.

But he's also saying that it's fine adding a new polymorphic method to the hierarchy as long as it's a "simple" hierarchy. But my question is when do you set your line and decide on what is simple and what is not.

Also, what if an hierarchy is a complex one, but adding a new method just make total sense to be and instance method rather than having the operation in a complete different class ?

Searching a bit more I found this article explaining the visitor pattern and it's uses
http://butunclebob.com/ArticleS.UncleBob.IuseVisitor

The author gave an example where writing an instance method makes the object coupled to something and moving the method to a different class (The visitor) breaks the decoupling. This made more sense to me, but I'm still not quite sure when this pattern should really be used, the first argument of "changing the hierarchy time every time you want to add a new polymorphic method..." seems to me like an excuse because if a method seems to fit in the hierarchy it should be there logically, Assuming the Animal example was a really complex hierarchy and I would have decided to add make sound method, adding an instance method would be a logical choice (in my mind).

But maybe I'm wrong so I'm here asking for more insight about this, maybe someone could enlighten me.

答:
you can use the visitor pattern to design a db saver, like shown in this diagram here:
class diagram for an object saver in different dbs

you can choose what db to use,
the client class will look similar to this:

```<code>    Visitor mongosaver = new MongodbSaver();
    Visitable user = new User();
    //save the user object using mongodb saver
    user.accept(mongosaver);
    Visitable post = new BlogPost();
    Visitor mysqlsaver = new MysqlSaver();
    //save the BlogPost using Mysql saver
    post.accept(mysqlsaver);
</code>```

you can refer to this also: https://www.oodesign.com/visitor-pattern.html

Body:
you can use the visitor pattern to design a db saver, like shown in this diagram here:
class diagram for an object saver in different dbs

you can choose what db to use,
the client class will look similar to this:

    Visitor mongosaver = new MongodbSaver();
    Visitable user = new User();
    //save the user object using mongodb saver
    user.accept(mongosaver);
    Visitable post = new BlogPost();
    Visitor mysqlsaver = new MysqlSaver();
    //save the BlogPost using Mysql saver
    post.accept(mysqlsaver);

you can refer to this also: https://www.oodesign.com/visitor-pattern.html


问:
can't change background-color of text input
Perhaps a stupid question:

Here is a screenshot of my login page with a text-input field where I set the background-color. In the developer tools the background color property shows up as red, but in the page it is still grey. Any ideas on how to change it to red?

edit: under computed it says:

edit:
this issue might be related to the chromium browser. On Chrome the background color is red

答:
As you can see

there is -internal-autofill-selected in computed tab

These are browser's default styles

Perhaps you should do to override it

```<code>#id_username,
input#id_username:-internal-autofill-selected {
    background-color: red  !important;
    color: #fff !important;
}
</code>```

Body:
As you can see

there is -internal-autofill-selected in computed tab

These are browser's default styles

Perhaps you should do to override it

#id_username,
input#id_username:-internal-autofill-selected {
    background-color: red  !important;
    color: #fff !important;
}


问:
How to extract repeating data separately using re
I have this piece of code with the html contents of a page:

```<code>page = """<div>
<span class="item_1" dt="2012.03.12">Item Name 1</span>
<span class="item_2" dt="2012.03.6" time="12:51">Item Name 2</span></div>"""

print(re.findall(('class="(a-z_0-9)"')('dt="(0-9.)"')('>(A-Za-z 0-9)<'),page))
</code>```

The last line is not working for me. Maybe it is wrong. What I need to do is to extract class, dt and text for each span separately.

Expected output:

```<code>[('item_1', '2012.03.12', 'Item Name 1'),
('item_2','2012.03.6','Item Name 2')]
</code>```

Please help!

答:
For parsing html it's better to use BeautifulSoup which makes parsing simple & easy.

```<code>from bs4 import BeautifulSoup

page = """<div>
<span class="item_1" dt="2012.03.12">Item Name 1</span>
<span class="item_2" dt="2012.03.6" time="12:51">Item Name 2</span></div>"""

soup = BeautifulSoup(page, "html.parser")
div = soup.find('div')

for elem in div.findAll(["span"]):
    print(elem.text, "|", elem.attrs)

Item Name 1 | {'class': ['item_1'], 'dt': '2012.03.12'}
Item Name 2 | {'class': ['item_2'], 'dt': '2012.03.6', 'time': '12:51'}
</code>```

Body:
I have this piece of code with the html contents of a page:

page = """

Item Name 1
Item Name 2
"""

print(re.findall(('class="(a-z_0-9)"')('dt="(0-9.)"')('>(A-Za-z 0-9)<'),page))

The last line is not working for me. Maybe it is wrong. What I need to do is to extract class, dt and text for each span separately.

Expected output:

[('item_1', '2012.03.12', 'Item Name 1'),
('item_2','2012.03.6','Item Name 2')]

Please help!

Body:
For parsing html it's better to use BeautifulSoup which makes parsing simple & easy.

from bs4 import BeautifulSoup

page = """<div>
<span class="item_1" dt="2012.03.12">Item Name 1</span>
<span class="item_2" dt="2012.03.6" time="12:51">Item Name 2</span></div>"""

soup = BeautifulSoup(page, "html.parser")
div = soup.find('div')

for elem in div.findAll(["span"]):
    print(elem.text, "|", elem.attrs)

Item Name 1 | {'class': ['item_1'], 'dt': '2012.03.12'}
Item Name 2 | {'class': ['item_2'], 'dt': '2012.03.6', 'time': '12:51'}


问:
Split String using backslash in javascript
This is my Temp url and I'm trying to get image name

```<code>var str='C:\fakepath\alfa_company.png';
</code>```

my expected out put like this:

```<code>var url='alfa_company.png';
</code>```

答:
In Javascript "\" has a special meaning.
So, it doesn't get included in your resultant string.

Try

```<code>let u = String.raw`C:\fakepath\alfa_company.png`;
u.split("\\")[u.split("\\").length-1]
</code>```

or

```<code>let u = String.raw`C:\fakepath\alfa_company.png`;
u.split("\\").pop()
</code>```

to understand it better go through How can I use backslashes (\) in a string?

Body:
This is my Temp url and I'm trying to get image name

var str='C:\fakepath\alfa_company.png';

my expected out put like this:

var url='alfa_company.png';

Body:
In Javascript "\" has a special meaning.
So, it doesn't get included in your resultant string.

Try

let u = String.raw`C:\fakepath\alfa_company.png`;
u.split("\\")[u.split("\\").length-1]

or

let u = String.raw`C:\fakepath\alfa_company.png`;
u.split("\\").pop()

to understand it better go through How can I use backslashes (\) in a string?


问:
How do I remove all map annotations in swift 2
I had working code to remove all map annotations with a button, but after my update to xcode 7 I am running into the error:

Type 'MKAnnotation' does not conform to protocol 'SequenceType'

```<code>if let annotations = (self.mapView.annotations as? MKAnnotation){
    for _annotation in annotations {
        if let annotation = _annotation as? MKAnnotation {
            self.mapView.removeAnnotation(annotation)
        }
    }
}
</code>```

答:
SWIFT 5

If you don't want to remove user location mark:

```<code>let annotations = mapView.annotations.filter({ !($0 is MKUserLocation) })
mapView.removeAnnotations(annotations)
</code>```

Body:
I had working code to remove all map annotations with a button, but after my update to xcode 7 I am running into the error:

Type 'MKAnnotation' does not conform to protocol 'SequenceType'

if let annotations = (self.mapView.annotations as? MKAnnotation){
    for _annotation in annotations {
        if let annotation = _annotation as? MKAnnotation {
            self.mapView.removeAnnotation(annotation)
        }
    }
}

Body:
SWIFT 5

If you don't want to remove user location mark:

let annotations = mapView.annotations.filter({ !($0 is MKUserLocation) })
mapView.removeAnnotations(annotations)


问:
Forecasting/prediction using ARIMA in python - how does it work?
I am very confused about how to predict/forecast using ARIMA.

Lets assume we have a series called y_orig that we split into y_train and y_test. Assuming that y_orig is not stationary, we could fit ARIMA using the code below

```<code># fit ARIMA model
from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(y_train, order=(2,1,2))
model_fit = model.fit(disp=0)
print(model_fit.summary())
</code>```

After fitting the model, we can predict using the code below

```<code>n_periods = len(`y_test`)

fc, -, - = model_fit.forecast(n_periods, alpha=0.05)  # 95% conf
</code>```

The value fc should give a forecast which i then compare to y_test. Please note that as expected, y_test is not used in the training phase. Also note that i am not looking for a rolling forecast but for a long term forecast where the parameters (once trained) are fixed.

I am very confused because y_test is not used at all in the forecasting phase.

For instance, if we were to use other prediction models (like in Keras or tensorflow). we would be coding it that way.

First, we fit the model in the training phase which i dont show- it does not matter for my question. Then we predict and see how good our fit is in sample using the code below.

```<code>y_pred_train=model.predict(y_train)
</code>```

then we test the model out of sample as below:

```<code>y_pred_test=model.predict(y_test)
</code>```

In this situation, the parameters are not re-estimated and y_test is used in the testing phase to forecast the next value (with fixed parameters).

Hence my confusion with ARIMA.  Why do we not do the same with ARIMA model?

Please help me understand as i am very confused.

Thanks so much!!

答: I think you're a bit confused by the .fit and the y_train in the ARIMA code block. y_train is just a poorly named variable here, it should just be y, the data I want to forecast. The ARIMA model has no training/test phase, it's not self-learning. It does a statistical analysis of the input data, and does a forecast. If you want to do another forecast (on y_test), you need to do another statistical analysis (using model.fit) and do another forecast (using model.forecast). The ARIMA model does not have any weights it trains in a training phase, nothing related to any previous data 'fitted' on is saved in the model. You can't use a "fitted" ARIMA model to forecast other data samples.

Body:
I am very confused about how to predict/forecast using ARIMA.

Lets assume we have a series called y_orig that we split into y_train and y_test. Assuming that y_orig is not stationary, we could fit ARIMA using the code below

# fit ARIMA model
from statsmodels.tsa.arima_model import ARIMA
model = ARIMA(y_train, order=(2,1,2))
model_fit = model.fit(disp=0)
print(model_fit.summary())

After fitting the model, we can predict using the code below

n_periods = len(`y_test`)

fc, -, - = model_fit.forecast(n_periods, alpha=0.05)  # 95% conf

The value fc should give a forecast which i then compare to y_test. Please note that as expected, y_test is not used in the training phase. Also note that i am not looking for a rolling forecast but for a long term forecast where the parameters (once trained) are fixed.

I am very confused because y_test is not used at all in the forecasting phase.

For instance, if we were to use other prediction models (like in Keras or tensorflow). we would be coding it that way.

First, we fit the model in the training phase which i dont show- it does not matter for my question. Then we predict and see how good our fit is in sample using the code below.

y_pred_train=model.predict(y_train)

then we test the model out of sample as below:

y_pred_test=model.predict(y_test)

In this situation, the parameters are not re-estimated and y_test is used in the testing phase to forecast the next value (with fixed parameters).

Hence my confusion with ARIMA.  Why do we not do the same with ARIMA model?

Please help me understand as i am very confused.

Thanks so much!!


问:
Looping through a numpy array
I have a 5 by 10 array and I want to flip a bit if a random number is greater than 0.9. However, this only works for the first row of the array and it doesn't get to the second and subsequent row. I replaced the bits with 3 and 4 so i can easily see if the flipping occurs. I have been getting results that look like this. </p>

```<code>[[3 1 1 1 4 1 3 1 0 1]
 [1 1 0 0 1 0 1 1 1 0]
 [1 0 1 0 1 0 1 1 1 1]
 [0 0 1 0 1 1 0 1 1 1]
 [0 1 1 0 0 0 0 1 1 1]]

</code>```

<p>Please help me figure out where I'm wrong. </p>

```<code>
from random import random
RM =  np.random.randint(0,2, size=(5,10))
print(RM)
for k in range(0, RM.shape[0]):
    for j in range(0, RM.shape[1]):
        A  = random()
        if A > 0.9:
            if RM[k,j] == 0:
                np.put(RM, [k,j], [3])
                print("k1",k)
                print("j1", j)
            else:
                np.put(RM, [k,j], [4])
                print("k2", k)
        else:
            continue

print(RM)
</code>```

答:
Looking at the documentation of np.put

```<code>numpy.put(a, ind, v, mode='raise')[source]
Replaces specified elements of an array with given values.
</code>```

under Examples:

```<code>a = np.arange(5)
np.put(a, [0, 2], [-44, -55])
a
array([-44,   1, -55,   3,   4])
</code>```

So, if you feed a list to the function, it replaces multiple values in the flattened array.

To make your loop work, simply assigning the values to the array should work:

```<code>from random import random
RM =  np.random.randint(0,2, size=(5,10))
print(RM)
for k in range(0, RM.shape[0]):
    for j in range(0, RM.shape[1]):
        A  = random()
        if A > 0.9:
            if RM[k,j] == 0:
                RM[k,j]=3
                print("k1",k)
                print("j1", j)
            else:
                RM[k,j] =4
                print("k2", k)
        else:
            continue
</code>```

Title: Looping through a numpy array

Body:
I have a 5 by 10 array and I want to flip a bit if a random number is greater than 0.9. However, this only works for the first row of the array and it doesn't get to the second and subsequent row. I replaced the bits with 3 and 4 so i can easily see if the flipping occurs. I have been getting results that look like this.

[[3 1 1 1 4 1 3 1 0 1]
 [1 1 0 0 1 0 1 1 1 0]
 [1 0 1 0 1 0 1 1 1 1]
 [0 0 1 0 1 1 0 1 1 1]
 [0 1 1 0 0 0 0 1 1 1]]

Please help me figure out where I'm wrong.

from random import random
RM =  np.random.randint(0,2, size=(5,10))
print(RM)
for k in range(0, RM.shape[0]):
    for j in range(0, RM.shape[1]):
        A  = random()
        if A > 0.9:
            if RM[k,j] == 0:
                np.put(RM, [k,j], [3])
                print("k1",k)
                print("j1", j)
            else:
                np.put(RM, [k,j], [4])
                print("k2", k)
        else:
            continue

print(RM)

Body:
Looking at the documentation of np.put

numpy.put(a, ind, v, mode='raise')[source]
Replaces specified elements of an array with given values.

under Examples:

a = np.arange(5)
np.put(a, [0, 2], [-44, -55])
a
array([-44,   1, -55,   3,   4])

So, if you feed a list to the function, it replaces multiple values in the flattened array.

To make your loop work, simply assigning the values to the array should work:

from random import random
RM =  np.random.randint(0,2, size=(5,10))
print(RM)
for k in range(0, RM.shape[0]):
    for j in range(0, RM.shape[1]):
        A  = random()
        if A > 0.9:
            if RM[k,j] == 0:
                RM[k,j]=3
                print("k1",k)
                print("j1", j)
            else:
                RM[k,j] =4
                print("k2", k)
        else:
            continue


问:
oksetState functions is not updated in react hooks
I am rendering the multiple forms upon the selection of no of users done through a dropdown button,
where I am generating helper text and userdata state for those generated users dynamically .

this is where I initialize the state objects

```<code>const [selectedIndex, setSelectedIndex] = React.useState(0);
const [registrationData,setRegistrationData]=React.useState([]);
const [error,setError]=React.useState({});
const [registrations]=React.useState([]);
const [campaign, setCampaign] = React.useState({});
</code>```

THis is the handling of the drop-down button:

```<code>const handleMenuItemClick=(event, index)=>{
        console.log("CLicked "+(index+1))
        setSelectedIndex(index);
        setAnchorEl(null);
};
</code>```

And this is the where the states are generated dynamically using the useEffect:

```<code> useEffect(()=>{
        setCampaign(prevCampaign => ({
            ...prevCampaign,
            ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
                [i]: {
                    sms_campaign: false,
                    email_campaign: false,
                }
            })))
        }));
        setError(prevError => ({
            ...prevError,
            ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
                [i]: {
                   uscf_id:"",
                   first_name:"",
                   middle_name:"",
                   last_name:"",
                   city:"",
                   state:"",
                   country:"",
                   phone_number:"",
                   email_id:"",
                }
            })))
        }));
        setRegistrationData(prevReg => ({
            ...prevReg,
            ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
                [i]: {
                uscf_id:"",
                first_name:"",
                middle_name:"",
                last_name:"",
                sections:"",
                city:"",
                state:"",
                country:"",
                phone_code:"",
                phone_number:"",
                email_id:"",
                sms:"",
                email:""
            }
            })))
        }));
        },[selectedIndex])

</code>```

This is where I render cards:

```<code>registrations.filter((index)=>{
                    return (index<selectedIndex);
                }).map((registration,index)=>{
                    return(
                        <Card className={classes.cardLayout}>
                            <Avatar className={classes.icon}>
                                {index+1}
                            </Avatar>
                            <CardContent className={classes.cardContent}>
                            <TextField

                                id="outlined-error-helper-text"
                                label="ID"
                                name="id"
                                placeholder="Enter ID"
                                variant="outlined"
                                helperText={error[index].id}
                                error={(error[index].id===null||error[index].d==="")?false:true}
                                onChange={(event)=>updateUserdata(event,index)}
                                className={classes.textField}
                            />
.....
.....
</code>```

This is where the dropdown button is handled:

```<code> <Button
                    aria-controls="simple-menu"
                    aria-haspopup="true"
                    variant="outlined"
                    color="inherit"
                    label="registrations"
                    style={{marginLeft:20}}
                    endIcon={<ArrowDropDownIcon/>}
                    onClick={(event)=>handleClick(event)}
                    >
                    <Typography variant="body2" color="inherit">
                    //here , I have mentioned selectedIndex+1 to just show 1 instead of 0
                        {selectedIndex+1}
                    </Typography>
                    </Button>
                        <Menu
                            id="simple-menu"
                            anchorEl={anchorEl}
                            keepMounted
                            open={Boolean(anchorEl)}
                            onClose={handleClose}
                        >
                        {[1,2,3,4,5,6,7,8,9,10].map((option, index) => (
                            <MenuItem
                                style={{paddingRight:85}}
                                key={option}
                                name="no_of_registrations"
                                selected={(index === selectedIndex)}
                                onClick={(event) => handleMenuItemClick(event, index)}
                            >
                                {option}
                            </MenuItem>
                            ))}
                        </Menu>
</code>```

I also have form validation which I call when the user clicks on the register button , to perform the checks on fields and now only the selectedIndex card fields are setting the error text previous remaining cards are not set with the error text.

```<code>const formValidation=()=>{
        console.log("entered Form validation function")
        let valid=true;
        for(var i=0;i<selectedIndex;i++){
            if(registrationData[i].id===""){
                setError((error)=>({...error,[i]:{...error[i],id:"required" }}))
                valid=false
            }else {
                if(!(registrationData[i].id).match(/^[0-9]+$/)||([registrationData[i].id].length!==10)){
                    setError((error)=>({...error,[i]:{...error[i],id:"required" }}))
                    valid=false
                }else{
                    setError((error)=>({...error,[i]:{...error[i],id:"required" }}))
                }
            }
....
</code>```

答:
You must update your state only once and not within a for loop specially without using a state updater callback.

You can perform the update by returning the object values within each state updater by mapping over an array created of length equal to selectedIndex

useEffect(()=>{
    setCampaign(prevCampaign => ({
        ...prevCampaign,
        ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
            [i]: {
                sms_campaign: false,
                email_campaign: false,
            }
        })))
    }));
    setError(prevError => ({
        ...prevError,
        ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
            [i]: {
               id:"",
               first_name:"",
               middle_name:"",
               last_name:"",
               city:"",
               state:"",
               country:"",
               phone_number:"",
               email_id:"",
            }
        })))
    }));
    setRegistrationData(prevReg => ({
        ...prevReg,
        ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
            [i]: {
            id:"",
            first_name:"",
            middle_name:"",
            last_name:"",
            sections:"",
            city:"",
            state:"",
            country:"",
            phone_code:"",
            phone_number:"",
            email_id:"",
            sms:"",
            email:""
        }
        })))
    }));
    },[selectedIndex])
```

Body:
I am rendering the multiple forms upon the selection of no of users done through a dropdown button,
where I am generating helper text and userdata state for those generated users dynamically .

this is where I initialize the state objects

const [selectedIndex, setSelectedIndex] = React.useState(0);
const [registrationData,setRegistrationData]=React.useState([]);
const [error,setError]=React.useState({});
const [registrations]=React.useState([]);
const [campaign, setCampaign] = React.useState({});

THis is the handling of the drop-down button:

const handleMenuItemClick=(event, index)=>{
        console.log("CLicked "+(index+1))
        setSelectedIndex(index);
        setAnchorEl(null);
};

And this is the where the states are generated dynamically using the useEffect:

 useEffect(()=>{
        setCampaign(prevCampaign => ({
            ...prevCampaign,
            ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
                [i]: {
                    sms_campaign: false,
                    email_campaign: false,
                }
            })))
        }));
        setError(prevError => ({
            ...prevError,
            ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
                [i]: {
                   uscf_id:"",
                   first_name:"",
                   middle_name:"",
                   last_name:"",
                   city:"",
                   state:"",
                   country:"",
                   phone_number:"",
                   email_id:"",
                }
            })))
        }));
        setRegistrationData(prevReg => ({
            ...prevReg,
            ...Object.assign({}, ...[...new Array(10).keys()].map(i => ({
                [i]: {
                uscf_id:"",
                first_name:"",
                middle_name:"",
                last_name:"",
                sections:"",
                city:"",
                state:"",
                country:"",
                phone_code:"",
                phone_number:"",
                email_id:"",
                sms:"",
                email:""
            }
            })))
        }));
        },[selectedIndex])

This is where I render cards:

registrations.filter((index)=>{
                    return (index<selectedIndex);
                }).map((registration,index)=>{
                    return(
                        <Card className={classes.cardLayout}>
                            <Avatar className={classes.icon}>
                                {index+1}
                            </Avatar>
                            <CardContent className={classes.cardContent}>
                            <TextField

                                id="outlined-error-helper-text"
                                label="ID"
                                name="id"
                                placeholder="Enter ID"
                                variant="outlined"
                                helperText={error[index].id}
                                error={(error[index].id===null||error[index].d==="")?false:true}
                                onChange={(event)=>updateUserdata(event,index)}
                                className={classes.textField}
                            />
.....
.....

This is where the dropdown button is handled:

 <Button
                    aria-controls="simple-menu"
                    aria-haspopup="true"
                    variant="outlined"
                    color="inherit"
                    label="registrations"
                    style={{marginLeft:20}}
                    endIcon={<ArrowDropDownIcon/>}
                    onClick={(event)=>handleClick(event)}
                    >
                    <Typography variant="body2" color="inherit">
                    //here , I have mentioned selectedIndex+1 to just show 1 instead of 0
                        {selectedIndex+1}
                    </Typography>
                    </Button>
                        <Menu
                            id="simple-menu"
                            anchorEl={anchorEl}
                            keepMounted
                            open={Boolean(anchorEl)}
                            onClose={handleClose}
                        >
                        {[1,2,3,4,5,6,7,8,9,10].map((option, index) => (
                            <MenuItem
                                style={{paddingRight:85}}
                                key={option}
                                name="no_of_registrations"
                                selected={(index === selectedIndex)}
                                onClick={(event) => handleMenuItemClick(event, index)}
                            >
                                {option}
                            </MenuItem>
                            ))}
                        </Menu>

I also have form validation which I call when the user clicks on the register button , to perform the checks on fields and now only the selectedIndex card fields are setting the error text previous remaining cards are not set with the error text.

const formValidation=()=>{
        console.log("entered Form validation function")
        let valid=true;
        for(var i=0;i<selectedIndex;i++){
            if(registrationData[i].id===""){
                setError((error)=>({...error,[i]:{...error[i],id:"required" }}))
                valid=false
            }else {
                if(!(registrationData[i].id).match(/^[0-9]+$/)||([registrationData[i].id].length!==10)){
                    setError((error)=>({...error,[i]:{...error[i],id:"required" }}))
                    valid=false
                }else{
                    setError((error)=>({...error,[i]:{...error[i],id:"required" }}))
                }
            }
....


问:
How to run CGI on Jboss?
(Oh, its Jboss not Apache. wow)

I am learning CGI and wish to run a test cgi on Jboss web server as first step lol. I have searched a lot of resources online and tried a lot of ways, but it still be opened as a text file on browser.

More facts:
1 I put script in a directory, and open it on browser e.g. www.xxxx.com/staging/welcome.cgi. Able to open, but shows text not run as script.

2 Script is starting with #!/usr/bin/perl -wT. And I have confirmed my server can run Perl script,path /usr/bin/perl is also correct.

3 I have tried modified httpd.conf according to the tutorials found online

答:
It's most probably to do with the fact that the directory your script is placed in doesn't not ExecCGI Option enabled.

Typically done with something like

```<code><Directory /usr/local/apache2/htdocs/somedir>
Options +ExecCGI
</Directory>
</code>```

http://httpd.apache.org/docs/2.0/howto/cgi.html

By default Apache nominates certain directories you can place CGI scripts in.

Title: How to run CGI on Jboss?

Body:
It's most probably to do with the fact that the directory your script is placed in doesn't not ExecCGI Option enabled.

Typically done with something like

<Directory /usr/local/apache2/htdocs/somedir>
Options +ExecCGI
</Directory>

http://httpd.apache.org/docs/2.0/howto/cgi.html

By default Apache nominates certain directories you can place CGI scripts in.


问:
How to improve time complexity in this code?
What my code try to do here is to sort items trough 2 dictionaries.
If they are similar or the same, I create a new list to append them.

The test file is relatively big, around 2000 data each dictionary.

My concern is that i need to maintain a linear time complexity trough out the code and i am not sure that my code is on a linear complexity.

So i would like an opinion on the complexity of this code. Is it linear?
If it is not is there any way to improve it ?

Thanks in advance!

```<code>import pandas as pd
import string
import textdistance as td
import time
start_time = time.time()

amazon =  pd.read_csv('amazon.csv')
google = pd.read_csv('google.csv')

title1 = amazon['title']
title2 = google['name']
id1 = amazon['idAmazon']
id2 = google['id']

out_amazon = {}
out_google = {}

list_a =[]
list_b =[]
list_c =[]
list_d =[]
list_e =[]
list_f =[]
list_g =[]
list_h =[]
list_i =[]
list_j =[]
list_k =[]
list_l =[]
list_m =[]
list_n =[]
list_o =[]
list_p =[]
list_q =[]
list_r =[]
list_s =[]
list_t =[]
list_u =[]
list_v =[]
list_w =[]
list_x =[]
list_y =[]
list_z =[]
list_unknown = []
duplicate_list = []

amazon_labeled = ([(name, 'Amazon') for name in title1])
google_labeled = ([(name, 'Google') for name in title2])

amazon_dict = dict(zip(amazon.idAmazon, amazon_labeled))
google_dict = dict(zip(google.id, google_labeled))

z = {**amazon_dict, **google_dict}
keys = sorted((z.values()))

i = 0
while i < (len(keys)) - 1:
    if (keys[i][0][0]) == 'a':
        list_a.append(keys[i])
    elif (keys[i][0][0]) == 'b':
        list_b.append(keys[i])
    elif (keys[i][0][0]) == 'c':
        list_c.append(keys[i])
    elif (keys[i][0][0]) == 'd':
        list_d.append(keys[i])
    elif (keys[i][0][0]) == 'e':
        list_e.append(keys[i])
    elif (keys[i][0][0]) == 'f':
        list_f.append(keys[i])
    elif (keys[i][0][0]) == 'g':
        list_g.append(keys[i])
    elif (keys[i][0][0]) == 'h':
        list_h.append(keys[i])
    elif (keys[i][0][0]) == 'i':
        list_i.append(keys[i])
    elif (keys[i][0][0]) == 'j':
        list_j.append(keys[i])
    elif (keys[i][0][0]) == 'k':
        list_k.append(keys[i])
    elif (keys[i][0][0]) == 'l':
        list_l.append(keys[i])
    elif (keys[i][0][0]) == 'm':
        list_m.append(keys[i])
    elif (keys[i][0][0]) == 'n':
        list_n.append(keys[i])
    elif (keys[i][0][0]) == 'o':
        list_o.append(keys[i])
    elif (keys[i][0][0]) == 'p':
        list_p.append(keys[i])
    elif (keys[i][0][0]) == 'q':
        list_q.append(keys[i])
    elif (keys[i][0][0]) == 'r':
        list_r.append(keys[i])
    elif (keys[i][0][0]) == 's':
        list_s.append(keys[i])
    elif (keys[i][0][0]) == 't':
        list_t.append(keys[i])
    elif (keys[i][0][0]) == 'u':
        list_u.append(keys[i])
    elif (keys[i][0][0]) == 'v':
        list_v.append(keys[i])
    elif (keys[i][0][0]) == 'w':
        list_w.append(keys[i])
    elif (keys[i][0][0]) == 'x':
        list_x.append(keys[i])
    elif (keys[i][0][0]) == 'y':
        list_y.append(keys[i])
    elif (keys[i][0][0]) == 'z':
        list_z.append(keys[i])
    else:
        list_unknown.append(keys[i])
    i += 1

def check_based_alphabet(alphabetList, k = 0, j = 0):
    while k < len(alphabetList) - 1:
        if alphabetList[k][1] != alphabetList[j][1]:
            distance = td.jaccard(alphabetList[k][0], alphabetList[j][0])
            if distance > 0.7:
                duplicate_list.append([alphabetList[k][0], alphabetList[j][0]])
            j += 1
        else:
            j += 1
        if j == len(alphabetList):
            j = 1
            k += 1

check_based_alphabet(list_a)
check_based_alphabet(list_b)
check_based_alphabet(list_c)
check_based_alphabet(list_d)
check_based_alphabet(list_e)
check_based_alphabet(list_f)
check_based_alphabet(list_g)
check_based_alphabet(list_h)
check_based_alphabet(list_i)
check_based_alphabet(list_j)
check_based_alphabet(list_k)
check_based_alphabet(list_l)
check_based_alphabet(list_m)
check_based_alphabet(list_n)
check_based_alphabet(list_o)
check_based_alphabet(list_p)
check_based_alphabet(list_q)
check_based_alphabet(list_r)
check_based_alphabet(list_s)
check_based_alphabet(list_t)
check_based_alphabet(list_u)
check_based_alphabet(list_v)
check_based_alphabet(list_w)
check_based_alphabet(list_x)
check_based_alphabet(list_y)
check_based_alphabet(list_z)
</code>```

答:
You ask specifically for time complexity, but finding the time complexity is easier when the code is clean. So I will: 1. clean the code; 2. find the time complexity and discuss of the possible improvement.

Clean the code

First, instead of creating list_a, list_b, ..., create a dict list_by_first_letter. We don't need to worry about indices:

```<code>for k in keys:
    letter = k[0][0]
    if not('a' <= letter <= 'z'):
        letter = 'UNK'
    list_by_first_letter.setdefault(letter, []).append(k)
</code>```

The setdefault will check if the letter is already a key in the dict, and else create it and map it to a default value (empty list [] here).

And then, to find the duplicates:

```<code>for alphabet_list in list_by_first_letter.values():
    check_based_alphabet(alphabet_list)
</code>```

Now, we have to clean check_based_alphabet. I replace while by for. Since you're matching every element against all the others you have:

```<code>def check_based_alphabet(alphabet_list):
    for k in range(len(alphabet_list)):
        first_key = alphabet_list[k]
        for j in range(len(alphabet_list))):
            second_key = alphabet_list[j]
            if first_key[1] != second_key[1]:
                distance = td.jaccard(first_key[0], second_key[0])
                if distance > 0.7:
                    duplicate_list.append([first_key[0], second_key[0]])
</code>```

You may have an issue here: you compare alphabet_list[0] to alphabet_list[1], alphabet_list[2], ..., then alphabet_list[1] to alphabet_list[0], alphabet_list[2], .... If your td.jaccard is symmetric, you'll get every couple twice. To avoid this, use:

```<code>...
for j in range(k+1, len(alphabet_list))):
...
</code>```

You get rid of the duplicates since second_key is always after first_key in the list. (I won't claim that the code is now totally clean, but it is at least readable.)

Time complexity

Now, we can find the time complexity. First, the time complexity of check_based_alphabet is O(n^2 * complexity of td.jaccard) where n is the number of element in the sublist. That's obvious because of the two imbricated loops.

If N is the number of elements in the list, you have a global time complexity of O(n1^2 * complexity of td.jaccard) + O(n2^2 * complexity of td.jaccard) + ... where N = n1 + n2 + ..., that is O(N^2 * complexity of td.jaccard):

In the worst case, we have N  = n1 and n2 = ... = 0 and the result is trivial.

In the best case, we have n1 = n2 = ... and we have 27 * O((N/27)^2 * complexity of td.jaccard) and that's still O(N^2 * complexity of td.jaccard), while this may be faster.

It seems hard to improve that time complexity. Look at your trial: you group the keys in buckets (all keys that somewhat start with the same letter) and compare each key with all keys of the same bucket. Good idea, but the flaw is that the size of the buckets still depends on N. To reduce the time complexity, you have to create buckets of a fixed size. That's not possible unless td.jaccard and/or the keys have specific properties.

(An example of specific properties: imagine you have a list of distinct integers, and that two integers i, j are said to be "duplicates" if |i -j| < K where K is independant of N. You can compare all pairs of integers (O(N^2)), but you can also sort the integers (O(N lg N) in the generic case) and then compare each element of the sorted list to the K next integers (above that, all integers are at least i + K, hence not duplicates): O(N * K). Hence, the time complexity is O(N (K + lg N)) better than O(N^2).)

Body:
What my code try to do here is to sort items trough 2 dictionaries.
If they are similar or the same, I create a new list to append them.

The test file is relatively big, around 2000 data each dictionary.

My concern is that i need to maintain a linear time complexity trough out the code and i am not sure that my code is on a linear complexity.

So i would like an opinion on the complexity of this code. Is it linear?
If it is not is there any way to improve it ?

Thanks in advance!

import pandas as pd
import string
import textdistance as td
import time
start_time = time.time()

amazon =  pd.read_csv('amazon.csv')
google = pd.read_csv('google.csv')

title1 = amazon['title']
title2 = google['name']
id1 = amazon['idAmazon']
id2 = google['id']

out_amazon = {}
out_google = {}

list_a =[]
list_b =[]
list_c =[]
list_d =[]
list_e =[]
list_f =[]
list_g =[]
list_h =[]
list_i =[]
list_j =[]
list_k =[]
list_l =[]
list_m =[]
list_n =[]
list_o =[]
list_p =[]
list_q =[]
list_r =[]
list_s =[]
list_t =[]
list_u =[]
list_v =[]
list_w =[]
list_x =[]
list_y =[]
list_z =[]
list_unknown = []
duplicate_list = []

amazon_labeled = ([(name, 'Amazon') for name in title1])
google_labeled = ([(name, 'Google') for name in title2])

amazon_dict = dict(zip(amazon.idAmazon, amazon_labeled))
google_dict = dict(zip(google.id, google_labeled))

z = {**amazon_dict, **google_dict}
keys = sorted((z.values()))

i = 0
while i < (len(keys)) - 1:
    if (keys[i][0][0]) == 'a':
        list_a.append(keys[i])
    elif (keys[i][0][0]) == 'b':
        list_b.append(keys[i])
    elif (keys[i][0][0]) == 'c':
        list_c.append(keys[i])
    elif (keys[i][0][0]) == 'd':
        list_d.append(keys[i])
    elif (keys[i][0][0]) == 'e':
        list_e.append(keys[i])
    elif (keys[i][0][0]) == 'f':
        list_f.append(keys[i])
    elif (keys[i][0][0]) == 'g':
        list_g.append(keys[i])
    elif (keys[i][0][0]) == 'h':
        list_h.append(keys[i])
    elif (keys[i][0][0]) == 'i':
        list_i.append(keys[i])
    elif (keys[i][0][0]) == 'j':
        list_j.append(keys[i])
    elif (keys[i][0][0]) == 'k':
        list_k.append(keys[i])
    elif (keys[i][0][0]) == 'l':
        list_l.append(keys[i])
    elif (keys[i][0][0]) == 'm':
        list_m.append(keys[i])
    elif (keys[i][0][0]) == 'n':
        list_n.append(keys[i])
    elif (keys[i][0][0]) == 'o':
        list_o.append(keys[i])
    elif (keys[i][0][0]) == 'p':
        list_p.append(keys[i])
    elif (keys[i][0][0]) == 'q':
        list_q.append(keys[i])
    elif (keys[i][0][0]) == 'r':
        list_r.append(keys[i])
    elif (keys[i][0][0]) == 's':
        list_s.append(keys[i])
    elif (keys[i][0][0]) == 't':
        list_t.append(keys[i])
    elif (keys[i][0][0]) == 'u':
        list_u.append(keys[i])
    elif (keys[i][0][0]) == 'v':
        list_v.append(keys[i])
    elif (keys[i][0][0]) == 'w':
        list_w.append(keys[i])
    elif (keys[i][0][0]) == 'x':
        list_x.append(keys[i])
    elif (keys[i][0][0]) == 'y':
        list_y.append(keys[i])
    elif (keys[i][0][0]) == 'z':
        list_z.append(keys[i])
    else:
        list_unknown.append(keys[i])
    i += 1

def check_based_alphabet(alphabetList, k = 0, j = 0):
    while k < len(alphabetList) - 1:
        if alphabetList[k][1] != alphabetList[j][1]:
            distance = td.jaccard(alphabetList[k][0], alphabetList[j][0])
            if distance > 0.7:
                duplicate_list.append([alphabetList[k][0], alphabetList[j][0]])
            j += 1
        else:
            j += 1
        if j == len(alphabetList):
            j = 1
            k += 1

check_based_alphabet(list_a)
check_based_alphabet(list_b)
check_based_alphabet(list_c)
check_based_alphabet(list_d)
check_based_alphabet(list_e)
check_based_alphabet(list_f)
check_based_alphabet(list_g)
check_based_alphabet(list_h)
check_based_alphabet(list_i)
check_based_alphabet(list_j)
check_based_alphabet(list_k)
check_based_alphabet(list_l)
check_based_alphabet(list_m)
check_based_alphabet(list_n)
check_based_alphabet(list_o)
check_based_alphabet(list_p)
check_based_alphabet(list_q)
check_based_alphabet(list_r)
check_based_alphabet(list_s)
check_based_alphabet(list_t)
check_based_alphabet(list_u)
check_based_alphabet(list_v)
check_based_alphabet(list_w)
check_based_alphabet(list_x)
check_based_alphabet(list_y)
check_based_alphabet(list_z)

Body:
You ask specifically for time complexity, but finding the time complexity is easier when the code is clean. So I will: 1. clean the code; 2. find the time complexity and discuss of the possible improvement.

Clean the code

First, instead of creating list_a, list_b, ..., create a dict list_by_first_letter. We don't need to worry about indices:

for k in keys:
    letter = k[0][0]
    if not('a' <= letter <= 'z'):
        letter = 'UNK'
    list_by_first_letter.setdefault(letter, []).append(k)

The setdefault will check if the letter is already a key in the dict, and else create it and map it to a default value (empty list [] here).

And then, to find the duplicates:

for alphabet_list in list_by_first_letter.values():
    check_based_alphabet(alphabet_list)

Now, we have to clean check_based_alphabet. I replace while by for. Since you're matching every element against all the others you have:

def check_based_alphabet(alphabet_list):
    for k in range(len(alphabet_list)):
        first_key = alphabet_list[k]
        for j in range(len(alphabet_list))):
            second_key = alphabet_list[j]
            if first_key[1] != second_key[1]:
                distance = td.jaccard(first_key[0], second_key[0])
                if distance > 0.7:
                    duplicate_list.append([first_key[0], second_key[0]])

You may have an issue here: you compare alphabet_list[0] to alphabet_list[1], alphabet_list[2], ..., then alphabet_list[1] to alphabet_list[0], alphabet_list[2], .... If your td.jaccard is symmetric, you'll get every couple twice. To avoid this, use:

...
for j in range(k+1, len(alphabet_list))):
...

You get rid of the duplicates since second_key is always after first_key in the list. (I won't claim that the code is now totally clean, but it is at least readable.)

Time complexity

Now, we can find the time complexity. First, the time complexity of check_based_alphabet is O(n^2 * complexity of td.jaccard) where n is the number of element in the sublist. That's obvious because of the two imbricated loops.

If N is the number of elements in the list, you have a global time complexity of O(n1^2 * complexity of td.jaccard) + O(n2^2 * complexity of td.jaccard) + ... where N = n1 + n2 + ..., that is O(N^2 * complexity of td.jaccard):

In the worst case, we have N  = n1 and n2 = ... = 0 and the result is trivial.

In the best case, we have n1 = n2 = ... and we have 27 * O((N/27)^2 * complexity of td.jaccard) and that's still O(N^2 * complexity of td.jaccard), while this may be faster.

It seems hard to improve that time complexity. Look at your trial: you group the keys in buckets (all keys that somewhat start with the same letter) and compare each key with all keys of the same bucket. Good idea, but the flaw is that the size of the buckets still depends on N. To reduce the time complexity, you have to create buckets of a fixed size. That's not possible unless td.jaccard and/or the keys have specific properties.

(An example of specific properties: imagine you have a list of distinct integers, and that two integers i, j are said to be "duplicates" if |i -j| < K where K is independant of N. You can compare all pairs of integers (O(N^2)), but you can also sort the integers (O(N lg N) in the generic case) and then compare each element of the sorted list to the K next integers (above that, all integers are at least i + K, hence not duplicates): O(N * K). Hence, the time complexity is O(N (K + lg N)) better than O(N^2).)


问:
How to turn a table vertical using PHP
I am just learning PHP ,and I want to create a table that display echo data that I submit to my database , the problem I have that the table displayed horizontally by default as you see Horizontal default table this my script

Name
Age
Height

<?php
$conn = mysqli_connect("localhost", "root", "", "class");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Name, Age, Height FROM student order by Name desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
" . $row["Name"]. "	" . $row["Age"] . "	"
. $row["Height"]. "
";
}
echo "
";
} else //{ echo "0 results"; }//
$conn->close();
?>

```

but I want it to be echoed vertically instead like this VERTICAL RESULT I WANT and I tried to change html in echo in my PHP code but I can't get the result at all and the shape of the table is far away from what I want and this is the full script of my page .

答:
I mocked your data into normal array, I used a while loop to create a new Array and create the format that we need to be able to flip the columns into rows, here is what I think you want:

```<code><?php
$users  = [
  [
    'name'=> 'James',
    'height'=> 1.75,
    'age'=> 18,
  ],
  [
    'name'=> 'Bill',
    'height'=> 170,
    'age'=> 16,
  ]
];

$newArr = [];

foreach($users as $key => $val) {
  $newArr['name'][$i] = $val['name'];
  $newArr['age'][$i] = $val['age'];
  $newArr['height'][$i] = $val['height'];
  $i++;
}

?>
<table border="1">
  <?php foreach($newArr as $key => $val): ?>
    <tr>
      <td><?php echo $key; ?></td>
      <?php foreach($val as $field): ?>
        <td><?php echo $field; ?></td>
      <?php endforeach; ?>
    </tr>
  <?php endforeach ?>
</table>
</code>```

Title: How to turn a table vertical using PHP

Body:
I am just learning PHP ,and I want to create a table that display echo data that I submit to my database , the problem I have that the table displayed horizontally by default as you see Horizontal default table this my script

Name
Age
Height

<?php
$conn = mysqli_connect("localhost", "root", "", "class");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Name, Age, Height FROM student order by Name desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
" . $row["Name"]. "	" . $row["Age"] . "	"
. $row["Height"]. "
";
}
echo "
";
} else //{ echo "0 results"; }//
$conn->close();
?>

but I want it to be echoed vertically instead like this VERTICAL RESULT I WANT and I tried to change html in echo in my PHP code but I can't get the result at all and the shape of the table is far away from what I want and this is the full script of my page .

Body:
I mocked your data into normal array, I used a while loop to create a new Array and create the format that we need to be able to flip the columns into rows, here is what I think you want:

<?php
$users  = [
  [
    'name'=> 'James',
    'height'=> 1.75,
    'age'=> 18,
  ],
  [
    'name'=> 'Bill',
    'height'=> 170,
    'age'=> 16,
  ]
];

$newArr = [];

foreach($users as $key => $val) {
  $newArr['name'][$i] = $val['name'];
  $newArr['age'][$i] = $val['age'];
  $newArr['height'][$i] = $val['height'];
  $i++;
}

?>
<table border="1">
  <?php foreach($newArr as $key => $val): ?>
    <tr>
      <td><?php echo $key; ?></td>
      <?php foreach($val as $field): ?>
        <td><?php echo $field; ?></td>
      <?php endforeach; ?>
    </tr>
  <?php endforeach ?>
</table>


问:
Javascript Interpreter for .NET
I've got a scenario where I need to run a Javascript interpreter in a .NET application.  This is going to be running on Windows Phone 7, so it needs to be Compact Framework-compliant and because it probably won't be pre-packaged for Windows Phone source could help there.

Also the licensing can be an issue.  We looked at using Jint which is under an MIT license, but it uses Antlr, which is under a BSD license (as I understand it Jint is in non-compliance for not redistributing the Antlr copyright and conditions).  Any other alternatives out there?

答:
You should try:
https://github.com/gatapia/js.net

I haven't tried on WP7 but may work?

Title: Javascript Interpreter for .NET


问:
Is it possible to Enable full screen exclusive mode if isDisplayChangeSupported() shows its not available
I'm new to java Game development.
Please let me know if its possible to Enable full screen exclusive mode if isDisplayChangeSupported() shows its not available.

about isDisplayChangeSupported() : By getting GraphicalEnvironment() we can perform many modifications in the graphics and change the display mode(like getting full access of the full-screen).
We use isDisplayChangeSupported() to verify if the written displaymode changes is getting applied or not, by simply checking does the system supports changes in Display mode.

If its possible to enable it then please tell me how.
Thank you!

答:
You are confusing two entirely different features:

Full Screen Window

Setting a full screen window always works. As the documentation says:

The entered full-screen mode may be either exclusive or simulated. Exclusive mode is only available if isFullScreenSupported returns true.

So when isFullScreenSupported returns false, it still works but is simulated.

Display mode changes

are about changing the resolution and/or color depth of the screen. They may require setting a full screen window first, as a prerequisite, but when display mode changes are not supported, full screen window still works:

Sets the display mode of this graphics device. This is only allowed if isDisplayChangeSupported() returns true and may require first entering full-screen exclusive mode using setFullScreenWindow(java.awt.Window) providing that full-screen exclusive mode is supported (i.e., isFullScreenSupported() returns true).


答:
It seems like the data type of bal is TEXT so it is ordered alphabetically.

You must convert bal to an integer either by adding 0 or explicitly:

```<code>SELECT bal, user_id
FROM moneyTable
WHERE guild_id = {ctx.guild.id}
ORDER BY bal + 0 DESC LIMIT 10
</code>```

or:

```<code>SELECT bal, user_id
FROM moneyTable
WHERE guild_id = {ctx.guild.id}
ORDER BY CAST(bal AS INTEGER) DESC LIMIT 10
</code>```

Title: Ordering SQLite3 Database greatest to least

Body:
Im trying to make a discord command that gets a column of a database greatest to least and gets the top 10 results, its working but its not printing in order greatest to least.

cursor.execute(f"SELECT bal, user_id FROM moneyTable WHERE guild_id = {ctx.guild.id} ORDER BY bal DESC LIMIT 10")
result = cursor.fetchall()
print(result)

My result is

[('635', '673990922945560599'), ('400', '317652126471815168'), ('200', '323669516489850882'), ('15000', '539305505445642250'), ('1260', '448573840893804614'), ('1000', '531664615755612161'), ('100', '419300570235666432'), ('100', '412106756349624331'), ('100', '408674114958524417'), ('100', '358517557772156929')]

Body:
It seems like the data type of bal is TEXT so it is ordered alphabetically.

You must convert bal to an integer either by adding 0 or explicitly:

SELECT bal, user_id
FROM moneyTable
WHERE guild_id = {ctx.guild.id}
ORDER BY bal + 0 DESC LIMIT 10

or:

SELECT bal, user_id
FROM moneyTable
WHERE guild_id = {ctx.guild.id}
ORDER BY CAST(bal AS INTEGER) DESC LIMIT 10


问:
Azure-DevOps Pipeline fails because of testhost.dll
I'm having some troubles with my azure devops testing-pipeline.
I'm using xunit tests and every test-project runs correctly and I can view the test results, but the pipeline fails anyway because of the following error

```<code>##[error]Testhost process exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
##[error]. Please check the diagnostic logs for more information.
</code>```

Like suggested here, I added an additional filter for testhost.dll so my yaml-snippet looks like this:

```<code>- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test.dll
      !**\*TestAdapter.dll
      !**\obj\**
      !**\testhost.dll
      !*testhost.dll
    searchFolder: '$(System.DefaultWorkingDirectory)'
    diagnosticsEnabled: true
    codeCoverageEnabled: true
</code>```

However, the error still occurs and my Build is marked as failed.
How can I fix this?

答:
This GitHub issue should hopefully help.

Things to try:

remove references from one test assembly to another

update Microsoft.NET.Test.SDK to 16.6.1

add an application.runtimeconfig.json file

Hope this helps!

Body:
I'm having some troubles with my azure devops testing-pipeline.
I'm using xunit tests and every test-project runs correctly and I can view the test results, but the pipeline fails anyway because of the following error

##[error]Testhost process exited with error: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
##[error]. Please check the diagnostic logs for more information.

Like suggested here, I added an additional filter for testhost.dll so my yaml-snippet looks like this:

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\*test.dll
      !**\*TestAdapter.dll
      !**\obj\**
      !**\testhost.dll
      !*testhost.dll
    searchFolder: '$(System.DefaultWorkingDirectory)'
    diagnosticsEnabled: true
    codeCoverageEnabled: true

However, the error still occurs and my Build is marked as failed.
How can I fix this?