I am having big trouble with classes. I don’t think I really understand them and sourly not as clear as functions. I do know that they are an important part of programming so I appreciate all the help I can get to get things clearer.
I am trying to rewrite this to one big calls but I doesn’t seem to work.
import random
def fac(n):
if n == 0:
return 1
else:
return (fac(n-1) * n)
def fel(antal):
n = len(antal)
if n==0: yield []
else:
for i in range(len(antal)):
for cc in fel(antal[:i]+antal[i+1:]):
yield [antal[i]]+cc
def k_fel(antal, n):
if n==0: yield []
else:
for i in range(len(antal)):
for ss in k_fel(antal, n-1):
if (not antal[i] in ss):
yield [antal[i]]+ss
def r_fel(list):
längd = len(list);
max = fac(längd);
index = random.randrange(0, max)
i = 0
for p in fel(list):
if i == index:
return p
i += 1
def alla_värden(värde, platser):
värde = r_fel(värde)
for s in k_fel(värde, platser):
yield(s)
print (lista)
And this is my go at calsses.
import random
class alla_värden (self, värde, antal):
self.värde = r_fel(self.värde)
for s in k_fel(värde, platser):
yield(s)
def __int__(self, värde, antal):
self.värde = värde
self.antal = antal
def fac(self, n):
if n == 0:
return 1
else:
return (fac(n-1) * n)
def fel (self, antal):
n = len(antal)
if n==0: yield []
else:
for i in range(len(antal)):
for cc in fel(antal[:i]+antal[i+1:]):
yield [antal[i]]+cc
def k_fel (self, antal, n):
if n==0: yield []
else:
for i in range(len(antal)):
for ss in k_fel(antal, n-1):
if (not antal[i] in ss):
yield [antal[i]]+ss
def r_fel(self, list):
längd = len(list);
max = fac(längd);
index = random.randrange(0, max)
i = 0
for p in fel(list):
if i == index:
return p
i += 1
It does not work due to error: yield outside function.