aboutsummaryrefslogtreecommitdiff
path: root/challenge-273/spadacciniweb/python
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2024-06-14 09:13:08 +0200
committerMariano Spadaccini <spadacciniweb@gmail.com>2024-06-14 09:13:08 +0200
commit9c95a292ea1d562a61a569f752954220ba6cbbc0 (patch)
treec5b4b50455c5a34ca1e164e42c6f43f338f49a49 /challenge-273/spadacciniweb/python
parentc7797f212a2db97cae7ac808542c20df8402eea5 (diff)
downloadperlweeklychallenge-club-9c95a292ea1d562a61a569f752954220ba6cbbc0.tar.gz
perlweeklychallenge-club-9c95a292ea1d562a61a569f752954220ba6cbbc0.tar.bz2
perlweeklychallenge-club-9c95a292ea1d562a61a569f752954220ba6cbbc0.zip
Add ch-1 and ch-2 in Python
Diffstat (limited to 'challenge-273/spadacciniweb/python')
-rw-r--r--challenge-273/spadacciniweb/python/ch-1.py61
-rw-r--r--challenge-273/spadacciniweb/python/ch-2.py46
2 files changed, 107 insertions, 0 deletions
diff --git a/challenge-273/spadacciniweb/python/ch-1.py b/challenge-273/spadacciniweb/python/ch-1.py
new file mode 100644
index 0000000000..bfbc5cbc57
--- /dev/null
+++ b/challenge-273/spadacciniweb/python/ch-1.py
@@ -0,0 +1,61 @@
+# Task 1: Percentage of Character
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str and a character $char.
+# Write a script to return the percentage, nearest whole, of given character in the given string.
+#
+# Example 1
+# Input: $str = "perl", $char = "e"
+# Output: 25
+#
+# Example 2
+# Input: $str = "java", $char = "a"
+# Output: 50
+#
+# Example 3
+# Input: $str = "python", $char = "m"
+# Output: 0
+#
+# Example 4
+# Input: $str = "ada", $char = "a"
+# Output: 67
+#
+# Example 5
+# Input: $str = "ballerina", $char = "l"
+# Output: 22
+#
+# Example 6
+# Input: $str = "analitik", $char = "k"
+# Output: 13
+
+def percentage(str, char):
+ print("%s -> %d" %
+ ( str,
+ round( str.count(char)*100/len(str) )
+ )
+ )
+
+if __name__ == "__main__":
+ str = "perl"
+ char = "e"
+ percentage(str, char)
+
+ str = "java"
+ char = "a"
+ percentage(str, char)
+
+ str = "python"
+ char = "m"
+ percentage(str, char)
+
+ str = "ada"
+ char = "a"
+ percentage(str, char)
+
+ str = "ballerina"
+ char = "l"
+ percentage(str, char)
+
+ str = "analitik"
+ char = "k"
+ percentage(str, char)
diff --git a/challenge-273/spadacciniweb/python/ch-2.py b/challenge-273/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..dce0bca454
--- /dev/null
+++ b/challenge-273/spadacciniweb/python/ch-2.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+# Task 2: B After A
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string, $str.
+# Write a script to return true if there is at least one b, and no a appears after the first b.
+#
+# Example 1
+# Input: $str = "aabb"
+# Output: true
+#
+# Example 2
+# Input: $str = "abab"
+# Output: false
+#
+# Example 3
+# Input: $str = "aaa"
+# Output: false
+#
+# Example 4
+# Input: $str = "bbb"
+# Output: true
+
+def out(str):
+ offset = str.find('b')
+ if offset >= 0 and str.find('a', offset) == -1:
+ res = "true"
+ else:
+ res = "false"
+ print("%s -> %s" %
+ ( str, res )
+ )
+
+if __name__ == "__main__":
+ str = "aabb"
+ out(str)
+
+ str = "abab"
+ out(str)
+
+ str = "aaa"
+ out(str)
+
+ str = "bbb"
+ out(str)