zoukankan      html  css  js  c++  java
  • 移动函数

    一个函数,有两个输入分别为一个链表和一个整数,完成这个函数使得链表中的数值按照给定整数向左或向右移动,如整数为正,则向右移动,若为负则向左移动。

    #Program a function that returns a new distribution 
    #q, shifted to the right by U units. If U=0, q should 
    #be the same as p.
    
    import copy
    p = [0, 1, 0, 0, 0]
    world = ['green', 'red', 'red', 'green', 'green']
    measurements = ['red', 'green']
    pHit = 0.6
    pMiss = 0.2
    
    
    def sense(p, Z):
        q = []
        for i in range(len(p)):
            hit = (Z == world[i])
            q.append(p[i] * (hit * pHit + (1 - hit) * pMiss))
        s = sum(q)
        for i in range(len(q)):
            q[i] = q[i] / s
        return q
    
    
    def move(p, U):
        #
        # ADD CODE HERE
        #
        #print (p)
        q=copy.copy(p)
        for i in range(len(p)):
            a.append(p[(i-U)%len(p)])
    
        return q
    
    
    print move(p, 1)
    

      输出:

    [0,0,1,0,0]
    

      精确移动

    非精确移动

    非精确移动的代码实现:

    #Modify the move function to accommodate the added
    #probabilities of overshooting or undershooting
    #the intended destination.

    p=[0, 1, 0, 0, 0]
    world=['green', 'red', 'red', 'green', 'green']
    measurements = ['red', 'green']
    pHit = 0.6
    pMiss = 0.2
    pExact = 0.8
    pOvershoot = 0.1
    pUndershoot = 0.1

    def sense(p, Z):
    q=[]
    for i in range(len(p)):
    hit = (Z == world[i])
    q.append(p[i] * (hit * pHit + (1-hit) * pMiss))
    s = sum(q)
    for i in range(len(q)):
    q[i] = q[i] / s
    return q

    def move(p, U):
       q=[]
       for i in range(len(p)):
    s=pExact*p[(i-U)%len(p)]
           s=s+pOvershoot*p[(i-U-1)%len(p)]
           s=s+pUndershoot*p[(i-U+1)%len(p)]
    q.append(s)
        return q 
    print move(p, 1)

      

  • 相关阅读:
    CNZZ友盟访问明细的采集办法
    实时采集新加坡交易所A50指数
    深入理解DIP、IoC、DI以及IoC容器
    PHP依赖注入(DI)和控制反转(IoC)详解
    PHP 的命令行模式
    浅析PHP中的闭包和匿名函数
    PHP_CodeSniffer 使用攻略
    PSR规范学习笔记
    Composer 的简介、安装及使用
    win7下使用手动安装composer
  • 原文地址:https://www.cnblogs.com/fuhang/p/8820445.html
Copyright © 2011-2022 走看看