Exercise function
1. Exercise 1
Topics requirements:
write a function cacluate, any number of numbers may be received, returns a tuple.
The first parameter is the average of all tuples value, the second value is greater than the average number of all.
def cacluate(*args):
avg = sum(args)/len(args)
up_avg=[]
for item in args:
if item > avg:
up_avg.append(item)
return avg,up_avg
print(cacluate(1,2,3,4,5))
Output:
2. English II
Topics requirements:
write a function that takes a string and returns a tuple, 'ehllo WROLD'
number is a capital letter of the first tuple, and the second value is the number of lower-case letters.
def fun(*args):
upper_count = 0
lower_count = 0
for i in args:
if i.isupper():
upper_count += 1
elif i.islower():
lower_count += 1
else:
continue
return (upper_count, lower_count)
a = 'ehllo,WROLD'
print(fun(*a))
Output:
3. Practice three
Topics requirements:
write function, receiving a list (containing 30 integers) and an integer number k, returns a new list of
function requirements:
corresponding (without k) the elements in reverse order until the list subscript k;
the subscript k and after the reverse elements;
[1,2,3,4,5] 2 [2,1,5,4,3]
import random
list = []
def fun(alist, k):
if k < 0 or k > len(alist):
return 'error key'
return alist[:k][::-1] + alist[k:][::-1]
print(fun([1, 2, 3, 4, 5], 2))
Output: