BuK/IIm20/Vorlesung 3 Nerlich

Aus ProgrammingWiki

Wiki-Quelltext dieser Seite.

<language>Python</language>
=== S.9 ===

<code id="6073186d90b37">
#/usr/bin/env python3

from random import Random


class WordGenerator:
	"""docstring for WordGenerator"""
	def __init__(self, alphabet: set):
		if not alphabet.isdisjoint({None}):
			raise ValueError("The alphabet cannot contain None")
		
		self._alphabet = frozenset(alphabet)
		self._generator = Random()
		self._word = ""
		self._ended = False
		
	def __iter__(self):
		return self
		
	def __next__(self):
		result = self._generator.choice(tuple(self._alphabet.union({None})))
		if result is None or self._ended:
			self._ended = True
			raise StopIteration()
		else:
			self._word = self._word + result
			return result
		
	@property
	def alphabet(self) -> frozenset:
		return self._alphabet
	
	@property
	def word(self):
		while not self._ended:
			try:
				next(self)
			except StopIteration:
				pass
		return self._word
		
	def next(self):
		return self.__next__()
		
	def reset(self):
		self._word = ""
		self._ended = False


def chi_length_3(w: WordGenerator):  # requires at most 4 checks
	for n in range(1,4):
		try:
			
			print("Letter #{} is '{}'".format(n, next(w)))  # up to 3 checks, determining lengths 0 to 2
			
		except StopIteration:
			print("Word '{}' is of length shorter than 3!".format(w.word))
			return False
	try:
		
		print("Letter #4 is '{}'".format(next(w)))  # fourth check shows whether of length 3
		
		print("Word '{}' is of length longer than 3!".format(w.word))
		return False
	except StopIteration:
		print("Word '{}' is of length 3.".format(w.word))
		return True

def chi_starts_with_letter(w: WordGenerator, letter):  # always requires only one check
	try:
		
		result = next(w)  # one check
		
		print("Letter #1 is '{}'".format(result))
		if result == letter:
			print("Word '{}' starts with '{}'!".format(w.word, letter))
			return True
		else:
			print("Word '{}' does not start with '{}'.".format(w.word, letter))
			return False
	except StopIteration:
		print("Word '{}' does not start at all.".format(w.word))
		return False


A = {'a', 'b'}
</code>
<run id="6073186d90bc4">chi_length_3(WordGenerator(A))</run>
<run id="6073186d90c2b">chi_starts_with_letter(WordGenerator(A), 'a')</run>



=== S.10 ===

Berechenbarkeit = $\exists$ Algorithmus, der für jedes $x \in D_f$ den Funktionswert $f$ effektiv ermittelt (effektiv = es muss ein entsprechendes Verfahren geben)

{| border="1" cellpadding="5" cellspacing="0"
|- 
| $A$					|| endlich
|-
| $A^\ast$				|| abzählbar unendlich
|-
| $\mathcal{P}(A^\ast)$			|| überabzählbar unendlich
|-
| $\mathcal{X}_M$			|| überabzählbar unendlich
|-
| $\mathcal{X}_M$ berechenbar		|| ?????
|-
| $\mathcal{X}_M$ implementierbar	|| abzählbar unendlich
|-
|}




=== S.15 ===

==== $G$ (gerade Zahlen) ====

	$G \subset \mathbb{N}$

	kann $\mathbb{N}$ auf $G$ abbilden: 0→0, 1→2, 2→4, etc.

==== $P$ (Primzahlen) ====

	$P \subset \mathbb{N}$, $\exists$ berechenbare Methode für Entscheidung $n \in P \hspace{4mm} \longrightarrow \hspace{4mm} f:$ durchprobieren

==== $A^\ast$ ====

	längenlexikografisch aufzählen