DEV Community

Simon Green
Simon Green

Posted on

Special zeros

Weekly Challenge 252

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in
Python first, and then converted to Perl. It's a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Special Numbers

Task

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.

My solution

Two pretty straight forward tasks this week, great way to get back into the new year. For this task, I create a variable length for the length of the list, and solution which starts with 0. Then it is simply a matter of iterating through the array as follows:

for pos in range(length):
    if length % (pos+1) == 0:
        solution += ints[pos] ** 2
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py 1 2 3 4
21

$ ./ch-1.py 2 7 1 19 18 3
63
Enter fullscreen mode Exit fullscreen mode

Task 2: Unique Sum Zero

Task

You are given an integer, $n.

Write a script to find an array containing $n unique integers such that they add up to zero.

My solution

Looking at the example, I don't seem obvious how Mohammad come up with the numbers. Not to worry, I only need to come up with a solution that meets the criteria.

If $n is an even number, I can create pairs of an integer and its negative counterpart to add up to zero. If $n is odd, I can add a zero to ensure we have the correct number of integers.

solution = []
half = n // 2

if n % 2 == 1:
    solution = [0]

for i in range(half):
    # Add a pair of numbers
    solution.insert(0, -i-1)
    solution.append(i+1)

print(*solution, sep=', ')
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py 5
-2, -1, 0, 1, 2

$ ./ch-2.py 3
-1, 0, 1

$ ./ch-2.py 1
0

$ ./ch-2.py 6
-3, -2, -1, 1, 2, 3
Enter fullscreen mode Exit fullscreen mode

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay