Skip to content

Commit f29d0b2

Browse files
authored
part 4 updated
1 parent 070340d commit f29d0b2

15 files changed

+213
-0
lines changed

part04/06_triangle.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def line(num, text):
2+
if text:
3+
print(text[0] * num)
4+
else:
5+
print('*' * num)
6+
7+
def triangle(size):
8+
i = 1
9+
while i <= size:
10+
line(i, "#")
11+
i += 1
12+
13+
# You can test your function by calling it within the following block
14+
if __name__ == "__main__":
15+
triangle(5)

part04/07_shape.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def line(num, text):
2+
if text:
3+
print(text[0] * num)
4+
else:
5+
print('*' * num)
6+
7+
8+
def triangle(size, character):
9+
i = 1
10+
while i <= size:
11+
line(i, character)
12+
i += 1
13+
14+
15+
def shape(triangle_size, triangle_char, rectangle_size, rectangle_char):
16+
triangle(triangle_size, triangle_char)
17+
for x in range(rectangle_size):
18+
line(triangle_size, rectangle_char)
19+
20+
21+
22+
# You can test your function by calling it within the following block
23+
if __name__ == "__main__":
24+
shape(5, "x", 2, "o")

part04/08_spruce.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Write your solution here
2+
def spruce(size):
3+
print('a spruce!')
4+
char = '*'
5+
i = 1
6+
j = 1
7+
8+
while i <= size:
9+
print(f'{" " * (size - i)}{char * (j)}')
10+
i += 1
11+
j += 2
12+
print(f'{" " * (size - 1)}{char}')
13+
14+
# You can test your function by calling it within the following block
15+
if __name__ == "__main__":
16+
spruce(5)

part04/09_greatest_number.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Write your solution here
2+
def greatest_number(a, b, c):
3+
if a >= b and a >= c:
4+
5+
return a
6+
7+
elif b >= c:
8+
9+
return b
10+
11+
else:
12+
13+
return c
14+
15+
16+
# You can test your function by calling it within the following block
17+
if __name__ == "__main__":
18+
greatest = greatest_number(5, 4, 8)
19+
print(greatest)

part04/10_same_characters.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def same_chars(text, num1, num2):
2+
try:
3+
char1 = text[num1]
4+
char2 = text[num2]
5+
if char1 == char2:
6+
return True
7+
else:
8+
return False
9+
except IndexError:
10+
return False
11+
12+
13+
14+
# You can test your function by calling it within the following block
15+
if __name__ == "__main__":
16+
print(same_chars("coder", 1, 28))

part04/11_first_second_last.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def first_word(sentence):
2+
space_index = sentence.find(' ')
3+
return sentence[:space_index]
4+
5+
6+
def second_word(sentence):
7+
space_index = sentence.find(' ')
8+
new_sentence = sentence[space_index+1:]
9+
second_space = new_sentence.find(' ')
10+
if second_space == -1:
11+
return new_sentence
12+
return new_sentence[:second_space]
13+
14+
15+
def last_word(sentence):
16+
last_space_index = sentence.rfind(' ')
17+
return sentence[last_space_index+1:]
18+
19+
20+
21+
# You can test your function by calling it within the following block
22+
if __name__ == "__main__":
23+
sentence = "first second"
24+
print(first_word(sentence))
25+
print(second_word(sentence))
26+
print(last_word(sentence))

part04/12_change_value_of_item.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
numbers = [1, 2, 3, 4, 5]
2+
3+
while True:
4+
index = int(input("Index: "))
5+
6+
if index == -1:
7+
break
8+
if index < 0 or index >= len(numbers):
9+
print("Out of range")
10+
continue
11+
new_value = int(input("New value: "))
12+
numbers[index] = new_value
13+
print(numbers)

part04/13_add_items_to_list.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
size = int(input("How many items: "))
2+
numbers = []
3+
i = 1
4+
while i <= size:
5+
item = int(input(f"Item {i}:"))
6+
numbers.append(item)
7+
i += 1
8+
print(numbers)

part04/14_addition_and_removal.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
lst = []
2+
3+
4+
while True:
5+
print('The list is now', lst)
6+
user = input('a(d)d, (r)emove or e(x)it: ')
7+
if user == 'd':
8+
if not lst:
9+
lst.append(1)
10+
else:
11+
lst.append(lst[-1]+1)
12+
elif user == 'r':
13+
last = len(lst) - 1
14+
lst.pop(last)
15+
else:
16+
print('Bye!')
17+
break

part04/15_same_word_twice.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
words = []
2+
3+
while True:
4+
user = input('Word: ')
5+
if user in words:
6+
print('You typed in {} different words'.format(len(words)))
7+
break
8+
words.append(user)
9+
10+

part04/16_list_twice.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
lst = []
2+
3+
while True:
4+
number = int(input("New item: "))
5+
if number == 0:
6+
break
7+
lst.append(number)
8+
print(f"The list now: {lst}")
9+
print(f"The list in order: {sorted(lst)}")
10+
print("Bye!")

part04/17_length_of_list.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def length(list):
2+
return len(list)
3+
4+
# You can test your function by calling it within the following block
5+
if __name__ == "__main__":
6+
my_list = [3, 6, -4]
7+
result = length(my_list)
8+
print(result)

part04/18_mean.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def mean(list : list):
2+
i = 0
3+
sum = 0
4+
while i < len(list):
5+
sum += list[i]
6+
i += 1
7+
return sum/len(list)
8+
9+
10+
11+
# You can test your function by calling it within the following block
12+
if __name__ == "__main__":
13+
my_list = [3, 6, -4]
14+
result = mean(my_list)
15+
print(result)

part04/19_range_of_list.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def range_of_list(lst : list):
2+
return max(lst) - min(lst)
3+
4+
5+
# You can test your function by calling it within the following block
6+
if __name__ == "__main__":
7+
my_list = [3, 6, -4]
8+
result = range_of_list(my_list)
9+
print(result)

part04/20_star_studded.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
number = int(input("Please type in a positive integer: "))
2+
3+
for i in range(-number, number+1):
4+
if i == 0:
5+
continue
6+
else:
7+
print(i)

0 commit comments

Comments
 (0)