小编Tom*_*cik的帖子

硬币所有组合 - 2种算法之间的奇怪差异

我试图解决硬币改变问题.因为我住在欧洲让我们成为欧元问题.我们需要5欧元.我们可以使用10,20,50美分,1欧元,2欧元和5欧元.有多少种可能获得5欧元?

这是leif这里发布的代码.

cents = 50
denominations = [50, 20, 10, 5, 2, 1]
names = {50: "", 20: "", 10 : "", 5 : "", 2 : "", 1: ""}

def count_combs(left, i, comb, add):
    if add: comb.append(add)
    if left == 0 or (i+1) == len(denominations):
        if (i+1) == len(denominations) and left > 0:
            comb.append( (left, denominations[i]) )
            i += 1
        while i < len(denominations):
            comb.append( (0, denominations[i]) )
            i += 1
        print " ".join("%d %s" …
Run Code Online (Sandbox Code Playgroud)

python algorithm math debugging permutation

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

Python-替换多个字符而没有.replace()

任务是将任何字符串转换为没有内置字符串的任何字符串.replace()。我失败了,因为我从技术上忘记了空格也是一个字符串字符。首先,我将此字符串转换为列表,但是现在我看到了不必要的操作。但是,它仍然不起作用。

  1. 我可以将“猫”替换为“狗”
  2. 我可以将“ c”替换为“ dog”

我不能将“猫”替换为“狗”。

我尝试使用lambdazip,但我真的不知道该怎么做。你有什么线索吗?

string = "Alice has a cat, a cat has Alice."
old = "a cat"
new = "a dog"

def rplstr(string,old,new):
    """ docstring"""

    result = ''
    for i in string:
        if i == old:
            i = new
        result += i
    return result

print rplstr(string, old, new)
Run Code Online (Sandbox Code Playgroud)

python replace

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

Python - 任何基数到十进制(other2dec)

因为一道题,我考试不及格。任务是:

“设计一个程序,将任何数字从任何系统转换为十进制。
我们限制在 2 到 22 范围内的系统。”

所以我来了。我知道二进制 [2]、八进制 [8]、十进制 [10] 和十六进制 [16] 系统。每个转换系统有1分,所以它必须是一个转换器:

2->10
3->10
...
22->10

我不知道这怎么可能。考试后我问我的教授怎么做,他说:“只是 x 的 y 次方,乘以,就是这样。所有这些都有相同的规则。”

他说的话我可能是错的,因为我处于考试后的意识状态。你们知道如何解决它吗?

我看到在stackoverflow上已经有一些类似的问题,但没有一个不能像我教授所说的那样解决问题。另外,我们大约在 4 个月前开始学习 Python,我们还没有学习回复中实现的一些选项。

"""IN
str/int, any base[2-22]
OUT
十进制整数或浮点数"""

python decimal base any

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

标签 统计

python ×3

algorithm ×1

any ×1

base ×1

debugging ×1

decimal ×1

math ×1

permutation ×1

replace ×1