Laborationer Laboratory | Laboration 1 - Datastruktur för bråk Exercise 1 - Data structure for fraction Formaliteter Formalities Alla laborationer görs i grupper om två. All laboratory work is done in groups of two. Din labbkompis är den som har fått samma datornummer som du på sitt placeringskort. Your friend's lab is the one who has had the same computer code as you in their place cards. - Logga in . Ni bör båda ha varsitt konto (användarnamn + lösenord). Logga in på det ena. Login. You should have two round of pleadings account (username + password). Log on to one.
- Checka in på kursen . Check in at the course.
Välj "rapp" i vänstermenyn och följ länken för att aktivera din kursregistrering. Select the "rap" in the left menu and follow the link to activate your course registration. Logga in med ditt kth-konto (som för Mina Sidor). Sign in with your KTH-account (which of my pages). Det här måste ni göra bägge två! This you must do both of them! - Öppna ett terminalfönster . Open a terminal window.
Under Applications hittar du Accessories och därunder Terminal . In Applications and Accessories you will find underneath Terminal. Prova att skriva whoami i terminalfönstret för att se vilken av er som är inloggad just nu. Try typing whoami in the terminal window to see which of you is seen at this time. - Skapa gemensam katalog med labbkompisen i två steg! Create a common directory with lab buddy in two steps!
Skriv följande i terminalfönstret: Type the following into the terminal window: course labdir tilda10 kompisens_användarnamn Låt sedan labkompisen logga in på sitt konto och ge kommandot Then let labkompisen log in to their account and give the command course labdir tilda10 ditt_eget_användarnamn Nu har ni en gemensam katalog tilda10 där ni kan spara alla program ni skriver i kursen. Now you have a common directory tilda10 where you can save all the programs you write in the course. - Starta Python . Start Python.
Under Applications hittar du Programming och därunder IDLE (du får prova DrPython om du vill) In the Applications Programming, and you'll find underneath IDLE (you may try DrPython if you want)
SimaManager SimaManager Under Applications och sedan CSC hittar du SimaManager . I fönstret som öppnas kan du välja kursen tilda . Denna kö använder du när det är full rulle under labbarna och du vill få tag på en assistent, till exempel för att fråga om hjälp eller för att redovisa dina färdiga labbar. In Applications and then CSC will find SimaManager. In the window that opens, select the course tilda. This cue use when it is full speed during the labs and you want to get hold of an assistant, for example, to ask for help or to report your completed labs. Är det lugnt så är det bara att vifta så kommer vi! Is it easy so you just wave as we come!
Funktionen gcd The function gcd Öppna ett nytt Python-fönster (under File i IDLE) och kopiera följande kod dit. Open a new window Python (under File in IDLE) and copy the following code there.
# -*- coding: Latin-1 -*- # -*- Coding: Latin-1 -*-
# gcd-funktionen från Miller & Ranums bok, s 35 # Gcd function from Miller & Ranum book's 35
def gcd(m,n): def gcd (m, n):
while m%n != 0: While m% n: = 0:
oldm = m oldm = m
oldn = n oldn = n
m=oldn m = oldn
n=oldm%oldn n = oldm% oldn
return n return n
| Lägg till ett huvudprogram som läser in två tal, beräknar gcd genom att anropa funktionen, och skriver ut resultatet. Add a main program that reads two numbers, calculate gcd by calling the function, and prints the result. Tips: Läs in med input och skriv ut med print . Spara programmet i tilda10-katalogen. Tip: Load the input and print to print . tilda10 Save program directory. Svara sedan på följande frågor: Then answer the following questions: - Vad är gcd(1331, 704)? What is gcd (1331, 704)?
- Hur många varv blir det i while-slingan? How many laps are in the while-loop? (Tips: lägg in en print-sats i funktionen) (Tip: add a print statement in the function)
- Går det lika bra att anropa funktionen med gcd(704, 1331)? Is it as good to call the function with gcd (704, 1331)? Vad händer då? What happens then?
Kommentera nu bort huvudprogrammet (under Format hittar du Comment Out Region ) men låt funktionen stå kvar. Comment now remove the main program (in the format you will find Comment Out Region) function, but let stand.
Klassen Fraction Class Fraction Kopiera hela klassen nedan och klistra in den efter din gcd-funktion. Copy the entire class below and paste it to your gcd function.
# Fraction-klassen från Miller & Ranums bok, s 35 # Fraction class from Miller & Ranum book's 35
class Fraction: class Fraction:
def __init__(self, top, bottom): def __init__ (self, top, bottom):
self.num = top self.num = top
self.den = bottom self.den = bottom
def __str__(self): def __str__ (self):
return str(self.num)+"/"+str(self.den) return str (self.num )+"/"+ str (self.den)
def show(self): def show (self):
print self.num,"/",self.den print self.num ,"/", self.den
def __add__(self,otherfraction): def __add__ (Self, Other fraction):
newnum = self.num*otherfraction.den + \ newnum = self.num otherfraction.den * + \
self.den*otherfraction.num self.den * otherfraction.num
newden = self.den * otherfraction.den newden = self.den * otherfraction.den
common = gcd(newnum,newden) common = gcd (newnum, newden)
return Fraction(newnum/common,newden/common) return Fraction (newnum / common, newden / common)
def __cmp__(self,otherfraction): def __cmp__ (Self, Other fraction):
num1 = self.num*otherfraction.den num1 = * self.num otherfraction.den
num2 = self.den*otherfraction.num num2 = self.den * otherfraction.num
if num1 < num2: IF num1 <num2:
return -1 return -1
else: else:
if num1 == num2: IF num1 == num2:
return 0 return 0
else: else:
return 1 return 1
|
- Skriv ett huvudprogram för att testa alla metoderna i Fraction-klassen. Här är några exempel: Write a main program to test all the methods in the Fraction class. Here are some examples:
vete = Fraction(2,10) wheat = Fraction (2.10)
vete.show() vete.show ()
print vete print wheat
korn = Fraction(1,3) barley = Fraction (1,3)
musli = vete + korn muesli = wheat + barley
print musli print muesli
- Lägg till en ny metod - du får själv välja vad den ska göra. Add a new approach - you can choose what to do. Provkör. Test run.
- Skriv en kommentar till varje metod som beskriver vad den gör. Write a comment for each method describing what it does.
- Svara sedan på följande frågor: Then answer the following questions:
- Vad är relationen mellan en klass och ett objekt ? What is the relationship between a class and an object?
- Hur skriver man för att skapa ett
Fraction -objekt? How do you write to create a Fraction object? - Vad gör
__init__ ? What makes __init__ ? - Vilka av metoderna ovan har returvärden? Which of the methods above have return values?
- Vad är
self ? What is self ? - När anropas metoden
__str__ ? When calling the method __str__ ? - När anropas metoden
__add__ ? When calling the method __add__ ? - När anropas metoden
__cmp__ ? When calling the method __cmp__ ? - Hur används parametern
otherfraction i __add__ och __cmp__ ? How is the parameter otherfraction of __add__ and __cmp__ ?
Redovisa ditt program och dina svar för någon av assistenterna! Describe your application and your answers to any of the assistants!
Väl labbat av ................................. Well labs of ................................. medger....................... admits ....................... den ............... the ............... |