"यदि कोई कर्मचारी अपना काम अच्छी तरह से करना चाहता है, तो उसे पहले अपने औजारों को तेज करना होगा।" - कन्फ्यूशियस, "द एनालेक्ट्स ऑफ कन्फ्यूशियस। लू लिंगगोंग"
मुखपृष्ठ > प्रोग्रामिंग > पर्ल और गो में पासवर्ड की ताकत और संख्या सत्यापन की खोज

पर्ल और गो में पासवर्ड की ताकत और संख्या सत्यापन की खोज

2024-11-18 को प्रकाशित
ब्राउज़ करें:452

Exploring Password Strength and Number Validation in Perl and Go

इस लेख में, मैं पर्ल वीकली चैलेंज #287 की दो चुनौतियों से निपटूंगा: कमजोर पासवर्ड को मजबूत करना और संख्याओं को मान्य करना। मैं पर्ल और गो में कार्यान्वयन का प्रदर्शन करते हुए दोनों कार्यों के लिए समाधान प्रदान करूंगा।

विषयसूची

  1. कमजोर पासवर्ड को मजबूत करना
  2. नंबरों का सत्यापन
  3. निष्कर्ष

कमजोर पासवर्ड को मजबूत करना

पहला कार्य पासवर्ड को मजबूत बनाने के लिए आवश्यक परिवर्तनों की न्यूनतम संख्या निर्धारित करना है। एक मजबूत पासवर्ड के मानदंड हैं:

  1. इसमें कम से कम 6 अक्षर हैं।
  2. इसमें कम से कम एक लोअरकेस अक्षर, एक अपरकेस अक्षर और एक अंक होता है।
  3. इसमें लगातार तीन समान अक्षर नहीं हैं।

उदाहरण

  • इनपुट: "ए" → आउटपुट: 5
  • इनपुट: "एबी2" → आउटपुट: 3
  • इनपुट: "PaaSW0rd" → आउटपुट: 0
  • इनपुट: "Paaasw0rd" → आउटपुट: 1
  • इनपुट: "आआआ" → आउटपुट: 2

समाधान

पर्ल कार्यान्वयन

#!/usr/bin/perl
use strict;
use warnings;
use List::Util 'max';

# Function to count groups of three or more repeating characters
sub count_repeats {
    my ($str) = @_;
    my $repeats = 0;

    # Find repeating characters and count the required changes
    while ($str =~ /(.)\1{2,}/g) {
        $repeats  = int(length($&) / 3);
    }

    return $repeats;
}

# Function to calculate the minimum steps to create a strong password
sub minimum_steps_to_strong_password {
    my ($str) = @_;
    my $length = length($str);

    # Check if the password contains the required character types
    my $has_lower = $str =~ /[a-z]/;
    my $has_upper = $str =~ /[A-Z]/;
    my $has_digit = $str =~ /\d/;

    # Calculate the number of types needed
    my $types_needed = !$has_lower   !$has_upper   !$has_digit;
    my $repeats = count_repeats($str);

    # Return the minimum steps based on the length of the password
    return ($length 



पर्ल कार्यान्वयन के लिए परीक्षण

use strict;
use warnings;
use Test::More;
require "./ch-1.pl";

my @tests = (
    ["a", 5],
    ["aB2", 3],
    ["PaaSW0rd", 0],
    ["Paaasw0rd", 1],
    ["aaaaa", 2],
);

foreach my $test (@tests) {
    my ($input, $expected) = @$test;
    my $result = minimum_steps_to_strong_password($input);
    is($result, $expected, "Input: '$input'");
}

done_testing();

कार्यान्वयन जाओ

package main

import (
    "regexp"
)

func countRepeats(password string) int {
    repeats := 0
    count := 1

    for i := 1; i  b {
        return a
    }
    return b
}

गो कार्यान्वयन के लिए परीक्षण

package main

import (
    "testing"
)

func TestMinimumStepsToStrongPassword(t *testing.T) {
    tests := []struct {
        password string
        expected int
    }{
        {"a", 5},
        {"aB2", 3},
        {"PaaSW0rd", 0},
        {"Paaasw0rd", 1},
        {"aaaaa", 2},
    }

    for _, test := range tests {
        result := minimumStepsToStrongPassword(test.password)
        if result != test.expected {
            t.Errorf("For password '%s', expected %d but got %d", test.password, test.expected, result)
        }
    }
}

संख्याओं का सत्यापन

दूसरे कार्य में संख्याओं को मान्य करना शामिल है। लक्ष्य यह निर्धारित करना है कि दी गई स्ट्रिंग एक वैध संख्या का प्रतिनिधित्व करती है या नहीं। वैध संख्या के मानदंड हैं:

  1. एक पूर्णांक जिसके बाद वैकल्पिक रूप से घातांकीय संकेतन आता है।
  2. एक दशमलव संख्या जिसके बाद वैकल्पिक रूप से घातांकीय अंकन होता है।
  3. एक पूर्णांक में वैकल्पिक रूप से एक चिह्न (- या ) और उसके बाद अंक हो सकते हैं।

उदाहरण

  • इनपुट: "1" → आउटपुट: सत्य
  • इनपुट: "ए" → आउटपुट: गलत
  • इनपुट: "।" → आउटपुट: गलत
  • इनपुट: "1.2e4.2" → आउटपुट: गलत
  • इनपुट: "-1।" → आउटपुट: सत्य
  • इनपुट: "1ई-8" → आउटपुट: सत्य
  • इनपुट: ".44" → आउटपुट: सत्य

समाधान

पर्ल कार्यान्वयन

#!/usr/bin/perl
use strict;
use warnings;

sub is_valid_number {
    my ($str) = @_;

    # Regex for valid numbers
    my $regex = qr{
        ^            # Start of the string
        [ -]?        # Optional sign
        (?:          # Start of the number group
            \d       # Integer: at least one digit
            (?:      # Start of the optional decimal part
                \.   # Decimal point
                \d*  # Followed by zero or more digits
            )?       # Group is optional
            |        # or
            \.       # Just a decimal point
            \d       # Followed by one or more digits
        )            # End of the number group
        (?:          # Start of the optional exponent group
            [eE]     # 'e' or 'E'
            [ -]?    # Optional sign
            \d       # Followed by one or more digits
        )?           # Exponent is optional
        $            # End of the string
    }x;

    # Return 1 for valid, 0 for invalid
    return $str =~ $regex ? 1 : 0;
}

1;

पर्ल कार्यान्वयन के लिए परीक्षण

#!/usr/bin/perl
use strict;
use warnings;
use Test::More;

require './ch-2.pl';

# Define test cases
my @test_cases = (
    ["1", 1, 'Valid integer'],
    ["a", 0, 'Invalid input'],
    [".", 0, 'Single dot'],
    ["1.2e4.2", 0, 'Invalid exponent'],
    ["-1.", 1, 'Valid decimal'],
    [" 1E-8", 1, 'Valid scientific notation'],
    [".44", 1, 'Valid decimal starting with dot'],
);

# Loop through test cases and run tests
foreach my $case (@test_cases) {
    my $result = is_valid_number($case->[0]);
    is($result, $case->[1], $case->[2]);
}

done_testing();

कार्यान्वयन जाओ

package main

import (
    "regexp"
)

// isValidNumber checks if the given string is a valid number.
func isValidNumber(str string) bool {
    regex := `^[ -]?((\d (\.\d*)?)|(\.\d ))([eE][ -]?\d )?$`
    matched, _ := regexp.MatchString(regex, str)
    return matched
}

गो कार्यान्वयन के लिए परीक्षण

package main

import (
    "testing"
)

func TestIsValidNumber(t *testing.T) {
    testCases := []struct {
        input    string
        expected bool
    }{
        {"1", true},
        {"a", false},
        {".", false},
        {"1.2e4.2", false},
        {"-1.", true},
        {" 1E-8", true},
        {".44", true},
    }

    for _, tc := range testCases {
        result := isValidNumber(tc.input)
        if result != tc.expected {
            t.Errorf("isValidNumber(%q) = %v; expected %v", tc.input, result, tc.expected)
        }
    }
}

निष्कर्ष

ये समाधान पासवर्ड की ताकत का मूल्यांकन करने और संख्याओं की शुद्धता को सत्यापित करने के लिए प्रभावी तरीके प्रदान करते हैं। दोनों कार्यों का पूरा कोड GitHub पर उपलब्ध है।

विज्ञप्ति वक्तव्य इस लेख को पुन: प्रस्तुत किया गया है: https://dev.to/aplgr/exploring-password-strengty-and-number-validation-in-perl-and-go-go-529p?1 यदि कोई उल्लंघन है, तो इसे हटाने के लिए [email protected] से संपर्क करें।
नवीनतम ट्यूटोरियल अधिक>

चीनी भाषा का अध्ययन करें

अस्वीकरण: उपलब्ध कराए गए सभी संसाधन आंशिक रूप से इंटरनेट से हैं। यदि आपके कॉपीराइट या अन्य अधिकारों और हितों का कोई उल्लंघन होता है, तो कृपया विस्तृत कारण बताएं और कॉपीराइट या अधिकारों और हितों का प्रमाण प्रदान करें और फिर इसे ईमेल पर भेजें: [email protected] हम इसे आपके लिए यथाशीघ्र संभालेंगे।

Copyright© 2022 湘ICP备2022001581号-3