Skip to content

Commit 85cba0f

Browse files
committed
efficient python
1 parent 0f6fcc2 commit 85cba0f

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import numpy as np
2+
3+
def get_largest(lst):
4+
lst = np.array(lst)
5+
return lst.argsort()[-3:][::-1]
6+
7+
8+
if __name__ == "__main__":
9+
lst = [1,2,3,4,5,8,3,4]
10+
print(get_largest(lst))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def is_armstrong_number(num):
2+
original_num = num
3+
total = 0
4+
remainder = num % 10
5+
6+
while remainder != 0:
7+
total += remainder ** 3
8+
num = num // 10
9+
remainder = num % 10
10+
11+
return total == original_num
12+
13+
14+
15+
if __name__ == "__main__":
16+
print(is_armstrong_number(371))
17+
print(is_armstrong_number(123))
18+
print(is_armstrong_number(153))

11.Learning Python - JM/enumerate.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
names = ['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
2+
3+
# Rewrite the for loop to use enumerate
4+
indexed_names = []
5+
for i,name in enumerate(names):
6+
index_name = (i,name)
7+
indexed_names.append(index_name[1])
8+
print(indexed_names)
9+
10+
# Rewrite the above for loop using list comprehension
11+
indexed_names_comp = [(i,name) for i,name in enumerate(names)]
12+
print(indexed_names_comp)
13+
14+
# Unpack an enumerate object with a starting index of one
15+
indexed_names_unpack = [*enumerate(names, start=1)]
16+
print(indexed_names_unpack)

11.Learning Python - JM/map.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
names = ['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
2+
3+
# Use map to apply str.upper to each element in names
4+
names_map = map(str.upper, names)
5+
6+
# Print the type of the names_map
7+
print(type(names_map))
8+
9+
# Unpack names_map into a list
10+
names_uppercase = [item for item in names_map]
11+
12+
# Print the list created above
13+
print(names_uppercase)

11.Learning Python - JM/party.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
3+
4+
def welcome_guest(guest):
5+
print(guest)
6+
7+
8+
9+
names = ['Jerry', 'Kramer', 'Elaine', 'George', 'Newman']
10+
11+
# Create a list of arrival times
12+
arrival_times = [*range(10,60,10)]
13+
14+
# Convert arrival_times to an array and update the times
15+
arrival_times_np = np.array(arrival_times)
16+
new_times = arrival_times_np - 3
17+
18+
# Use list comprehension and enumerate to pair guests to new times
19+
guest_arrivals = [(names[i],time) for i,time in enumerate(new_times)]
20+
21+
#Map the welcome_guest function to each (guest,time) pair
22+
welcome_map = map(welcome_guest, guest_arrivals)
23+
24+
# guest_welcomes = [*welcome_map]
25+
# print(*guest_welcomes, sep='\n')

11.Learning Python - JM/range.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Create a range object that goes from 0 to 5
2+
nums = range(6)
3+
print(type(nums))
4+
5+
# Convert nums to a list
6+
nums_list = list(nums)
7+
print(nums_list)
8+
9+
# Create a new list of odd numbers from 1 to 11 by unpacking a range object
10+
nums_list2 = [*range(1,12,2)]
11+
print(nums_list2)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import numpy as np
2+
3+
# Create a list of integers (0-50) using list comprehension
4+
nums_list_comp = [num for num in range(51)]
5+
print(nums_list_comp)
6+
7+
# Create a list of integers (0-50) by unpacking range
8+
nums_unpack = [*range(51)]
9+
print(nums_unpack)

0 commit comments

Comments
 (0)