Skip to content

Instantly share code, notes, and snippets.

@fcamel
fcamel / coil.py
Last active December 15, 2015 14:49
A strait-forward implementation for the problem: http://www.careercup.com/question?id=14968041
# Problem description: http://www.careercup.com/question?id=14968041
def find_coil(n, x, y, dxs, dys):
nsteps = []
for nstep in xrange(2, n - 1, 2):
nsteps.append(nstep)
nsteps.append(nstep)
nsteps.append(nstep + 1)
rs = [y * n + x + 1]
idx = 0
@fcamel
fcamel / gist:5664081
Created May 28, 2013 16:34
Java's special syntax to access outer class
$ cat Outer.java
class Outer {
class Inner {
void foo() {
System.out.println(Outer.this.x);
}
}
private int x = 3;
public static void main(String[] args) {
Outer outer = new Outer();
#include <stdio.h>
int main(void) {
return 0;
}
#!/bin/bash
OUT="/tmp/htop.out"
/bin/bash -c "sleep 1; killall htop" &
htop > $OUT
# Ref. Remove color codes (special characters) with sed
# http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
result=$(sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" $OUT | sed 's/.*Mem.*[^0-9]\([0-9]\+\/[0-9]\+\)MB.*Swp.*/\1\n/')
// Ref. chromium/src/base/debug/debugger_posix.cc
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#!/bin/bash
# Ref. http://sourceware.org/gdb/onlinedocs/gdb/Index-Files.html
if [ $# -ne 1 ]; then
echo -e "$0 <binary>\n"
echo -e "Add gdb index to <binary>\n"
exit 1
fi
binary="$1"
import gdb
import re
class ShorternBacktraceCommand(gdb.Command):
'''Show a backtrace without argument info in each frame.'''
def __init__(self):
super(ShorternBacktraceCommand, self).__init__ ("bt",
gdb.COMMAND_SUPPORT,
gdb.COMPLETE_NONE)
def invoke(self, arg, from_tty):
import gdb
class ShorternBacktraceCommand(gdb.Command):
'''Show a backtrace without argument info in each frame.'''
def __init__(self):
super(ShorternBacktraceCommand, self).__init__ ("bt",
gdb.COMMAND_SUPPORT,
gdb.COMPLETE_NONE)
def invoke(self, arg, from_tty):
#include <stdio.h>
#ifdef DEBUG_PRINT
#define DebugPrintf(format, args...) fprintf(stderr, "%s this=%p " format, __PRETTY_FUNCTION__, this, ##args)
#else
#define DebugPrintf(format, args...)
#endif
class Calculator
{
import gdb
def read_source_code(filename, target_line, num_lines):
with open(filename, 'r') as fr:
target_line -= 1
lines = [' | ' + line[:-1] for line in fr]
lines[target_line] = '->| ' + lines[target_line][4:]
offset = num_lines / 2
return lines[target_line - offset : target_line + offset]