-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlist-comprehension.py
155 lines (123 loc) · 5.23 KB
/
list-comprehension.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
The gaming.txt file is attached to this excercise. This file has been loaded into
the text variable. From the text list, delete all newline characters. Then delete
emply lines and print the text to the console.
text before:
['Activision Blizzard\n',
'\n',
'Activision Blizzard, Inc. is a developer and publisher of interactive entertainment content and services. The Company develops and distributes content and services across various gaming platforms,\n',
'including video game consoles, personal computers (PC) and mobile devices. Its segments include Activision Publishing, Inc. (Activision), Blizzard Entertainment, Inc. (Blizzard),\n',
'King Digital Entertainment (King) and Other. Activision is a developer and publisher of interactive software products and content. Blizzard is engaged in developing and publishing of interactive\n',
'software products and entertainment content, particularly in PC gaming. King is a mobile entertainment company. It is engaged in other businesses, including The Major League Gaming (MLG) business;\n',
'The Activision Blizzard Studios (Studios) business, and The Activision Blizzard Distribution (Distribution) business. It also develops products spanning other genres, including action/adventure,\n',
'role-playing and simulation.']
"""
from lib2to3.pgen2 import token
with open('gaming.txt', 'r') as file:
text = file.readlines()
text = [line.replace('\n', '') for line in text]
text = [line for line in text if len(line)>0]
print(text)
"""
The list of product prices is given:
net_price = [5.5, 4.0, 9.0, 10.0]
The VAT for these products is equal to 23%
tax = 0.23
Using the list comprehension, calculate the gross price for each product. Round the
price to two decimal places and print result to console.
"""
tax = 0.23
net_price = [5.5, 4.0, 9.0, 10.0]
gross_price = [round(num*(1+0.23),2) for num in net_price]
print(gross_price)
"""
The present value - pv and the investment value - n are given below:
pv = 1000
n = 10
Depending on the interest rates given below, calculate the future value fv of your
investment
rate = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07]
round the result to full cent and print the result to the console
Expected result:
[1104.62, 1218.99, 1343.92, 1480.24, 1628.89, 1790.85, 1967.15]
"""
pv = 1000
n = 10
rate = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07]
fv = [round(pv*(1 + r)**n, 2) for r in rate]
print(fv)
"""
The present value - pv and the investment period - n are given below:
pv = 1000
n = 10
Depending on the interest rates given below, calculate the value of interest on
investments:
rate = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07]
Round the result to the full cent and print the result to the console.
Expected result:
[104.62, 218.99, 343.92, 480.24, 628.89, 790.85, 967.15]
"""
pv = 1000
n = 10
rate = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07]
fvs = [round(pv*(1 + r)**n, 2) for r in rate]
interest = [round(fv - pv, 2) for fv in fvs]
print(interest)
"""
The contents of the file plw.txt were loaded as follows:
with open('plw.txt', 'r') as file:
lines = file.read().splitlines()
lines variable:
['PLAYWAY',
'',
'PlayWay is a producer and publisher of computer and mobile games. The company is characterized by a very large number of development teams and a large number of games produced simultaneously.',
"PlayWay sells, among others via the STEAM portal, AppStore and GooglePlay. The US and German markets are the two largest markets for the Group's sales.",
'In addition, the company has PlayWay Campus - a campus for cooperating development teams.']
Task to perform:
1. Get rid of blank lines
2. Split each line into tokens/words as hsown below
Formatted result:
"""
with open('plw.txt', 'r') as file:
lines = file.read().splitlines()
lines = [line for line in lines if len(line) > 0]
lines = [line.split() for line in lines]
print(lines)
"""
The contents of the file plw.txt were loaded as follows:
with open('plw.txt', 'r') as file:
lines = file.read()
lines variable:
Tasks to perform:
1. Change uppercase letters to lowercase
2. Remove commas and periods
3. Split the text into tokens
4. Extract words with minimum of 8 characters length
5. Sort the words alphabetically
"""
with open('plw.txt', 'r') as file:
lines = file.read()
lines = lines.lower()
lines = lines.replace(',','').replace('.','')
tokens = lines.split()
tokens = [token for token in tokens if len(token) >= 8]
tokens = sorted(tokens)
print(tokens)
"""
The following dictionary is given:
data = dict(zip(('a', 'b', 'c', 'd', 'e', 'f'),(1, 2, 3, 4, 5, 6)))
Convert the dictionary into the following list and print the result to console
Expected result:
[['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5], ['f', 6]]
"""
data = dict(zip(('a', 'b', 'c', 'd', 'e', 'f'),(1, 2, 3, 4, 5, 6)))
data = [[key, val] for key, val in data.items()]
print(data)
"""
You are given three integers x, y and z representing the dimensions of a cuboid along with an
integer n. Print a list of all possible coordinates given by (i,j,k) on a 3D grid where
the sum of i+j+k is not equal to n. Here, 0<=i<=x; 0<=j<=y<=k<=z. Use list comprehensions
"""
x=1; y=1; z=2; n=3
result = [[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if (i+j+k) != n]
print(result)