Browse Source

4: loops solutions

Daniel Rose-Levine 1 year ago
parent
commit
3623eb9612

+ 16 - 0
loops/solutions/count_adjacent_repeats.py

@@ -0,0 +1,16 @@
+def count_adjacent_repeats(s):
+	"""
+	(str) -> int
+	For string s, return the number of occurrences
+	of a character and an adjacent character
+	being the same.
+	>>> count_adjacent_repeats('abccdeffggh')
+	3
+	"""
+	count = 0
+	for i in range(len(s) - 1):
+		if s[i] == s[i + 1]:
+			count += 1
+	return count
+
+print(count_adjacent_repeats('abccdeffggh'))

+ 8 - 0
loops/solutions/print_bumps.py

@@ -0,0 +1,8 @@
+from print_triangle import *
+
+def print_bumps(num, symbol1, symbol2):
+	for a in range(1, num + 1):
+		print_triangle(a, symbol1, False)
+		print_triangle(a, symbol2, True)
+
+print_bumps(4, '%', '#')

+ 23 - 0
loops/solutions/print_crazy_striped_diamond.py

@@ -0,0 +1,23 @@
+def print_crazy_striped_diamond(width, symbol1, symbol2, symbol1_width, symbol2_width):
+	for a in range(1, width + 1):
+		string = ''
+		for b in range(width - a):
+			string += ' '
+		for c in range(a):
+			if c % (symbol1_width + symbol2_width) < symbol1_width:
+				string += symbol1 + ' '
+			else:
+				string += symbol2 + ' '
+		print(string)
+	for d in range(1, width):
+		string = ''
+		for e in range(d):
+			string += ' '
+		for f in range(width - d):
+			if (d + f) % (symbol1_width + symbol2_width) < symbol1_width:
+				string += symbol1 + ' '
+			else:
+				string += symbol2 + ' '
+		print(string)
+				
+print_crazy_striped_diamond(7, '.', '%', 2, 1)

+ 17 - 0
loops/solutions/print_diamond.py

@@ -0,0 +1,17 @@
+def print_diamond(width, symbol):
+	for a in range(1, width + 1):
+		string = ''
+		for b in range(width - a):
+			string += ' '
+		for c in range(a):
+			string += symbol + ' '
+		print(string)
+	for d in range(1, width):
+		string = ''
+		for e in range(d):
+			string += ' '
+		for f in range(width - d):
+			string += symbol + ' '
+		print(string)
+			
+print_diamond(3, '+')

+ 8 - 0
loops/solutions/print_rect.py

@@ -0,0 +1,8 @@
+def print_rect(width, height, symbol):
+	string = ''
+	for b in range(width):
+		string += symbol + ' '
+	for a in range(height):
+		print(string)
+
+print_rect(4, 6, '%')

+ 23 - 0
loops/solutions/print_striped_diamond.py

@@ -0,0 +1,23 @@
+def print_striped_diamond(width, symbol1, symbol2):
+	for a in range(1, width + 1):
+		string = ''
+		for b in range(width - a):
+			string += ' '
+		for c in range(a):
+			if c % 2 == 0:
+				string += symbol1 + ' '
+			else:
+				string += symbol2 + ' '
+		print(string)
+	for d in range(1, width):
+		string = ''
+		for e in range(d):
+			string += ' '
+		for f in range(width - d):
+			if (d + f) % 2 == 0:
+				string += symbol1 + ' '
+			else:
+				string += symbol2 + ' '
+		print(string)
+
+print_striped_diamond(7, '.', '%') 

+ 14 - 0
loops/solutions/print_triangle.py

@@ -0,0 +1,14 @@
+def print_triangle(leg, symbol, right_side_up):
+	for a in range(1, leg + 1):
+		string = ''
+		if right_side_up:
+			for b in range(leg + 1 - a):
+				string += symbol + ' '
+		else:
+			for b in range(a + 1):
+				string += symbol + ' '
+		print(string)
+
+print_triangle(3, '@', False)
+print('\n')
+print_triangle(3, '@', True)

+ 21 - 0
loops/solutions/shift_left.py

@@ -0,0 +1,21 @@
+def shift_left(t):
+	""" (list) -> NoneType
+	Shift each item in t one position to the left
+	and shift the first item to
+	the last position.
+	Precondition: len(L) >= 1
+	>>> lst = ['a', 'b', 'c', 'd']
+	>>> shift_left(lst)
+	>>> lst
+	['b', 'c', 'd', 'a']
+	"""
+	a = t[0]
+	for i in range(1, len(t) + 1, 2):
+		b = t[-i]
+		t[-i] = a
+		a = t[-i - 1]
+		t[-i - 1] = b
+
+lst = ['a', 'b', 'c', 'd']
+shift_left(lst)
+print(lst)