Skip to content

Commit 03af107

Browse files
author
BASNETANAMIKA
committed
adding practice for data structures and collections
1 parent e538433 commit 03af107

File tree

4 files changed

+5187
-0
lines changed

4 files changed

+5187
-0
lines changed

Self_Practice_Modules/2Collections.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from collections import namedtuple,defaultdict
2+
3+
import csv
4+
from urllib.request import urlretrieve
5+
6+
url = "https://raw.githubusercontent.com/sundeepblue/movie_rating_prediction/master/movie_metadata.csv"
7+
movies_csv = 'movie_metadata.csv'
8+
urlretrieve(url, 'movie_metadata.csv')
9+
#### Movie namedtuple
10+
Movie = namedtuple('Movie',['title', 'year','score'])
11+
12+
13+
def get_movies_by_director(data = movies_csv):
14+
directors = defaultdict(list)
15+
with open(data) as f:
16+
for line in csv.DictReader(f):
17+
try:
18+
director = line['director_name']
19+
movie = line['movie_title'].replace('\xa0', '')
20+
year = int(line['title_year'])
21+
score = float(line['imdb_score'])
22+
except ValueError:
23+
continue
24+
m = Movie(title=movie, year=year, score=score)
25+
directors[director].append(m)
26+
27+
return directors
28+
29+
30+
directors=get_movies_by_director()

Self_Practice_Modules/Dicts.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Given the provided cars dictionary:
2+
#
3+
# Get all Jeeps
4+
# Get the first car of every manufacturer.
5+
# Get all vehicles containing the string Trail in their names (should work for other grep too)
6+
# Sort the models (values) alphabetically
7+
8+
9+
cars = {
10+
'Ford': ['Falcon', 'Focus', 'Festiva', 'Fairlane'],
11+
'Holden': ['Commodore', 'Captiva', 'Barina', 'Trailblazer'],
12+
'Nissan': ['Maxima', 'Pulsar', '350Z', 'Navara'],
13+
'Honda': ['Civic', 'Accord', 'Odyssey', 'Jazz'],
14+
'Jeep': ['Grand Cherokee', 'Cherokee', 'Trailhawk', 'Trackhawk']
15+
}
16+
17+
18+
def get_all_jeeps(cars=cars):
19+
"""return a comma + space (', ') separated string of jeep models
20+
(original order)"""
21+
22+
vehicles = cars['Jeep']
23+
separator = ', '
24+
separator.join(vehicles)
25+
print(separator.join(vehicles))
26+
return separator.join(vehicles)
27+
28+
29+
30+
def get_first_model_each_manufacturer(cars=cars):
31+
"""return a list of matching models (original ordering)"""
32+
first_model_list = []
33+
for values in cars.values():
34+
first_model_list.append(values[0])
35+
print(first_model_list)
36+
return first_model_list
37+
38+
39+
def get_all_matching_models(cars=cars, grep='trail'):
40+
"""return a list of all models containing the case insensitive
41+
'grep' string which defaults to 'trail' for this exercise,
42+
sort the resulting sequence alphabetically"""
43+
import re
44+
matching_models_list = []
45+
for manufacturer, cars_list in cars.items():
46+
for car in cars_list:
47+
match_found = re.search(grep, car, re.IGNORECASE)
48+
if match_found:
49+
matching_models_list.append(car)
50+
print(matching_models_list)
51+
matching_models_list.sort()
52+
return matching_models_list
53+
54+
55+
def sort_car_models(cars=cars):
56+
"""return a copy of the cars dict with the car models (values)
57+
sorted alphabetically"""
58+
for model, car_list in cars.items():
59+
print(car_list)
60+
car_list.sort()
61+
# prints car_list.sort() returns None
62+
cars[model] = car_list
63+
64+
print(cars)
65+
return cars
66+
67+
68+
if __name__=="__main__":
69+
# get_all_jeeps()
70+
# get_first_model_each_manufacturer()
71+
get_all_matching_models(grep="CO")
72+
# get_all_matching_models()
73+
# sort_car_models()

Self_Practice_Modules/Lists.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
numlist = [1,2,3,4,5]
2+
print(numlist)
3+
# reverse
4+
numlist.reverse()
5+
print(numlist)
6+
# sort values
7+
numlist.sort()
8+
print(numlist)
9+
10+
for num in numlist:
11+
print(str(num))
12+
13+
mystring = "abracadabra"
14+
print(list(mystring))
15+
16+
l = list(mystring)
17+
# return and remove the last element
18+
returned_var = l.pop()
19+
print("value returned due to pop {0}".format(returned_var))
20+
print("list now {0}".format(l))
21+
22+
# insert value at the end
23+
l.insert(len(l), "a")
24+
print("reformed list {0}".format(l))
25+
26+
del l[0]
27+
print("list after deletion {0}".format(l))
28+
29+
l.insert(0, "a")
30+
print("l after insert is {0}".format(l))
31+
32+
# pop using index
33+
a = l.pop(0)
34+
print("return value in pop {0}".format(a))
35+
36+
new_tuple = tuple(mystring)
37+
print(new_tuple)
38+
39+
for letter in new_tuple:
40+
print(letter)

0 commit comments

Comments
 (0)