跳至主要內容

NYU CS-UY 1134 Lab1

AI悦创原创python 1v1NYU – Tandon School of Engineering大约 4 分钟...约 1346 字

Vitamins (10 minutes)

Write the output for the following lines of code given the Student class. (10 minutes).

为以下代码写输出,给定的是Student类。(10分钟)。

原代码
class Student:
    def __init__(self, name="student", age=18):
        self.name = name
        self.age = age
        self.courses = []

    def add_course(self, course):
        self.courses.append(course)

    def remove_course(self, course):
        if course in self.courses:
            self.courses.remove(course)
            print("Removed Course:", course)
        else:
            print("Course Not Found:", course)

    def __repr__(self):  # str representation needed for print( )
        info = "Name: " + self.name
        info += "\nAge: " + str(self.age)
        info += "\nCourses: " + " ,  ".join(self.courses)
        return info + "\n"
Q1
peter = Student(16)
print(peter.name, peter.age)

# out
16 18

Coding

In this section, it is strongly recommended that you solve the problem on paper before writing code. This will be good practice for when you write code by hand on the exams.

  1. Implement the following function (30 minutes):
def can_construct(word, letters): """
      word - type: str
      letters - type: str
      return value - type: bool
      """

This function is passed in a string containing a word, and another string containing letters in your hand. When called, it will return True if the word can be constructed with the letters provided; otherwise, it will return False.

Notes:

  • Each letter provided can only be used one.
  • You may assume that the word and letters will only contain lower-case letters.
  • You may not use a dictionary for this question.
  • Hint : Try to think about how you can use a list to implement a dictionary
ex) can_construct("apples", "aples") will return False.
ex) can_construct("apples", "aplespl") will return True.
详情
def can_construct(word, letters): 
    """
      word - 类型: str
      letters - 类型: str
      返回值 - 类型: bool
    """

这个函数会传入一个包含单词的字符串和另一个包含你手里的字母的字符串。当被调用时,如果可以用所提供的字母构建该单词,它将返回True;否则,它将返回False。

注意:

  • 每个提供的字母只能使用一次。
  • 您可以假设 word 和 letters 只包含小写字母。
  • 对于这个问题,您不能使用字典。
  • 提示: 尝试思考如何使用列表来实现字典
例如 can_construct("apples", "aples") 将返回False。
例如 can_construct("apples", "aplespl") 将返回True
def can_construct(word, letters):
    # 将letters的每个字符转化为列表
    letters_list = list(letters)
    
    # 对于word中的每个字符,检查它是否在letters_list中
    for char in word:
        if char in letters_list:
            # 如果字符在letters_list中,从中移除该字符
            letters_list.remove(char)
        else:
            # 如果字符不在letters_list中,返回False
            return False
    
    # 如果所有字符都在letters_list中,返回True
    return True
公众号:AI悦创【二维码】

AI悦创·编程一对一

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

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

方法一:QQopen in new window

方法二:微信:Jiabcdefh

上次编辑于:
贡献者: AndersonHJB
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度