-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor_loopbasicII.py
More file actions
92 lines (84 loc) · 3.28 KB
/
Copy pathfor_loopbasicII.py
File metadata and controls
92 lines (84 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big".
# Example: biggie_size([-1, 3, 5, -5]) returns that same list, but whose values are now [-1, "big", "big", -5]
def biggie_size(list):
for i in range(len(list)):
if list[i] > 0:
list[i] = "big"
return list
a = biggie_size([-1,3,5,-5])
print(a)
# Count Positives - Given a list of numbers, create a function to replace the last value with the number of positive values. (Note that zero is not considered to be a positive number).
# Example: count_positives([-1,1,1,1]) changes the original list to [-1,1,1,3] and returns it
# Example: count_positives([1,6,-4,-2,-7,-2]) changes the list to [1,6,-4,-2,-7,2] and returns it
def countplus(list):
count = 0
for i in range(len(list)):
if list[i] > 0:
count +=1
list[len(list)-1] = count
print(list)
return list
countplus([1,6,-4,-2,-7,-2])
# Sum Total - Create a function that takes a list and returns the sum of all the values in the array.
# Example: sum_total([1,2,3,4]) should return 10
# Example: sum_total([6,3,-2]) should return 7
def sum_total(list):
sum = 0
for i in list:
sum+=i
print(sum)
return sum
sum_total([1,2,3,4])
# Average - Create a function that takes a list and returns the average of all the values.
# Example: average([1,2,3,4]) should return 2.5
def average(list):
sum = 0
for i in list:
sum+=i
avg = sum/len(list)
print(avg)
return avg
average([1,2,3,4])
# Length - Create a function that takes a list and returns the length of the list.
# Example: length([37,2,1,-9]) should return 4
# Example: length([]) should return 0
def length(list):
return len(list)
print(length([37,2,1,-9]))
# Minimum - Create a function that takes a list of numbers and returns the minimum value in the list. If the list is empty, have the function return False.
# Example: minimum([37,2,1,-9]) should return -9
# Example: minimum([]) should return False
def mini(list):
print(min(list))
return min(list)
mini([37,2,1,-9])
# Maximum - Create a function that takes a list and returns the maximum value in the array. If the list is empty, have the function return False.
# Example: maximum([37,2,1,-9]) should return 37
# Example: maximum([]) should return False
def maxi(list):
print(max(list))
return max(list)
maxi([37,2,1,-9])
# Ultimate Analysis - Create a function that takes a list and returns a dictionary that has the sumTotal, average, minimum, maximum and length of the list.
# Example: ultimate_analysis([37,2,1,-9]) should return {'sumTotal': 31, 'average': 7.75, 'minimum': -9, 'maximum': 37, 'length': 4 }
def ultimate(list):
sum = 0
for i in list:
sum+=i
avg = sum/len(list)
return {
"sumTotal": sum,
"average": avg,
"minimum": min(list),
"maximum": max(list),
"length": len(list)
}
A = ultimate([37,2,1,-9])
print(A)
# Reverse List - Create a function that takes a list and return that list with values reversed. Do this without creating a second list. (This challenge is known to appear during basic technical interviews.)
# Example: reverse_list([37,2,1,-9]) should return [-9,1,2,37]
def reverse(list):
list.reverse()
print(list)
return list
reverse([37,2,1,-9])