RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

全部问题

Martin Hope
Belka_zub
Asked: 2024-11-23 19:50:23 +0000 UTC

如何编写一个返回同性别用户平均年龄的函数? [关闭]

  • 5
关闭这个问题是题外话。目前不接受对此问题的答复。

该问题是由于无法重现或拼写错误造成的。虽然类似的问题可能与本网站相关,但该问题的解决方案不太可能对未来的访问者有帮助。您通常可以通过在发布问题之前编写和研究一个最小的程序来重现问题来避免此类问题。

2 天前关闭。

改进问题

我无法解决这个问题。我们需要创建一个函数 getUsersNamesInAgeRange(users, sex) 来返回相同性别用户的平均年龄。目前我只能这样写,但结果是错误的:

function getUsersNamesInAgeRange(users, gender) {
    const filteredGender = users.filter((user) => {
        return user.gender === gender;
    });
    const ageSum = users.reduce((sum, user) => {
    return sum + user.age
}, 0);

  return ageSum / users.length
}

告诉我,我做错了什么?预先感谢您的回答!

javascript
  • 3 个回答
  • 24 Views
Martin Hope
Asker
Asked: 2024-11-23 19:21:16 +0000 UTC

如何在 fs/promises 中正确使用 createReadStream

  • 5

在 fs 中使用 Promise API 时,首先我必须打开文件描述符,然后才能访问我需要的方法,在我的例子中是 createReadStream

 const fileHandle = await fs.open(filePath, 'r')
 const readableStream = fileHandle.createReadStream(opt)

我怎样才能通过promise正确使用这个方法呢?最重要的是,如何正确且优雅地关闭文件描述符,因为当将await fileHandle?.close() 行插入finally 块时,文件没有时间被读取并立即关闭,这就是我尝试的方法。

const fs = require("fs/promises");

async function foo() {
  const fileHandle = await fs.open("text.txt", "r");

  const readableStream = fileHandle.createReadStream();

  try {
    return new Promise((resolve, reject) => {
      let data = "";

      readableStream .on("data", (chunk) => {
        data += chunk;
      });

      readableStream .on("end", () => {
        resolve(data);
      });

      readableStream .on("end", reject);
    });
  } catch (err) {
    console.log(err);
  } finally {
    await fileHandle?.close();
  }
}

foo().then((data) => console.log(data));
javascript
  • 1 个回答
  • 32 Views
Martin Hope
Roxie Cloud
Asked: 2024-11-23 19:18:23 +0000 UTC

编译 C++ 运算符和操作数的字典,以使用 Halsted Metric 评估程序的复杂性

  • 7

建议代码:

using namespace std;

int main(int argc, char* argv[]) {
    srand(time(0));
    const int array_size = 200000; // 1

    int a[array_size]; 
  
    for (int counter = 0; counter < array_size; counter++) { // 2
        a[counter] = rand() % 50 - rand() % 50; // 3
        cout << a[counter] << " "; 
    }

    int min = a[0]; // 4
    for (int counter = 1; counter < array_size; counter++) { // 5
        if (min > a[counter]) {
            min = a[counter]; // 6
        }
    }

    cout << "\nmin = " << min << endl; 
    cout << "runtime = " << clock() / 1000.0 << endl; 

    system("pause"); 
    return 0; 
}

我得到以下信息:

在此输入图像描述

不过,我并不完全确定,因为……就霍尔斯特德度量而言,过程和函数名称是运算符,函数是操作数,我无法弄清楚给定程序中什么是函数以及什么是函数名称。

c++
  • 1 个回答
  • 73 Views
Martin Hope
Egor Polianskii
Asked: 2024-11-23 18:22:53 +0000 UTC

为什么附加序列求和的时间复杂度为 O(n) 而不是 O(n logn)?

  • 6
static int
    list_resize(PyListObject *self, Py_ssize_t newsize)
    {
    PyObject **items;
    size_t new_allocated, num_allocated_bytes;
    Py_ssize_t allocated = self->allocated;

    /* Bypass realloc() when a previous overallocation is large enough
       to accommodate the newsize.  If the newsize falls lower than half
       the allocated size, then proceed with the realloc() to shrink the list.
    */
    if (allocated >= newsize && newsize >= (allocated >> 1)) {
        assert(self->ob_item != NULL || newsize == 0);
        Py_SIZE(self) = newsize;
        return 0;
    }

    /* This over-allocates proportional to the list size, making room
     * for additional growth.  The over-allocation is mild, but is
     * enough to give linear-time amortized behavior over a long
     * sequence of appends() in the presence of a poorly-performing
     * system realloc().
     * The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
     * Note: new_allocated won't overflow because the largest possible value
     *       is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
     */
    new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
    if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
        PyErr_NoMemory();
        return -1;
    }

    if (newsize == 0)
        new_allocated = 0;
    num_allocated_bytes = new_allocated * sizeof(PyObject *);
    items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
    if (items == NULL) {
        PyErr_NoMemory();
        return -1;
    }
    self->ob_item = items;
    Py_SIZE(self) = newsize;
    self->allocated = new_allocated;
    return 0;
}

查看CPython方法的源代码append(上面列出),我发现append只要数组中有空闲空间,复杂度就是O(1),否则Python以指数方式增加新的大小(+3/+6固定量)列表的内存)并将现有元素复制到新数组中。事实证明,由于分配的数组大小呈指数增长,随着列表的增长,重新分配的次数会减少。因此,例如:

lst = []
for i in range(10):  
  lst.append(i)

第一次重新分配时,复制 1 个元素,第二次复制 4 个元素,第三次复制 10 个元素,依此类推,理论上给出了以下形式的几何级数:1+4+10+⋯≤O(n)。据我了解,随着每次后续迭代,重新分配之间的操作数量线性增长,并且 n 加法的重新分配总数以 O(logn) 的形式减少。那么,在这种情况下,为什么对于 n 个附加序列,总复杂度是 O(n),而不是 O(n logn)?

python
  • 1 个回答
  • 42 Views
Martin Hope
said rajabov
Asked: 2024-11-23 16:30:03 +0000 UTC

初始化和赋值

  • 5
int test;

这是一个变量声明

test = 5;

这是她的初始化

int test2 = 5;

这是什么?就好像同时有一个公告和初始化。是否有单独的名称?

还有几个问题(与上面的部分无关):初始化是第一次给变量赋值,以后只是赋值,我理解正确吗?是不是可以说定义而不是挪用,这不重要?我只是到处看到不同的名字,因此我的头很困惑。

c#
  • 1 个回答
  • 33 Views
上一页
下一页

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5