What is the optimal Jewish toenail cutting algorithm?

前端 未结 6 1884
暖寄归人
暖寄归人 2020-12-22 18:52

I am working on the software for a machine that will automatically trim toenails, so that users can simply put their feet in it and run it instead of having to manually do i

6条回答
  •  太阳男子
    2020-12-22 19:29

    There's really no reason to introduce randomness into this problem. There are only 14 sequences that satisfy this problem, and surely some ordering of those sequences would best satisfy the aesthetic sense you're trying to accommodate. Thus, you should just reduce this problem to an algorithm for picking a sequence from those 14, probably in a pre-set order.

    Javascript implementation of algorithm for finding the 14:

    function swap (a, i, j) {
      var temp = a[i]
      a[i]=a[j]
      a[j]=temp
    }
    
    function permute (b, n, a) {
      if (n==4) {
        b.push(a.slice(0)) //copy array
      }
      else {
        for (var i = n; i < 5; i++) {
          swap(a,n,i)
          permute(b, n+1, a)
          swap(a,n,i)
        }
      }
    }
    
    var a = [1,2,3,4,5]
    var b = []
    var c = []
    
    permute(b,0,a)
    
    for (var i = 1; i < b.length-1; i++) {
      var good = true
      for (var j = 0; j < b[i].length; j++) {
        if (Math.abs(b[i][j-1]-b[i][j]) < 2 || Math.abs(b[i][j]-b[i][j+1]) < 2) {
          good = false
        }
      }
      if (good) c.push(b[i].join(''))
    }
    
    console.log(c)
    

    EDIT: The new requirement that the sequences have to be generated randomly cannot be easily met. The best you can probably do is to generate them pseudorandomly, which is just as deterministic as hard-coding them ahead of time, and so should not satisfy anyone's superstitions.

提交回复
热议问题