[SYSU][大二下] 高级编程技术HW Week-2 Lecture-1

Question

教材中课后的练习,3-1到3-11,选一些写到你的博客上


Answer

'''
SYSU - [专选]高级编程技术 - Week2, Lecture1 HW 
by Duan 
2018.03.12
'''  
  
  
'''
3-3 自己的列表:
想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印
一系列有关这些通勤方式的宣言,如"I would like to won a Honda motorcycle."
'''  
commuting = ['walk', 'run', 'ride bike']
for ways in commuting:
	print("I like to", ways, "to university sometimes.")
#Output:
#I like to walk to university sometimes.
#I like to run to university sometimes.
#I like to ride bike to university sometimes.


'''
3-4 嘉宾名单:
如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列
表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。
'''
name_list = ['Feynman', 'Gauss', 'Landau']
for name in name_list:
	print(str(name_list.index(name) + 1) + ".", "\nDear", name)
	print("\tI'd like to invite you to my dinner.")
	print("\t\t\t\t\tYours\n\t\t\t\t\tDuan\n")
#Output:
#1.
#Dear Feynman
#        I'd like to invite you to my dinner.
#                                        Yours
#                                        Duan
#
#2.
#Dear Gauss
#        I'd like to invite you to my dinner.
#                                        Yours
#                                        Duan
#
#3.
#Dear Landau
#        I'd like to invite you to my dinner.
#                                        Yours
#                                        Duan


'''
3-7 缩减名单:
略
'''
name_list = ['Feynman', 'Gauss', 'Landau']
print("Name list:", name_list, '\n')
print("Unfortunately, Gauss says he can't come, so we have to change the list.")
name_list[1] = 'Dirichlet'
print("New name list:", name_list, '\n')
print("WE FIND A LARGER TABLE FOR DINNER!!So we can invite another 3 people.")
name_list.insert(0, 'Newton')
name_list.insert(int(len(name_list) / 2), 'Li Bai')
name_list.append('Planck')
print('New name list:', name_list, '\n')
print("Unfortunately, the new table can't arrive punctually, so only 2 guests will be invited.")

while len(name_list) > 2:
	name_list.pop()
print('New name list:', name_list, '\n')

for name in name_list:
	print(str(name_list.index(name) + 1) + ".", "\nDear", name)
	print("\tI'd like to invite you to my dinner.")
	print("\t\t\t\t\tYours\n\t\t\t\t\tDuan\n")

while len(name_list) != 0:
	del name_list[0]
print("name list:", name_list)
#Output:
# Name list: ['Feynman', 'Gauss', 'Landau']
#
# Unfortunately, Gauss says he can't come, so we have to change the list.
# New name list: ['Feynman', 'Dirichlet', 'Landau']
#
# WE FIND A LARGER TABLE FOR DINNER!!So we can invite another 3 people.
# New name list: ['Newton', 'Feynman', 'Li Bai', 'Dirichlet', 'Landau', 'Planck']
#
# Unfortunately, the new table can't arrive punctually, so only 2 guests will be invited.
# New name list: ['Newton', 'Feynman']
#
# 1.
# Dear Newton
#         I'd like to invite you to my dinner.
#                                         Yours
#                                         Duan
#
# 2.
# Dear Feynman
#         I'd like to invite you to my dinner.
#                                         Yours
#                                         Duan
#
# name list: []


'''
3-8 放眼世界:
略
'''
destination = ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
print("Original list is", destination)
print("1. sorted():\t\t\t", sorted(destination))
print("2. After sorted():\t\t", destination)
print("3. sorted(reverse = Ture):\t", sorted(destination, reverse = True))
print("4. After sorted(reverse = Ture):", destination)
destination.reverse()
print("5. reverse():\t\t\t", destination)
destination.reverse()
print("6. Again reverse():\t\t", destination)
destination.sort()
print("7. sort():\t\t\t", destination)
destination.sort(reverse = True)
print("8. sort(reverse = True):\t", destination)
#Output:
#Original list is ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
# 1. sorted():                     ['Fiji', 'Home', 'Norway', 'Paris', 'Yellowstone']
# 2. After sorted():               ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
# 3. sorted(reverse = Ture):       ['Yellowstone', 'Paris', 'Norway', 'Home', 'Fiji']
# 4. After sorted(reverse = Ture): ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
# 5. reverse():                    ['Home', 'Yellowstone', 'Paris', 'Norway', 'Fiji']
# 6. Again reverse():              ['Fiji', 'Norway', 'Paris', 'Yellowstone', 'Home']
# 7. sort():                       ['Fiji', 'Home', 'Norway', 'Paris', 'Yellowstone']
# 8. sort(reverse = True):         ['Yellowstone', 'Paris', 'Norway', 'Home', 'Fiji']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值