查找通过从Python中的数字中删除最小位数而形成的最大多维数据集

假设我们有一个数字N,我们必须确定可以通过从数字中删除最小位数(可能为0)来生成的最大正方体。我们可以删除给定数字中的任何数字以达到目标。众所周知,如果N = M ^ 3(对于某些整数M),则数字N被称为理想立方体。

因此,如果输入类似于806,则输出将为8,因为我们可以从数字中删除0和6,那么我们将得到8,这是2的理想立方体。

为了解决这个问题,我们将遵循以下步骤-

  • 定义一个功能preProcess()。这将花费n

  • temp_cubes:=一个新列表

  • 对于范围1至上限n ^(1/3)的i

    • 立方= i ^ 3

    • cubeString:=多维数据集作为字符串

    • 在temp_cubes的末尾插入cubeString

    • 返回temp_cubes

  • 定义一个功能solve()。这将需要num,temp_cubes

  • 反向temp_cubes

  • totalCubes:= temp_cubes的大小

  • 对于范围为0到totalCubes的i,执行

    • 如果num [j]与temp [index]相同,则

    • 如果digitsInCube与索引相同,则

    • 索引:=索引+ 1

    • 返回温度

    • temp:= temp_cubes [i]

    • digitsInCube:=临时大小

    • 索引:= 0

    • digitsInNumber:= num的大小

    • 对于0到digitsInNumber范围内的j,执行

    • 返回“不可能”

    • 从方法中执行以下操作-

    • temp_cubes:=预处理(n)

    • num:= n作为字符串

    • 回答:= solve(num,temp_cubes)

    • 返回ans

    示例

    让我们看下面的实现以更好地理解-

    import math
    def preProcess(n):
       temp_cubes = list()   for i in range(1, math.ceil(n**(1. / 3.))):
          cube = i**3
          cubeString = str(cube)
          temp_cubes.append(cubeString)
       return temp_cubes
    def solve(num,temp_cubes):
       temp_cubes = temp_cubes[::-1]
       totalCubes = len(temp_cubes)
       for i in range(totalCubes):
          temp = temp_cubes[i]
          digitsInCube = len(temp)
          index = 0
          digitsInNumber = len(num)
          for j in range(digitsInNumber):
          if (num[j] == temp[index]):
             index += 1
          if (digitsInCube == index):
             return temp
       return "Not Possible"
    def getLargestCube(n):
       temp_cubes = preProcess(n)
       num = str(n)
       ans = solve(num, temp_cubes)
       return ans
    n = 806
    print(getLargestCube(n) )

    输入值

    806

    输出结果

    8
    猜你喜欢