Write 2 seperate programs in the language of python please. TYPE UP PLEASE it\'s
ID: 3832192 • Letter: W
Question
Write 2 seperate programs in the language of python please. TYPE UP PLEASE it's easier to read.
1. Iterator for Prime Number (Sieve of Eratosthenes) Write the sieve of Eratosthenes as a Generator Function.
2. Write a class named Permutation in a file named myPermutation.py that will generate on demand all the permutations of a given list. Write a short main function that demonstrates its use on the following lists: [1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['b', 'c', 'a']. Please hard code the lists into your main function. (Do not use anything from the itertools module.)
Explanation / Answer
2)
# Python function to print permutations of a given list
def permutation(lst):
# If lst is empty then there are no permutations
if len(lst) == 0:
return []
# If there is only one element in lst then, only
# one permuatation is possible
if len(lst) == 1:
return [lst]
# Find the permutations for lst if there are
# more than 1 characters
l = [] # empty list that will store current permutation
# Iterate the input(lst) and calculate the permutation
for i in range(len(lst)):
m = lst[i]
# Extract lst[i] or m from the list. remLst is
# remaining list
remLst = lst[:i] + lst[i+1:]
# Generating all permutations where m is first
# element
for p in permutation(remLst):
l.append([m] + p)
return l
# Driver program to test above function
data = list('123')
for p in permutation(data):
print p
# Python function to print permutations of a given list
def permutation(lst):
# If lst is empty then there are no permutations
if len(lst) == 0:
return []
# If there is only one element in lst then, only
# one permuatation is possible
if len(lst) == 1:
return [lst]
# Find the permutations for lst if there are
# more than 1 characters
l = [] # empty list that will store current permutation
# Iterate the input(lst) and calculate the permutation
for i in range(len(lst)):
m = lst[i]
# Extract lst[i] or m from the list. remLst is
# remaining list
remLst = lst[:i] + lst[i+1:]
# Generating all permutations where m is first
# element
for p in permutation(remLst):
l.append([m] + p)
return l
# Driver program to test above function
data = list('123')
for p in permutation(data):
print p
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.