小编Ch3*_*teR的帖子

在列表字典中查找键的索引

给定一个列表字典

node_to_index = {"global": [0],
                  "l_eye": list(range(42, 48)),
                  "r_eye": list(range(36, 42)),
                  "l_brow": list(range(22, 27)),
                  "r_brow": list(range(17, 22)),
                  "mouth": list(range(48, 68)),
                  "nose": list(range(27, 35)),
                 }
Run Code Online (Sandbox Code Playgroud)

给定一个数字,例如 37,它将返回“r_eye”键的索引,即 2

python

0
推荐指数
1
解决办法
62
查看次数

如何打印列表中两个乘积之间可能的每次迭代?

给定一个列表,其中的数字介于0和之间5。我希望此列表中的两个数字的每个可能结果都返回 true (a ** 2 + b ** 2) < 12

我的代码是:

from random import choices
from math import factorial

nodes = list(range(0,6))
lis = []
for e in range(0,factorial(5)):
    nodesf = choices(nodes, k= 2)
    if not nodesf in lis:
        if (nodesf[0]**2 + nodesf[1]**2) <= 12:
            print(nodesf)
            lis.append(nodesf)
            lis.append(nodesf[::-1])
            print(lis)
        else:
            lis.append(nodesf)
Run Code Online (Sandbox Code Playgroud)

但是,正如您所看到的,这可能是解决此问题的一种糟糕方法。如果您看到更清晰、更有效的代码来解决这个问题,请帮助我。

python

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

使用 3 组坐标的三角形面积

我正在做一个问题,我需要找到给定 3 组坐标的三角形面积

那么将数组转换为 (a1,b1) (a2,b2) (a3,b3) 中的对的逻辑是什么以及如何使用该顶点查找三角形的面积

这是我的代码

def getTriangleArea(x, y):

///What will be the code 


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    x_count = int(input().strip())

    x = []

    for _ in range(x_count):
        x_item = int(input().strip())
        x.append(x_item)

    y_count = int(input().strip())

    y = []

    for _ in range(y_count):
        y_item = int(input().strip())
        y.append(y_item)

    result = getTriangleArea(x, y)

    fptr.write(str(result) + '\n')

    fptr.close()
Run Code Online (Sandbox Code Playgroud)

python area python-3.x

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

在一个字典中更新多个字典

CLUB_CARDS = range(13)
DIAMONDS_CARDS = range(13, 26)
HEART_CARDS = range(26, 39)
SPADE_CARDS = range(39, 52)
CARDS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"]

def get_cards():
    d = {k: v for k, v in zip(CLUB_CARDS, CARDS)}
    d.update({k: v for k, v in zip(DIAMONDS_CARDS, CARDS)})
    d.update({k: v for k, v in zip(HEART_CARDS, CARDS)})
    d.update({k: v for k, v in zip(SPADE_CARDS, CARDS)})
    return d
Run Code Online (Sandbox Code Playgroud)

如何重构这段代码?

python algorithm python-3.x

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

检查字符串是否是回文。C++

这有什么问题吗?

#include<bits/stdc++.h>
using namespace std;

bool isPalindrome(string str)
{
  char temp[1000];
  int len=str.length();
  int i=0;
  while(len)
  {
    len--;
    temp[i++]=str[len];
  }
  temp[i]='\0';
  if (strcmp(str,temp)==0)
     return true;
  else
     return false;
 }
Run Code Online (Sandbox Code Playgroud)

c++ string palindrome

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

标签 统计

python ×4

python-3.x ×2

algorithm ×1

area ×1

c++ ×1

palindrome ×1

string ×1