raden = raw_input("Ge en rad tal: ") raden = raw_input ("Give a series of numbers:") raden_i_delar = raden.split() raden_i_delar raden.split = () for tal in raden_i_delar: for speech in raden_i_delar: print tal print numbers
Algoritm: Algorithm:
120 modulo 65 = 55 120 modulo 65 = 55 |
65 modulo 55 = 10 65 modulo 55 = 10 |
55 modulo 10 = 5 55 modulo 10 = 5 |
10 modulo 5 = 0 10 modulo 5 = 0 |
#encoding:Latin1 #encoding:Latin1 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 |
append(x)
Lägger till x sist i listan. append(x)
Adds x end of the list. insert(i,x)
Lägger till x på plats i. insert(i,x)
Adds x to place in. pop()
Plockar bort sista elementet från listan. pop()
Remove the last element from the list. sort()
Sorterar listan. sort()
Sorts the list. reverse()
Vänder på listan. reverse()
reverses the list. index(x)
Returnerar index för första förekomsten av x. index(x)
Returns index of first occurrence of x. count(x)
Räknar antalet xi listan. count(x)
Counts the number of x in the list. remove(x)
Tar bort första förekomsten av x. remove(x)
Removes the first occurrence of x. [i]
Returnerar i:te elementet i listan. [i]
Returns the ith element of the list. [:]
Plockar ut en dellista. [:]
Picking out a Parts list. +
Konkatenerar två listor +
concatenated two lists *
Flerdubblar listan. *
Multiple list. in
Kollar om ett element finns med i listan. in
Checks if an element is in the list. len
Returnerar listans längd. len
Returns the length of the list. I kursen ska vi själva implementera datastrukturer, oftast genom att skriva egna klasser i Python. In this course we will implement the data structures, usually by writing your own classes in Python.
* Temperatur kan anges i olika skalor. * Temperature can be set at different scales. En abstrakt datatyp minskar risken för missförstånd. An abstract data type reduces the risk of misunderstanding. Definiera klassen temp med metoderna setK, setC, setF och getK, getC, getF. Defining class methods with temp Setka, setC, setF and getK, getca, getF.
* Använd sedan klassen i ett program som läser in utomhustemperaturen (Celsius) och skriver ut temperaturen så att en amerikan förstår (Fahrenheit). * Then use the class in a program that reads the outside temperature (Celsius) and prints the temperature in order to understand an American (Fahrenheit).
# -*- coding: Latin-1 -*- # -*- coding: Latin-1 -*- """Abstrakt datatyp för temperatur""" "" "Abstract data type of temperature" '" nollC = 273.15 nollC = 273.15 nollF = 255.3666666 #F-nollan i Kelvin nollF = 255.3666666 # F-zero in the Kelvin class Temp: class Temp: def __init__(self): def __init__ (self): self.K = 0 #Temperatur i Kelvin self.K = 0 # Temperature in Kelvin def setK(self,K): def Setka (self, K): self.K = K self.K = K def setC(self,C): def setC (self, C): self.K = nollC+C self.K nollC = C + def setF(self,F): def setF (self, F): self.K = nollF+5*F/9 self.K nollF = +5 * F / 9 def getK(self): def getK (self): return self.K Return self.K def getC(self): def getca (self): return self.K-nollC Return self.K-nollC def getF(self): def getF (self): return (self.K-nollF)*9/5 return (self.K-nollF) * 9 / 5
# encoding: Latin1 (tillåt å, ä och ö)> #encoding: Latin1 (to A, ä and ö)
# Fraction-klassen # Fraction class
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
return Fraction(newnum,newden) return Fraction (newnum, newden)
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
|
Filen stack.py (känd från föreläsning) The file stack.py (known from the lecture)
class Stack: class Stack: def __init__(self): def __init__ (self): self.top = None self.top = None def push(self,x): def push (self, x): ny = Node(x)ny = Node (x) ny.next = self.top ny.next = self.top self.top = ny self.top = new def pop(self): def pop (self): x = self.top.value x = self.top.value self.top = self.top.next self.top = self.top.next return x return x def isempty(self): def isempty (self): if self.top == None: self.top f == None: return True Return True else: else: return False Return False class Node: class Node: def __init__(self, x): def __init__ (self, x): self.value = x self.value = x self.next = None self.next = None