Homework 1

Q1: A Plus Abs B

源码:

from operator import add, sub

def a_plus_abs_b(a, b):
    """Return a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    """
    if b < 0:
        f = _____
    else:
        f = _____
    return f(a, b)

实现一个函数,返回 a + b的绝对值,但不使用 abs 函数。

from operator import add, sub

def a_plus_abs_b(a, b):
    """Return a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    """
    if b < 0:
        f = sub
    else:
        f = add
    return f(a, b)

使用python3 ok -q a_plus_abs_b命令,测试结果如下:
请添加图片描述

结果正确。

Q2: Two of Three

源码:

def two_of_three(a, b, c):
    """Return x*x + y*y, where x and y are the two largest members of the
    positive numbers a, b, and c.

    >>> two_of_three(1, 2, 3)
    13
    >>> two_of_three(5, 3, 1)
    34
    >>> two_of_three(10, 2, 8)
    164
    >>> two_of_three(5, 5, 5)
    50
    """
    return _____

实现一个从初始值为a,b,c中选择最大的两个数字x,y,返回值为xx + yy
的函数。

def two_of_three(a, b, c):
    """Return x*x + y*y, where x and y are the two largest members of the
    positive numbers a, b, and c.

    >>> two_of_three(1, 2, 3)
    13
    >>> two_of_three(5, 3, 1)
    34
    >>> two_of_three(10, 2, 8)
    164
    >>> two_of_three(5, 5, 5)
    50
    """
    return a*a + b*b + c*c - pow(min(a, b, c), 2)

使用命令python3 ok -q two_of_three测试代码如下:
请添加图片描述
运行结果正确。

Q3: Largest Factor

源码:

def largest_factor(n):
    """Return the largest factor of n that is smaller than n.

    >>> largest_factor(15) # factors are 1, 3, 5
    5
    >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
    40
    >>> largest_factor(13) # factor is 1 since 13 is prime
    1
    """
    "*** YOUR CODE HERE ***"

实现一个能找到比 n 小的最大的因子的函数。

def largest_factor(n):
    """Return the largest factor of n that is smaller than n.

    >>> largest_factor(15) # factors are 1, 3, 5
    5
    >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
    40
    >>> largest_factor(13) # factor is 1 since 13 is prime
    1
    """
    cnt = n
    while cnt > 0:
        cnt = cnt - 1
        if n % cnt == 0:
            print(cnt)
            break

使用命令:python3 ok -q largest_factor获得测试结果如下:
请添加图片描述
运行结果正确。

Q4: If Function vs Statement

源码:

def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.

    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """
    if condition:
        return true_result
    else:
        return false_result

实现一个与if语句功能相同的函数。(当分别输入with_if_function和with_if_statement 时,其中一个返回1,但是另外一个不是)

def with_if_statement():
    """
    >>> with_if_statement()
    1
    """
    if c():
        return t()
    else:
        return f()

def with_if_function():
    return if_function(c(), t(), f())

def c():
    "*** YOUR CODE HERE ***"

def t():
    "*** YOUR CODE HERE ***"

def f():
    "*** YOUR CODE HERE ***"

我的答案:


def if_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.

    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """
    if condition:
        return true_result
    else:
        return false_result


def with_if_statement():
    """
    >>> with_if_statement()
    1
    """
    if c():
        return t()
    else:
        return f()

def with_if_function():
    return if_function(c(), t(), f())

def c():
    return 1
def t():
    print(1)
def f():
    print(0)

运行结果如下:
请添加图片描述

Q5: Hailstone

源码:

def hailstone(n):
    """Print the hailstone sequence starting at n and return its
    length.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    "*** YOUR CODE HERE ***"

实现一个打印从 n 开始的冰雹序列并返回其长度的函数。

def hailstone(n):
    """Print the hailstone sequence starting at n and return its
    length.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    cnt = 1
    print(n)
    while n != 1:
        if n % 2:
            n = n * 3 + 1
            print(int(n))
            cnt = cnt + 1
        else:
            n = n / 2
            print(int(n))
            cnt = cnt + 1
    return cnt

使用命令:python ok -q hailstone运行结果如下:
请添加图片描述
结果正确。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
哈工大人工智能原理homework1共有三道题目,首先是关于人工智能的定义和发展历程,其次是关于强化学习和监督学习的区别,最后一个题目是关于逻辑回归和线性回归的比较。 第一题,人工智能的定义和发展历程。人工智能是一门研究如何使计算机的智能实现类似人类的智能的学科。在发展历程中,人工智能经历了符号系统的阶段、连接主义的阶段、统计学习的阶段以及深度学习的阶段。每个阶段都对人工智能的发展产生了推动作用,并且取得了不同的成果。 第二题,强化学习和监督学习的区别。强化学习是一种通过试错来学习的方法,它需要智能体根据环境的奖励和惩罚来调整自己的行为。而监督学习是一种通过输入和输出数据的对应关系来学习的方法,需要有标注的数据作为学习的参考。 第三题,逻辑回归和线性回归的比较。逻辑回归是一种分类算法,它通过sigmoid函数将线性回归的输出映射到[0,1]之间,用于解决二分类问题。而线性回归是一种回归算法,它直接对输入数据进行线性拟合,用于解决连续数值预测的问题。 综上所述,人工智能原理homework1涵盖了人工智能的定义和发展历程、强化学习和监督学习的区别以及逻辑回归和线性回归的比较,通过完成这些题目,可以帮助学生更好地理解人工智能的基本概念和相关算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Balaaam

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值