Lädt...


🔧 Nested beauty


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Weekly Challenge 300

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

With this being the three hundredth challenge, let me personally thank Mohammad for all the work he does each week on behalf of everyone in Team PWC.

Task 1: Beautiful Arrangement

Task

You are given a positive integer, $int.

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

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. perm[i] is divisible by i
  2. i is divisible by perm[i]

My solution

For this task, I use the permutations function from the itertool module to work through all permutations.

Then it's just a matter of determining if this permutation meets the specified criteria. If it doesn't, I move to the next permutation. If it does, I add one to the count variable.

def beautiful_arrangement(n: list) -> str:
    count = 0

    for p in permutations(range(1, n+1)):
        for i in range(n):
            if p[i] % (i+1) != 0 and (i+1) % p[i] != 0:
                break
        else:
            count += 1

    return count

There may be a more efficient way to compute the results that doesn't involve brute force. My code would become very inefficient on larger numbers. I didn't not spend any time investigating this.

Examples

$ ./ch-1.py 1
1

$ ./ch-1.py 2
2

$ ./ch-1.py 10
700

Task 2: Nested Array

Task

You are given an array of integers, @ints of length n containing permutation of the numbers in the range [0, n - 1].

Write a script to build a set, set[i] = ints[i], ints[ints[i]], ints[ints[ints[i]]], ..., subjected to the following rules:

  1. The first element in set[i] starts with the selection of elements ints[i].
  2. The next element in set[i] should be ints[ints[i]], and then ints[ints[ints[i]]], and so on.
  3. We stop adding right before a duplicate element occurs in set[i].

My solution

This is relatively straight forward. I start with a variable called longest_set, set to 0. I then iterate through each starting position and set the this_set list to be the first item of the set (i.e ints[i]). I keep adding to this set while ints[this_set[-1]] does not appear in the this_set list. After this is done, I compare the length of the this_set list against the longest_set value. If it is greater, I update the longest_set value.

def nested_array(ints: list) -> int:
    longest_set = 0

    for start in range(len(ints)):
        this_set = [ints[start]]

        while ints[this_set[-1]] not in this_set:
            this_set.append(ints[this_set[-1]])

        if longest_set < len(this_set):
            longest_set = len(this_set)

    return longest_set

Examples

$ ./ch-2.py 5 4 0 3 1 6 2
4

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

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

📰 Nested Dictionary Python — A Complete Guide to Python Nested Dictionaries


📈 30.47 Punkte
🔧 AI Nachrichten

🔧 Nested beauty


📈 29.41 Punkte
🔧 Programmierung

🕵️ Medium CVE-2017-17595: Beauty parlour booking script project Beauty parlour booking script


📈 28.34 Punkte
🕵️ Sicherheitslücken

🎥 Dove’s Real Beauty campaign thrived by targeting a common enemy: unrealistic beauty standards.


📈 28.34 Punkte
🎥 Video | Youtube

🎥 Dove’s Real Beauty campaign thrived by targeting a common enemy: unrealistic beauty standards.


📈 28.34 Punkte
🎥 Video | Youtube

🔧 „Augmented Beauty“: So will L’Oréal Beauty ins Metaverse bringen


📈 28.34 Punkte
🔧 Programmierung

🕵️ P&G online beauty store First Aid Beauty hit by Magecart attack


📈 28.34 Punkte
🕵️ Hacking

🔧 Problem with redirection and nested routes


📈 15.23 Punkte
🔧 Programmierung

🐧 Developing for Mobile Linux with Phosh - Part 0: Running Nested


📈 15.23 Punkte
🐧 Linux Tipps

🐧 Need help with an IPTables Rule, nested VM has no internet connectivity


📈 15.23 Punkte
🐧 Linux Tipps

🔧 Nextjs Nested Route/segment


📈 15.23 Punkte
🔧 Programmierung

🔧 Python day-28 Dictionary, Frequency of character using nested loops


📈 15.23 Punkte
🔧 Programmierung

🐧 [$] Nested bottom-half locking for realtime kernels


📈 15.23 Punkte
🐧 Linux Tipps

🔧 TIL: Nested for/while loops can have O(N) time complexity in some cases (DSA for technical interviews)


📈 15.23 Punkte
🔧 Programmierung

🪟 Ah, Build Nummer 20303! Nested Virtualization für Ryzen


📈 15.23 Punkte
🪟 Windows Server

🔧 Nested Virtualization using VMware Workstation, Esxi and Windows 10 on Linux - Part 1


📈 15.23 Punkte
🔧 Programmierung

🔧 Flattening a Deeply Nested Object: A Step-by-Step Guide


📈 15.23 Punkte
🔧 Programmierung

🔧 Daily JavaScript Challenge #JS-31: Flatten Nested Objects


📈 15.23 Punkte
🔧 Programmierung

🔧 How to Work with Nested Forms in Rails


📈 15.23 Punkte
🔧 Programmierung

🔧 Nested Tree vs Flat Tree


📈 15.23 Punkte
🔧 Programmierung

🔧 List Within a List in Python – How to Initialize a Nested List


📈 15.23 Punkte
🔧 Programmierung

⚠️ #0daytoday #Linux Nested User Namespace idmap Limit Local Privilege Escalation Exploit [#0day #Exploit]


📈 15.23 Punkte
⚠️ PoC

🔧 CSS Grid: Nested Grid Layouts


📈 15.23 Punkte
🔧 Programmierung

🔧 Efficiently Extract Nested URL Parameters with Recursive Logic in JavaScript


📈 15.23 Punkte
🔧 Programmierung

🔧 The Right Way to Clone Nested Object/Array (Deep Clone) in Javascript


📈 15.23 Punkte
🔧 Programmierung

🔧 TIL: Nested for/while loops can have O(N) time complexity in some cases (DSA for technical interviews)


📈 15.23 Punkte
🔧 Programmierung

🕵️ package nested-object-assign up to 1.0.3 Prototype code injection


📈 15.23 Punkte
🕵️ Sicherheitslücken

🔧 Nested Virtualization using VMware Workstation, Esxi and Windows 10 on Linux - Part 1


📈 15.23 Punkte
🔧 Programmierung

🔧 Understanding CSS Background Coloring for Nested Elements


📈 15.23 Punkte
🔧 Programmierung

🔧 Quick Guide: Accessing Array Values in Nested Hashes in Ruby - AI generated


📈 15.23 Punkte
🔧 Programmierung

🐧 How to Enable Nested Virtualization on Proxmox VE 8 Virtual Machines (VMs)


📈 15.23 Punkte
🐧 Linux Tipps

🐧 How to Create Nested Table in HTML


📈 15.23 Punkte
🐧 Linux Tipps

⚠️ [local] Linux - Nested User Namespace idmap Limit Local Privilege Escalation (Metasploit)


📈 15.23 Punkte
⚠️ PoC

matomo