Tuesday, June 12, 2012
Design of t-shirt for cryptoclass 2012
Friday, November 11, 2011
YouTube video offline mode
Recently I enrolled to AI class online course. Each week they submit dozen of explanatory and Q&A videos on youtube and reference them from AI-class site. As I mostly have time to study those videos when I'm offline I've decided to explore possibilities of getting videos to local computer for offline viewing. Purpose of this post is just to summarize steps which I did in order to achieve this goal on linux.
Get tools
After unsuccessful attempt to use clive/cclive, I decided to use youtube-dl which you can download at github project page. Although it is single purpose youtube only downloader it works and does exactly what I need. You need python interpreter to run it.
grake
Grake is utility to parse youtube links out of HTML and it's hosted at code.google.com. Grake is Perl module, but installation is straightforward, as one only has to follow instructions in INSTALL file.
Get list of videos
First step is using youtube.com to find playlist of videos. For example I've just typed 'AI class unit9 videos' to google/youtube to get this link: 'http://www.youtube.com/playlist?list=PL9163DC3C43AF7612'.
Next step is to download list using Grake and edit it
Downloaded file looks like:
Get videos
Last step is to get the videos by using youtube-dl. I've used this:
Get tools
First thing first, I had to equip myself with tools.
youtube-dlAfter unsuccessful attempt to use clive/cclive, I decided to use youtube-dl which you can download at github project page. Although it is single purpose youtube only downloader it works and does exactly what I need. You need python interpreter to run it.
grake
Grake is utility to parse youtube links out of HTML and it's hosted at code.google.com. Grake is Perl module, but installation is straightforward, as one only has to follow instructions in INSTALL file.
Get list of videos
First step is using youtube.com to find playlist of videos. For example I've just typed 'AI class unit9 videos' to google/youtube to get this link: 'http://www.youtube.com/playlist?list=PL9163DC3C43AF7612'.
Next step is to download list using Grake and edit it
$ cd ~/Videos/Unit9 $ grake http://www.youtube.com/playlist?list=PL9163DC3C43AF7612 < Unit9.lst
Downloaded file looks like:
http://youtube.com/watch?v=videoseries http://youtube.com/watch?v=DgH6NaJHfVQ http://youtube.com/watch?v=9D35JSWSJAg http://youtube.com/watch?v=9QMZQkKuYjo http://youtube.com/watch?v=YfSBYf9h7qk ...I've just deleted first line using vim.
Get videos
Last step is to get the videos by using youtube-dl. I've used this:
$ cat Unit9.lst | xargs youtube-dl -tA
With -tA options I told downloader to use video name as filename instead of funky hash and autoprefix it with number as it's downloaded.
Tuesday, September 20, 2011
Groovy Junit tests in Eclipse Indigo
I'm playing with Groovy and Grails a little bit and recently I had a problem running unit-test from recent Eclipse Indigo (3.7) with groovy-eclipse plugin installed.
Problem manifests itself with following error, when I try to righ-click and "run as" junit test-case.
I've spent some time trying to find solution and it was obvious. Think it might be helpful to keep solution in some form. I'll probably post more Groovy/Grails related stuff in the future, so why not start with this.
Let's have simple class MyClass
and test class MyClassTest in same package
Right-click in eclipse and run as JUnit test should work, but you have to:
Problem manifests itself with following error, when I try to righ-click and "run as" junit test-case.
junit.framework.AssertionFailedError: No tests found in path.to.your.class
I've spent some time trying to find solution and it was obvious. Think it might be helpful to keep solution in some form. I'll probably post more Groovy/Grails related stuff in the future, so why not start with this.
Let's have simple class MyClass
package sk.adino.poc.groovy.unittests
class MyClass {
x() { return 1;}
}
and test class MyClassTest in same package
package sk.adino.poc.groovy.unittests
class MyClassTest extends GroovyTestCase {
testX { assert x==1 }
}
Right-click in eclipse and run as JUnit test should work, but you have to:
- have groovy-eclipse plugin installed
- add external JAR pointing to junit-x.y.z.jar from your groovy installation lib directory - otherwise it won't even compile
- add void as a return type of your testX function so it should look like:
package sk.adino.poc.groovy.unittests
class MyClassTest extends GroovyTestCase {
void testX { assert x==1 }
}
Thursday, August 18, 2011
Java puzzler - Letter went missing
Recently I've implemented function that takes String and populates long so that it contains character values from the back of the string. However my unit test is failing because one Letter is missing. Are you able to put it to the right place so that unit tests will not fail without debugging the code?
Solution bellow (highlight to show):
import static org.junit.Assert.*;
import org.junit.Test;
public class StringToolsTest {
static final char [] data = {0xca, 0xfe, 0xeb, 0xab, 0xee};
static final String string5 = String.valueOf(data);
static final String string0 = "";
static final String string1 = "a";
static final String string9 = "987654321";
@Test
public void testFillBackwards() {
long l = 0;
assertEquals(0xcafeebabeeL, StringTools.fillBackwards(string5, l));
assertEquals(0L, StringTools.fillBackwards(string0, l));
assertEquals(0x61L, StringTools.fillBackwards(string1, l));
assertEquals(0x3837363534333231L, StringTools.fillBackwards(string9, l));
}
}
public class StringTools {
public static long fillBackwards(String s, long d)
{
long result = 0L;
byte[] bytes = s.getBytes();
for (int i=0; i<Math.min(8,bytes.length); i++){
result |= ((bytes[bytes.length-i-1] & 0xff) << (8*i));
}
return result;
}
}
Solution bellow (highlight to show):
The problem is on the line 29 in "& 0xFF". I should be doing &0xFFL instead. Why ? bytes[x]&0xff is by default int (both operands are widened to int), but then << is applied, result of which is type of left operand (java spec) and this is |= to long result. Solution is to make & operator to work on long values by adding L to 0xFF.
Puzzler on snipplr.
Saturday, August 13, 2011
Question Interview
One of my friends got this question on his interview: "Given is an arbitrary long string, revert order of words in the string". If the input is "Hello World!", function should modify string to: "World! Hello". Function must have this interface: "void revert(char * string)". It must work in linear time O(n) and have no special memory requirements M(1).
My solution (spoiler alert)
My solution (spoiler alert)
Thursday, July 21, 2011
Java puzzler - How many integers are in the box ?
This is most basis Java Puzzler. Question is, how will the output look like ?
- Will it print out powers of 10 starting from 1 ending at 10000000.
- Or will it print just 1.
- Or will it print any other number.
- Or will it loop forever.
- Or anything else.
package sk.adino.puzzlers;
import static org.junit.Assert.*;
import org.junit.Test;
public class AutoboxingTest {
@Test
public void test() {
int i=1;
while(i<10000000) {
System.out.println("value:" + i);
Integer j=i;
Integer k=i;
if (j==k){
i=i*10;
}else break;
}
assertEquals(i,1000);
}
}
Thursday, June 2, 2011
Puzzler for C developer role
Today, I've created puzzler for C developer at company I work for. This is one of rare cases where we can publish the code, and it's few lines anyhow, so here I give it a shot. You can try to solve the puzzler, as described in comments. It's very easy and you should have no problem solving it without Solaris. All you need is a C compiler.
Bonus question: why did I choose (R) and (C) instead of R and C ?
Bellow is the code which you can find also on snipplr:
Bonus question: why did I choose (R) and (C) instead of R and C ?
Bellow is the code which you can find also on snipplr:
- /** This program should be compiled using following command:
- $ cc -g -o test_hr_test test_hr_test.c
- Original developer Malvin left company to pursue his dream and went
- to live with wild ligers. No-one ever heard any word about him since
- he left. We've managed to recover source code from his source repo.,
- but program seems to be broken. Strangely Malvin used iso-8859-1 as
- his console encoding.
- Hints:
- - program does not compile
- - we were not able to recover whole function permutateBlockRev()
- - program crashes on our X86 solaris 10 matchine (not on linux)
- Are you the one which can help us recover the password ?
- Send it to: rpc-hr@ri-rpc.sk
- */
- #include <stdio.h>
- #include <string.h>
- /** bit manipulation macros */
- #define CLRBIT( STR, IDX ) ( (STR)[(IDX)/8] &= ~(0x01 << (7 - ((IDX)%8))) )
- #define SETBIT( STR, IDX ) ( (STR)[(IDX)/8] |= (0x01 << (7 - ((IDX)%8))) )
- #define GETBIT( STR, IDX ) (( ((STR)[(IDX)/8]) >> (7 - ((IDX)%8)) ) & 0x01)
- /** permutation works on unsigned char blocks of size 8 */
- #define BLOCK_SIZE 8
- typedef unsigned char[BLOCK_SIZE] block;
- /** Function does reverse permutation to DES initial block permutation as described at
- http://en.wikipedia.org/wiki/DES_supplementary_material
- @param b - block which will be permutated in-place
- @note indexes start from 0 contrary to wikipedia initialization vector - C like index
- */
- void permutateBlockRev(block b)
- {
- int i;
- static const int initialPermuteMap[64] =
- {
- 57, 49, 41, 33, 25, 17, 9, 1,
- 59, 51, 43, 35, 27, 19, 11, 3,
- 61, 53, 45, 37, 29, 21, 13, 5,
- 63, 55, 47, 39, 31, 23, 15, 7,
- 56, 48, 40, 32, 24, 16, 8, 0,
- 58, 50, 42, 34, 26, 18, 10, 2,
- 60, 52, 44, 36, 28, 20, 12, 4,
- 62, 54, 46, 38, 30, 22, 14, 6
- };
- /* There are few lines of code missing */
- }
- int main (void)
- {
- unsigned char str[BLOCK_SIZE + 1] = "DummyStr";
- block permutated = { 0x7a, 0xd2, 0x21, 0x3c, 0x05, 0x85, 0x8d, 0x71 };
- permutateBlockRev(permutated);
- strcpy(str, permutated);
- }
Subscribe to:
Posts (Atom)

