Jeswang's Blog

盲目跟随还是独立去做,To be or not to be?

Reverse a Char

| Comments

I met this problem in one interview: How to reverse a char? It took me a long time to figure out which I thought was very easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

unsigned char reverse_char(unsigned char in) {
    unsigned char high = 0x80;
    unsigned char res = 0x0, tmp = 0x0;
    for (int i=7; i>=0; i--) {
        tmp = (in&0x1)<<i;
        in = in>>1;
        res = (res&~high)|tmp;
        high = high>>1;
    }
    return res;
}

int main(int argc, char *argv[]) {
    printf("%x\n", reverse_char(0xc4));
}

You have to pay attention to the difference of char and unsigned char.

More info about signed char: What does it mean for a char to be signed? - Stack Overflow

- EOF -

Fix Soft Links in an Archived File

| Comments

OS X 10.8 sdk was not included in Xcode 6.1, so if you want to use that sdk, you have to add it to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/ yourself. For me, the reason I needed 10.8 sdk was I wanted to compile objc runtime source code with it so I can debug the code.

How to get the previous sdks

You can extract previous sdks from old versions fo Xcode. Or you can get them more efficently from this repository(phracker/MacOSX-SDKs) by using git or just downloading the zip file.

But if you choose downloading the zip file, the zip format will break all the soft link by changing them into plain text files.

How to fix it

In order to fix it, I wrote several lines of python. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Jeswang<wangyi724@gmail.com>
#         http://blog.jeswang.org
# Created on 2014-10-22 13:03:12

import os

def file_exists(filename):
    try:
        with open(filename) as f:
            return True
    except IOError:
        return False

def folder_exists(foldername):
    try:
        os.walk(foldername).next()
        return True
    except StopIteration:
        return False

def generateSoftLink(realfile, linkfile):
    print("%s -> %s" % (linkfile, realfile))
    os.symlink(realfile, linkfile)

def generateSoftLinkForPath(path):
    list_of_files = {}
    for (dirpath, dirnames, filenames) in os.walk(path):
        for filename in filenames:
            print filename
            linkfile = dirpath+"/"+filename

            if os.path.islink(linkfile) or os.path.getsize(linkfile) > 200:
                continue

            f = open(linkfile)
            relpath = f.readline()
            f.close()

            if len(relpath) == 0:
                continue

            relpath = relpath.replace("\n", "")

            if len(relpath.replace("/", "")) == 0:
                continue

            realfile = dirpath+"/"+relpath

            if file_exists(realfile) or folder_exists(realfile):
                os.remove(linkfile)
                generateSoftLink(relpath, linkfile)

if __name__ == "__main__":
    generateSoftLinkForPath("/Users/jeswang/Downloads/MacOSX-SDKs-master")

The tricky part is, under mac os x python’s function os.path.exists can’t deal well with soft links, so you have to test whether there is a file or folder in given path by trying to read the file or list the folder.

Because of the recursion of references, you may need to run this script several time to make sure all the soft link have be changed back.

- EOF -

(++a)+(++a)+(++a) 的计算结果

| Comments

今天被问到如下代码的输出是多少:

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
    int a = 0;
    int b = (++a) + (++a) + (++a);
    cout<<b;
    return 0;
}

面试中被问到的 iOS 开发相关的问题

| Comments

说来惭愧,已经面了几个公司的 iOS 开发岗位,出现了几次被问倒的情况——有些问题回答得比较含糊,有些问题只知道结果,不知道原理。根据回忆,在这里总结一下,学习的同时也减少再次被问倒的尴尬。

LeetCode for iOS 1.1 版

| Comments

1.1.0 版发布说明:

  • 新功能:
    • 查看每道题给出的样例解法了
    • 支持 iPhone 6/6 Plus
  • 修正 Bug:
    • iPad 上的搜索动画修复

《Objective-C 编程之道》读书笔记

| Comments

所谓设计模式就是给常用的一些代码编写组织方法起个名字。

因为在简历里写了设计模式一项,所以在几个公司的面试中都被问到了相关的问题。尽管之前粗略的翻看过《Objective-C 编程之道——iOS 设计模式解析》,在实际的项目中也用过一些设计模式,但是并不清楚哪些代码实现的是哪些模式,使用这些模式的目的是什么、好处是什么、缺点是什么,所以好好重读了《Objective-C 编程之道——iOS 设计模式解析》,希望能有所收获。

树的后续遍历

| Comments

题目:树的后序遍历

解法:

一共尝试了 3 种解法:

  • 使用两个 Map 记录是否访问过了左右子树(当然也可以放到一个 Map 中来)
  • 只用一个栈以及上次访问的节点做后序遍历
  • 传统的递归方法,用于验证