วันอาทิตย์ที่ 25 ตุลาคม พ.ศ. 2558

Transpose a square matrix in place without creating a new matrix.

def setup():
   rowone = [1, 2, 3]
   rowtwo = [4, 5, 6]
   rowthree = [7, 8, 9]
   maxtrix = [rowone, rowtwo, rowthree]
   dis(maxtrix)
   tran(maxtrix)
   print()
   print('transpote')
   print()
   dis(maxtrix)

 
def dis(maxtrix):
   i = 0
   while(i < len(maxtrix)):
      j = 0
      print("|",end = "")
      while(j < len(maxtrix[i])):
          print(" ",maxtrix[i][j]," ",end  = "")
          j += 1
      print("|")
      i +=1
 
def tran(maxtrix):
   i = 0
   while(i < len(maxtrix)):
      j = i
      while(j < len(maxtrix[i])):
         temp = maxtrix[i][j]
         maxtrix[i][j] = maxtrix[j][i]
         maxtrix[j][i] = temp
         j = j + 1
      i = i + 1
       
setup()

Subtract two matrices

def setup():
   rowone = [1, 2, 3]
   rowtwo = [4, 5, 6]
   rowthree = [7, 8, 9]
   maxtrixone = [rowone, rowtwo, rowthree]
   rowfour = [10, 11, 12]
   rowfive = [13, 14, 15]
   rowsix = [16, 17, 18]
   maxtrixtwo = [rowfour, rowfive, rowsix]
   addtwo(maxtrixone,maxtrixtwo)
 
def addtwo(maxtrixone,maxtrixtwo):
   i = 0
   while(i < len(maxtrixone)):
      j = 0
      print("|", end = "")
      while(j < len(maxtrixone[i])):
         print("",maxtrixone[i][j]-maxtrixtwo[i][j],"",end = "")
         j = j + 1
      print("|")
      i = i + 1
   
setup()

Add two matrices

def setup():
   rowone = [1, 2, 3]
   rowtwo = [4, 5, 6]
   rowthree = [7, 8, 9]
   maxtrixone = [rowone, rowtwo, rowthree]
   rowfour = [10, 11, 12]
   rowfive = [13, 14, 15]
   rowsix = [16, 17, 18]
   maxtrixtwo = [rowfour, rowfive, rowsix]
   addtwo(maxtrixone,maxtrixtwo)
 
def addtwo(maxtrixone,maxtrixtwo):
   i = 0
   while(i < len(maxtrixone)):
      j = 0
      print("|", end = "")
      while(j < len(maxtrixone[i])):
         print("",maxtrixone[i][j]+maxtrixtwo[i][j],"",end = "")
         j = j + 1
      print("|")
      i = i + 1
   
setup()

Display the matrix

def setup():
   rowone = [1, 2, 3]
   rowtwo = [4, 5, 6]
   rowthree = [7, 8, 9]
   maxtrixone = [rowone, rowtwo, rowthree]
   rowfour = [10, 11, 12]
   rowfive = [13, 14, 15]
   rowsix = [16, 17, 18]
   maxtrixtwo = [rowfour, rowfive, rowsix]
   display(maxtrixone)
   print()
   display(maxtrixtwo)
 
def display(maxtrixone):
   i = 0
   while(i < len(maxtrixone)):
      j = 0
      print("|", end = "")
      while(j < len(maxtrixone[i])):
         print("",maxtrixone[i][j],"",end = "")
         j = j + 1
      print("|")
      i = i + 1
   
setup()

Find indices of the room(s) with maximum number of chairs (in the building)

def setup():
   one = [10, 20, 30, 40, 50]
   two = [10, 70, 50, 20, 90]
   three = [10, 80, 40, 20, 90]
   four = [50, 30, 20, 40, 60]
   a = [one, two, three, four]
   find(a)
 
def find(a):
    i = 0
    value = a[0][0]
    while(i < len(a)):
        x = 0
        while(x < len(a[i])):
            if(a[i][x] > value):
                value = a[i][x]            
            x += 1
        i += 1
    i = 0
    while(i < len(a)):
        x = 0
        while(x < len(a[i])):
           if( value == a[i][x]):
               print("Indices is"," [",i,"][",x,"] ",end = "")
           x += 1
        i += 1

setup()

Find index of the floor(s) with maximum number of chairs (in the building)

def setup():
   one = [10, 20, 30, 40, 50]
   two = [60, 50, 40, 30, 20]
   three = [10, 80, 50, 20, 90]
   four = [50, 30, 20, 40, 60]
   a = [one, two, three, four]
   find(a)
 
def find(a):
   i = 0
   total = 0
   value = 0
   imax = 0
   while(i < len(a)):
      total = 0
      x = 0
      while(x < len(a[i])):
         total = total + a[i][x]
         x = x +1
      if(total > value):
         value = total
         imax = i
      i = i + 1
   print("Index is",imax)
 
setup()

Find total number of chairs (in the building)


def setup():
   one = [10, 20, 30, 40, 50]
   two = [60, 50, 40, 30, 20]
   three = [50, 30, 20, 40, 60]
   four = [10, 80, 50, 20, 90]
   a = [one, two, three, four]
   total(a)
 
def total(a):
   i = 0
   total = 0
   while(i < len(a)):
      x = 0
      while(x < len(a[i])):
         total = total + a[i][x]
         x = x +1
      i = i + 1
   print("Total is",total)
 
setup()

Display student records with weight < 50

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 35]
   weight = [70, 45, 60]
   height = [1.60, 1.75, 1.50]
   dis(name, weight)
 
def dis(n, w):
   i = 0
   while(i < len(w)):
      if(w[i] < 50):
         print(n[i], "weight =", w[i])
      i = i + 1

setup()

Find/count number of students with weight < 50

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 35]
   weight = [70, 45, 60]
   height = [1.60, 1.75, 1.50]
   print(count(weight))
 
def count(w):
   i = 0
   count = 0
   while(i < len(w)):
      if(w[i] < 50):
         count = count + 1
      i = i + 1
   return count

setup()

Find minimum weight of students

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 35]
   weight = [70, 55, 60]
   height = [1.60, 1.75, 1.50]
   minn(name, weight)
 
def minn(n, w):
   i = 0
   namemin = 0
   minn = w[i]
   while(i < len(w)):
      if(minn > w[i]):
         minn = w[i]
         namemin = n[i]
      i = i + 1
   print(namemin,"minimum weight =",minn)

setup()

Display student records, sorted by age, use insertion sort

def setup():
   name=['aa','bb','cc','dd','ee','ff']
   ID=[54321,51234,52341,53412,54123,54132]
   age=[9,10,15,16,8,9]
   weight=[45,55,40,50,70,60]
   height=[145,155,140,150,170,160]
   i=0
   sort(name,ID,age,weight,height)
 
   while(i<len(weight)):
      display(name,ID,age,weight,height,i)
      i += 1

def display(n,I,a,w,h,i):
   print('Name :',n[i])
   print("ID =",I[i])
   print("age =",a[i])
   print("weight =",w[i])
   print("height =",h[i])
   print('---------------------')
     
def sort(n,I,a,w,h):
   i=1
   while(i<len(a)):
      x_a = a[i]
      x_n = n[i]
      x_I = I[i]
      x_w = w[i]
      x_h = h[i]
      j = i
      while(j>0 and a[j-1] > x_a):
         a[j] = a[j-1]
         n[j] = n[j-1]
         I[j] = I[j-1]
         w[j] = w[j-1]
         h[j] = h[j-1]
         j = j - 1
      a[j] = x_a
      n[j] = x_n
      I[j] = x_I
      w[j] = x_w
      h[j] = x_h
      i += 1
     


 
setup()

Find/count number of students with age < 30

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 35]
   weight = [70, 55, 60]
   height = [1.60, 1.75, 1.50]
   print(count(age))
 
def count(a):
   i = 0
   count = 0
   while(i < len(a)):
      if(a[i] < 30):
         count = count + 1
      i = i + 1
   return count

setup()

Find average age of students

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 5]
   weight = [70, 55, 60]
   height = [1.60, 1.75, 1.50]
   print(average(age))
 
def average(a):
   i = 0
   summ = 0
   n = len(a)
   average = 0
   while(i < len(a)):
      summ = summ + a[i]
      i = i + 1
   average = summ/n
   return average

setup()

Display student records with BMI > 25

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 5]
   weight = [70, 55, 60]
   height = [1.60, 1.75, 1.50]
   BMI(name, weight, height)
 
def BMI(n, w, h):
   i = 0
   while(i < len(n)):
      BMI = w[i]/(h[i]*h[i])
      if(BMI > 25):
         print(n[i], "weight", w[i], "height", h[i], "BMI =", "%.3f"%BMI)
      i = i + 1
   
setup()

Find/count number of students with BMI > 25

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 5]
   weight = [70, 55, 60]
   height = [1.60, 1.75, 1.50]
   print(count(name, weight, height))
 
def count(n, w, h):
   i = 0
   count = 0
   while(i < len(n)):
      BMI = w[i]/(h[i]*h[i])
      if(BMI > 25):
         count = count + 1
      i = i + 1
   return count
   
setup()

Find student BMI

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 5]
   weight = [60, 45, 50]
   height = [1.60, 1.45, 1.50]
   BMI(name,weight, height)
 
def BMI(n,w,h):
   i = 0
   while(i < len(n)):
      BMI=w[i]/(h[i]*h[i])
      print(n[i],'BMI =','%.5f'%BMI)
      i += 1
 

setup()

Display student records (data)

def setup():
   name = ["AA", "BB", "CC"]
   ID = [54321, 12345, 51234]
   age = [15, 25, 5]
   weight = [60, 45, 50]
   height = [160, 145, 150]
   display(name, ID, age, weight, height)
 
def display(n, I, a, w, h):
   i = 0
   while(i < len(n)):
      print(n[i])
      print("ID =", I[i])
      print("age =", a[i])
      print("weight =", w[i])
      print("height =", h[i])
      print()
      i = i + 1
 

setup()

วันอาทิตย์ที่ 4 ตุลาคม พ.ศ. 2558

my_strip

def setup():
   string = 'thailand    '
   strip = '        '
   print("world is", my_strip(string, strip))
   assert my_strip(string, strip) == 'thailand'

def my_strip(string, strip):
   nostrip = ''
   i = 0
   while (i < len(string)):
      if (i < len(strip)):
         nostrip = nostrip + string[i]
      i = i+1
   return nostrip

setup()

my_startwith

def setup():
   string = 'thailand'
   startstring = 'sun'
   assert my_startswith(string, startstring) == False
     
def my_startswith(string, startstring):
   result = False
   i = 0
   if (len(string) > len(startstring)):
      while (i < len(startstring)):
         if (string[i] == startstring[i]):
            result = True
         else:
            result = False
         i = i+1
   return result
 
setup()

my_replace

def setup():
   string = 'thailand'
   replace = 'good'
   print(string, "New", my_replace(string, replace))
   assert my_replace(string, replace) == 'goodland'

def my_replace(string, replace):
   new = ''
   i = 0
   while (i < len(string)):
      if (i < len(replace)):
         new = new + replace[i]
      elif (i >= len(replace)):
         new = new + string[i]
      i = i+1
   return new

setup()

my_find

def setup():
      my_find = 'thailand'
      i = 0
      findi = i
      while (i < len(my_find)):
         if ('i' == my_find[i]):
            findi = i
         i = i+1
      print(findi)
      assert (findi == 3)
setup()

my_endwith

def setup():
   string = 'thailand'
   endstring = 'and'
   assert my_endswith(string, endstring) == True

def my_endswith(string, endstring):
   result = False
   i = 0
   if (len(string) > len(endstring)):
      while (i < len(endstring)):
         if (string[i+(len(string)-len(endstring))] == endstring[i]):
            result = True
         else:
            result = False
         i = i+1
   return result

setup()

my_count

def setup():
      my_count = 'thailand'
      i = 0
      count = 0
      check = True
      while (i < len(my_count)):
         if ('a' == my_count[i]):
            check = True
            count = count+1
         else:
            check = False
         i = i+1
      print(count)
      assert (count == 2)
setup()

sum array

def setup():
   n=[4,50,98,117,350,482,971,1500,563]
   i=0
   sum=0
   while(i<len(n)):
      sum=sum+n[i]
      i=i+1
   print("sum=",sum)
setup()

+num array

def setup():
   n = [ 9,-5,18,-2,0,7]
   i=0
   summ=0
   count=0
   while(i<len(n)):
      if(n[i]>=0):
         summ=summ+n[i]
         count=count+1
      i=i+1
   print("count positive number of array = ",count)  
   print("sum of positive in array =",summ)
 
setup()

Increase/decrease array

def setup():
   n = [5, 9, -80, 156, 9, -105]
   fix(n)
 
def fix(n):
   i = 0
   sum = 0
   a = 0 #edit
   while(i < len(n)):
      a = n[i] + 10
      sum = sum+a
      i = i+1
      print("new index ","n[",i,"]", "= ", a)
   print("sum = ", sum)

setup()

phyton base10 to 2

def setup():
      base10 = 9
      print(base10)
      base2 = ""
      print(base2)
      while (base10 > 0):
         if (base10 % 2 == 0):
            base2 = "0" + base2
            base10 = base10/2
            print(base10,base2)
         elif (base10 % 2 == 1):
            base2 = "1" + base2
            base10 = (base10/2) - 0.5
            print(base10,base2)
     
      print("change base10 to base2 =",base2)

setup()

phyron avr array

def setup():
   n = [ 9,5,18,2,0,7]
   i=0
   summ=0
   avr=0
   while(i<len(n)):
      summ=summ+n[i]
      i=i+1
      avr=summ/len(n)
 
   print("sum =",summ)
   print("average =",avr)
setup()

phyron array max

def setup():
   n = [ 9,5,18,2,0,7]
 
   print("max =",cal_max(n))
   print("index of first =",cal_max_first(n))
   print("index of last =",cal_max_last(n))
def cal_max(a):
   maxx=a[0]
   i=1
   while(i<len(a)):
      if(a[i]>maxx):
         maxx=a[i]
      i=i+1
   return maxx

def cal_max_first(c):
   maxx=c[0]
   i=1
   while(i<len(c)):
      if(c[i]>maxx):
         maxx=c[i]
         return i
      i=i+1
 


def cal_max_last(b):
   maxxx=b[5]
   i=4
   while(i>=0 and i<=5):
      if(b[i]>maxxx):
         maxxx=b[i]
         return i
      i=i-1
   
 
   
setup()