跳至主要內容

Programming Assignment 1

AI悦创原创2023年4月8日Python 一对一教学uicUIC Information SpacePython 一对一教学uicUIC Information Space大约 6 分钟103约 1832 字

1. 选择题

1. 「C」

2. 「B」

3. 「D」

4.「C」

5.「D」

6.「B 」

7. 「C」

8. 「A」

image-20230408105345224
image-20230408105345224

9. 「A」

10. 「B」

11. 「A」

image-20230408105804973
image-20230408105804973

12. 「C」

13. 「D」

14. 「D」

15. 「C」

16.「A」

17. 「B」

In [13]: for i in s.split():
    ...:     print(i, end=", ")
    ...:
my, name, is, x,

18. 「C」

In [14]: "abcd"[2:]
Out[14]: 'cd'

19. 「B」

20. 「D」

image-20230408105159191
image-20230408105159191

2. Code

This is the Part 2 of your Assignment 1. Together with the Part 1 multiple choice questions on iSpace, it accounts for maximum 10% of the final grade.

这是作业1的第二部分。与iSpace上的第一部分多项选择题一起,它们最多占据期末成绩的10%。

Note

Question 1

Define the function remove_special(string1) that returns the string1 with the characters ['?', '!'] removed.

定义函数remove_special(string1),该函数返回删除字符['?', '!']后的string1

Examples

remove_special("python is such a fun!") -> "python is such a fun"
remove_special("?18?? UIC!") -> "18 UIC"
def remove_special(string1):
    # YOUR CODE HERE

Answer

def remove_special(string1):
    # YOUR CODE HERE
    return string1.replace("?", "").replace("!", "")
def remove_special(string1):
    # YOUR CODE HERE
    return string1.replace("?", "").replace("!", "")


print(remove_special("python is such a fun!"))
print(remove_special("?18?? UIC!"))

Question 2

Given day of the week encoded as 0 = Sun, 1 = Mon, 2 = Tue, ... 6 = Sat, and a boolean vacation indicating if we are on vacation, write the function alarm_clock(day, vacation).

给定一周的某个day,其中0 = 周日,1 = 周一,2 = 周二,... 6 = 周六,以及一个布尔值vacation,表示我们是否在度假,编写函数alarm_clock(day, vacation)

Examples

alarm_clock(3, False) -> "7:00"
alarm_clock(6, False) -> "10:00"
alarm_clock(1, True) -> "10:00"
alarm_clock(6, True) -> "off"

Answer

Question 3

Define the function is_prime(num) to check if given positive integer is a prime number or not. The function returns a boolean True if a number is divisible only by 1 and itself, and returns a boolean False if it is divisible by any other number than 1 or itself. 1 is considered as a prime number. 2 is also a prime number.

定义函数is_prime(num),用于检查给定的正整数是否是质数。如果一个数仅能被1和它本身整除,该函数返回布尔值True,否则,如果它可以被1或它本身以外的其他数整除,该函数返回布尔值False。1被认为是质数,2也是质数。

Examples

is_prime(7) -> True
is_prime(100) -> False
is_prime(1) -> True
is_prime(2) -> True

Answer

def is_prime(num):
    # YOUR CODE HERE
    if num < 1:
        return False
    for i in range(2, num):
        if num % i == 0:
            return False
    return True


print(is_prime(7))
print(is_prime(100))
print(is_prime(1))
print(is_prime(2))

Question 4

Define the function second_highest(list_arg) that returns the second highest number in the list argument list_arg. The input list has at least two unique numbers.

定义函数second_highest(list_arg),该函数接受一个列表参数list_arg,并返回该列表中第二大的数字。输入列表至少包含两个不同的数字。

Examples

second_highest([1,2,3,4,5]) -> 4
second_highest([1,2,3,5,5]) -> 3

Answer

Question 5

Define the function intersect_list(list1, list2) that returns a list whose element are common elements between list1 and list2 without duplicates. You may want to convert list1 and list2 to set first.

定义一个函数 intersect_list(list1, list2),返回一个列表,其中的元素是 list1list2 之间的公共元素,且不包含重复元素。你可能需要先将 list1list2 转换为集合。

Examples

list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
intersect_list(list1, list2) -> [5]

list3 = [1, 2, 3, 4, 5, 5, 6, 7]
list4 = [5, 5, 6, 7, 8, 9]
intersect_list(list3, list4) -> [5, 6, 7]

Answer

公众号:AI悦创【二维码】

AI悦创·编程一对一

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh

C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh

方法一:QQ

方法二:微信:Jiabcdefh

你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
通知
关于编程私教&加密文章