aboutsummaryrefslogtreecommitdiff
path: root/challenge-250/jeanluc2020/python/ch-2.py
blob: 3ffa4cf1e3c16d8b72619b36e2ccf2ce30087e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/#TASK2
#
# Task 2: Alphanumeric String Value
# =================================
#
# You are given an array of alphanumeric strings.
#
# Write a script to return the maximum value of alphanumeric string in the
# given array.
#
# The value of alphanumeric string can be defined as
#
# a) The numeric representation of the string in base 10 if it is made up of
#    digits only.
# b) otherwise the length of the string
#
#
## Example 1
##
## Input: @alphanumstr = ("perl", "2", "000", "python", "r4ku")
## Output: 6
##
## "perl" consists of letters only so the value is 4.
## "2" is digits only so the value is 2.
## "000" is digits only so the value is 0.
## "python" consits of letters so the value is 6.
## "r4ku" consists of letters and digits so the value is 4.
#
## Example 2
##
## Input: @alphanumstr = ("001", "1", "000", "0001")
## Output: 1
#
############################################################
##
## discussion
##
############################################################
#
# Try to match each string against a non-digit character. If that succeeds (or
# the length is 0), use the length. If it doesn't, convert the string to an
# integer.

import re

def alphanumeric_string_value(alphanumstr: list):
    print("Input: (", ", ".join(alphanumstr) , ")")
    max = 0
    for string in alphanumstr:
        if len(string) > 0:
            if re.match('[^\d]', string):
                if len(string) > max:
                    max = len(string)
            else:
                if int(string) > max:
                    max = int(string)
    print(f"Output: {max}")



alphanumeric_string_value(["perl", "2", "000", "python", "r4ku"])
alphanumeric_string_value(["001", "1", "000", "0001"])