소스 파일명: testWhile.py

  1. # coding=euc-kr
  2. #  Filename: testWhile.py
  3. #
  4. #  Purpose:  Example using the while loop syntax
  5. #                while ....
  6. #
  7. # Execute: python testWhile.py -200 300
  8. #
  9. import sys
  10. # 사용법 표시
  11. def printUsage():
  12.     print "Using: python testWhile.py [integer1] [integer2]"
  13.     print "This finds the greatest common divisor of the given two integers."
  14. if len(sys.argv) != 3:
  15.     printUsage()
  16.     sys.exit(1)
  17. # --------------------------------------
  18. # 명령행 인자의 두 스트링을 가져와서
  19. # 정수 타입으로 변환하여
  20. # 변수 val1과 val2에 저장한다.
  21. val1 = long(sys.argv[1])
  22. val2 = long(sys.argv[2])
  23. # a는 |val1|, |val2| 중 큰 값
  24. a = abs(val1)
  25. b = abs(val2)
  26. if a < b:
  27.     a = abs(val2)
  28.     b = abs(val1)
  29. if b == 0:
  30.     print "GCD(%d, %d) = %d" % (val1, val2, a)
  31.     sys.exit(0)
  32. # --------------------------------------
  33. # Euclidean 알고리즘의 시작
  34. #
  35. # a를 b로 나누어 몫은 q에, 나머지는 r에 저장
  36. q = a / b
  37. r = a % b
  38. # --------------------------------------
  39. # Euclidean 알고리즘의 반복 (나머지 r이 0이 될 때 까지)
  40. while r != 0:
  41.     a = b
  42.     b = r
  43.     q = a / b
  44.     r = a % b
  45. # 나머지가 0이면 그 때 나눈 수(제수) b가 최대공약수(GCD)이다.
  46. gcd = b
  47. # 최대공약수(GCD)를 출력한다.
  48. print "GCD(%d, %d) = %d" % (val1, val2, gcd)


실행:

Command> python testWhile.py
Using: python testWhile.py [integer1] [integer2]
This finds the greatest common divisor of the given two integers.

Command> python testWhile.py -200 300
GCD(-200, 300) = 100

Command> python testWhile.py -200 0
GCD(-200, 0) = 200

Command> python testWhile.py 125 100
GCD(125, 100) = 25

Command> python testWhile.py 23 25
GCD(23, 25) = 1


위의 소스 코드는 ipy 명령에 의하여 IronPython에서도 수정 없이 그대로 실행된다.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

소스 파일명: testWhile.rb

  1. #
  2. #  Filename: testWhile.rb
  3. #
  4. #  Purpose:  Example using the while loop syntax
  5. #                while ....
  6. #
  7. # Execute: ruby testWhile.rb -200 300
  8. #
  9. # 사용법 표시
  10. def printUsage()
  11.     print "Using: ruby testWhile.rb [integer1] [integer2]\n"
  12.     print "This finds the greatest common divisor of the given two integers.\n"
  13. end
  14. if (ARGV.length != 2)
  15.     printUsage()
  16.     exit(1)
  17. end
  18. # --------------------------------------
  19. # 명령행 인자의 두 스트링을 가져와서
  20. # 정수 타입으로 변환하여
  21. # 변수 val1과 val2에 저장한다.
  22. val1 = ARGV[0].to_i
  23. val2 = ARGV[1].to_i
  24. # a는 |val1|, |val2| 중 큰 값
  25. a = val1.abs
  26. b = val2.abs
  27. if (a < b) then
  28.     a = val2.abs
  29.     b = val1.abs
  30. end
  31. if (b == 0)
  32.     print "GCD(#{val1}, #{val2}) = #{a}\n"
  33.     exit(0)
  34. end
  35. # --------------------------------------
  36. # Euclidean 알고리즘의 시작
  37. #
  38. # a를 b로 나누어 몫은 q에, 나머지는 r에 저장
  39. q = a / b
  40. r = a % b
  41. # --------------------------------------
  42. # Euclidean 알고리즘의 반복 (나머지 r이 0이 될 때 까지)
  43. while (r != 0)
  44.     a = b
  45.     b = r
  46.     q = a / b
  47.     r = a % b
  48. end
  49. # 나머지가 0이면 그 때 나눈 수(제수) b가 최대공약수(GCD)이다.
  50. gcd = b
  51. # 최대공약수(GCD)를 출력한다
  52. print "GCD(#{val1}, #{val2}) = #{gcd}\n"




실행:

Command> ruby testWhile.rb
Using: ruby testWhile.rb [integer1] [integer2]
This finds the greatest common divisor of the given two integers.

Command> ruby testWhile.rb 23 25
GCD(23, 25) = 1

Command> ruby testWhile.rb 230 25
GCD(230, 25) = 5

Command> ruby testWhile.rb 230 125
GCD(230, 125) = 5

Command> ruby testWhile.rb -200 300
GCD(-200, 300) = 100




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

간단한 Groovy 스크립트:

def name='World'; println "Hello $name!"

다소 복잡한 Groovy 스크립트:

class Greet {
  def name
  Greet(who) { name = who[0].toUpperCase() + who[1..-1] }
  def salute() { println "Hello $name!" }
}

g = new Greet('world')  // 객체 생성
g.salute()              // "Hello World!"를 출력




Apache의 commons.lang 라이브러리를 이용한 스크립트
(한 개의 소스 파일로 저장한다.):

class Greet {
  def name
  Greet() {  }
  Greet(who) { name = who[0].toUpperCase() + who[1..-1] }
  def salute() { println "Hello, $name!" }
}
g = new Greet('world')  // 객체 생성
g.salute()              // "Hello, World!"를 출력

import static org.apache.commons.lang.WordUtils.*

















명령행 스크립트로 실행한 경우:
groovy -e "println 'Hello ' + args[0] + '!'" World
Posted by Scripter
,