diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 9200579b8..6d13881f9 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -9,4 +9,5 @@ Maxime Dherbécourt Jess 3Jane Pen Pal Chinmaya Mahesh -Unlambder \ No newline at end of file +Kjetil Johannessen +Unlambder diff --git a/chapters/sorting_searching/bogo/bogo_sort.md b/chapters/sorting_searching/bogo/bogo_sort.md index 8a1db8d81..a8efb5bf3 100644 --- a/chapters/sorting_searching/bogo/bogo_sort.md +++ b/chapters/sorting_searching/bogo/bogo_sort.md @@ -25,6 +25,8 @@ In code, it looks something like this: [import:2-17, lang:"java"](code/java/bogo.java) {% sample lang="js" %} [import:1-16, lang:"javascript"](code/js/bogo.js) +{% sample lang="py" %} +[import:4-12, lang:"python"](code/python/bogo.py) {% sample lang="hs" %} [import, lang:"haskell"](code/haskell/bogoSort.hs) {% sample lang="m" %} diff --git a/chapters/sorting_searching/bogo/code/python/bogo.py b/chapters/sorting_searching/bogo/code/python/bogo.py new file mode 100644 index 000000000..eaf9f4933 --- /dev/null +++ b/chapters/sorting_searching/bogo/code/python/bogo.py @@ -0,0 +1,20 @@ +import random + + +def is_sorted(a): + for i in range(len(a)-1): + if a[i+1] < a[i]: + return False + return True + +def bogo_sort(a): + while not is_sorted(a): + random.shuffle(a) + +def main(): + a = [1., 3, 2, 4] + bogo_sort(a) + print(a) + +main() +