Lädt...


🔧 Double Luhn


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Weekly Challenge 290

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: Double Exist

Tasks

You are given an array of integers, @ints.

Write a script to find if there exist two indices $i and $j such that:

  1. $i != $j
  2. 0 <= ($i, $j) < scalar @ints
  3. $ints[$i] == 2 * $ints[$j]

My solution

This seems relatively straight forward, but there is a massive gotcha that hopefully other Team PWC members also noticed. For this task, I loop through the list and see if a value that is twice its value exist.

However, if the value is '0' (and thus 0 × 2 = 0), I need to check that there were at least two zeros in the list.

def double_exists(ints: list) -> bool:

    for i in ints:
        if i * 2 in ints:
            if i != 0 or ints.count(0) > 1:
                return True

    return False

Examples

$ ./ch-1.py 6 2 3 3
true

$ ./ch-1.py 3 1 4 13
false

$ ./ch-1.py 2 1 4 2
true

$ ./ch-1.py 1 3 0
false

$ ./ch-1.py 1 0 3 0
true

Task 2: Luhn’s Algorithm

Task

You are given a string $str containing digits (and possibly other characters which can be ignored). The last digit is the payload; consider it separately. Counting from the right, double the value of the first, third, etc. of the remaining digits.

For each value now greater than 9, sum its digits.

The correct check digit is that which, added to the sum of all values, would bring the total mod 10 to zero.

Return true if and only if the payload is equal to the correct check digit.

My solution

I start this task by removing non-digit characters from the string, and turn the reversed string into a list of integers.

I then use the supplied formula, alternating between adding the value to count or multiplying it by two and removing 9. If the resulting count is divisible by 10, I return True, otherwise I return False.

def luhn_algorithm(s: str) -> bool:
    s = re.sub('[^0-9]', '', s)
    ints = [int(n) for n in s[::-1]]

    count = 0
    for pos, i in enumerate(ints):
        if pos % 2 == 1:
            i *= 2
            if i > 9:
                i -= 9
        count += i
    return count % 10 == 0

Examples

$ ./ch-2.py  17893729974
true

$ ./ch-2.py  "4137 8947 1175 5904"
true

$ ./ch-2.py "4137 8974 1175 5904"
false
...

🔧 Double Luhn


📈 38.29 Punkte
🔧 Programmierung

🔧 Finding Double Existence and Applying Luhn's Algorithm


📈 38.29 Punkte
🔧 Programmierung

🔧 The Luhn's algorithm in practice


📈 28.58 Punkte
🔧 Programmierung

🔧 Luhn's Algorithm: Credit Card Validation


📈 28.58 Punkte
🔧 Programmierung

🔧 Python Guide: Credit Card Number Validation Using Luhn's Algorithm


📈 28.58 Punkte
🔧 Programmierung

📰 Luhn-Formel (Modulo 10)


📈 28.58 Punkte
📰 IT Security Nachrichten

📰 Luhn-Formel (Modulo 10)


📈 28.58 Punkte
📰 IT Security Nachrichten

🔧 Double the Talk, Double the Recording: Capturing Both Sides in Interpreted Zoom Meetings


📈 19.42 Punkte
🔧 Programmierung

📰 Call of Duty Black Ops Cold War: Double XP und Double Weapon XP am Wochenende


📈 19.42 Punkte
📰 IT Nachrichten

🔧 System Design 11 - Data Replication: Double the Data, Double the Availability


📈 19.42 Punkte
🔧 Programmierung

📰 Huawei Mate 9 vs Porsche Design Mate 9: Double the Price, Double the Fun


📈 19.42 Punkte
📰 IT Security

🔧 DATATYPES IN C (double, long double, void, bool)


📈 19.42 Punkte
🔧 Programmierung

📰 Huawei Mate 9 vs Porsche Design Mate 9: Double the Price, Double the Fun


📈 19.42 Punkte
📰 IT Security

🐧 Double Dragon is coming back from Arc System Works with Double Dragon Revive


📈 19.42 Punkte
🐧 Linux Tipps

🍏 JSAUX FlipGo Portable Dual Monitor review: Double your screens, double your productivity


📈 19.42 Punkte
🍏 iOS / Mac OS

📰 Jubiläum: 70. Double-Double von Jakob Pöltl in der NBA


📈 19.42 Punkte
📰 IT Nachrichten

📰 Jakob Pöltl erzielt Drittes Double-Double in Folge


📈 19.42 Punkte
📰 IT Nachrichten

📰 Linux gets double-quick double-update to fix kernel Oops!


📈 19.42 Punkte
📰 IT Security Nachrichten

📰 Raptors verlieren trotz Pöltls Double-Double gegen Lakers


📈 19.42 Punkte
📰 IT Nachrichten

📰 60. Double-Double von Jakob Pöltl in der NBA


📈 19.42 Punkte
📰 IT Nachrichten

📰 Pöltl gelingt bei zweitem Spurs-Saisonsieg erstes Double-Double


📈 19.42 Punkte
📰 IT Nachrichten

🕵️ Double your signal, double your fun


📈 19.42 Punkte
🕵️ Hacking

🕵️ Double your signal, double your fun


📈 19.42 Punkte
🕵️ Hacking

🪟 Mouse Double Clicks (FIXED): Why Is My Mouse Double Clicking?


📈 19.42 Punkte
🪟 Windows Tipps

📰 Mouse Double Clicks (FIXED): Why Is My Mouse Double Clicking?


📈 19.42 Punkte
🖥️ Betriebssysteme

📰 Black Ops Cold War & Warzone: Double XP und Double Weapon XP an diesem Wochenende – Neues Update ...


📈 19.42 Punkte
📰 IT Nachrichten

🕵️ CVE-2023-25801 | Google TensorFlow up to 2.11.0 double free (GHSA-f49c-87jh-g47q)


📈 9.71 Punkte
🕵️ Sicherheitslücken

📰 Tesla Keeps Crossing Double-Yellow Causing Head-on Collisions


📈 9.71 Punkte
📰 IT Security Nachrichten

🕵️ CVE-2024-30027 | Microsoft Windows up to Server 2022 23H2 NTFS double free


📈 9.71 Punkte
🕵️ Sicherheitslücken

📰 The Double Pendulum and Beyond Investigating Chaos With Multi-Armed Pendulums


📈 9.71 Punkte
🔧 AI Nachrichten

💾 XNU Data Race Remote Double-Free


📈 9.71 Punkte
💾 IT Security Tools

📰 Double Fine: Studio von Microsoft übernommen – Erster Gameplay-Trailer zu Psychonauts 2


📈 9.71 Punkte
📰 IT Nachrichten

🕵️ CVE-2024-45402 | h2o picotls TLS Protocol double free (GHSA-w7c8-wjx9-vvvv)


📈 9.71 Punkte
🕵️ Sicherheitslücken

matomo