Logo

dev-resources.site

for different kinds of informations.

My Python Language Solution to Task 1: Beautiful Arrangement from The Weekly Challenge 300

Published at
12/17/2024
Categories
theweeklychallenge
python
perl
raku
Author
rcmcintosh
Author
10 person written this
rcmcintosh
open
My Python Language Solution to Task 1: Beautiful Arrangement from The Weekly Challenge 300

1. Introduction

The Weekly Challenge, organized by Mohammad S. Anwar, is a friendly competition in which developers compete by solving a pair of tasks. It encourages participation from developers of all languages and levels through learning, sharing, and having fun.

Task 1: Beautiful Arrangement from The Weekly Challenge invites developers to find the number of beautifully arranged permutations from among all permutations generated from a positive integer.

In this post I discuss, and present my solution to, Task 1: Beautiful Arrangement, and end a brief conclusion.

The Weekly Challenge 300 deadline is Sunday, December 23, 2024 at 23:59 (UK Time). To avoid bias, consider reading this post after competing.

2. Task 1: Beautiful Arrangement

You are given a positive integer, $int.

Write a script to return the number of beautiful arrangements that you can construct from $int.

A permutation of n integers, 1-indexed, is considered a beautiful arrangement if for every i (1 <= i <= n) either of the following is true:

  1. permutation[i] is divisible by i
  2. i is divisible by permutation[i]

The Weekly Challenge 300, Task 1: Beautiful Arrangement

Examples 1 and 2 present the expected outputs from given inputs.

Example 1

Input: $n = 2
Output: 2
Enter fullscreen mode Exit fullscreen mode

For n = 2 and with i integers such that (1 <= i <= n) there are two permutations (1, 2) and (2, 1). Output: 2 because both meet the beautiful arrangement requirements.

The permutation (1, 2) is a beautiful arrangement because all of its elements match the first condition:

  • At i = 1, permutation[1] = 1 satisfies the first condition, since one is divisible by one.
  • At i = 2, permutation[2] = 2 satisfies the first conditions, since two is divisible by two.

The permutation(2, 1) is also a beautiful arrangement because all of its elements match either the first or second condition:

  • At i = 1, permutation[1] = 2 satisfies the first condition, since two is divisible by one.
  • At i = 2, permutation[2] = 1 satisfies the second condition, since two is divisible by one.

Example 2

Input: $n = 1
Output: 1
Enter fullscreen mode Exit fullscreen mode

Example 3

Input: $n = 10
Output: 700
Enter fullscreen mode Exit fullscreen mode

3. My solution to Task 1

from itertools import permutations

def generate_permutations(n)
    iterable = list(range(1, n + 1))
    return permutations(iterable)

def count_beautiful_arrangements(perms):
    num_beautiful_arr = 0
    for perm in perms:
        is_beautiful_arr = True
        for value_index, value in enumerate(perm):
            if value % (value_index + 1) == 0:
                continue
            elif (value_index + 1) % value == 0:
                continue
            else:
                is_beautiful_arr = False
                break
        if is_beautiful_arr == True:
            num_beautiful_arr += 1
    return num_beautiful_arr
Enter fullscreen mode Exit fullscreen mode

My inelegant and unsophisticated solution utilizes two functions generate_permutations and count_beautiful_arrangements.

generate_permutations returns, for the parameter n, all permutations for the set where 1 <= i <= n.

  • iterable = list(range(1, n + 1)) generates a list of integers where 1 <= i <= n.
  • permutations(iterable), imported from the itertools module, generates all permutations of iterable.

count_beautiful_permutations returns, for the permutations iterable perms parameter, the total number of permutations in perms that match the beautiful arrangement conditions.

  • The outer loop for perm in... iterates through each permutation.
  • It starts with the assumption that perm is a beautiful arrangement (is_beautiful_arr = True).
    • The inner loop for value_index, value in... checks if each element of perm matches either condition 1 or condition 2.
      • If all elements match either condition, perm is counted as a beautiful arrangement.
      • Otherwise, if any element matches neither condition 1 nor condition 2, then is_beautiful_arr is set to False, the loop breaks early and perm is not counted as a beautiful arrangement.

4. Conclusion

In this post I discussed Task 1: Beautiful Arrangement and I presented my solution. My 'inelegant and unsophisticated' solution works, but it has considerable room for improvement.

perl Article's
30 articles in total
Favicon
How I used a named pipe to save memory and prevent crashes (in Perl)
Favicon
Perl 🐪 Weekly #703 - Teach me some Perl!
Favicon
Earn with 3 digits
Favicon
Step zero, step one
Favicon
Perl 🐪 Weekly #701 - Happier New Year!
Favicon
Hammering lists
Favicon
Nested beauty
Favicon
Catalyst Tricks: Map Request Parameters to a Model
Favicon
Rose::DB ORM and Perl
Favicon
Perl 🐪 Weekly #699 - Happy birthday Perl
Favicon
The one about words
Favicon
Perl 🐪 Weekly #702 - Perl Camel
Favicon
Perl 🐪 Weekly #700 - White Camel Award 2024
Favicon
Maximally Indexed Indices (PWC 298)
Favicon
Solving the Weekly Challenge 302 Task 1: Ones and Zeroes in Python
Favicon
Solving the Weekly Challenge 302 Task 2: Step by Step in Python
Favicon
Habemus Perl Logo!
Favicon
Weekly Challenge 297
Favicon
Perl Weekly #696 - Perl 5 is Perl
Favicon
The Run-Length of Matchsticks (PWC 296)
Favicon
PWC 296 String Compression
Favicon
PWC 293 Similar Dominos Done Badly
Favicon
Jump, but Don't Break the Game (PWC 295)
Favicon
Ups and Downs, Beginnings and Ends (PWC 297)
Favicon
The Break Game
Favicon
How to create Mac GUI applications in SPVM?
Favicon
My Python Language Solution to Task 2: Nested Array from The Weekly Challenge 300
Favicon
My Python Language Solution to Task 1: Beautiful Arrangement from The Weekly Challenge 300
Favicon
My Python Language Solution to Task 1 from The Weekly Challenge 299
Favicon
Perl Weekly #698 - Perl v5.41.7

Featured ones: