diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-16 18:34:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-16 18:34:09 +0000 |
| commit | a0df2b64bcda24f6ea2b4cd00b9cefe82bcae8ed (patch) | |
| tree | 51f53decb2e9975cdec74117d375e5fd8ff8a280 /challenge-252/jeanluc2020/python/ch-1.py | |
| parent | 90c91178a89e0d1e49b4eb0cf4145288c9f8c924 (diff) | |
| parent | 459539a24263e4d6ed17df295649d2ae521bd3b9 (diff) | |
| download | perlweeklychallenge-club-a0df2b64bcda24f6ea2b4cd00b9cefe82bcae8ed.tar.gz perlweeklychallenge-club-a0df2b64bcda24f6ea2b4cd00b9cefe82bcae8ed.tar.bz2 perlweeklychallenge-club-a0df2b64bcda24f6ea2b4cd00b9cefe82bcae8ed.zip | |
Merge pull request #9412 from jeanluc2020/jeanluc-252
Add solution 252.
Diffstat (limited to 'challenge-252/jeanluc2020/python/ch-1.py')
| -rwxr-xr-x | challenge-252/jeanluc2020/python/ch-1.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-252/jeanluc2020/python/ch-1.py b/challenge-252/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..baf5f0e66e --- /dev/null +++ b/challenge-252/jeanluc2020/python/ch-1.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/#TASK1 +# +# Task 1: Special Numbers +# ======================= +# +# You are given an array of integers, @ints. +# +# Write a script to find the sum of the squares of all special elements of the +# given array. +# +## An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +## Where n is the length of the given array. Also the array is 1-indexed for the task. +# +## Example 1 +## +## Input: @ints = (1, 2, 3, 4) +## Output: 21 +## +## There are exactly 3 special elements in the given array: +## $ints[1] since 1 divides 4, +## $ints[2] since 2 divides 4, and +## $ints[4] since 4 divides 4. +## +## Hence, the sum of the squares of all special elements of given array: +## 1 * 1 + 2 * 2 + 4 * 4 = 21. +# +## Example 2 +## +## Input: @ints = (2, 7, 1, 19, 18, 3) +## Output: 63 +## +## There are exactly 4 special elements in the given array: +## $ints[1] since 1 divides 6, +## $ints[2] since 2 divides 6, +## $ints[3] since 3 divides 6, and +## $ints[6] since 6 divides 6. +## +## Hence, the sum of the squares of all special elements of given array: +## 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 +# +############################################################ +## +## discussion +## +############################################################ +# +# We initialize a variable $result = 0, then we look at each +# element in the list and add its square to $result if $i divides n. + + +def special_numbers(ints: list) -> int: + print("Input: (", ", ".join([str(x) for x in ints]), ")") + n = len(ints) + result = 0 + for i in range(n): + elem = ints[i] + if n % (i+1) == 0: + result += elem*elem + print(f"Output: {result}") + return result + +special_numbers([1, 2, 3, 4]) +special_numbers([2, 7, 1, 19, 18, 3]) + |
