Ad

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

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.

Code
Diff
  • 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]
Code
Diff
  • 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 = 0
    • for num in string:
    • num = int(num)
    • all_sum += num
    • if num % 2 == 0:
    • even_list.append(num)
    • if num % 2 != 0:
    • odd_list.append(num)
    • if option == 0:
    • return all_sum
    • if 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

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
Code
Diff
  • 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"

Code
Diff
  • 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)

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