小编Sna*_*rre的帖子

素数分解 - 列表

我正在尝试实现一个函数primeFac(),该函数将正整数作为输入,n并返回包含素数因子分解中所有数字的列表n.

我已经做到这一点,但我认为在这里使用递归会更好,不知道如何在这里创建递归代码,什么是基本情况?首先.

我的代码:

def primes(n):
    primfac = []
    d = 2
    while (n > 1):
         if n%d==0:
             primfac.append(d)
    # how do I continue from here... ?
Run Code Online (Sandbox Code Playgroud)

python prime-factoring python-3.x

25
推荐指数
6
解决办法
8万
查看次数

Wireshark - 读取加密数据

我正在尝试学习如何正确使用 Wireshark,我想知道 Wireshark 是否可以读取它在嗅探时捕获的加密数据?如果是这样,我该如何启动?

encryption sniffing wireshark

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

模拟掷骰子游戏

我正在尝试实现一个craps()不带参数的函数,模拟一个掷骰子游戏,1如果玩家赢了以及0玩家输了则返回.

游戏规则:游戏开始时玩家投掷一对骰子.如果玩家总共掷出7或11,则玩家获胜.如果玩家总共掷出2,3或12,则玩家输了.对于所有其他掷骰价值,游戏继续进行,直到玩家滚动初始值agaian(在这种情况下玩家获胜)或7(玩家输掉).

我想我越来越近了,但我还没有,我不认为我的while循环工作正常.这是我到目前为止的代码:

def craps():
    dice = random.randrange(1,7) + random.randrange(1,7)
    if dice in (7,11):
        return 1
    if dice in (2,3,12):
        return 0
    newRoll = craps()
    while newRoll not in (7,dice):
        if newRoll == dice:
            return 1
        if newRoll == 7:
            return  0
Run Code Online (Sandbox Code Playgroud)

如何修复while循环?我真的找不到它的问题,但我知道这是错误的或不完整的.

python while-loop python-3.x

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

有多少组合可能?

用于计算k从一组n项目中选择项目的方式的数量的递归公式表示C(n,k)为:

           1                    if K = 0
C(n,k) = { 0                    if n<k
           c(n-1,k-1)+c(n-1,k)  otherwise
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个使用这个递归公式C计算的递归函数C(n,k).我写的代码应该按照自己的方式工作,但它没有给我正确的答案.

这是我的代码:

def combinations(n,k):
    # base case
    if k ==0:
        return 1
    elif n<k:
        return 0
    # recursive case
    else:
        return combinations(n-1,k-1)+ combinations(n-1,k)
Run Code Online (Sandbox Code Playgroud)

答案应如下所示:

>>> c(2, 1)
0
>>> c(1, 2)
2
>>> c(2, 5)
10
Run Code Online (Sandbox Code Playgroud)

但我得到其他数字......不要在我的代码中看到问题所在.

python recursion python-3.x

3
推荐指数
2
解决办法
1252
查看次数

计算多项式系数

我正在尝试编写一个函数,该函数将多项式 p(x) 的系数列表 (a0, a1, a2, a3.....an) 和值 x 作为输入。该函数将返回 p(x),它是多项式在 x 处求值时的值。

系数为 a0, a1, a2, a3........an 的 n 次多项式是函数

p(x)= a0+a1*x+a2*x^2+a3*x^3+.....+an*x^n
Run Code Online (Sandbox Code Playgroud)

所以我不确定如何解决这个问题。我在想我需要一个范围,但我怎样才能使它可以处理 x 的任何数字输入?我不指望你们给出答案,我只是需要一点点启动。我是否需要 for 循环、while 循环或递归在这里可以选择?

def poly(lst, x)
Run Code Online (Sandbox Code Playgroud)

我需要迭代列表中的项目,我是否使用索引,但如何让它迭代未知数量的项目?

我想我可以在这里使用递归:

    def poly(lst, x):
        n = len(lst)
        If n==4:
           return lst[o]+lst[1]*x+lst[2]*x**2+lst[3]*x**3
        elif n==3:
           return lst[o]+lst[1]*x+lst[2]*x**2
        elif n==2:
           return lst[o]+lst[1]*x
        elif n==1:
           return lst[o]
        else:
            return lst[o]+lst[1]*x+lst[2]*x**2+lst[3]*x**3+lst[n]*x**n
Run Code Online (Sandbox Code Playgroud)

这适用于 n<=4,但我收到一个索引错误:列出 n>4 的索引超出范围,但不明白为什么。

python iteration python-3.x

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

试着学习JAVA,我想写99瓶啤酒歌

我是JAVA编程的新手,之前只用Python编程.我正在试图找出为什么当我执行我的代码时,我会在墙上找到一堆重复的"啤酒瓶#".

    package BeerBottle;

    public class BeerBot {
  public static void main (String [] args){
      int beerNum = 99;
      String word = "bottles";

      while (beerNum > 0) {

      if (beerNum == 1) {
      word = "bottle";
      } else {
      word = "bottles";
      }
    System.out.println(beerNum + " " + word + " " + "of beer on the wall");
    System.out.println(beerNum + " " + word + " " + "of beer");
    System.out.println("Take one down");
    System.out.println("pass it around");
    beerNum = beerNum -1;

    if …
Run Code Online (Sandbox Code Playgroud)

java

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

模拟几轮摇滚,纸和剪刀

我正在尝试实现一个带整数n的函数,并在玩家1和玩家2之间模拟n轮Rock,Paper,Scissors.获得最多轮次的玩家赢得n轮游戏,可以获得关系.该功能应打印游戏结果,如图所示.

    >>> simul(1)
    Player 1
    >>> simul(1)
    Tie
    >>>simul(100)
    Player 2
Run Code Online (Sandbox Code Playgroud)

我想我需要以模块化方式处理这个问题.换句话说,我需要结合至少2个函数,我的问题是我似乎无法弄清楚如何做到这一点.如何在调用函数时激活嵌入式函数的结果simul()

所以我创建了一个函数,通过执行函数来模拟游戏Rock,Paper,Scissors rps(p1, p2).代码如下:

    def rps(p1,p2):
        #tie
        if (p1==p2):
            return 0
        # player 1 wins    
        elif p1+p2 in ['PR','RS','SP']:
            return -1
        else:
            return 1
        # player 2 wins
Run Code Online (Sandbox Code Playgroud)

这是我有点卡住的地方.我需要在执行功能时激活此功能simul()- 我该怎么做?到目前为止我所拥有的是以下内容:

    def rps(p1,p2):
    #tie
        if (p1==p2):
            return 0
    # player 1 wins    
        elif p1+p2 in ['PR','RS','SP']:
            return -1
        else:
            return 1
    # player 2 wins

    def choose_rps():
        import random
        random.choice('RPS')

    def simul(n):
        p1_wins, p2_wins = …
Run Code Online (Sandbox Code Playgroud)

python

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

试图理解Levy曲线(分形)

我被要求实现一个递归函数,它以非负整数n作为输入并返回用字母L,R和F编码的turtle指令,其中L表示向左旋转45度,R表示向右旋转45度,F表示向前旋转.

附加信息我有:对于每个非负整数n> 0,Levy曲线L(n)可以用Levy曲线定义L(n-1); Levy曲线L(0)只是一条直线.

    usage:
    >>> lev(0)
    'F'
    >>> lev(1)
    'LFRRFL'
Run Code Online (Sandbox Code Playgroud)

我对此很新,我不知道如何开始:

到目前为止我只得到:

    from turtle import Screen, Turtle
    def lev(n):
        # base case
        if n ==0:
           return 'F'
        # recursive case
        else:
            return lev(n-1)
Run Code Online (Sandbox Code Playgroud)

我在这里需要一些好的指示.

python fractals

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

此代码中存在多个问题 - 类型:Missmatch

所以我写了一个JAVA代码,该代码应该告诉用户如果将另外两个(从列表中)随机选择的颜色组合,他将获得什么颜色.请注意,我是JAVA的新手(以前只用Python编程).

码:

    package ListOfWords;

     public class testListWords {
  public static void main (String[] args) {

      String [] colors = {"red","green","gray","black","blue","yellow"};

      int colorsLength = colors.length;

      int rand1 = (int) (Math.random() * colorsLength);
      int rand2 = (int) (Math.random() * colorsLength);

      while(rand1==rand2){
          int rand2 = (int) (Math.random() * colorsLength);
      }

      String phrase1 = colors[rand1];
      String phrase2 = colors[rand2];       

      while(phrase1 = "green"){

       if (phrase2 = "red") {
          System.out.print("Combining" + " " + phrase1 + " " + "with" + " " + phrase2 + …
Run Code Online (Sandbox Code Playgroud)

java

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