Skip to content

Commit 9d39cb7

Browse files
committed
Initial commit
0 parents  commit 9d39cb7

37 files changed

+1428
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

Check_If_Power_Recursion.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def is_power_of(number, base):
2+
# Base case: when number is smaller than base.
3+
if number < base:
4+
# If number is equal to 1, it's a power (base**0).
5+
if number == 1:
6+
return True
7+
else:
8+
return False
9+
# Recursive case: keep dividing number by base.
10+
return is_power_of(number / base, base)
11+
12+
13+
print(is_power_of(8, 2)) # Should be True
14+
print(is_power_of(64, 4)) # Should be True
15+
print(is_power_of(70, 10)) # Should be False

Class.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
class Flower:
2+
color = 'unknown'
3+
4+
5+
rose = Flower()
6+
rose.color = "Red"
7+
violet = Flower()
8+
violet.color = "Blue"
9+
this_pun_is_for_you = "This pun is for you."
10+
print("Roses are {},".format(rose.color))
11+
print("Violets are {},".format(violet.color))
12+
print(this_pun_is_for_you)
13+
14+
15+
class Dog:
16+
years = 0
17+
18+
def dog_years(self):
19+
return self.years * 7
20+
21+
22+
fido = Dog()
23+
fido.years = 3
24+
print(fido.dog_years())
25+
26+
27+
class Person:
28+
name = ""
29+
30+
def __init__(self, name):
31+
self.name = name
32+
33+
def __str__(self):
34+
return "You just called __str__ method."
35+
36+
def greeting(self):
37+
"""Returns Greeting Text."""
38+
return "hi, my name is " + self.name + "."
39+
40+
41+
some_person = Person("Cool Dude")
42+
print(some_person.greeting())
43+
print(some_person)
44+
45+
46+
class Clothing:
47+
material = ""
48+
49+
def __init__(self, name):
50+
self.name = name
51+
52+
def checkmaterial(self):
53+
print("This {} is made of {}".format(self.name, self.material))
54+
55+
56+
class Shirt(Clothing):
57+
material = "Cotton"
58+
59+
60+
polo = Shirt("Polo")
61+
polo.checkmaterial()
62+
63+
64+
class Clothing:
65+
stock = {'name': [], 'material': [], 'amount': []}
66+
67+
def __init__(self, name):
68+
material = ""
69+
self.name = name
70+
71+
def add_item(self, name, material, amount):
72+
Clothing.stock['name'].append(self.name)
73+
Clothing.stock['material'].append(self.material)
74+
Clothing.stock['amount'].append(amount)
75+
76+
def Stock_by_Material(self, material):
77+
count = 0
78+
n = 0
79+
for item in Clothing.stock['material']:
80+
if item == material:
81+
count += Clothing.stock['amount'][n]
82+
n += 1
83+
return count
84+
85+
86+
class shirt(Clothing):
87+
material = "Cotton"
88+
89+
90+
class pants(Clothing):
91+
material = "Cotton"
92+
93+
94+
polo = shirt("Polo")
95+
sweatpants = pants("Sweatpants")
96+
polo.add_item(polo.name, polo.material, 4)
97+
sweatpants.add_item(sweatpants.name, sweatpants.material, 6)
98+
current_stock = polo.Stock_by_Material("Cotton")
99+
print(current_stock)

Class_Event_Reports.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def get_event_date(event):
2+
return event.date
3+
4+
5+
def current_users(events):
6+
events.sort(key=get_event_date)
7+
machines = {}
8+
for event in events:
9+
if event.machine not in machines:
10+
machines[event.machine] = set()
11+
if event.type == "login":
12+
machines[event.machine].add(event.user)
13+
elif event.type == "logout" and event.user in machines[event.machine]:
14+
machines[event.machine].remove(event.user)
15+
return machines
16+
17+
18+
def generate_report(machines):
19+
for machine, users in machines.items():
20+
if len(users) > 0:
21+
user_list = ", ".join(users)
22+
print("{}: {}".format(machine, user_list))
23+
24+
25+
class Event:
26+
def __init__(self, event_date, event_type, machine_name, user):
27+
self.date = event_date
28+
self.type = event_type
29+
self.machine = machine_name
30+
self.user = user
31+
32+
33+
events = [
34+
Event('2020-01-21 12:45:56', 'login', 'myworkstation.local', 'jordan'),
35+
Event('2020-01-22 15:53:42', 'logout', 'webserver.local', 'jordan'),
36+
Event('2020-01-21 18:53:21', 'login', 'webserver.local', 'lane'),
37+
Event('2020-01-22 10:25:34', 'logout', 'myworkstation.local', 'jordan'),
38+
Event('2020-01-21 08:20:01', 'login', 'webserver.local', 'jordan'),
39+
Event('2020-01-23 11:24:35', 'logout', 'mailserver.local', 'chris'),
40+
]
41+
42+
users = current_users(events)
43+
print(users)
44+
generate_report(users)

Count_Digits.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def digits(n):
2+
count = 0
3+
if n == 0:
4+
return 1
5+
while n > 0:
6+
count += 1
7+
n = n // 10
8+
return count
9+
10+
11+
print(digits(25)) # Should print 2
12+
print(digits(144)) # Should print 3
13+
print(digits(1000)) # Should print 4
14+
print(digits(0)) # Should print 1

Counters.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
def for_counter():
2+
print()
3+
sum1 = 0
4+
for i in range(100):
5+
print(i)
6+
sum1 = sum1 + i
7+
print("\nSum: ", sum1)
8+
9+
10+
def for_rev_counter():
11+
print()
12+
list0 = []
13+
for i in reversed(range(1, 100)):
14+
list0.append(i)
15+
print(list0)
16+
17+
18+
def string_traverse():
19+
print()
20+
for i in "String":
21+
print(i)
22+
23+
24+
def simple_while():
25+
x = 0
26+
while x < 5:
27+
print("Not there yet, x=" + str(x))
28+
x = x + 1
29+
print("x=" + str(x))
30+
print()
31+
32+
33+
def attempts(n):
34+
x = 1
35+
while x <= n:
36+
print("Attempt " + str(x))
37+
x += 1
38+
print("Done")
39+
print()
40+
41+
42+
def count_down(start_number):
43+
current = start_number
44+
while current > 0:
45+
print(current)
46+
current -= 1
47+
print("Zero!")
48+
print()
49+
50+
51+
def print_range(start, end):
52+
n = start
53+
while n <= end:
54+
print(n)
55+
n += 1
56+
print()
57+
58+
59+
def ask_done():
60+
while True:
61+
inpt = input("Enter anything: ")
62+
if inpt == "Done":
63+
print("Exiting...")
64+
break
65+
elif inpt[0] == '#':
66+
print("Skipping this iteration...")
67+
continue
68+
# print("This print statement will not be executed...")
69+
70+
71+
def while_counter(start, stop):
72+
x = start
73+
if x > stop:
74+
return_string = "Counting down: "
75+
while x > stop:
76+
return_string += str(x) + ","
77+
x -= 1
78+
else:
79+
return_string = "Counting up: "
80+
while x < stop:
81+
return_string += str(x) + ","
82+
x += 1
83+
return_string += str(stop)
84+
return return_string
85+
86+
87+
print()
88+
simple_while()
89+
attempts(5)
90+
count_down(5)
91+
print_range(1, 5)
92+
ask_done()
93+
for_counter()
94+
for_rev_counter()
95+
string_traverse()
96+
print("\n", while_counter(1, 10))

Dictonary.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
print()
2+
toc = {"Introduction": 1, "Chapter 1": 4, "Chapter 2": 11, "Chapter 3": 25, "Chapter 4": 30}
3+
toc["Epilogue"] = 39
4+
toc["Chapter 3"] = 24
5+
del toc["Chapter 4"]
6+
print("Chapter 5" in toc)
7+
print(toc)
8+
print(toc.values())
9+
print(toc.keys())
10+
11+
print()
12+
cool_beasts = {"octopuses": "tentacles", "dolphins": "fins", "rhinos": "horns"}
13+
for a, b in cool_beasts.items():
14+
print("{} have {}".format(a, b))
15+
16+
print()
17+
wardrobe = {"shirt": ["red", "blue", "white"], "jeans": ["blue", "black"]}
18+
for x in wardrobe:
19+
for y in wardrobe[x]:
20+
print("{} {}".format(y, x))
21+
22+
print()
23+
wardrobe = {'shirt': ['red', 'blue', 'white'], 'jeans': ['blue', 'black']}
24+
new_items = {'jeans': ['white'], 'scarf': ['yellow'], 'socks': ['black', 'brown']}
25+
wardrobe.update(new_items)
26+
print(wardrobe)

Dictonary_Count_Letters.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def count_letters(text):
2+
result = {}
3+
# Go through each letter in the text
4+
for letter in text:
5+
# Check if the letter needs to be counted or not
6+
if letter.isalpha():
7+
if letter.lower() not in result:
8+
result[letter.lower()] = 1
9+
else:
10+
result[letter.lower()] += 1
11+
# Add or increment the value in the dictionary
12+
return result
13+
14+
15+
print(count_letters("AaBbCc"))
16+
# Should be {'a': 2, 'b': 2, 'c': 2}
17+
18+
print(count_letters("Math is fun! 2+2=4"))
19+
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}
20+
21+
print(count_letters("This is a sentence."))
22+
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}

Dictonary_Groups_per_User.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def groups_per_user(group_dictionary):
2+
user_groups = {}
3+
for group, users in group_dictionary.items():
4+
for user in users:
5+
if user not in user_groups:
6+
user_groups[user] = []
7+
user_groups[user].append(group)
8+
return user_groups
9+
10+
11+
print(groups_per_user({"local": ["admin", "userA"], "public": ["admin", "userB"], "administrator": ["admin"]}))

Dictonary_Users_and_Domins.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def email_list(domains):
2+
emails = []
3+
for domain, users in domains.items():
4+
for user in users:
5+
emails.append(user + "@" + domain)
6+
return emails
7+
8+
9+
print(email_list(
10+
{"gmail.com": ["clark.kent", "diana.prince", "peter.parker"], "yahoo.com": ["barbara.gordon", "jean.grey"],
11+
"hotmail.com": ["bruce.wayne"]}))

Display_Grade.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def function1():
2+
val2 = ""
3+
val = float(input("Enter Score: "))
4+
if val >= 0.9:
5+
val2 = "A"
6+
elif val >= 0.8:
7+
val2 = "B"
8+
elif val >= 0.7:
9+
val2 = "C"
10+
elif val >= 0.6:
11+
val2 = "D"
12+
elif val < 0.6:
13+
val2 = "F"
14+
else:
15+
print("Enter a correct value.")
16+
print(val2)
17+
18+
19+
try:
20+
function1()
21+
except Exception as e:
22+
print("Enter float or integer value only.")

Dominos.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def domino():
2+
for i in range(7):
3+
for j in range(i, 7):
4+
print("[ ", i, j, " ]", end=" ")
5+
print()
6+
7+
8+
domino()

Factorial.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def factorial(n):
2+
fct = 1
3+
for i in range(n + 1):
4+
if i != 0:
5+
fct *= i
6+
print(i)
7+
return fct
8+
9+
10+
print("\nFactorial of 5 is:", factorial(5))

0 commit comments

Comments
 (0)