aboutsummaryrefslogtreecommitdiff
path: root/challenge-215/sgreen/python
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-07 23:12:11 +0100
committerGitHub <noreply@github.com>2023-05-07 23:12:11 +0100
commit1505ba9e3cfb9c3f5b23e4394b1240f159e404cc (patch)
tree92cd288642daa14ddef476ab9e60d50372eb008e /challenge-215/sgreen/python
parente9e461ad6083aedfe2ac346184b5b0f9ecbe393d (diff)
parente7f115d3012c151de4903eb739b4826bae71d172 (diff)
downloadperlweeklychallenge-club-1505ba9e3cfb9c3f5b23e4394b1240f159e404cc.tar.gz
perlweeklychallenge-club-1505ba9e3cfb9c3f5b23e4394b1240f159e404cc.tar.bz2
perlweeklychallenge-club-1505ba9e3cfb9c3f5b23e4394b1240f159e404cc.zip
Merge pull request #8028 from simongreen-net/master
Simon's solution to challenge 214 and 215
Diffstat (limited to 'challenge-215/sgreen/python')
-rwxr-xr-xchallenge-215/sgreen/python/ch-1.py27
-rwxr-xr-xchallenge-215/sgreen/python/ch-2.py27
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-215/sgreen/python/ch-1.py b/challenge-215/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..b10b6e1547
--- /dev/null
+++ b/challenge-215/sgreen/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(words):
+ current_word = words.pop(0)
+ unsorted_words = 0
+
+ if any(word < current_word for word in words):
+ # If the first word is not the smallest, the whole list is unsorted
+ print(len(words)+1)
+ return
+
+ for word in words:
+ if word >= current_word:
+ # The next word is in order
+ current_word = word
+ else:
+ # The word is less than the current word
+ unsorted_words += 1
+
+ print(unsorted_words)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
diff --git a/challenge-215/sgreen/python/ch-2.py b/challenge-215/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..9fc8a9a7e9
--- /dev/null
+++ b/challenge-215/sgreen/python/ch-2.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+import sys
+
+
+def main(array):
+ to_place = array.pop()
+
+ for i in range(1, len(array)-1):
+ # Are the number before the current one, the current one and the next one all zero?
+ if array[i-1] == 0 and array[i] == 0 and array[i+1] == 0:
+ # If so, we can put a one here
+ array[i] = 1
+ to_place -= 1
+ if to_place == 0:
+ # We have placed all the ones
+ print(1)
+ return
+
+ # There is no possible solution
+ print(0)
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)