我正在编写一个小型的C客户端/服务器应用程序,但在使用外部IP地址时无法使连接正常工作.客户端和服务器的代码都来自这里,特别是客户端:
char *default_server_name = "localhost";
char *server_name = NULL;
int nport = DEFAULT_DAMA_PORT;
char port[15];
// Parse the command line options
if (parse_options(argc, argv, &server_name, &nport) < 0) {
return -1;
}
if (server_name == NULL) {
server_name = default_server_name;
}
snprintf(port, 15, "%d", nport);
// Connect to the server
int client_socket;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(server_name, port, &hints, &servinfo)) != 0) { …Run Code Online (Sandbox Code Playgroud) 我们假设我有两个词典:
dic1 = { "first":1, "second":4, "third":8}
dic2 = { "first":9, "second":5, "fourth":3}
Run Code Online (Sandbox Code Playgroud)
有没有直接的方法来获得如下所示的东西?
dic3 = { "first":[1,9], "second":[4,5], "third":[8], "fourth":[3]}
Run Code Online (Sandbox Code Playgroud)
我使用列表存储值,但元组也很好.
我希望文本输入仅接受数字序列。任何其他字符都应该被默默地忽略。这是我的组件的简化版本:
<template>
<div id="app">
<input :value="tel" @input="setTel" placeholder="only numbers" />
<p>{{ tel }}</p>
</div>
</template>
<script>
export default {
name: "App",
data: () => ({
tel: "1234"
}),
methods: {
setTel(v) {
const val = v.target.value.replace(/[^0-9]/g, "");
this.tel = val;
/*this.tel = v.target.value = v.target.value.replace(/[^0-9]/g, "");*/
}
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
在React中,有受控组件的概念,但我在Vue中似乎没有类似的东西。
我找到的解决方法(你可以在评论中看到)是手动修改输入元素的值,但这有点违背了使用 Vue 的目的。
我也尝试过使用v-model,但问题仍然存在。
我必须交错给定的表单数组
{a1,a2,....,an,b1,b2,...,bn}
Run Code Online (Sandbox Code Playgroud)
作为
{a1,b1,a2,b2,a3,b3}
Run Code Online (Sandbox Code Playgroud)
在 O(n) 时间和 O(1) 空间中。
例子:
Input - {1,2,3,4,5,6}
Output- {1,4,2,5,3,6}
Run Code Online (Sandbox Code Playgroud)
这是按索引排列的元素:
Initial Index Final Index
0 0
1 2
2 4
3 1
4 3
5 5
Run Code Online (Sandbox Code Playgroud)
通过一些例子的观察,我发现ai (i<n/2) goes from index (i) to index (2i)& bi (i>=n/2) goes from index (i) to index (((i-n/2)*2)+1). 您可以自行验证。如果我错了,请纠正我。
但是,我无法在代码中正确应用此逻辑。
我的伪代码:
for (i = 0 ; i < n ; i++)
if(i < n/2)
swap(arr[i],arr[2*i]);
else
swap(arr[i],arr[((i-n/2)*2)+1]);
Run Code Online (Sandbox Code Playgroud)
它不起作用。
如何编写算法来解决这个问题?
假设的流程链的流程树以树结构表示。每个进程产生与它的进程号相等的进程数,即进程号 3 有 3 个子进程。进程按级别顺序命名,root 为 1,其子/子进程从 2 命名,依此类推。给定进程的父进程的进程号是多少?
1
\
2
/ \
3 4
| |
+-+-+ +-+--+-+
| | | | | | |
5 6 7 8 9 10 11
Run Code Online (Sandbox Code Playgroud)
因此,对于 6,父进程将为 3。
我在 O(n) 中编写了一个函数,它只是将树构建到 n 并从树中找到父节点,但我相信有更好的方法来解决这个问题。
我有一个包含 10 辆车的结构体,例如:
type struct car {
engine
window
wheel
}
Run Code Online (Sandbox Code Playgroud)
所以切片 cars 包含 10 个汽车结构。
我想知道是否存在一个函数,例如:
var engines string[] = cars.Getfield("engine") // engines will contain 10 engines names
Run Code Online (Sandbox Code Playgroud) 假设我有这POST条接收一些数据的路由。
app.post('/getData', function(req, res){
var retrievedData = req.body.exampleVariable;
// Send data to GET method
});
Run Code Online (Sandbox Code Playgroud)
我有这个GET呈现页面的方法,但需要我在该POST方法中检索到的数据
app.get('/displayData', function(req, res){
// Retrieve data from POST method and display it.
res.render('/examplePage.ejs', {retrievedData : req.retrievedData});
});
Run Code Online (Sandbox Code Playgroud)
将retrievedData变量从给定POST路线传递到GET路线的最佳方法是什么?
作为旁注,该res.render()方法似乎只适用于app.get()类型方法
我有一个包含这些值的数组:
let elements = ["div", "span", "button"]
Run Code Online (Sandbox Code Playgroud)
如何使用 ReactJS 中的迭代器在 DOM 中动态生成这些forEach元素map ?
所以作为输出我想要:
let elements = ["div", "span", "button"]
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个应用程序,对随机单词进行谷歌图像搜索并选择/单击第一个结果图像。
我成功了,直到代码尝试选择结果图像并在我的终端中抛出以下错误:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection
id: 1): Error: Protocol error (Runtime.callFunctionOn): Cannot find
context with specified id undefined
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
const pup = require('puppeteer');
const random = require('random-words');
const url = 'http://images.google.com';
(async() => {
const browser = await pup.launch({headless: false});
const page = await browser.newPage();
await page.goto(url);
const searchBar = await page.$('#lst-ib');
await searchBar.click();
await page.keyboard.type(`${random()}`);
const submit = await page.$('#mKlEF');
await submit.click();
await page.keyboard.type(random());
await page.keyboard.press('Enter');
const pic = await page.evaluate(() => {
return document.querySelectorAll('img');
});
pic.click(); …Run Code Online (Sandbox Code Playgroud) 这是用 Jest (v20.0.4) 编写的测试套件。
前 3 个测试是预期行为,我的问题与 Test4 相关。
test('Test1: the list should contain 7', () => {
const data = [1, 2, 7, 9];
expect(data).toContain(7);
});
// > Passes as expected
Run Code Online (Sandbox Code Playgroud)
test('Test2: the list should contain 7', () => {
const data = [1, 2, 7, 9];
expect(data).toContain(8);
});
// > Fails as expected; Expected array: [1, 2, 7, 9] To contain value: 8
Run Code Online (Sandbox Code Playgroud)
test('Test3: the list should contain 7', (done) => {
function callback(data) {
expect(data).toContain(7);
done();
} …Run Code Online (Sandbox Code Playgroud)