跳至主要內容

UIC All QUIZ

AI悦创原创Python 一对一教学uicUIC Information SpacecodingbatPython 一对一教学uicUIC Information Spacecodingbat大约 28 分钟...约 8509 字

Chapter 2 - Quiz

This is the in-class quiz for Chapter 2. Your solution can account for maximum 2.5% of your final grade.

这是第2章的课堂测验。你的答案最多可以占你最终成绩的2.5%。

Note

  • Write your code after you see # YOUR CODE HERE

    在看到 # YOUR CODE HERE 后编写你的代码。

  • Read the instruction of each question. You have a limited time to submit: Tue March 7, h. 17:00. Only your last submission counts.

    请阅读每个问题的说明。你有有限时间提交:3月7日星期二,下午17:00。只有你的最后一次提交有效。

  • Copying the solution of other students is forbidden.

    禁止抄袭其他学生的解答。

  • This quiz will be auto-graded. The auto-grading will check that your answers to the question is correct (or close to be correct). If your submission fails the auto-grade, you will get 0.

    本次测验将采用自动评分方式。自动评分将检查您对问题的回答是否正确(或接近正确)。如果您的提交未通过自动评分,您将得零分。

Exercise 1

Exercise 1. Create a two-variable addition calculator cal_sum(a, b) that returns the sum of the two variables.

Exercise 1: 创建一个双变量加法计算器 cal_sum(a, b),它会返回这两个变量的和。

def cal_sum(a, b):
    # YOUR CODE HERE
题目
# In order to make sure your program works, testcases are written and executed.
# Here is an example testcase that you might write for testing.

# In python assert keyword helps in debugging. 
# This statement simply takes input a boolean condition, which doesn’t return anything when it's true,
# but if it is computed to be false, then it raises an AssertionError along with the optional message provided. 

assert cal_sum(1, 2) == 3

Exercise 2

EN

Exercise 2. In a function FullName(name, capital=True),

  • a. Create a string variable str1 that stores the string "Full Name: "

  • b. if capital == True, use upper() function to convert name into Uppercase letters and assign this to a variable NewName

  • c. if capital == False, use title() function to convert the first character of each word in name to Uppercase letter and the other characters in lower case and assign this to a variable NewName

  • return the string "Full Name: " + NewName

Example

FullName("guido vAn rossum", capital = True)
Expected output: 'Full Name: GUIDO VAN ROSSUM'


FullName("guido vAn rossum", capital = False), 
Expected output: 'Full Name: Guido Van Rossum'
def FullName(name, capital=True):
    str1 = "Full Name: "
    # YOUR CODE HERE

Exercise 3

EN

Exercise 3. In a function first_word(arg1), use split() function to separate the argument arg1 by "," and return the first word.

Example

first_word('Hello, world!')

Expected output:

'Hello'
def first_word(arg1):
    # YOUR CODE HERE

Exercise 4

EN

Exercise 4. In a function clean_string(arg1),

  • use strip() function to remove trailing and ending white spaces from "arg1", and
  • capitalize the first character.
  • return the output.

Example

clean_string("   having fun in Python   ")

Expected output:

"Having fun in Python"
def clean_string(arg1):
    # YOUR CODE HERE

Exercise 5

EN

Exercise 5. In a function first_last(arg1), return the a+b, where a is the first character and b is the last character of arg1.

Example

clean_string("My Name is Andrew")

Expected output:
"Mw"

Hint

  • The function len() gives you the length of the list. For example, len('abcd') returns 4. So, the last element of arg1 is therefore indexed at len(arg1) - 1.
def first_last(arg1):  
    # YOUR CODE HERE

Exercise 6

EN

Exercise 6. In a function volume_c(radius, height),

  • Create a variable vol to calculate the volume of cone, assuming pi is equal to 3.14. Given the formula for volume of cone is: $ volume = \frac{1}{3}, \pi r^{2}h $, where r is the radius and h is the height.

  • return vol

def volume_c(radius, height):
    # YOUR CODE HERE

Chapter 3 - Quiz 3

This is the in-class quiz for Chapter 3. Your solution can account for maximum 2.5% of your final grade.

Note

  • Write your code after you see # YOUR CODE HERE
  • Read the instruction of each question. You have a limited time to submit: Tuesday, Mar 14, h. 17:00. Only your last submission counts.
  • Copying the solution of other students is forbidden.
  • This quiz will be auto-graded. The auto-grading will check that your answers to the question is correct (or close to be correct). If your submission fails the auto-grade, you will get 0.

Question 1

EN

Define a function divisible_by_2(num). The function checks if a number is divisible by 2 and returns True if it is and False otherwise.

Example

divisible_by_2(10) -> True
divisible_by_2(15) -> False

Hint

  • if a%2 == 0 is True, then the variable a is divisible by 2.
def divisible_by_2(num):
    # YOUR CODE HERE

Question 2

EN

Question 2. Define a function isAdult(age). The function checks if a person is an adult or not (age >= 18 is adult). It returns True if a person is an adult and False otherwise.

Example

isAdult(10) -> False
isAdult(18) -> True
def isAdult(age):
    # YOUR CODE HERE

Question 3

EN

Question 3. Define a function letter_grade(grade), where grade is a float argument.

  • If grade is above or equal to 85, assign "A" to the variable lettergrade
  • If grade is between 65 (included) and 85 (excluded), assign "B" to the variable lettergrade
  • If grade is between 45 (included) and 65 (excluded), assign "C" to the variable lettergrade
  • If grade is between 25 (included) and 45 (excluded), assign "D" to the variable lettergrade
  • If grade is below 25, assign "F" to the variable lettergrade
  • Returns the variable lettergrade
def letter_grade(grade):
    lettergrade = ''
    # YOUR CODE HERE

Question 4

EN

Question 4. Define a function cumsum(num). The function returns the sum of all positive integers smaller or equal to num.

Example

cumsum(4) -> 1 + 2 + 3 + 4 = 10
cumsum(5.5) -> 1 + 2 + 3 + 4 + 5 = 15

Hint

  • int() converts a float type value into an interger.
def cumsum(num):
    # YOUR CODE HERE

Question 5

EN

Question 5. Define a function even_sum(num), where num is a positive number. The function iterates all the integers numbers from 0 to num, sums them up if they are even numbers, and returns the sum.

Example

even_sum(4) -> 2 + 4 = 6
even_sum(8.5) -> 2 + 4 + 6 + 8 = 20
def even_sum(num):
    # YOUR CODE HERE

Question 6

EN

Question 6. Define a function num_char_before_period(sentence). The function returns the number of characters in a given string before encountering the symbol ".", using while loop. Assume the argument sentence always include a period "."
Example

num_char_before_period('I love python.') -> 13
num_char_before_period('I hate you. I love python.') -> 10
num_char_before_period('...') -> 0

Hint

  • != denotes not equal.
  • the element on index i can be accessed by using sentence[i].
  • len(sentence) gives the number of characters in a the sentence
def num_char_before_period(sentence):
    # YOUR CODE HERE

Question 7

EN

Question 7. Define a function remove_vowel(string1). The function returns a new string with all vowels removed. Vowels are any of a, e, i, o, u and A, E, I, O, U.

Example

remove_vowel('python') -> 'pythn'
remove_vowel('I love ice cream') -> ' lv c crm'

Hint

  • you can check if a character or an element (not) in a string or a list by using in, or not in.
def remove_vowel(string1):
    vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
    output = ""
    # YOUR CODE HERE

Chapter 4 - Quiz 4

This is the in-class quiz for Chapter 4 (Data Structures Part 1) for students of session 1004. Your solution can account for maximum 2.5% of your final grade.

Note

  • Write your code after you see # YOUR CODE HERE
  • Read the instruction of each question. You have a limited time to submit: Tue 21 Mar, h.17:00. Only your last submission counts.
  • Copying the solution of other students is forbidden.
  • This quiz will be auto-graded. The auto-grading will check that your answers to the question is correct (or close to be correct). If your submission fails the auto-grade, you will get 0.

Question1

EN

Question1. Define a function first_last(listA) that returns a list with the first and last element of listA interchanged.

Example

first_last([1, 2, 3]) -> [3, 2, 1]
first_last(["a", "b", "c", "d"]) -> ["d", "b", "c", "a"]

Hint

  • To create a list with the same dimension of list1
n = len(listA)
list0 = [0]*n
def first_last(listA):
    n = len(listA)
    list0 = [0]*n
    # YOUR CODE HERE

Question2

EN

Question2. Define a function odd_numbers(). The function iterates all the numbers from 0 to 20 using a for loop and returns all the odd numbers in a list.

Hint

  • list.append() is a built-in method in Python to store items into a list.
def odd_numbers():
    res = []
    # YOUR CODE HERE

Question3

EN

Question3. Define a function group_student(set1, set2, left = True). The function returns a set:

1. Students who are in `set1` but not in `set2`, if `left=True` 
2. Students who are in both `set1` and `set2` at the same time, if `left=False` 

Hint

  • Use set1 = {...} to create a set.
  • a - b returns items in a but not in b
  • a & b returns items in both a and b

Example

python = {"Alice", "Bob", "Fiona", "Luke", "Tom"}
econ = {"Alice", "Blaire", "Courtney", "David", "Luke"}
group_student(python, econ) -> {'Bob', 'Fiona', 'Tom'}
def group_student(set1={"Alice","Bob","Fiona","Luke","Tom"}, set2={"Alice","Blaire","Courtney","David","Luke"}, left = True):
    # YOUR CODE HERE

Question4

EN

Write a function called tuple_stats that takes in a tuple of integers as input and returns a tuple containing the following statistics:

  1. The minimum value in the tuple.
  2. The maximum value in the tuple.
  3. The sum of all the values in the tuple.
  4. The mean of all the values in the tuple.

Example

tup1 = (3, 5, 1, 8, 2)
tuple_stats(tup1) -> (1, 8, 19, 3.8)

Hint

  • To find the minimum and maximum values in a tuple, you can use the built-in min and max functions in Python. For example, min((3, 5, 1, 8, 2)) will return 1.

  • To find the sum of all the values in a tuple, you can use a loop to iterate through each element in the tuple and add it to a running total. You can start the total at 0 and update it with each iteration of the loop.

  • To find the mean of all the values in a tuple, you can divide the sum of all the values by the length of the tuple. You can use the len function to find the length of the tuple.

def tuple_stats(tup):
    # YOUR CODE HERE

Question5

EN

Question5. Write a function called count_vowel that takes in a string and return the number of unique vowels forming the string. This function is case sensitive, such that the maximum number of vowel is 10.

Example

count_vowel("Hello World") -> 2
count_vowel("AAA nsn") -> 1
count_vowel("ll cnsnt") -> 0
count_vowel("aAeE") -> 4

Hint

vowel = ["a","e","i","o","u", "A","E","I","O","U"]
`set()` # convert a list to set, where element are unique 
def count_vowel(str_arg):
    # YOUR CODE HERE

Chapter Quiz 5

This is the in-class quiz for Chapter 5 (Data Structures Part 2) for students of session 1004. Your solution can account for maximum 2.5% of your final grade.

Note

  • Write your code after you see # YOUR CODE HERE
  • Read the instruction of each question. You have a limited time to submit: Tue 28 Mar, h.17:00. Only your last submission counts.
  • Copying the solution of other students is forbidden.
  • This quiz will be auto-graded. The auto-grading will check that your answers to the question is correct (or close to be correct). If your submission fails the auto-grade, you will get 0.

Question 1

EN

Question 1. Define a function manipulate_list(list1) that returns a new list with 1 added to every element using list comprehension.

Example

list1 = [1, 3, 2, 5, 8, 7]

Expected output:
[2, 4, 3, 6, 9, 8]

Hint

  • List comprehension syntax:
[expression for variable in list]
[expression for variable in list if test_expression]
公众号: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
评论
  • 按正序
  • 按倒序
  • 按热度