Archive

Tag Archives: cha

1. Open a terminal window.

2. At the input prompt you will see this structure:
“`
nicholas@computer-name:~$ _
“`

3. So you have to edit the hostname file:
“`
sudo nano /etc/hostname
“`

4. When prompted, enter the administrator password and hit Enter.

5. The hostname file will open, showing the current computer name. Replace the name with the desired new name.

6. Hit Ctrl+X to save and exit.

7. New name will show when you open a new terminal window.

Unable to install Homebrew with this?

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

What’s happened

-e:70: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
Password:
==> Downloading and installing Homebrew...
remote: Counting objects: 192274, done.
remote: Compressing objects: 100% (52298/52298), done.
remote: Total 192274 (delta 138823), reused 192246 (delta 138803)
Receiving objects: 100% (192274/192274), 38.59 MiB | 189 KiB/s, done.
Resolving deltas: 100% (138823/138823), done.
From https://github.com/Homebrew/homebrew
 * [new branch]      master     -> origin/master
error: unable to unlink old 'Library/Aliases/0mq' (Permission denied)
error: unable to unlink old 'Library/Aliases/4store' (Permission denied)
error: unable to unlink old 'Library/Aliases/Secret Rabbit Code' (Permission denied)
error: unable to unlink old 'Library/Aliases/ag' (Permission denied)
error: unable to unlink old 'Library/Aliases/alut' (Permission denied)
error: unable to unlink old 'Library/Aliases/android' (Permission denied)
error: unable to unlink old 'Library/Aliases/apache-activemq' (Permission denied)
error: unable to unlink old 'Library/Aliases/apache-fop' (Permission denied)
error: unable to unlink old 'Library/Aliases/aws-as' (Permission denied)
error: unable to unlink old 'Library/Aliases/aws-mon' (Permission denied)
error: unable to unlink old 'Library/Aliases/beanstalkd' (Permission denied)
error: unable to unlink old 'Library/Aliases/bjam' (Permission denied)
error: unable to unlink old 'Library/Aliases/boehmgc' (Permission denied)
error: unable to unlink old 'Library/Aliases/boost-jam' (Permission denied)
error: unable to create symlink Library/Aliases/boot2docker-cli (Permission denied)
error: unable to unlink old 'Library/Aliases/bzr' (Permission denied)
error: unable to unlink old 'Library/Aliases/cowthink' (Permission denied)
error: unable to unlink old 'Library/Aliases/cpanm' (Permission denied)
error: unable to unlink old 'Library/Aliases/csvfix' (Permission denied)
error: unable to unlink old 'Library/Aliases/ctags-exuberant' (Permission denied)
error: unable to unlink old 'Library/Aliases/db' (Permission denied)
error: unable to unlink old 'Library/Aliases/dbus' (Permission denied)
error: unable to unlink old 'Library/Aliases/dejagnu' (Permission denied)
error: unable to unlink old 'Library/Aliases/eyeD3' (Permission denied)
error: unable to unlink old 'Library/Aliases/fastcgi' (Permission denied)
error: unable to unlink old 'Library/Aliases/fishfish' (Permission denied)
error: unable to unlink old 'Library/Aliases/fluidsynth' (Permission denied)
error: unable to unlink old 'Library/Aliases/gearmand' (Permission denied)
error: unable to unlink old 'Library/Aliases/git-tig' (Permission denied)
error: unable to unlink old 'Library/Aliases/gnu-scientific-library' (Permission denied)
error: unable to unlink old 'Library/Aliases/google-go' (Permission denied)
error: unable to unlink old 'Library/Aliases/gperftools' (Permission denied)
error: unable to unlink old 'Library/Aliases/gpg' (Permission denied)
error: unable to unlink old 'Library/Aliases/gpg2' (Permission denied)
error: unable to unlink old 'Library/Aliases/gs' (Permission denied)
error: unable to unlink old 'Library/Aliases/gtk' (Permission denied)
error: unable to unlink old 'Library/Aliases/gtypist' (Permission denied)
error: unable to unlink old 'Library/Aliases/hashdeep' (Permission denied)
error: unable to unlink old 'Library/Aliases/heroku' (Permission denied)
error: unable to unlink old 'Library/Aliases/hg' (Permission denied)
error: unable to unlink old 'Library/Aliases/htop' (Permission denied)
error: unable to unlink old 'Library/Aliases/hudson' (Permission denied)
error: unable to unlink old 'Library/Aliases/ipsum' (Permission denied)
error: unable to unlink old 'Library/Aliases/jocr' (Permission denied)
error: unable to unlink old 'Library/Aliases/lcms' (Permission denied)
error: unable to unlink old 'Library/Aliases/lcms2' (Permission denied)
error: unable to unlink old 'Library/Aliases/leg' (Permission denied)
error: unable to unlink old 'Library/Aliases/libcryptopp' (Permission denied)
error: unable to unlink old 'Library/Aliases/libgd' (Permission denied)
error: unable to unlink old 'Library/Aliases/libgeoip' (Permission denied)
error: unable to unlink old 'Library/Aliases/libjpeg' (Permission denied)
error: unable to unlink old 'Library/Aliases/libjpeg-turbo' (Permission denied)
error: unable to unlink old 'Library/Aliases/libjpg' (Permission denied)
error: unable to unlink old 'Library/Aliases/liblabjackusb' (Permission denied)
error: unable to unlink old 'Library/Aliases/libmad' (Permission denied)
error: unable to unlink old 'Library/Aliases/libmcrypt' (Permission denied)
error: unable to unlink old 'Library/Aliases/libnettle' (Permission denied)
error: unable to unlink old 'Library/Aliases/liboggz' (Permission denied)
error: unable to unlink old 'Library/Aliases/libqrencode' (Permission denied)
error: unable to unlink old 'Library/Aliases/libtag' (Permission denied)
error: unable to unlink old 'Library/Aliases/libtasn' (Permission denied)
error: unable to unlink old 'Library/Aliases/libtcnative' (Permission denied)
error: unable to unlink old 'Library/Aliases/littlecms' (Permission denied)
error: unable to unlink old 'Library/Aliases/mc' (Permission denied)
error: unable to unlink old 'Library/Aliases/mediainfo' (Permission denied)
error: unable to unlink old 'Library/Aliases/mongo' (Permission denied)
error: unable to unlink old 'Library/Aliases/mosh' (Permission denied)
error: unable to unlink old 'Library/Aliases/mp4box' (Permission denied)
error: unable to unlink old 'Library/Aliases/mpich' (Permission denied)
error: unable to unlink old 'Library/Aliases/myrepos' (Permission denied)
error: unable to unlink old 'Library/Aliases/node.js' (Permission denied)
error: unable to unlink old 'Library/Aliases/nodejs' (Permission denied)
error: unable to unlink old 'Library/Aliases/npm' (Permission denied)
error: unable to unlink old 'Library/Aliases/nsis' (Permission denied)
error: unable to unlink old 'Library/Aliases/o-caml' (Permission denied)
error: unable to unlink old 'Library/Aliases/ocaml' (Permission denied)
error: unable to unlink old 'Library/Aliases/ocio' (Permission denied)
error: unable to unlink old 'Library/Aliases/offlineimap' (Permission denied)
error: unable to unlink old 'Library/Aliases/ooc' (Permission denied)
error: unable to unlink old 'Library/Aliases/openmpi' (Permission denied)
error: unable to unlink old 'Library/Aliases/openocd' (Permission denied)
error: unable to unlink old 'Library/Aliases/pgrep' (Permission denied)
error: unable to unlink old 'Library/Aliases/pipeviewer' (Permission denied)
error: unable to unlink old 'Library/Aliases/pkgconfig' (Permission denied)
error: unable to unlink old 'Library/Aliases/pkill' (Permission denied)
error: unable to unlink old 'Library/Aliases/pocketsphinx' (Permission denied)
error: unable to unlink old 'Library/Aliases/postgres' (Permission denied)
error: unable to create symlink Library/Aliases/pt (Permission denied)
error: unable to unlink old 'Library/Aliases/qt4' (Permission denied)
error: unable to unlink old 'Library/Aliases/shell-fm' (Permission denied)
error: unable to unlink old 'Library/Aliases/slang' (Permission denied)
error: unable to unlink old 'Library/Aliases/sphinxbase' (Permission denied)
error: unable to unlink old 'Library/Aliases/sqlite3' (Permission denied)
error: unable to unlink old 'Library/Aliases/stax-sdk' (Permission denied)
error: unable to unlink old 'Library/Aliases/style' (Permission denied)
error: unable to unlink old 'Library/Aliases/svn' (Permission denied)
error: unable to unlink old 'Library/Aliases/tinyfugue' (Permission denied)
error: unable to create symlink Library/Aliases/twemproxy (Permission denied)
error: unable to unlink old 'Library/Aliases/twolame' (Permission denied)
error: unable to unlink old 'Library/Aliases/unix2dos' (Permission denied)
error: unable to unlink old 'Library/Aliases/urxvt' (Permission denied)
error: unable to unlink old 'Library/Aliases/usb-multiplex-daemon' (Permission denied)
error: unable to create symlink Library/Aliases/vid.stab (Permission denied)
error: unable to unlink old 'Library/Aliases/wxwidgets' (Permission denied)
error: unable to unlink old 'Library/Aliases/xmlsec1' (Permission denied)
error: unable to unlink old 'Library/Aliases/zmq' (Permission denied)
error: unable to unlink old 'Library/Contributions/brew_bash_completion.sh' (Permission denied)
error: unable to unlink old 'Library/Contributions/brew_fish_completion.fish' (Permission denied)
error: unable to unlink old 'Library/Contributions/brew_zsh_completion.zsh' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-aspell-dictionaries.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-beer.rb' (Permission denied)
error: unable to create file Library/Contributions/cmd/brew-bundle-dir.rb (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-bundle.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-cleanup-installed' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-gist-logs.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-graph' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-man' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-profile.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-pull.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-server' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-services.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-switch.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-tap-readme.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-test-bot.rb' (Permission denied)
error: unable to create file Library/Contributions/cmd/brew-versions.rb (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/brew-which.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/git' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/public/bootstrap.min.css' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/public/glyphicons-halflings-white.png' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/public/glyphicons-halflings.png' (Permission denied)
error: unable to unlink old 'Library/Contributions/cmd/svn' (Permission denied)
error: unable to unlink old 'Library/Contributions/example-formula.rb' (Permission denied)
error: unable to unlink old 'Library/Contributions/manpages/brew.1.md' (Permission denied)
error: unable to unlink old 'Library/ENV/3.2.6' (Permission denied)
error: unable to unlink old 'Library/ENV/4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/ant' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/apr-1-config' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/bsdmake' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/c++' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/c89' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/c99' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/cc' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/clang' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/clang++' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/cpp' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.3' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.4' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.5' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.6' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.7' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.8' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/g++-4.9' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.3' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.4' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.5' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.6' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.7' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.8' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gcc-4.9' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/git' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/gmake' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/i686-apple-darwin11-llvm-g++-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/i686-apple-darwin11-llvm-gcc-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/ld' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/llvm-g++' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/llvm-g++-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/llvm-gcc' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/llvm-gcc-4.2' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/make' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/mig' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/pod2man' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/sed' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/svn' (Permission denied)
error: unable to unlink old 'Library/ENV/4.3/xcrun' (Permission denied)
fatal: cannot create directory at 'Library/ENV/pkgconfig/10.10': Permission denied
Failed during: git reset --hard origin/master

Reason

Probably you have multiple users on your computer. And you uninstalled the old Homebrew and trying to re-install it.

Solution

Theorotically you can remove the directory /usr/local/Library but this will happens again when you try to use Homebrew with another user. It is better to change the permission of directory with this:

sudo chmod -R 775 /usr/local/Library

and after a successful install, you better fix the permission for another directory. This directory may appear after you run some ruby commands.

sudo chmod -R 775 /usr/local/Cellar

 

Well, it is always good to have a refreshing layout. I spent like 2 days to tweak my website layout. It’s really awesome. Useless widgets are removed and background changed. There is even a whole new typography. I love it so much!

nicholas-workshop

Question

This is an actual question I encountered in an Amazon phone interview in November 2013. You are going to design the money changing algorithm for a vending machine. That is, after any purchase, the machine makes change to users with a combination of coins. And the machine only have 3 types of coins: nickel (5 cents), dime (10 cents) and quarter (25 cents). Coins with higher values are preferred in the change.

Solution

Well this is actually a simple question, I could just fill the sum from the highest value coin to the lowest. However I chose to use a simplified version of knapsack algorithm. Whatever, they both works.

Sample

# coin values
coins = [25, 10, 5]

# simple solution for this problem
def simple_solution(sum):
    combination = []
    for coin in coins:
        for i in xrange(sum / coin): combination.append(coin)
        sum %= coin
    return combination

# result: [25, 25, 10, 5]
print simple_solution(65)

 

Question

Write a program to right-rotate a string by m characters. Right-rotating a string means moving m characters at the left of string to the right. Is it required the time complexity is O(n) and helper memory size is O(1). For example, right-rotate “abcdefghi” by 3 characters gives “defghiabc”.

Solution

There are 2 ways to do it. The first one is that we divide the string into 2 parts (XY). X is the part to move to the right and Y is the one moving on to the left. Reverse them in place 2 times.

XY: "abcdefghi"
R(X) R(T): "cbaihgfed"
YX: "defghiabc"

Secondly we can swap character one by one. Let p0 at position 0, p1 at position m. Swap their characters repeatedly with moving both pointer right by 1.

abcdefghij
dbcaefghij
decabfghij
defabcghij
defgbcahij
defghcabij
defghiabcj
defghijbca  <- p1 doesn't move since it reaches the end
defghijacb
defghijabc
#include <iostream>

// reverse the string between start and end
void reverse_string(char* start_pointer, char* end_pointer)
{
	while (start_pointer < end_pointer) {
		char temp_char = *start_pointer;
		*start_pointer = *end_pointer;
		*end_pointer = temp_char;
		start_pointer++;
		end_pointer--;
	}
}

// solution 1
void left_rotate_string_1(char* string_pointer, int n)
{
	if (string_pointer == NULL) return;

	int length = strlen(string_pointer);
	if (n > length || n <= 0) return;

	char* first_start_pointer = string_pointer;
	char* first_end_pointer = string_pointer + n - 1;
	char* second_start_pointer = string_pointer + n;
	char* second_end_pointer = string_pointer + length - 1;
	reverse_string(first_start_pointer, first_end_pointer);
	reverse_string(second_start_pointer, second_end_pointer);
	reverse_string(first_start_pointer, second_end_pointer);
}

// solution 2
void left_rotate_string_2(char* string_pointer, int n)
{
	if (string_pointer == NULL) return;

	int length = strlen(string_pointer);
	if (n > length || n <= 0) return;

	char* pointer_1 = string_pointer;
	char* pointer_2 = string_pointer + n;
	char* pointer_end = string_pointer + length - 1;
	while (pointer_1 != pointer_2) {
		char temp_char = *pointer_1;
		*pointer_1 = *pointer_2;
		*pointer_2 = temp_char;
		if (pointer_1 != pointer_end) pointer_1++;
		if (pointer_2 != pointer_end) pointer_2++;
	}
}

// main
int main()
{
	char string_1[] = "abcdefghij";
	left_rotate_string_1(string_1, 3);
	printf("%sn", string_1); // defghijabc

	char string_2[] = "abcdefghij";
	left_rotate_string_2(string_2, 3);
	printf("%sn", string_2); // defghijabc
}

Question

Given a function prototype: int continumax(char *output_string,char *input_string). Implement it to find the longest consecutive digits. This function must return the length of the longest digits. The found longest digits should be written to the memory location that output_string is pointing. For example, if input_string is “abcd12345ed125ss123456789”, function returns 9 and output_string becomes “123456789”.

Solution

This question must be implemented in C/C++. Just skip letters, count the length and save the head position to a temp pointer.

Sample

#include <iostream>

int continumax(char *output_string, char *input_string)
{
	int max_length = 0;
	char *max_string = new char[sizeof(input_string)];

	while (true) {

		// skip all non-digit characters
		while (*input_string != 0 && (*input_string < '0' || *input_string > '9')) input_string++;
		if (*input_string == 0) break;

		// current char is digit
		int length = 0;
		char *temp_string = input_string;
		while (*input_string!=0 && *input_string >= '0' && *input_string <= '9') {
			length++;
			input_string++;
		}

		// check if this is longer
		if (length > max_length) {
			max_length = length;
			max_string = temp_string;
		}
	}

	// write maximum string to output and add null to end.
	int i;
	for (i = 0; i < max_length; i++) output_string[i] = max_string[i];
	output_string[i] = 0;

	return max_length;
}

int main()
{
	char input[] = "abcd12345ed125ss123456789";
	char *output = new char[sizeof(input)];
	int length = continumax(output, input);
	printf("Length: %dn", length);
	printf("Output: %sn", output);
}

Question

Convert the inputted string to an integer. For example, “345” will output 345.

Solution

Though it looks simple, in fact it is pretty tricky. First we need to make the concept clear.

1. Scan each character from the left to right, then multiply the previously obtained integer by ten before adding the scanned digit to it.

2. Integer can be positive or negative, check if the first character is ‘+’ or ‘-‘.

3. Check for exceptions such as non-digit or null input.

4. Check for overflow. Overflow means the inputted integer is greater than the maximum size of integer that can be stored in memory. In this case we should output 0, or throw an exception.

string_to_integer(string)
  if string is null then return 0
  positive_int = true
  long_number = 0
  for i equals 0 to length of string
    if i == 0
      if string[i] == '+' then
        continue
      else if string[i] == '-' then
        positive_int = false
        continue
    if string[i] >= 0 and string[i] <= 9 then
      digit = string[i] - '0'
      long_number = long_number * 10 + digit
      if int_overflow(long_number) then
        long_number = 0
        throw exception
    else
      long_number = 0
      throw exception
  if not positive_int then
    long_number = 0 - long_number
  return long_number

Example

import sys

def int_overflow(long):
    return not -sys.maxint-1 <= long <= sys.maxint

def string_to_integer(string):
    if string is None: return 0
    positive_int = True
    long_number = 0
    for i in xrange(0, len(string)):
        print string[i]
        if i == 0:
            if string[i] == '+':
                continue
            elif string[i] == '-':
                positive_int = False
                continue
        if ord('0') <= ord(string[i]) <= ord('9'):
            digit = ord(string[i]) - ord('0')
            long_number = long_number * 10 + digit
            if int_overflow(long_number):
                long_number = 0
                return Exception('integer overflew')
        else:
            long_number = 0
            return Exception('invalid digit in string')
    if not positive_int:
        long_number = 0 - long_number
    return long_number

print string_to_integer('-345')
enum Status {kValid = 0, kInvalid};
int g_nStatus = kValid;

int StrToInt(const char* str)
{
      g_nStatus = kInvalid;
      long long num = 0;

      if(str != NULL)
      {
            const char* digit = str;

            // the first char in the string maybe '+' or '-'
            bool minus = false;
            if(*digit == '+')
                  digit ++;
            else if(*digit == '-')
            {
                  digit ++;
                  minus = true;
            }

            // the remaining chars in the string
            while(*digit != '/0')
            {
                  if(*digit >= '0' && *digit <= '9')
                  {
                        num = num * 10 + (*digit - '0');

                        // overflow
                        if(num > std::numeric_limits<int>::max())
                        {
                              num = 0;
                              break;
                        }

                        digit ++;
                  }
                  // if the char is not a digit, invalid input
                  else
                  {
                        num = 0;
                        break;
                  }
            }

            if(*digit == '/0')
            {
                  g_nStatus = kValid;
                  if(minus)
                        num = 0 - num;
            }
      }
      return static_cast<int>(num);
}
%d bloggers like this: