【每日一题20220720】4个连续数字的最低乘积

:mage:‍创建一个函数,该函数返回以字符串形式给出的数字中 4个连续数字的最小乘积。

这仅适用于数字有 4 位或更多位的情况。如果不是,则返回“数字太小”。

例子

lowest_product("123456789") --> 24 (1x2x3x4)
lowest_product("35") --> "Number is too small"

题目难度:一般
题目来源:https://www.codewars.com/kata/554e52e7232cdd05650000a0

def lowest_product(input: str) -> int:
    # your code here

assert lowest_product("123456789") == 24
assert lowest_product("987654321") == 24
assert lowest_product("234567899") == 120
assert lowest_product("2345611117899") == 1
assert lowest_product("2305611117899") == 0
assert lowest_product("9999998999999") == 5832
assert lowest_product("333") == "Number is too small"
assert lowest_product("1234111321") == 3

    public static String lowest_product(String str){
        if(str.length()<4){
            return "Number is too small";
        }
        String[] s = str.split("");
        List ins = new ArrayList<Integer>();
        for (int i = 0; i < s.length - 3; i++) {
            ins.add( tansferInt(s[i])  * tansferInt(s[i+1]) * tansferInt(s[i+2]) * tansferInt(s[i+3]));
        }
        return (String) Collections.min(ins);
    }

    public static int tansferInt (String s){
        return Integer.parseInt(s);
    }
from functools import reduce
def lowest_product(input: str) -> int:
    if len(input) >= 4:
        result = []
        for i in range(len(input) - 3):
            list_new = input[i:i + 4]
            result.append(reduce((lambda x, y: int(x) * int(y)), list_new))
        return min(result)
    else:
        return "Number is too small"

assert lowest_product("123456789") == 24
assert lowest_product("987654321") == 24
assert lowest_product("234567899") == 120
assert lowest_product("2345611117899") == 1
assert lowest_product("2305611117899") == 0
assert lowest_product("9999998999999") == 5832
assert lowest_product("333") == "Number is too small"
assert lowest_product("1234111321") == 3
1 个赞
def lowest_product(input: str) -> int:
	l = list(input)
	l2=[]
	i = 0
	if len(input)>=4:
		while i + 3 <len(l):
			mult1 =int(l[i]) * int(l[i + 1] )* int(l[i + 2] )*int(l[i + 3])
			i=i+1
			l2.append(mult1)
		return min(l2)
	else:
		return "Number is too small"

assert lowest_product("123456789") == 24
assert lowest_product("987654321") == 24
assert lowest_product("234567899") == 120
assert lowest_product("2345611117899") == 1
assert lowest_product("2305611117899") == 0
assert lowest_product("9999998999999") == 5832
assert lowest_product("333") == "Number is too small"
assert lowest_product("1234111321") == 3
def lowest_product(input: str) -> int:
    # your code here
    from functools import reduce
    res = []
    num_list = list(input)
    if len(num_list) < 4:
        return "Number is too small"
    for i in range(len(num_list)-3):
        res.append(reduce(lambda x,y:x*y,[int(i) for i in num_list[i:i+4]]))
    return min(res)

assert lowest_product("123456789") == 24
assert lowest_product("987654321") == 24
assert lowest_product("234567899") == 120
assert lowest_product("2345611117899") == 1
assert lowest_product("2305611117899") == 0
assert lowest_product("9999998999999") == 5832
assert lowest_product("333") == "Number is too small"
assert lowest_product("1234111321") == 3
# 使用滑动窗口
def lowest_product(input):
    if len(input) < 4:
        return "Number is too small"
    if "0" in input:
        return 0
    left = 0
    right = 0
    import math
    res = math.inf
    cur_res = 1
    while(right < len(input)):
        cur = int(input[right])
        right += 1
        cur_res *= cur
        if right - left == 4:
            res = min(cur_res,res)
            cur_res /= int(input[left])
            left += 1
    return res
def lowest_product(input: str) -> int:
    def accumulate_product(left,right):
        return reduce(lambda x, y: int(x) * int(y), input[left:right])
    if len(input)<4:
        return "Number is too small"
    else:
        left = 0
        right = 4
        min_product = accumulate_product(left,right)
        while right < len(input):
            left += 1
            right += 1
            prod=accumulate_product(left,right)
            if prod<min_product:
                min_product=prod
        return min_product
def lowest_product(input: str) -> int:
    t = lambda x: int(x[0]) * int(x[1]) * int(x[2]) * int(x[3])
    if len(input) < 4:
        return "Number is too small"
    return t(min([input[i:i + 4] for i in range(0, len(input) - 3)], key=t))


if __name__ == '__main__':
    assert lowest_product("123456789") == 24
    assert lowest_product("987654321") == 24
    assert lowest_product("234567899") == 120
    assert lowest_product("2345611117899") == 1
    assert lowest_product("2305611117899") == 0
    assert lowest_product("9999998999999") == 5832
    assert lowest_product("333") == "Number is too small"
    assert lowest_product("1234111321") == 3
def lowest_product(input: str) -> int:
    if len(input) >= 4:
        if '0' in input:
            return 0
        else:
            lresult = []
            while len(input) - 3 > 0:
                result = 1
                for i in range(4):
                    result *= int(input[i])
                lresult.append(result)
                input = input[1:]
                #print(lresult)
            return min(lresult)
    else:
        return "Number is too small"


#print(lowest_product("123456789"))
assert lowest_product("123456789") == 24
assert lowest_product("987654321") == 24
assert lowest_product("234567899") == 120
assert lowest_product("2345611117899") == 1
assert lowest_product("2305611117899") == 0
assert lowest_product("9999998999999") == 5832
assert lowest_product("333") == "Number is too small"
assert lowest_product("1234111321") == 3
def lowest_product(input: str):
    res_list=[]
    if len(input)<4:
        return "Number is too small"
    else:
        word_list=[input[i:i+4] for i in range(len(input)-3)]
        for word in word_list:
            s=1
            for i in range(len(word)):
                s*=int(word[i])
            res_list.append(s)
        return min(res_list)

assert lowest_product("123456789") == 24
assert lowest_product("987654321") == 24
assert lowest_product("234567899") == 120
assert lowest_product("2345611117899") == 1
assert lowest_product("2305611117899") == 0
assert lowest_product("9999998999999") == 5832
assert lowest_product("333") == "Number is too small"
assert lowest_product("1234111321") == 3