Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more

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]
            
  • 11 def square(n):
    2- l=''
    3- square1=[]
    4- for i in range(n):
    5- l+='*'
    6- for i in range(n):
    7- square1.append(l)
    8- for i in range(n):
    9- print(square1[i])
    10- return square1
    2+ print(("*" *n+"\n")*n) # to see the result
    3+
    4+ return (("*" *n+"\n")*n)[:-1]
    5+
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
  • 11 def add(string, option):
    2- even_list = []
    3- odd_list = []
    4- all_sum = 0
    5- for num in string:
    6- num = int(num)
    7- all_sum += num
    8- if num % 2 == 0:
    9- even_list.append(num)
    10- if num % 2 != 0:
    11- odd_list.append(num)
    12- if option == 0:
    13- return all_sum
    14- if option == 1:
    15- return sum(odd_list)
    16- if option == 2:
    17- return sum(even_list)
    18-
    2+ odd,even = 0,0
    3+ for x in string:
    4+ x = int(x)
    5+ if x%2 ==0:even+=x
    6+ if x%2 ==1:odd+=x
    7+ if option ==0:
    8+ return odd+even
    9+ 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)
  • 1-def distance(array):
    2- for i in range(len(array)):
    3- if "y" in array[i] : y = abs(array[i].index("y") - i)
    4- if "x" in array[i] : x = abs(array[i].index("x") - i)
    5- return (x+y)
    6-
    7-
    8-
    1+def distance(a):
    2+ y,y1 = 0,0
    3+ for i in range(len(a)):
    4+ if "y" in a[i]: y = i
    5+ if "x" in a[i]: y1 = i
    6+
    7+ x ,x1 = a[y].index("y"), a[y1].index("x")
    8+ 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)    
    
        
       
  • 11 def distance(array):
    22 for i in range(len(array)):
    33 if "y" in array[i] : y = abs(array[i].index("y") - i)
    44 if "x" in array[i] : x = abs(array[i].index("x") - i)
    55 return (x+y)
    6+
    7+
    8+

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