diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2025-09-22 13:04:29 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2025-09-22 13:04:29 +0200 |
| commit | 73b7940b487e99e182d406a38bad8362cfa194dd (patch) | |
| tree | c66a42eebdddbe6f05a9506a7552578f3e8b5f7e | |
| parent | fbf2a1b7a246de38ca0a2e74d3bb6982d2984120 (diff) | |
| download | perlweeklychallenge-club-73b7940b487e99e182d406a38bad8362cfa194dd.tar.gz perlweeklychallenge-club-73b7940b487e99e182d406a38bad8362cfa194dd.tar.bz2 perlweeklychallenge-club-73b7940b487e99e182d406a38bad8362cfa194dd.zip | |
Add ch-2 in Python
| -rw-r--r-- | challenge-340/spadacciniweb/python/ch-2.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-340/spadacciniweb/python/ch-2.py b/challenge-340/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..2aa0256c51 --- /dev/null +++ b/challenge-340/spadacciniweb/python/ch-2.py @@ -0,0 +1,61 @@ +# Task 2: Ascending Numbers +# Submitted by: Mohammad Sajid Anwar +# +# You are given a string, $str, is a list of tokens separated by a single space. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters. +# Write a script to check if all the numbers in the given string are strictly increasing from left to right. +# +# Example 1 +# Input: $str = "The cat has 3 kittens 7 toys 10 beds" +# Output: true +# +# Numbers 3, 7, 10 - strictly increasing. +# +# Example 2 +# Input: $str = 'Alice bought 5 apples 2 oranges 9 bananas' +# Output: false +# +# Example 3 +# Input: $str = 'I ran 1 mile 2 days 3 weeks 4 months' +# Output: true +# +# Example 4 +# Input: $str = 'Bob has 10 cars 10 bikes' +# Output: false +# +# Example 5 +# Input: $str = 'Zero is 0 one is 1 two is 2' +# Output: true + + +# printf "'%s' -> %s\n", $str, $sorted +# ? 'true' +import re + +def ascending_numbers(str): + numbers = re.findall(r'\d+', str) + + sorted = True + if (len(numbers) > 0): + curr = numbers.pop(0) + for i in numbers: + if (curr >= i): + sorted = False + break + + print("'%s' -> %s" % ( str, sorted ) ) + +if __name__ == "__main__": + str = "The cat has 3 kittens 7 toys 10 beds" + ascending_numbers( str ) + + str = 'Alice bought 5 apples 2 oranges 9 bananas' + ascending_numbers( str ) + + str = 'I ran 1 mile 2 days 3 weeks 4 months' + ascending_numbers( str ) + + str = 'Bob has 10 cars 10 bikes' + ascending_numbers( str ) + + str = 'Zero is 0 one is 1 two is 2' + ascending_numbers( str ) |
