Comp9021 practice solution(1-6)【Python】
发布时间
阅读量:
阅读量
Practice1
temperature_conversions:
# %%writefile celsius_to_fahrenheit.py
# Written by Zerek for COMP9021
# Prints out a conversion table of temperatures from Fahrenheit
# to Celsius degrees, with the former ranging from 0 to 300 in
# steps of 10.
min_temperature = 0
max_temperature = 100
step = 10
# \t: A tab
print('Celsius\tFahrenheit')
# up to the largest value smaller than max_temperature + step
for celsius in range(min_temperature, max_temperature + step, step):
fahrenheit = celsius * 9 / 5 + 32
print(f'{celsius:7}\t{fahrenheit:10.0f}')
AI写代码
Practice2 max_element_and_span_in_a_list:
# %%writefile span_in_list.py
# wirtten by Zerek
from random import seed, randint
import sys
# Prompts the user for an integer to provide as argument to the
# seed() function.
try:
arg_for_seed = int(input('Feed the seed with an integer: '))
except ValueError:
print('Input is not an integer, giving up.')
sys.exit()
# Prompts the user a strictly positive number, nb_of_elements.
try:
nb_of_elements = int(input('How many elements do you want to generate? '))
except ValueError:
print('Input is not an integer, giving up.')
sys.exit()
if nb_of_elements <= 0:
print('Input should be strictly positive, giving up.')
sys.exit()
seed(arg_for_seed)
# Generates a list of nb_of_elements random integers between 0 and 99.
L = [randint(0, 99) for _ in range(nb_of_elements)]
# Prints out the list.
print('\nThe list is:', L)
# Computes the maximum element of the list without using the
# builtin max().
max_element = 0
min_element = 99
for e in L:
if e > max_element:
max_element = e
if e < min_element:
min_element = e
# Prints out the value of the maximum element within text out.
print('\nThe maximum difference between largest and smallest values in this list is:', max_element - min_element)
# Confirms the value with the builtin max().
print('Confirming with builtin operations:', max(L) - min(L))
AI写代码
Practice3
classifying_elements_in_a_list:
# %%writefile intervals.py
# Written by Zerek for COMP9021
from random import seed, randrange
import sys
# Prompts the user for an integer to provide as argument to the
# seed() function.
try:
arg_for_seed = int(input('Feed the seed with an integer: '))
except ValueError:
print('Input is not an integer, giving up.')
sys.exit()
# Prompts the user a strictly positive number, nb_of_elements.
try:
nb_of_elements = int(input('How many elements do you want to generate? '))
except ValueError:
print('Input is not an integer, giving up.')
sys.exit()
if nb_of_elements <= 0:
print('Input should be strictly positive, giving up.')
sys.exit()
seed(arg_for_seed)
# Generates a list of nb_of_elements random integers between 0 and 99.
L = [randrange(20) for _ in range(nb_of_elements)]
# Prints out the list.
print('\nThe list is:' , L)
print()
# Computes the number of elements equal to 0, 1, 2 3 modulo 4.
# - remainders_modulo_4[0] to record the number of elements
# equal to 0 modulo 4,
# - remainders_modulo_4[1] to record the number of elements
# equal to 1 modulo 4,
# - remainders_modulo_4[2] to record the number of elements
# equal to 2 modulo 4,
# - remainders_modulo_4[3] to record the number of elements
# equal to 3 modulo 4.
remainders_modulo_4 = [0]
# Prints those numbers out.
for i in range(len(L)):
if L[i] >= 0 and L[i] <= 4:
remainders_modulo_4[0] += 1
elif L[i] >= 5 and L[i] <= 9:
remainders_modulo_4[1] += 1
elif L[i] >= 10 and L[i] <= 14:
remainders_modulo_4[2] += 1
elif L[i] >= 15 and L[i] <= 19:
remainders_modulo_4[3] += 1
for t in range(4):
if remainders_modulo_4[t] == 0:
print('There is no element', end='')
elif remainders_modulo_4[t] == 1:
print('There is 1 element', end='')
else:
print('There are ' + str(remainders_modulo_4[t]) + ' elements', end='')
print(' between ' + str((5 * t)) + ' and ' + str((5 * (t + 1) - 1)) + ".")
AI写代码
Practice4
mean_median_standard_deviation:
# %%writefile mean_median_standard_deviation.py
# written by zerek
import sys
import numpy as np
from random import seed, randrange
try:
seed_number = int(input('Feed the seed with an integer: '))
except ValueError:
print('Input is not an integer, giving up.')
sys.exit()
try:
nb_of_elements = int(input('How many elements do you want to generate? '))
except ValueError:
print('Input is not an integer, giving up.')
sys.exit()
if nb_of_elements <= 0:
print('Input should be strictly positive, giving up.')
sys.exit()
seed(seed_number)
L = [randrange(-50, 51) for _ in range(nb_of_elements)]
print('\nThe list is:', L)
print()
# 自己造轮子
# 平均值
total_num = 0
for i in L:
total_num += i
average_num = total_num / len(L)
print(f'The mean is {average_num:.2f}.')
# 中值
L.sort()
medium_num = 0
if nb_of_elements % 2 != 0:
medium_num = L[nb_of_elements // 2]
else:
medium_num = (L[nb_of_elements // 2 - 1] + L[nb_of_elements // 2]) / 2
print(f'The median is {medium_num:.2f}.')
# 标准差
sum_of_difference = 0
for i in L:
sum_of_difference += (i - average_num) *
standard_deviation = (sum_of_difference / nb_of_elements) ** 0.5
print(f'The standard deviation is {standard_deviation:0.2f}.')
# 用numpy的轮子
print()
print('Confirming with functions from the statistics module:')
# mean()平均值
mean_num = np.mean(L)
print(f'The mean is {mean_num:.2f}.')
# median()中位数
median_num = np.median(L)
print(f'The median is {median_num:.2f}.')
# std()标准差
std_num = np.std(L)
print(f'The standard deviation is {std_num:.2f}.')
AI写代码
Pracitce5
drawing_polygons_with_turtle:
没什么可说的,就是video模块功能
Practice 6
characters_triangle:
# %%writefile characters_triangle.py
# written by Zerek
import sys
# written by Zerek
try:
height = int(input('Enter a strictly positive integer: '))
except ValueError:
print('Input is not an integer, giving up.')
sys.exit()
if height <= 0:
sys.exit()
letter_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
count = 0
for i in range(height):
left_half = letter_list[count:(count + i + 1):1]
wright_half = left_half[::-1]
new_wright_half = wright_half[1::1]
print(' '*(height - (i + 1)) + ''.join(left_half) + ''.join(new_wright_half))
count += (i + 1)
AI写代码
全部评论 (0)
还没有任何评论哟~
