FÖRBÄTTRINGSARBETE PÅGÅR! Improvement work in progress!

TILDA, ÖVNING 1 TILDA, EXERCISE 1

Pythonuppgifter, abstrakta datatyper Python data, abstract data types

    INLÄSNING OCH UTSKRIFT Scan and Print

  1. Skriv ett program som läser in en rad tal och skriver ut dom med ett tal per rad. Write a program that reads a series of numbers and print them with one number per line.
    
    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
    
    
    

    GCD-algoritmen GCD algorithm

  2. Beräkna minsta gemensamma faktor mellan två tal. Calculate the lowest common factor between two numbers.
    Indata: Två heltal m och n Input: Two integers m and n
    Utdata: Deras minsta gemensamma faktor Output: Their lowest common factor

    Algoritm: Algorithm:

    Exempel: Example:

    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

    I Python In Python

    #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
    
    




    Listan - exempel på en datastruktur i Python The list - example of a data structure in Python

  3. Data: ett antal tal, tecken eller objekt Data: a set of numbers, characters or objects

    Metoder och operationer: Methods and Operations:

  4. Skriv ett program som går igenom en lista med alla namn i grupp1 och läser in ålder för varje person och lägger in i en ny lista. Write a program that goes through a list of all names in Group 1 and loads the age of each person and put into a new list.
    Sortera den nya listan, vänd på den och räkna ut hur många element den har. Sort the new list, turn it over and figure out how many elements it has.

    Vi skriver egna datastrukturer We are writing their own data structures

    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.

  5. Abstrakt datatyp för temperatur Abstract data type for the temperature

    * 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).


    Filen temp.py: The file temp.py:
    # -*- 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
    
    

  6. Fraction-klassen: Ett exempel från Miller & Ranums bok, s 35: Fraction-class: An example from Miller & Ranum's book, p. 35:
    # 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
    

    Stacken The stack


  7. 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