0% found this document useful (0 votes)
15 views5 pages

Homework01

Uploaded by

haotian.sheng001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Homework01

Uploaded by

haotian.sheng001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Homework01

1. Review the built-in function of Python


1.1 len()

When you wanna to calcute how many items(elements) in the List or String

data = [1,3,5,7]
hello = 'hello'
n = len(data)
m = len(hello)
print('length of data: ', n) #4, four eles in the variable of data
print('length of hello: ', m) #5, five letter in the variable of hello

1.2 max()

When you wanna get the biggest num form List or two variable

data = [7, 2, 5, 9, 1]
print('the biggest num of data is :', max(data))

a = 10
b = 15
print('the biggest num from a and b is :', max(a, b))

1.3 input()

Tips: the data type of input from console always String , Don't forget to convert to the appropriate data
type, such as integer.

This function is usually used with split function.

For example, split the input string by specific letter ',' , and convert the string number to float number
one by one based on for loop

raw_input_data = input() # '1.1,2.2,3.3,4.4'


input_data_lst = raw_input_data.split(',') # ['1.1','2.2','3.3','4.4']
n = len(input_data_lst)
for i in range(n):
input_data_lst[i] = float(input_data_lst[i])
print(input_data_lst) # [1.1, 2.2, 3.3, 4.4]
1.4 range()

This function is usually used with for loop and len() function to traversal the element by index from
List or String

data = [1, 3, 5, 7]
for i in range(len(data)):
print(i, data[i])

hello = 'hello'
for j in range(len(hello)):
print(j, hello[j])

Also, it could generate the List of interge with List function.

# Do not forget use list function converting the data type to list
# including the start, excluding the end
a = list(range(5)) # range(start=0,end=5,step=1) => [0,1,2,3,4]
b = list(range(3, 5)) # range(start=3,end=5,step=1 =>[3,4]
c = list(range(3, 10, 2)) # range(start=3,end=10,step=2 =>[3,5,7,9]
print(a)
print(b)
print(c)

2. Review the loop function


When it comes to loops, we should master the use of for loops and while loops.

Keep in your mind, how to control the Loop by using the key word of continue and break or return

The continue is used when you wanna to skip the current loop

The break or return is used to terminate the whole loop, jump out of the whole loop.

2.1 for Loop

You do not need to control the pointer move to next.

2.1.1 Traversal item by index (use with range function)

students = ['Bobby', 'Sam', 'Alvin']


for ele in students:
print(ele)

2.1.2 Trabersal item by value


students = ['Bobby', 'Sam', 'Alvin']
for i in range(len(students)):
print(students[i])

2.1.3 with continue and break

1. Try to learn the use of format function, to print complicated informations.

2. Try to analysis the output to fully understand the contine and break .

3. Detect the num either odd num or even, we use the num mod (%) 2 to verify it.

for i in range(10):
if i > 7:
break
if i == 5 or i == 3:
continue
if i % 2 == 0:
print('the num={} is odd'.format(i))
else:
print('the num={} is even'.format(i))

2.2 while Loop

You have to control the pointer move to next.

2.2.1 Traversal item by index

students = ['Bobby', 'Sam', 'Alvin']


n = len(students)
i = 0
while i < n:
print(students[i])
i = i + 1

2.2.2 with continue and break


i = -1
while i < 10:
i = i + 1
if i > 7:
break
if i == 5 or i == 3:
continue
if i % 2 == 0:
print('the num={} is odd'.format(i))
else:
print('the num={} is even'.format(i))

3. Challenge
Do not search the answer from online or ask ChatGPT !!!

Try to think by yourself, or discuss your idea with your friends or classmates!!!

Do not search the answer from online or ask ChatGPT !!!

Try to think by yourself, or discuss your idea with your friends or classmates!!!

Do not search the answer from online or ask ChatGPT !!!

Try to think by yourself, or discuss your idea with your friends or classmates!!!

3.1 Task1
Given the list of interger data , return the top2 biggest num which form a new list of [biggest_num,
second_biggest_num]

Constraints: the length of data great than 2

Example1

Input: data = [8,2,1,9,5,6]

Output: [9,8] #biggest is 9, second biggest is 8, so return the [9,8]

Example2

Input: data = [2,8]

Output: [8,2] #biggest is 8, second biggest is 2, so return the [8,2]

Example3

Input: data = [1,0]


Output: [1,0] #biggest is 1, second biggest is 0, so return the [1,0]

def get_top2_biggest_num(data=[]):
return xxx

print(get_top2_biggest_num(data=[8, 2, 1, 9, 5, 6])) #[9, 8]


print(get_top2_biggest_num(data=[2, 8])) #[8, 2]
print(get_top2_biggest_num(data=[1, 0])) #[1, 0]

3.2 Task2
Given the list of interge data , and a interger flag , if flag =1, then return the list which contains all
odd num, if flag =0, then return the list which contains all even num , otherwise, return the original list.

Constraints: the length of data great than 1

Example1

Input: data = [8,2,1,9,5,6], flag = 1

Output: [1,9,5] # 1,9,5 are all odd num, so return [1,9,5]

Example2

Input: data = [8,2,1,9,5,6], flag = 0

Output: [8,2,6] # 8,2,6 are all even num, so return [8,2,6]

Example3

Input: data = [8,2,1,9,5,6], flag = 2

Output: [8,2,1,9,5,6] # keep the same as input

def format_list(data=[], flag=-1):


return xxx

print(format_list(data=[8, 2, 1, 9, 5, 6], flag=1)) #[1, 9, 5]


print(format_list(data=[8, 2, 1, 9, 5, 6], flag=0)) #[8, 2, 6]
print(format_list(data=[8, 2, 1, 9, 5, 6], flag=3)) #[8, 2, 1, 9, 5, 6]

You might also like