잊지 않겠습니다.

'시끌벅적'에 해당되는 글 43건

  1. 2009.01.07 0002. Minesweeper
  2. 2009.01.07 0001. Three plus one
  3. 2009.01.07 주인장입니다.

0002. Minesweeper

시끌벅적 2009. 1. 7. 11:59

import os
import unittest


class Minesweeper:
    def __init__(self):
        self.rows = []
        pass
   
    def clearRow(self):
        self.rows = []
   
    def insertRow(self, row):
        self.rows.append(row)
   
    def printArray2D(self, array_2d):
        for row in array_2d:
            for item in row:
                print item,
            print
   
    def printMine(self):
        self.printArray2D(self.rows)
   
    def initLocation(self):
        mine_locations = []
        for row in self.rows:
            row_locations = []
            for item in row:
                row_locations.append(0)
            mine_locations.append(row_locations)
        self.mine_locations = mine_locations
   
    def isInRange(self, row_location, col_location):
        if row_location >= 0 and col_location >= 0 and row_location < len(self.rows) and col_location < len(self.rows[0]):
            return True
        else:
            return False
       
    def getMineLocation(self):
        self.initLocation()       
        row_location = 0
        for row in self.rows:
            col_location = 0
            for item in row:
                if item == '*':
                    self.mine_locations[row_location][col_location] = '*'
                    for row_index in [-1, 0, 1]:
                        for col_index in [-1, 0, 1]:
                            if self.isInRange(row_location + row_index, col_location + col_index) and self.mine_locations[row_location + row_index][col_location + col_index] != '*':
                                self.mine_locations[row_location + row_index][col_location + col_index] += 1
                col_location += 1
            row_location += 1
   
    def printMineLocation(self):
        self.printArray2D(self.mine_locations)
   
class MinesweeperTest(unittest.TestCase):
    def setUp(self):
        pass
    def testCheckValue(self):
        m = Minesweeper()
        m.insertRow(['.','.','*','.'])
        m.insertRow(['.','*','.','.'])
        m.printMine()
       
        self.assertEqual(m.isInRange(0,0), True)
        self.assertEqual(m.isInRange(5,0), False)
        self.assertEqual(m.isInRange(1,0), True)
        self.assertEqual(m.isInRange(0,5), False)
       
        m.getMineLocation()
        m.printMineLocation()
   
    def testCheckMine2(self):
        m = Minesweeper()
        m.insertRow(['*', '*', '.', '.', '.'])
        m.insertRow(['.', '.','.','.','.'])
        #m.insertRow(['.', '.','.','.','.'])
        m.insertRow(['.', '*','.','.','.'])

        m.getMineLocation()
        m.printMineLocation()
        self.assertEqual(True, True)
    #def tearDown(self):
    #    pass

if __name__ == '__main__':
    unittest.main()

  

require 'test/unit'

class Minesweeper
    attr_reader :rows, :calc_locations
    attr_writer :rows, :calc_locations
   
    def initialize
        self.rows = []
        self.calc_locations = []
    end
   
    def print_rows
        @rows.length.times do |row_index|
            @rows[row_index].length.times {|col_index| print @rows[row_index][col_index].to_s() + ' ' }  
            puts ' '
        end
    end

    def calc_rows
        self.calc_locations = []
        rows.length.times do |row_index|
            row = []
            rows[row_index].length.times do |col_index|
               if rows[row_index][col_index] != '*' : row.push(0)
               else  row.push('*')
               end
            end
            @calc_locations.push(row)
        end

        @rows.length.times do |row_index|
            @rows[row_index].length.times do |col_index|
                -1.upto(1) { |i| -1.upto(1) { |j| @calc_locations[i+row_index][j+col_index] += 1 if self.is_in_range(i+row_index,j+col_index) } } if @rows[row_index][col_index] == '*'
            end
        end
    end

    def is_in_range(row_index, col_index)
        return false if row_index < 0
        return false if col_index < 0
        return false if row_index >= @calc_locations.length
        return false if col_index >= @calc_locations[row_index].length
        return false if @rows[row_index][col_index] == '*'
        return true       
    end

    def print_calc_rows
        @calc_locations.length.times do |row_index|
            @calc_locations[row_index].length.times {|col_index| print @calc_locations[row_index][col_index].to_s() + ' ' }  
            puts ' '
        end
    end   
end

class MinesweeperTest < Test::Unit::TestCase
    def test_case1
        mine = Minesweeper.new()
        mine.rows.push(['.', '*', '.', '.', '*'])
        mine.rows.push(['.', '.', '*', '.', '.'])
        mine.rows.push(['.', '.', '*', '.', '.'])
        mine.rows.push(['.', '.', '*', '.', '.'])
        mine.print_rows()
        mine.calc_rows()
        mine.print_calc_rows()
        assert_equal(1, 1)

        assert_equal(true, mine.is_in_range(0,0))
        assert_equal(false, mine.is_in_range(6, 0))
        assert_equal(false, mine.is_in_range(0, 1))
        assert_equal(1, mine.calc_locations[0][0])
        assert_equal(2, mine.calc_locations[0][2])
    end
end

Posted by Y2K
,

Python

import sys
import unittest

class ThreePlusOne:
    def __init__(self):
        pass
    
    def getArray(self, number):
        return self.__calculateValues(number)

    def __calculateValues(self, number):
        current_value = number
        calculated_values = []
        calculated_values.append(current_value)
        
        while current_value != 1:
            if (current_value % 2) == 0:
                current_value = current_value / 2
            else:
                current_value = current_value * 3 + 1
            calculated_values.append(current_value)

        return calculated_values

    def getLength(self, number):
        return len(self.__calculateValues(number))
    
    def getMaxLengthWithRange(self, start_number, end_number):
        max_length = 0
        for current_number in range(start_number, end_number):
            length = self.getLength(current_number)
            if length > max_length:
                max_length = length
        return max_length
        
    def displayResult(self, start_number, end_number):
        max_length = self.getMaxLengthWithRange(start_number, end_number)
        print '%d %d %d' % (start_number, end_number, max_length)

class ThreePlusOneTest(unittest.TestCase):
    def setUp(self):
        pass

    def testCheckValue(self):
        correct_values = [22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
        t = ThreePlusOne()
        make_values = t.getArray(22)

        self.assertEqual(make_values, correct_values)
        
    def testCheckSize(self):
        correct_value = 16
        t = ThreePlusOne()
        make_value = t.getLength(22)
        self.assertEqual(make_value, correct_value)
    
    def testCheckRange(self):
        t = ThreePlusOne()
        
        result = t.getMaxLengthWithRange(1, 10)
        self.assertEqual(result, 20)
        
        result = t.getMaxLengthWithRange(100, 200)
        self.assertEqual(result, 125)

        result = t.getMaxLengthWithRange(201, 210)
        self.assertEqual(result, 89)

        result = t.getMaxLengthWithRange(900, 1000)
        self.assertEqual(result, 174)
    
    def testDisplay(self):
        t = ThreePlusOne()
        t.displayResult(1, 10)
        t.displayResult(100, 200)
        t.displayResult(201, 210)
        t.displayResult(900, 1000)
    
if __name__ == '__main__':
    unittest.main()

Ruby

require 'test/unit'

class ThreePlusOne
    attr_reader :recent_results, :recent_number
    attr_writer :recent_results, :recent_number
    
    def getArray(number)
        self.calculate(number)
        return self.recent_results
    end
    
    def getLength(number)
        self.calculate(number)
        return  self.recent_results.length
    end
    
    def getMaxLengthWithRange(start_number, end_number)
        max_length = 0
        for i in start_number..end_number
            current_length = self.getLength(i)
            max_length = [max_length, current_length].max
        end
        return max_length
    end
    
    def calculate(number)
        @recent_number = number
        @recent_results = []
        current_value = number
        @recent_results.push(current_value)
        while current_value != 1
            if current_value % 2 == 0 : current_value = current_value / 2
            else current_value = current_value * 3 + 1
            end
            @recent_results.push(current_value)
        end
        return @recent_results
    end        
end

class TestThreePlusOne < Test::Unit::TestCase
    def test_case
        t = ThreePlusOne.new()
        assert_equal(t.getArray(22), [22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1])
        assert_equal(t.getLength(22), 16)
        assert_equal(t.getMaxLengthWithRange(1, 10), 20)
        assert_equal(t.getMaxLengthWithRange(100, 200), 125)
        assert_equal(t.getMaxLengthWithRange(201, 210), 89)
        assert_equal(t.getMaxLengthWithRange(900, 1000), 174)
    end
end

  

  

Posted by Y2K
,

주인장입니다.

시끌벅적 2009. 1. 7. 10:34

Name : YoungKwan Yoon

Job : Programmer

Company : www.hostway.co.kr

Language : C, C++, VB, C#, Python, Ruby

My favorite language : C#

 

1단계 : 초보자란 한가지 언어의 기본적인 기능들을 사용할수 있는 프로그래머를 말한다.
초보자는 클래스와 루틴, 루프, 조건문을 작성할수 있으며 해당언어의 많은 기능을 사용할수 있다.

2단계 : 입문 초보 단계를 넘어온 중급수준의 프로그래머는 여러언어의 기본적인 기능들을 사용할 수 있으며 적어도 한가지의 언어를 능숙하게 다룬다.

3단계 : 유능한 프로그래머는 한가지의 언어나 환경 또는 둘 다 에 전문적인 기술을 가지고 있다.
이 단계에 있는 프로그래머는 J2EE에 있는 모든 복잡한 단계를 알고 있던가 C++ Reference를 암기하고 있을 것이다.
이 단계에 있는 프로그래머는 회사에서 중요한 위치에 있게 되는데, 많은 개발자들이 이 단계를 넘어서질 못한다.

4단계 : 리더는 3단계 프로그래머에 대한 전문적인 지식을 가지고 있다. 그리고 프로그래밍에서 컴퓨터와의 의사소통은 오직 15% 정도이며 85%가 사람과의 의사소통임을 알고 있다. 일반적인 프로그래머는 30%만 혼자서 일한다. 심지어 그보다 더 적은 시간을 컴퓨터와 작업하는데 보낸다. 전문가는 기계보다는 사람을 위해서 코드를 작성한다. 진정한 전문가 수준의 프로그래머는 보석처럼 명확한 코드를 작성하며 문서화한다. 그들은 한줄의 주석으로 표현하여 이해할 수 있는 코드의 로직을 재작성하느라 머리를 쓰지 않는다.

 

Know what your library is hiding from you and why you chose it. 


Posted by Y2K
,