Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- """One-passage list shuffler"""
- import random as r
- class Shufflable:
- """Shufflable lists"""
- def __init__(self, lst=None):
- self.lst = lst
- def swap(self, from_index, to_index):
- """Swapping values"""
- temp = self.lst[from_index]
- self.lst[from_index] = self.lst[to_index]
- self.lst[to_index] = temp
- def shuffle(self):
- """Shuffling items"""
- if self.lst:
- for i in range(len(self.lst)):
- target_index = r.randint(0, i)
- self.swap(i, target_index)
- return self.lst
- NUMBERS = Shufflable(list(range(16)))
- NUMBERS.shuffle()
- WORDS = Shufflable(["foo", "bar", "baz", "ham", "spam", "eggs"])
- WORDS.shuffle()
- print(NUMBERS.lst, '\n', WORDS.lst, sep='')
- # [9, 0, 4, 15, 6, 13, 1, 12, 8, 14, 5, 10, 2, 11, 7, 3]
- # ['bar', 'ham', 'baz', 'foo', 'eggs', 'spam']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement