'Programming...'에 해당되는 글 6건
- 2009/11/27 SRT2SMI (2)
- 2007/04/30 아름다운 코드들
- 2006/08/28 Cygwin/X on remote host 설정법
- 2005/12/12 C99... 간단한 요약 (6)
- 2005/11/14 참고 사이트 (4)
- 2005/08/11 지정된 경로의 파일들 이름의 일부를 변경하는 프로그램, python
SRT2SMI
아름다운 코드들
* 0~(n-1)의 랜덤 넘버.
JAVA, 출처: 철규형 소스 어딘가.
비트 쉬프트로 if를 없앴다. 아름답지 아니한가? >.<
====================================================================
int getRandomNumber(n) {
return (Random.nextInt() >>> 1) % n;
}
====================================================================
* 0~(n-1) 사이의 다중 토글
C, JAVA, 출처: 효상형 소스 어딘가.
난 if 하나라도 빼야 빠를 거라는 모바일에서의 진리!
====================================================================
i = (i + 1) % n;
====================================================================
* n자리 필터 만들어 내는 함수
C, 출처: Queen Problem 문제 풀이, "누워서 보는 알고리즘"
if도 loop도 필요 없다. 정말 감탄한 소스 중 하나.
====================================================================
int makefilter(int n) {
int i = 1;
return (i << n) - 1;
}
====================================================================
* 0이면 0 아니면 1로 만드는 방법
C, 출처: mpeg2 인/디코더 소스
이미 알고 있었지만, 실제로 쓰고 있는 건 처음 봤음.
간단히 #define 해서 쓰면 좋을 듯
====================================================================
n = !!n ;
====================================================================
* 위에꺼 응용: 값을 2진수로 출력하는 함수
C, 출처: 후배 숙제 어딘가 도와주다가
위 소스 보고 바로 다음 날 후배가 도와 달라고 해서 함수 짜주다가 사용
====================================================================
void printBin(int n) {
for (int i = 0; i < sizeof(int) * 8; i++) {
printf("%d", !!(n & 0x80000000));
n <<= 1;
}
printf("\n");
}
====================================================================
* List에 포함된 문자열 또는 시퀀스 자료 개수 순서대로 정렬하는 방법
Python, 출처: 자연어처리 숙제용 사전 만들다가.
사실 if 문은 필요도 없음..;;;
====================================================================
def mycmp(s1, s2):
if len(s2) == len(s1):
return cmp(s1, s2)
return len(s2) - len(s1)
L.sort(mycmp)
====================================================================
뭐.. 간단한 것들이지만, 이런 간단한 아이디어가 정말 멋진 코드를 만들어 내는 것 같다.
문득 써두면 뭔가 있어 보일 것 같아서 포스팅...
Cygwin/X on remote host 설정법
making CygWin/X work with Fedora Core2
With lots of trial and error and some googling, I finally managed to get CygWin/X running on my Windows box work with my Fedora Core2 Linux system. The steps are actually quite simple:
1. start the X in multiwindow mode from cygwin's bash prompt:
$ X -multiwindow &
This would run the X server in the background, showing an X icon in the Windows system tray (also known as notification area). The root X window is hidden.
2. launch xterm on the local machine:
$ xterm -display localhost:0 &
This opens an x window on the local machine.
3. From this X window, ssh into the remote Linux system:
$ ssh -X -l <user> <linux-host>
Now, any program with GUI run from this window on the remote system will open a window on the local machine. Simple, isn't it!
So, why did it take me signficant amount of trial and error and googling?
Perhpas I was misled by the ease with which CygWin/X works with an HPUX system:
$X -query <hpux-host>
This doesn't work for Linux. The explanation I found is that the above command requires xdm daemon to be running on the target host, which is disabled by default on most Linux systems (for security reasons). Apparently, the steps to enable and run xdm are not very straightforward and I gave up after a couple of false starts.
Other route I tried was to run X server on the local machine, then ssh into the remote Linux machine from a Windows command window, set the DISPLAY env. variable and then run an X program from the ssh session. This used to show the following error on the remote Linux machine:
Xlib: connection to "<local-host>:0.0" refused by server
Xlib: No protocol specified
xterm Xt error: Can't open display: <local-host>:0
And the following message on the local machine:
AUDIT: Wed Sep 22 23:52:18 2004: 2040 X: client 1 rejected from IP 192.168.1.103
These messages had me baffled for quite a while! But now that I know how to make things work, even these error messages make sense.
C99... 간단한 요약
숙제하다가.. 평소에 잘 쓰던 코드 갑자기 cygwin에서 컴파일 안 되어서 흠칫 했다가...
에러 메세지 보고서는 잽싸게 확인 했다..
com.h:21:29: warning: __VA_ARGS__ can only appear in the expansion of a C99 variadic macro
__VA_ARGS__가 C99부터였군...
근데.. 난 어떻게 저거 쓰고 있었던 거지? -_-;;
일단 매크로 없애고, inline 함수로 바꿔 놓고 넘어감...
참고 사이트
http://www.osix.net/modules/article/?id=670
GCC 기본 사용법
http://blog.naver.com/hyperlancer/120012699615
지정된 경로의 파일들 이름의 일부를 변경하는 프로그램, python
import string
seperator = os.sep
def getDestName(name, sep, newsep):
return string.join(string.split(name, sep), newsep)
def replace(path, src, dst):
files = os.listdir(path);
for filename in files:
if os.path.isdir(filename) == 1:
replace(path + seperator + filename, src, dst)
elif string.find(filename, src) >= 0:
os.rename(path + seperator + filename, path + seperator + getDestName(filename, src, dst))
사용은 "replace(경로, 원래 문자열, 바꿀 문자열)"
c:\temp\abc.txt
c:\ttt.abc
가 있고,
replace(c:\, abc, cba) 를 실행하면,
c:\temp\cbs.txt
c:\ttt.cba 가 된다.
하도 오랜만에 만져본 파이썬이라, 더 짧고 빠르게 짤수도 있을 것 같은데...
이 코드는 왠지 무쟈게 느린 것 같다...
일단은 이 코드로 내가 쓰는 데 별 지장 없으므로 일단 오키...
PS) 맘대로 퍼가셔서 써도 됩니다...만, 그럴 사람이 있을까? -_-;;




