Menu

Weekly Challenge: A different average
📰

Weekly Challenge: A different average

DEV Community·Simon Green·8 days ago
#LO7IGMgw
#dev#word#words#return#article
Reading 0:00
15s threshold

Weekly Challenge 398

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. Unless otherwise stated, Copilot (and other AI tools) have NOT been used to generate the solution. It's a great way for us all to practice some coding.

Challenge , My solutions

Task 1: Divisible Average

You are given an array of positive integers.

Write a script to return the closest integer to the mean value of all even integers that are divisible by 3. If none is divisible, return 0.

My solution

An even number that is divisible by 3, means it must be divisible by 6. I create a list (array in Perl) called eligible_ints which has all integers that match this critera.

If the list is empty (i.e. there are no integers divisble by 6), I return 0. Otherwise I return the nearest integer.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.