Monday, November 19, 2018

Q14: lowest product of 4 consecutive digits in a number

Question 14: Create a function that returns the lowest product of 4 consecutive digits in a number given as a string.

This should only work if the number has 4 digits or more. If not, return "Number is too small".

##Example

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

2 comments:

  1. # To find lowest product of 4 consecutive digits in a number
    def lowest_product(value):
        t = [int(i) for i in set(value)]
        if len(t) < 4:
            return "Number is too small"
        else:
            t.sort()
            k = 1
            for j in t[:4]: k = k * j
            return "{0} -> {1}".format(t[:4], k)


    print(lowest_product("123456789"))
    print(lowest_product("35"))

    ReplyDelete
  2. import numpy
    def lowest_product(num):
    list_num = [int(x) for x in num]
    len_num = len(list_num)
    if len_num<4:
    return "Number is too small"
    return min([numpy.prod(list_num[i:i+4]) for i in range(len_num) if i+4<=len_num])

    ReplyDelete