小编ron*_*ook的帖子

如何检查字符串是否包含两个括号之间的数字并返回位置?

说我有str = "qwop(8) 5",我想要返回8的位置.

我有以下解决方案:

import re

str = "qwop(8) 5"
regex = re.compile("\(\d\)")
match = re.search(regex, string) # match object has span = (4, 7)
print(match.span()[0] + 1)       # +1 gets at the number 8 rather than the first bracket
Run Code Online (Sandbox Code Playgroud)

这看起来非常混乱.有更复杂的解决方案吗?最好使用re我已导入的用于其他用途.

python regex string parsing python-3.x

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

创造"马里奥风格金字塔"

我正在浏览哈佛CS50在线课程,其中一个问题是使用空格和哈希创建一个"马里奥风格的金字塔".我已经解决了空间,但是哈希给了我麻烦.这是代码:

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    //get height between 1 and 23
    int height;
    do
    {
        printf("Please enter a height: ");
        height = GetInt();
    }
    while (height < 1 || height > 23);

    //build pyramid
    for (int i = 0; i < height ; i++)
    {
        //add spaces
        for (int space = height - 1 - i; space >= 0; space--)
            printf(" ");

        //add hashtags
        for (int hash = 2 + i; hash <= height; hash++)
            printf("#");

        printf("\n"); …
Run Code Online (Sandbox Code Playgroud)

c loops cs50

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

express.js - 服务器正在侦听但本地主机拒绝连接

尝试开始设置基本服务器。这是我的代码:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('this is fubar'); 
})

app.listen(3000, '0.0.0.0', () => console.log(`listening on port 3000`));
Run Code Online (Sandbox Code Playgroud)

当我运行node server.js,listening on port 3000被记录。

但是当我访问时,https://localhost/3000我收到消息“无法访问此站点。本地主机拒绝连接。”

我跑了netstat -ab | findstr :3000,但没有结果。我也试过端口 5000 和 80。我还能做什么?

使用 Windows 10。

javascript node.js express server

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

交换两个相邻元素的功能不起作用

当我尝试移动合法的图块(即与'空白'图块0相邻的图块)时,没有任何反应.如果磁贴是非法的,程序将按预期运行.这是移动功能:

bool move(int tile)
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] == tile)
            {
                // stops program from going out of bounds
                if (j < d)
                {
                    if (board[i][j + 1] == 0)
                        {
                            swap(board[i][j], board[i][j + 1]);
                            return true;
                        }
                }

                if (j > 0)
                {
                    if (board[i][j - 1] == 0)
                        {
                            swap(board[i][j], board[i][j - 1]);
                            return true;
                        }
                }

                if (i …
Run Code Online (Sandbox Code Playgroud)

c arrays multidimensional-array cs50

-2
推荐指数
1
解决办法
61
查看次数