Skip to content

Commit 5ff1177

Browse files
committed
add sleep sort and quick sort in place
1 parent fce86d3 commit 5ff1177

File tree

5 files changed

+104
-59
lines changed

5 files changed

+104
-59
lines changed

CSV_files/assets/course_name.csv

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
name,course
2-
Noura,python40python401
3-
Mahmoud,python401
4-
Nizar,python401
5-
Raneem,python401
6-
Omer,python401
1+
Name,Score
2+
Omar,82
3+
Mahmoud,86
4+
Noura,84
5+
Raneem,65

CSV_files/read_csv.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
import csv
2+
23
import pandas as pd
3-
4+
5+
46
def read_using_DictReader(path):
57
# opening the CSV file
6-
with open(path, mode ='r') as file:
7-
# reading the CSV file
8-
csvFile = csv.DictReader(file)
9-
main_dic=[]
10-
# displaying the contents of the CSV file
11-
for lines in csvFile:
12-
main_dic.append(lines)
13-
return main_dic
14-
15-
#retrun the top 5 rows in CSV file
8+
with open(path, mode='r') as file:
9+
# reading the CSV file
10+
csvFile = csv.DictReader(file)
11+
main_dic = []
12+
# displaying the contents of the CSV file
13+
for lines in csvFile:
14+
main_dic.append(lines)
15+
return main_dic
16+
17+
18+
# return the top 5 rows in CSV file
1619
def read_by_pandas_head(path):
17-
data=pd.read_csv(path)
18-
return data.head()
20+
data = pd.read_csv(path)
21+
return data.head()
1922

2023

21-
#retrun the bottom 5 rows in CSV file
24+
# return the bottom 5 rows in CSV file
2225
def read_by_pandas_tail(path):
23-
data=pd.read_csv(path)
24-
return data.tail()
25-
26-
if __name__=="__main__":
27-
28-
path='./assets/addresses.csv'
26+
data = pd.read_csv(path)
27+
return data.tail()
28+
29+
30+
if __name__ == "__main__":
31+
path = './assets/addresses.csv'
2932
print(read_using_DictReader(path))
3033
print(read_by_pandas_head(path))
3134
print(read_by_pandas_tail(path))
32-

CSV_files/write_csv.py

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
import csv
2-
import pandas as pd
32

3+
import pandas as pd
44

5-
65

7-
def write_to_csv_files_using_DictWriter_class(data,fields,filename):
8-
9-
with open(filename, 'w') as csvfile:
6+
def write_to_csv_files_using_DictWriter_class(data, fields, filename):
7+
with open(filename, 'w') as csvfile:
108
# creating a csv dict writer object
11-
writer = csv.DictWriter(csvfile, fieldnames = fields)
12-
9+
writer = csv.DictWriter(csvfile, fieldnames=fields)
10+
1311
# writing headers (field names)
14-
writer.writeheader()
15-
12+
writer.writeheader()
13+
1614
# writing data rows
17-
writer.writerows(data)
18-
19-
def write_by_pandas(name_dict):
20-
df = pd.DataFrame(name_dict)
21-
return df
22-
23-
if __name__=="__main__":
24-
# my data rows as dictionary objects
25-
mydata =[{'name': 'Noura', 'course': 'python40python401'},
26-
{'name': 'Mahmoud', 'course': 'python401'},
27-
{'name': 'Nizar', 'course': 'python401'},
28-
{'name': 'Raneem', 'course': 'python401'},
29-
{'name': 'Omer', 'course': 'python401'}, ]
30-
15+
writer.writerows(data)
16+
17+
18+
def write_by_pandas(name_dict, filename):
19+
df = pd.DataFrame(name_dict)
20+
df.to_csv(filename, index=False)
21+
return df
22+
23+
24+
if __name__ == "__main__":
25+
# my data rows as dictionary objects
26+
mydata = [{'name': 'Noura', 'course': 'python40python401'},
27+
{'name': 'Mahmoud', 'course': 'python401'},
28+
{'name': 'Nizar', 'course': 'python401'},
29+
{'name': 'Raneem', 'course': 'python401'},
30+
{'name': 'Omer', 'course': 'python401'}, ]
31+
3132
# field names
32-
fields = ['name','course']
33-
33+
fields = ['name', 'course']
34+
3435
# name of csv file
3536
filename = "assets/course_name.csv"
36-
37-
# writing to csv file
38-
print(write_to_csv_files_using_DictWriter_class(mydata,fields,filename))
3937

38+
# writing to csv file
39+
print(write_to_csv_files_using_DictWriter_class(mydata, fields, filename))
4040

4141
name_dict = {
42-
'Name': ['Omar','Mahmoud','Noura','Raneem'],
43-
'Score': [82,86,84,65]
44-
}
45-
print(write_by_pandas(name_dict))
42+
'Name': ['Omar', 'Mahmoud', 'Noura', 'Raneem'],
43+
'Score': [82, 86, 84, 65]
44+
}
45+
write_by_pandas(name_dict, filename)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
4+
def partition(nums, low, high) -> int:
5+
piv = nums[high]
6+
i = low
7+
for j in range(low, high):
8+
if nums[j] <= piv:
9+
nums[i], nums[j] = nums[j], nums[i]
10+
i += 1
11+
12+
nums[i], nums[high] = nums[high], nums[i]
13+
return i
14+
15+
16+
def quick_sort(nums: List, low, high):
17+
if low < high:
18+
p = partition(nums, low, high)
19+
quick_sort(nums, low, p - 1)
20+
quick_sort(nums, p + 1, high)
21+
22+
23+
def sort(nums):
24+
quick_sort(nums, 0, len(nums) - 1)
25+
return nums
26+
27+
28+
print(sort([5, 8, 1, 4, 7, 9, 6, 3, 2]))
29+
print(sort([]))
30+
print(sort([0, 1, 2, 6, -1]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import threading
2+
import time
3+
4+
items = [2, 4, 5, 2, 1, 7]
5+
6+
7+
# just fun
8+
def sleep_sort(i):
9+
time.sleep(i)
10+
print(i)
11+
12+
13+
ts = [threading.Thread(target=sleep_sort, args=(i,)) for i in items]
14+
[t.start() for t in ts]

0 commit comments

Comments
 (0)