forked from bmajoros/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShuffler.py
executable file
·46 lines (43 loc) · 1.58 KB
/
Shuffler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#=========================================================================
# This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
# License (GPL) version 3, as described at www.opensource.org.
# Copyright (C)2016 William H. Majoros ([email protected]).
#=========================================================================
from __future__ import (absolute_import, division, print_function,
unicode_literals, generators, nested_scopes, with_statement)
from builtins import (bytes, dict, int, list, object, range, str, ascii,
chr, hex, input, next, oct, open, pow, round, super, filter, map, zip)
import random
#=========================================================================
# Attributes:
#
# Instance Methods:
# shuffler=Shuffler()
# Class Methods:
# Shuffler.shuffleArray(array)
# s=Shuffler.shuffleString(s)
#=========================================================================
class Shuffler:
"""Shuffler shuffles arrays and strings"""
def __init__(self):
pass
@classmethod
def shuffleArray(cls,array):
L=len(array)
for i in range(L):
j=random.randint(0,L-1)
temp=array[i]
array[i]=array[j]
array[j]=temp
@classmethod
def shuffleString(cls,string):
L=len(string)
ret=string
for i in range(L):
j=random.randint(0,L-1)
if(j==i): continue
if(j>i):
ret=ret[0:i]+ret[j]+ret[i+1:j]+ret[i]+ret[j+1:L]
else:
ret=ret[0:j]+ret[i]+ret[j+1:i]+ret[j]+ret[i+1:L]
return ret