File size: 2,339 Bytes
87151cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | from typing import List
class DoubleForLoop:
@staticmethod
def sum_square(n: int) -> int:
"""Sum of squares of numbers from 0 to n (exclusive)
Args:
n (int): Number to sum up to
Returns:
int: Sum of squares of numbers from 0 to n
"""
sum_ = 0
for i in range(n):
for j in range(n):
if i == j:
sum_ += i * j
return sum_
@staticmethod
def sum_triangle(n: int) -> int:
"""Sum of triangle of numbers from 0 to n (exclusive)
Args:
n (int): Number to sum up to
Returns:
int: Sum of triangle of numbers from 0 to n
"""
sum_ = 0
for i in range(n):
for j in range(i + 1):
sum_ += j
return sum_
@staticmethod
def count_pairs(arr: List[int]) -> int:
"""Count pairs of numbers in an array
A pair is defined as exactly two numbers in the array that are equal.
Args:
arr (List[int]): Array of integers
Returns:
int: Number of pairs in the array
"""
count = 0
for i in range(len(arr)):
ndup = 0
for j in range(len(arr)):
if arr[i] == arr[j]:
ndup += 1
if ndup == 2:
count += 1
return count // 2
@staticmethod
def count_duplicates(arr0: List[int], arr1: List[int]) -> int:
"""Count duplicates between two arrays
Args:
arr0 (List[int]): Array of integers
arr1 (List[int]): Array of integers
Returns:
int: Number of duplicates between the two arrays
"""
count = 0
for i in range(len(arr0)):
for j in range(len(arr1)):
if i == j and arr0[i] == arr1[j]:
count += 1
return count
@staticmethod
def sum_matrix(m: List[List[int]]) -> int:
"""Sum of matrix of integers
Args:
m (List[List[int]]): Matrix of integers
Returns:
int: Sum of matrix of integers
"""
sum_ = 0
for i in range(len(m)):
for j in range(len(m[i])):
sum_ += m[i][j]
return sum_
|