Weekly Challenge 142
Simon Green
Posted on December 12, 2021
Before I begin...
As Mohammad mention in his blog, December 18th will mark 1,000 days since The Weekly Challenge was born. I'd like to thank him for all the tireless work he puts into the challenge to make it a success.
I'd also like to shout out to Colin Crain for his weekly reviews of the Perl submission, and Pete Sergeant and the Perl Careers team for donating the monthly giveaways.
And finally thank you to every member of Team PWC. As much as I enjoy doing the challenge myself, I get even more out of it by reading other blog posts to see how other people tackle each task.
TASK #1 › Divisor Last Digit
Task
You are given positive integers, $m
and $n
.
Write a script to find total count of divisors of $m
having last digit $n
.
My solution
This is a pretty straight forward task. It's a mash up of the two tasks last week, the first dealt with divisors, and the second taking an input of $m
and $n
.
I do have an observation about the task itself. The second value has to be a positive integer, but the solutions only match the 'last digit'. That means any value above nine isn't valid. As the number zero is not a positive integer, it also does not meet the criteria. I was definitely in two minds about including the option of 0, but the task clearly states a 'positive integer', so I stuck with that.
As for the task itself, I have a function _get_divisors
. Given a number, it will return a list of all divisors. Like last weeks task, I only made modest optimizations to perform this quickly.
I filter this list by checking that the last digit of each divisor ( × % 10
) is the second value, and display the number of elements in the list.
The Perl code is a transliteration of the Python code.
Examples
$ ./ch-1.py 24 2
2
$ ./ch-1.py 30 5
2
TASK #2 › Sleep Sort
Task
Another joke sort similar to JortSort suggested by champion Adam Russell.
You are given a list of numbers. Write a script to implement Sleep Sort
My solution
I found this task interesting because I've never really used threads in either Python or Perl. Once I read up on it, it was a matter of creating a thread for each number. After creating all the threads, we join them so our script won't exit before all the threads are completed. Each thread will sleep for the required number of seconds, and then display the number.
Like with the first task, the Perl code is a transliteration of the Python code. The only important difference is that I use Time::HiRes's version of sleep to allow sleeping for parts of a second.
Examples
$ ./ch-2.py 3 0.1 2 2.5 1
0.1
1
2
2.5
3
$ ./ch-2.pl 3 0.1 2 2.5 1
0.1
1
2
2.5
3
Posted on December 12, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.