+ 3
A positive integer m is a sum of squares if it can be written as k + l where k > 0, l > 0 and both k and l are perfect squares.
A positive integer m is a sum of squares if it can be written as k + l where k > 0, l > 0 and both k and l are perfect squares. Write a Python function sumofsquares(m) that takes an integer m returns True if m is a sum of squares and False otherwise. (If m is not positive, your function should return False.) Here are some examples to show how your function should work. >>> sumofsquares(41) True >>> sumofsquares(30) False >>> sumofsquares(17) True https://www.sololearn.com/discuss/1089286/?ref=app
5 Respuestas
+ 1
def sumofsquares(n):
if n<0:
return False
for i in range(1, int(n**0.5) + 1 ):
for j in range(1, int(n**0.5) + 1 ):
m = i**2 + j**2
if m == n:
return True
elif m > n:
break
return False
def wellbracketed(ss):
depth=0
for i in range(len(ss)):
char = ss[i]
if char=="(":
depth+=1
elif char==")":
if depth>0:
depth-=1
else:
return False
return depth==0
def rotatelist(ll, shft):
l = len(ll)
shft = shft % l
ans = ll[l-shft:] + ll[:l-shft]
return ans
Is this answer correct ? Plzz verify it and suggest me the changes
+ 3
+ 1
Define a Python function ascending(l) that returns True if each element in its input list is at least as big as the one before it.
Define a Python function alternating(l) that returns True if the values in the input list alternately go up and down (in a strict manner).
A two dimensional matrix can be represented in Python row-wise, as a list of lists: each inner list represents one row of the matrix. For instance, the matrix1 2 3 4 5 6 would be represented as [[1,2,3],[4,5,6]].
Write a Python function matmult(m1,m2) that takes as input two matrices using this row-wise representation and returns the matrix product m1*m2 using the same representation.
You may assume that the input matrices are well-formed and have compatible dimensions.
For instance:
>>> matmult([[1,2],[3,4]],[[1,0],[0,1]]) [[1,2],[3,4]] >>> matmult([[1,2,3],[4,5,6]],[[1,4],[2,5],[3,6]]) [[14, 32], [32, 77]]
0
it works....
nice job...
no changes everything is fine..
0
Histogram
Write a Python function histogram(l) that takes as input a list of integers with repetitions and returns a list of pairs as follows:
for each number n that appears in l, there should be exactly one pair (n,r) in the list returned by the function, where r is is the number of repetitions of n in l.
the final list should be sorted in ascending order by r, the number of repetitions. For numbers that occur with the same number of repetitions, arrange the pairs in ascending order of the value of the number.