小编Bor*_*ald的帖子

关于C中两种算法的不同运行时间的混淆

我有一个数组,long matrix[8*1024][8*1024]和两个函数sum1sum2:

long sum1(long m[ROWS][COLS]) {
    long register sum = 0;
    int i,j;

    for (i=0; i < ROWS; i++) {
        for (j=0; j < COLS; j++) {
            sum += m[i][j];
        }
    }
    return sum;
}

long sum2(long m[ROWS][COLS]) {
    long register sum = 0;
    int i,j;

    for (j=0; j < COLS; j++) {
        for (i=0; i < ROWS; i++) {
            sum += m[i][j];
        }
    }

    return sum;
}
Run Code Online (Sandbox Code Playgroud)

当我用给定的数组执行这两个函数时,我得到运行时间:

sum1:0.19s

sum2:1.25s

任何人都可以解释为什么会有这么大的差异?

c

28
推荐指数
2
解决办法
978
查看次数

文档参考无效。文档引用必须有偶数个段,但城市/SF/地标有 3 个

你好,我正在关注 firestore 教程

https://firebase.google.com/docs/firestore/query-data/queries#web-version-9_14

首先,他们指示我为数据库播种

import { collection, doc, setDoc } from "firebase/firestore"; 

const citiesRef = collection(db, "cities");

await setDoc(doc(citiesRef, "SF"), {
    name: "San Francisco", state: "CA", country: "USA",
    capital: false, population: 860000,
    regions: ["west_coast", "norcal"] });
await setDoc(doc(citiesRef, "LA"), {
    name: "Los Angeles", state: "CA", country: "USA",
    capital: false, population: 3900000,
    regions: ["west_coast", "socal"] });
await setDoc(doc(citiesRef, "DC"), {
    name: "Washington, D.C.", state: null, country: "USA",
    capital: true, population: 680000,
    regions: ["east_coast"] });
await setDoc(doc(citiesRef, "TOK"), {
    name: "Tokyo", …
Run Code Online (Sandbox Code Playgroud)

firebase typescript google-cloud-firestore

13
推荐指数
1
解决办法
3万
查看次数

找不到模块:错误:无法解析“./templates”

我有这个 javascript (TypeScript) 文件

索引.ts

import 
'../node_modules/bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap';
import * as templates from './templates';

document.body.innerHTML = templates.main;

const mainElement = 
document.body.querySelector('.b4.main');
const alertsElement = 
document.body.querySelector('.b4-alerts');

mainElement.innerHTML = templates.welcome;
alertsElement.innerHTML = templates.alert;
Run Code Online (Sandbox Code Playgroud)

当我运行我的项目(使用 webpack 开发服务器)时,出现以下错误:

Module not found: Error: Can't resolve './templates' in '/Users/BorisGrunwald/Desktop/Node/b4-app/app'
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它似乎找不到“templates.ts”,因为这两个文件都在同一个文件夹中。

这是我的项目结构的图片:

在此处输入图片说明

我的 webpack 配置:

'use strict';

const path = require('path');
const distDir = path.resolve(__dirname,'dist');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');

 module.exports = {
    entry: './app/index.ts',
    output: {
        filename: 'bundle.js',
        path: distDir
    },
    devServer: …
Run Code Online (Sandbox Code Playgroud)

javascript typescript webpack

8
推荐指数
1
解决办法
2万
查看次数

(React) 带有 css 模块的 CSSTransition

我正在尝试在我的项目中实现一个模态的 CSSTransition。问题是我正在使用 css 模块。

我的模态渲染方法

render() {
        return (
            <Aux>
                <Backdrop
                    show={this.props.show}
                    clicked={this.props.modalClosed}/>
                <CSSTransition
                    in={this.props.show}
                    timeout={1000}
                    mountOnEnter
                    unmountOnExit
                    classNames={?}
                >
                    <div
                        className={classes.Modal}
                    >
                        {this.props.children}
                    </div>
                </CSSTransition>
            </Aux>
        )
    }
Run Code Online (Sandbox Code Playgroud)

我的 Modal.css

    .fade-enter {

    }

    .fade-enter-active {
        animation:openModal 0.4s ease-out forwards;
    }

    .fade-exit{

    }

    .fade-exit-active{
        animation: closeModal 0.4s ease-out forwards;
    }
Run Code Online (Sandbox Code Playgroud)

为了使其工作,我将什么传递给 CSSTransition 组件中的 classNames 属性?

css css-transitions reactjs

7
推荐指数
2
解决办法
4565
查看次数

i18n 错误(错误:您正在传递未定义的模块!请检查您传递给 i18next.use() 的对象)

我正在尝试在我的 Gatsby 项目中设置 i18n。

我一直在逐步遵循本教程:

https://www.gatsbyjs.org/blog/2017-10-17-building-i18n-with-gatsby/

首先我下载所需的包:

npm i -S i18next i18next-xhr-backend i18next-browser-languagedetector react-i18next
Run Code Online (Sandbox Code Playgroud)

然后我设置了 i18n 组件

import i18n from "i18next"
import Backend from "i18next-xhr-backend"
import LanguageDetector from "i18next-browser-languagedetector"
import { reactI18nextModule } from "react-i18next"

i18n
.use(Backend)
.use(LanguageDetector)
.use(reactI18nextModule)
.init({
    fallbackLng: "en",

    // have a common namespace used around the full app
    ns: ["translations"],
    defaultNS: "translations",

    debug: true,

    interpolation: {
    escapeValue: false, // not needed for react!!
    },

    react: {
    wait: true,
    },
})

export default i18n
Run Code Online (Sandbox Code Playgroud)

将其导入到我的布局组件中:

import React …
Run Code Online (Sandbox Code Playgroud)

gatsby react-i18next

7
推荐指数
1
解决办法
1万
查看次数

h1 标签内的 SVG

我有一个 Gatsby 项目,我想在其中使用 svg 作为我的主标题。

import Header from "../images/header.svg"

return (
  <h1>
      <Header/>
  </h1>
)
Run Code Online (Sandbox Code Playgroud)

svg 中没有文本(文本纯粹使用矩形和路径制作),那么在可访问性和 SEO 优化方面我该怎么做?

svg accessibility gatsby

6
推荐指数
1
解决办法
6733
查看次数

在二维数组中查找最短路径(Javascript)

我正在尝试实现一种算法,该算法在以下二维数组中找到最短路径(从左上角到右下角):

      [ [ 'A', 'A', 'A', 'B', 'A' ],
        [ 'B', 'B', 'B', 'B', 'B' ],
        [ 'A', 'B', 'A', 'A', 'A' ],
        [ 'A', 'B', 'B', 'B', 'B' ],
        [ 'A', 'A', 'A', 'A', 'A' ] ]
Run Code Online (Sandbox Code Playgroud)

规则是,路径必须在 A 和 B 之间交替。

输出必须是一个数字,指定遍历数组所需的最小步数。(在示例中,预期输出为 13)

有谁知道一个简单的 Graph 实现可以帮助我解决这个问题?

javascript algorithm graph-theory

5
推荐指数
1
解决办法
4400
查看次数

带有HTML和CSS的吉他制表法

我正在尝试使用html / css创建吉他制表法。

我有这个基本的布局,你可以在这里看到

样式

* {
    margin:0;
    padding:0;
    box-sizing: border-box;
    font-family:sans-serif;
}

.wrapper {
    height:100px;
    width:600px;
    margin: 70px auto;
    display:flex;
    flex-direction:column;
    justify-content:space-between;
}


.string {
    height:1px;
    width:100%;
    background:black;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="wrapper">
    <div class="string"></div>
    <div class="string"></div>
    <div class="string"></div>
    <div class="string"></div>
    <div class="string"></div>
    <div class="string"></div>
</div>
<div class="wrapper">
    <div class="string"></div>
    <div class="string"></div>
    <div class="string"></div>
    <div class="string"></div>
    <div class="string"></div>
    <div class="string"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

码笔

我希望能够在像这样的字符串上放置音符(数字)

------0-
-----0--
----0---
---2----
--2-----
-0------
Run Code Online (Sandbox Code Playgroud)

也像和弦

-0------
-0------
-0------
-2------
-2------
-0------
Run Code Online (Sandbox Code Playgroud)

实现这一目标的最简单方法是什么?

html css

5
推荐指数
1
解决办法
134
查看次数

尝试使用 jest 使用 v9 firestore sdk 初始化测试环境

我正在尝试设置我的测试环境来使用 firestore 测试我的安全性。我从https://firebase.google.com/docs/rules/unit-tests#before_you_run_the_emulator复制了此代码

let testEnv : RulesTestEnvironment;

beforeAll(async () => {

    testEnv = await initializeTestEnvironment({
        projectId: "demo-project-1234",
        firestore: {
            rules: fs.readFileSync('firestore.rules', 'utf8'),
        },
    });

});
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误。

必须指定 firestore 模拟器的主机和端口。(您可以使用 firebase emulators:exec './your-test-script' 包装测试脚本以启用自动发现,或通过initializeTestEnvironment({firestore: {host, port}})手动指定。

有人知道如何解决这个问题吗?

编辑

我尝试将主机和端口添加到正在运行的模拟器中,如下所示

let testEnv : RulesTestEnvironment;

beforeAll(async () => {

    testEnv = await initializeTestEnvironment({
        projectId: "comment-section-e9c09",
        firestore: {
            rules: fs.readFileSync('firestore.rules', 'utf8'),
            host:'localhost',
            port:8080
        },
    });

});
Run Code Online (Sandbox Code Playgroud)

现在它似乎能够连接到我的模拟器,但是当我尝试 fx 清除数据库时

test("sefse", () => {
    testEnv.clearDatabase()
})
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

[UnhandledPromiseRejection:此错误的根源是在没有 catch 块的情况下抛出异步函数内部,或者拒绝未使用 .catch() 处理的 Promise。该承诺被拒绝,原因是“错误:必须指定数据库模拟器的主机和端口。(您可以使用 'firebase …

firebase firebase-security jestjs google-cloud-firestore

5
推荐指数
1
解决办法
1533
查看次数

Node.js TypeError:“文件”参数必须是非空字符串

我查看了有关此错误的其他帖子,但找不到解决我的特定问题的帖子

我遵循“Node.js 8 the right way”一书中的示例,其中我正在制作一个简单的程序来监视文件中的更改。

观察者-spawn.js

'use strict';

const fs = require('fs');
const spawn = require('child_process').spawn();

const filename = process.argv[2];

if(!filename)
throw Error('A file must be specified');


fs.watch(filename, ()=> {
const ls = spawn('ls', ['l','h',filename]);
ls.stdout.pipe(process.stdout)

});
Run Code Online (Sandbox Code Playgroud)

当我跑步时

node watcher-spawn.js target.txt
Run Code Online (Sandbox Code Playgroud)

我收到错误

TypeError: "file" argument must be a non-empty string
at normalizeSpawnArguments (child_process.js:380:11)
at Object.exports.spawn (child_process.js:487:38)
at Object.<anonymous> (/Users/BorisGrunwald/Desktop/filesystem/watcher-spawn.js:4:40)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup …
Run Code Online (Sandbox Code Playgroud)

node.js

4
推荐指数
1
解决办法
5601
查看次数