courses:2013w:703000:algorithmik_und_programmieren_solutions

Brückenkurs Materialien

Solutions

sort.py
import time
import random

def printNumbers(numbers):
	for number in numbers:
		print "#" * number
	print "\n" * 3


def insertSort(l):
	for i in range(1,len(l)):
		while i > 0 and l[i] <= l[i-1]:
			l[i], l[i-1] = l[i-1], l[i]
			i = i - 1
		printNumbers(l)
		time.sleep(0.5)
	return l

insertSort(random.sample(xrange(100),32))
solutions.py
import random


# Uebungsblatt 1

# 1.2.1
def incr(x):
	return x+1
	
# 1.2.3
def leapYear(year):
	if year % 4 != 0:
		return False
	elif (year % 100 == 0) and (year % 400 != 0):
		return False
	else: 
		return True	
		
# 1.3.2
def printNumbers():
	i=25
	while (i>0):
		print(i)
		i = i-1
		
# 1.3.3
def printFib(n):
	fibn=0
	fibm=1
	if n == 0:
		print(fibn)
	elif n == 1:
		print(fibn)
		print(fibm)
	else:
		print(fibn)
		print(fibm)
	for i in range(n-2):
		aux = fibm
		fibm = fibn + fibm
		fibn = aux
		print(fibm)
	
	
	
# Uebungsblatt 2

# 2.1.1
def sub(listA, listB):
	listC = []
	for i in listA:
		if i not in listB:
			listC.append(i)
	return listC		

# 2.1.2
def getMin(listA):
	if listA == []:
		return None
	else:	
		min = listA[0]
		for i in listA:
			if i < min:
				min = i
		return min	

# 2.2.1
def add(tupelA, tupelB):
	x = tupelA[0] + tupelB[0]
	y = tupelA[1] + tupelB[1]
	return (x,y)

# 3.1
def printContactInfo(phonebook, name):
	if name in phonebook:
		print(phonebook[name])
	else:
		print("No such name!")	

def printAllContacts(phonebook):
	keys = phonebook.keys();
	for i in keys:
		print(i + ": " + str(phonebook[i]))






# Uebungsblatt 3

#3.1.1
def generatePassword(characters, length):
	password = ""
	for i in range(length):
		randomNumber = random.randint(0, len(characters)-1)
		password += characters[randomNumber]
	return password	
		
	
	
	
	
	


#Tests

#print(incr(4))	
#print(leapYear(4))	
#printNumbers()
#printFib(6)
#print(sub([1,2,3],[2,3]))
#print(getMin([4,2,3,5,]))
#print(add((1,2),(3,4)))
#printContactInfo({"muster":10233, "huber":19233}, "no")
#printAllContacts({"muster":10233, "huber":19233})
#print(generatePassword("abcdefg12345", 5))
courses/2013w/703000/algorithmik_und_programmieren_solutions.txt · Last modified: 2018/09/03 19:35 (external edit)