-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4.py
82 lines (61 loc) · 1.7 KB
/
lab4.py
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
import math
"""
Josue Ruiz
CS 299
Lab 4
"""
def median(data_lst):
data_lst = sorted(data_lst)
len_size = len(data_lst)
position = (len_size - 1) // 2
if len_size % 2:
return data_lst[position]
else:
return (data_lst[position] + data_lst[position + 1])/2.0
def index(d_list, pos):
list_size = len(d_list)
if pos not in d_list:
return -1
for i in range(list_size):
if pos is d_list[i]:
return i
def reverse(d_lst):
return d_lst[::-1]
# First Part - Part A
lst = ["Hello", 7, 7, 9, 'a', 'cat', False]
print("Created list:\n", lst)
# Part B
lst.append(math.pi)
lst.append(7)
print("Added PI and another 7\n", lst)
# Part C
lst[3] = 'dog'
print("Added the word dog in position 3\n", lst)
# Part D
indx = lst.index('cat')
print("Index number for \'cat\' is ", indx)
# Part E
occurrence = lst.count(7)
print("In the list the number 7 shows up ", occurrence, " times.")
# Part F
lst.remove(7)
print("Removed the first 7 from the list\n", lst)
# Part G
lst.pop(lst.index('dog'))
print("List with dog removed by finding the index and then removing with pop\n", lst)
# Second Part of assignment
# Part A
lst = reverse(lst)
print("Reversed List from Part 1\n", lst)
# Part B
rev_lst = index(lst, 'armor')
print("Trying to find word that doesn't exist such as \"armor\" \n", rev_lst)
rev_lst2 = index(lst, 'cat')
print("Trying to find word that exists such as \"cat\" \n", rev_lst2)
# Part C
aList = [24, 5, 8, 2, 9, 15, 10]
med_aList = median(aList)
print("Current List ", aList, "\nImplemented Median:\n", med_aList)
bList = [24, 5, 8, 2, 9, 15, 10, 54]
med_bList = median(bList)
print("Current List ", bList, "\nImplemented Median:\n", med_bList)