Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Sum-of-squares/Python/sum-of-squares-1.py
Normal file
5
Task/Sum-of-squares/Python/sum-of-squares-1.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
sum(x * x for x in [1, 2, 3, 4, 5])
|
||||
# or
|
||||
sum(x ** 2 for x in [1, 2, 3, 4, 5])
|
||||
# or
|
||||
sum(pow(x, 2) for x in [1, 2, 3, 4, 5])
|
||||
27
Task/Sum-of-squares/Python/sum-of-squares-2.py
Normal file
27
Task/Sum-of-squares/Python/sum-of-squares-2.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# using lambda and map:
|
||||
sum(map(lambda x: x * x, [1, 2, 3, 4, 5]))
|
||||
# or
|
||||
sum(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))
|
||||
# or
|
||||
sum(map(lambda x: pow(x, 2), [1, 2, 3, 4, 5]))
|
||||
|
||||
# using pow and repeat
|
||||
from itertools import repeat
|
||||
sum(map(pow, [1, 2, 3, 4, 5], repeat(2)))
|
||||
|
||||
# using starmap and mul
|
||||
from itertools import starmap
|
||||
from operator import mul
|
||||
a = [1, 2, 3, 4, 5]
|
||||
sum(starmap(mul, zip(a, a)))
|
||||
|
||||
# using reduce
|
||||
from functools import reduce
|
||||
powers_of_two = (x * x for x in [1, 2, 3, 4, 5])
|
||||
reduce(lambda x, y : x + y, powers_of_two)
|
||||
# or
|
||||
from operator import add
|
||||
powers_of_two = (x * x for x in [1, 2, 3, 4, 5])
|
||||
reduce(add, powers_of_two)
|
||||
# or using a bit more complex lambda
|
||||
reduce(lambda a, x: a + x*x, [1, 2, 3, 4, 5])
|
||||
3
Task/Sum-of-squares/Python/sum-of-squares-3.py
Normal file
3
Task/Sum-of-squares/Python/sum-of-squares-3.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import numpy as np
|
||||
a = np.array([1, 2, 3, 4, 5])
|
||||
np.sum(a ** 2)
|
||||
Loading…
Add table
Add a link
Reference in a new issue