given a list of lists. each inner list is same size.
they can represent a square or a rectangle.
return information about it.
return its perimeter , area,total number of "*", rectangle/square
s = "*"
array = [[s,s,s,s],[s,s,s,s],[s,s,s,s],[s,s,s,s]]
===> permimeter = 16 ,area = 16, "*" = 16 , "square"
def determine_params(list):
return #its perimeter , area,total number of "*", rectangle/square
# s = "*"
# array = [[s,s,s,s],[s,s,s,s],[s,s,s,s],[s,s,s,s]]
# test.assert_equals(determine_params(array),16,16,16,"square")
print a square of numbers but in a string rather then a list.
improvement ideas, add a second parameter to choose whether the output should be a list or string.
def square(n): print(("*" *n+"\n")*n) # to see the result return (("*" *n+"\n")*n)[:-1]
- def square(n):
l=''square1=[]for i in range(n):l+='*'for i in range(n):square1.append(l)for i in range(n):print(square1[i])return square1- print(("*" *n+"\n")*n) # to see the result
- return (("*" *n+"\n")*n)[:-1]
# TODO: Given a number, write a function that prints a square with the caracter '*' # These are some of the methods available: # test.expect(boolean, [optional] message) # test.assert_equals(actual, expected, [optional] message) # test.assert_not_equals(actual, expected, [optional] message) # You can use Test.describe and Test.it to write BDD style test groupings # test.assert_equals(square(2),['**', '**']) # test.assert_equals(square(3),['***', '***', '***']) # test.assert_equals(square(4),['****', '****', '****', '****']) # test.assert_equals(square(5),['*****', '*****', '*****', '*****', '*****']) test.assert_equals(square(2),"**\n**") test.assert_equals(square(5),'*****\n*****\n*****\n*****\n*****') test.assert_equals(square(4),'****\n****\n****\n****')
- # TODO: Given a number, write a function that prints a square with the caracter '*'
- # These are some of the methods available:
- # test.expect(boolean, [optional] message)
- # test.assert_equals(actual, expected, [optional] message)
- # test.assert_not_equals(actual, expected, [optional] message)
- # You can use Test.describe and Test.it to write BDD style test groupings
test.assert_equals(square(2),['**', '**'])test.assert_equals(square(3),['***', '***', '***'])test.assert_equals(square(4),['****', '****', '****', '****'])test.assert_equals(square(5),['*****', '*****', '*****', '*****', '*****'])- # test.assert_equals(square(2),['**', '**'])
- # test.assert_equals(square(3),['***', '***', '***'])
- # test.assert_equals(square(4),['****', '****', '****', '****'])
- # test.assert_equals(square(5),['*****', '*****', '*****', '*****', '*****'])
- test.assert_equals(square(2),"**\n**")
- test.assert_equals(square(5),'*****\n*****\n*****\n*****\n*****')
- test.assert_equals(square(4),'****\n****\n****\n****')
def add(string, option): odd,even = 0,0 for x in string: x = int(x) if x%2 ==0:even+=x if x%2 ==1:odd+=x if option ==0: return odd+even return even if option == 2 else odd
- def add(string, option):
even_list = []odd_list = []all_sum = 0for num in string:num = int(num)all_sum += numif num % 2 == 0:even_list.append(num)if num % 2 != 0:odd_list.append(num)if option == 0:return all_sumif option == 1:return sum(odd_list)if option == 2:return sum(even_list)- odd,even = 0,0
- for x in string:
- x = int(x)
- if x%2 ==0:even+=x
- if x%2 ==1:odd+=x
- if option ==0:
- return odd+even
- return even if option == 2 else odd
test.assert_equals(add("1234567", 0),28) test.assert_equals(add("1234567", 1),16) test.assert_equals(add("1234567", 2),12)
test.assert_equals(add("1234567" , 0),28)test.assert_equals(add("1234567" , 1),16)test.assert_equals(add("1234567" , 2),12)- test.assert_equals(add("1234567", 0),28)
- test.assert_equals(add("1234567", 1),16)
- test.assert_equals(add("1234567", 2),12)
add the individual digits together.
the function also takes in 3 different integers with the following meaning
0 = add all of them
1 = add only odd numbers
2 = add only even numbers
e.g
add("1234567" , 0) ----> "1234567" = 28
add("1234567" , 1) ----> "1234567" = 16
add("1234567" , 2) ----> "1234567" = 12
def add(string, option):
return 28 #total depending on the option
test.assert_equals(add("1234567" , 0),28)
def distance(a): y,y1 = 0,0 for i in range(len(a)): if "y" in a[i]: y = i if "x" in a[i]: y1 = i x ,x1 = a[y].index("y"), a[y1].index("x") return abs(x -x1) + abs(y-y1)
def distance(array):for i in range(len(array)):if "y" in array[i] : y = abs(array[i].index("y") - i)if "x" in array[i] : x = abs(array[i].index("x") - i)return (x+y)- def distance(a):
- y,y1 = 0,0
- for i in range(len(a)):
- if "y" in a[i]: y = i
- if "x" in a[i]: y1 = i
- x ,x1 = a[y].index("y"), a[y1].index("x")
- return abs(x -x1) + abs(y-y1)
array should always have "y" and "x"
def distance(array): for i in range(len(array)): if "y" in array[i] : y = abs(array[i].index("y") - i) if "x" in array[i] : x = abs(array[i].index("x") - i) return (x+y)
- def distance(array):
- for i in range(len(array)):
- if "y" in array[i] : y = abs(array[i].index("y") - i)
- if "x" in array[i] : x = abs(array[i].index("x") - i)
return (x+y)- return (x+y)
array = [1,1,1,"y",1],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1] test.assert_equals(distance(array),3) array = [1,1,1,1,"y"],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1] test.assert_equals(distance(array),4) array = [1,1,1,1,"y",1,1],[1,1,1,1,"x",1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1] test.assert_equals(distance(array),1) array = [1,"y",1],[1,1,1],[1,1,"x"] test.assert_equals(distance(array),3)
- array = [1,1,1,"y",1],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1]
- test.assert_equals(distance(array),3)
- array = [1,1,1,1,"y"],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1]
- test.assert_equals(distance(array),4)
- array = [1,1,1,1,"y",1,1],[1,1,1,1,"x",1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1],[1,1,1,1,1,1,1]
- test.assert_equals(distance(array),1)
- array = [1,"y",1],[1,1,1],[1,1,"x"]
- test.assert_equals(distance(array),3)
cannot go diagonally
array = [1,1,1,y,1],
[1,1,1,1,1],
[1,1,x,1,1],
[1,1,1,1,1]
here you have to make 3 moves to get from y to x
def distance(array):
y = 0
y1 = 0
for inner_list in range(len(array)):
if "y" in array[inner_list]:
y = inner_list
if "x" in array[inner_list]:
y1 = inner_list
x = array[y].index("y")
x1 = array[y1].index("x")
dist = abs(x -x1) + abs(y-y1)
return dist
array = [1,1,1,"y",1],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1]
test.assert_equals(distance(array),3)
array = [1,1,1,1,"y"],[1,1,1,1,1],[1,1,"x",1,1],[1,1,1,1,1]
test.assert_equals(distance(array),4)