Monday, December 09, 2013

[UVa] 12718 - Dromicpalin Substrings

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

bool isOdd(int val) {
    if (val & 1) return true; // bitwise checking
    else return false;
}
int counts[1000]; // only 'a' - 'z' is significant
int main( ) {

    //freopen("j.input.txt","r",stdin);
    //freopen("j.analysis.txt","w",stdout);

    int test, kase=1;
    string inputstr;

    cin >> test;

    while (test--) {

        cin >> inputstr;

        int palindromes=0;
        for (int i = 0 ; i<inputstr.size() ; i++) {
            for (int ii = 'a' ; ii<='z' ; ii++) counts[ii] = 0;
            int odds = 0;
            for (int j=i ; j<inputstr.size() ; j++) {
                counts[inputstr[j]]++;

                if ( isOdd(counts[inputstr[j]]) ) odds++;
                else odds--;
                int rangeLength = j-i+1;
                if ( isOdd(rangeLength) && odds==1 ) palindromes++;
                if ( !isOdd(rangeLength) && odds==0 ) palindromes++;
            }
        }
        cout << "Case " << kase++ << ": " << palindromes << endl;
    }

    return 0;
}


[UVa] 12712 - Pattern Locker

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

#define MOD 10000000000007

typedef long long lint;

int main() {

    lint test, l, m, n, kase=1;

    cin >> test;

    while (test--) {

        cin >> l >> m >> n;
        lint init = 1;
        for (lint i = l*l ; i>(l*l-m) ; i--) {
            init = ((init * i)%MOD);
        }
        lint sum = init;
        for (lint i = (l*l-m) ; i>(l*l-n) ; i--) {
            init = ((init * i)%MOD);
            sum = ((sum + init)%MOD);
        }

        cout << "Case " << kase++ << ": " << sum << endl;


    }


    return 0;
}


Sunday, November 10, 2013

Using Google Chrome with a proxy server

Works: When there's no other Chrome instance is running already, that means the one you are going to open has to be the first
This method is useful for those who use a lot of proxy and the ones who use proxies for only specific sites.
To open Chrome with a proxy server support, use the following command (Note: Wasn't tried on Windows).
google-chrome --proxy-server="<host>:<port>"

Saturday, November 02, 2013

Convert images and apply various effects on them using the command line

One thing is for sure, something so beautiful as Linux needs exploring. You can't expect anyone to trully show it's virtues that might come in handy to you. Everybody has their own cut, you have to do your.
So this other minute I was looking for some cheatsheet wallpaper to put up on my Desktop. Got a lot of them. Chose one, but the images is Black text on white backgroud, a serious problem to a maniac like me. Figured it needs some invertion, but firing up GIMP for such trivial task? :/ After a bit of drooling on ideas, Imagemagick hit my mind. One of the best tools for batch processing of images using the terminal. To install, type in:
sudo apt-get install imagemagick
There are many many options and things that can be done using this gem, cropping, resizing, inverting, even combining multiple effects. Some example commands are listed below
# Change format
convert howtogeek.png howtogeek.jpg

# Enforcing compression level
convert howtogeek.png -quality 95 howtogeek.jpg

# Resizing
convert example.png -resize 200×100 example.png
# enforce specification
convert example.png -resize 200×100! example.png
# maintain ratio
convert example.png -resize 200 example.png
# or 
convert example.png -resize x100 example.png

# Rotation
convert howtogeek.jpg -rotate 90 howtogeek-rotated.jpg

# Effects
# charcoal
convert howtogeek.jpg -charcoal 2 howtogeek-charcoal.jpg
# implode
convert howtogeek.jpg -implode 1 howtogeek-imploded.jpg
# invert
convert howtogeek.jpg -negate imploded.jpg

# Combination
convert howtogeek.png -resize 400×400 -rotate 180 -charcoal 4 -quality 95 howtogeek.jpg
You should explore the endless possibilities yourself.
NOTE: The commands have been copied from www.howtogeek.com

Friday, November 01, 2013

Make a backup of your dotfiles

If you are a regular Linux user, chances are you change distros to experiment with them. I am one of such persons who like to experiment with various Linux distros for both educational and experimental reasons. This activity may sometimes lead to problems regarding your Desktop Environment (DE) and others. It also might be the case that you just want to clean up your /home but keep your configurations intact using your dotfiles (The directories and files in your /home whose name start with a '.'). Moving these files manually is cumbersome (at least for me). The following script does it for me. And moves it to a folder called "dotfiles" in your ~/.
CAUTION: It removes any folder called 'dotfiles" in you ~/ and creates a new one.
You MUST run this in your ~/
rm -rf dotfiles
mkdir dotfiles
while read line; do
        if [ "$line" = "" ]; then
                continue
        fi
        if [ -d "$PWD/$line" ]; then
                echo "$PWD/$line is a folder"
                cp $PWD/$line $PWD/dotfiles/$line # substitute cp with mv to move the files
        else
                echo "$PWD/$line is a file"
                cp $PWD/$line $PWD/dotfiles/$line # substitute cp with mv to move the files
        fi
done < <(find . -maxdepth 1 -name ".*" -printf '%P\n')

Saturday, October 26, 2013

Linux shell cheatsheet (Can it become one?)

I love the command line, the CLI. I wish somethings were a little bit different allowing me to use CLI for every task, but the world is a cruel place, apart from stupid M$ mistakes regarding their crappy GUI. You do realize there wouldn't be such a crappy product without dependency on the GUI, right?
I'm not categorizing it right now, because there's not much to categorize actually
Show line number in "grep" output: grep "text" -n

Running your first PHP application in Google App Engine (Fix for the --php_executable_path flag () issue)

As I may (or may not) have mentioned before, I mostly blog here for the purpose of note-keeping for myself. In most of the cases I don't really care about how good the tutorials (not so much) are coming out. So, please do your homework if you are following any of my tutorials at all. This is gonna be a disclaimer somewhere, but see? I can't give a damn here. Thanks.
I was trying my hands on the Google App Engine. My system is Linux so I downloaded, followed the linux part of things from their official how-to-first-time page here [ https://developers.google.com/appengine/docs/php/gettingstarted/installing ]
As you can see from that page (yeah, that's a dependency) you
1. Download the PHP SDK
2. Extract it some nice place
3. Write a "hello world!" PHP script
4. Execute the python script (dev_appserver.py) in the the "google_appengine" (if you haven't done it otherwise) folder.
BUT ! I got stuck here for sometime, and looked up for the fix.
Source of the problem:
_PHPBinaryError: The path specified with the --php_executable_path flag () does not exist.
Which naturally implied 2 things
1. The PHP installation was NOT as the app engine wants it to be
2. God doen't want me to code anymore
I naturally believe God loves it when I'm busy with my favourite thing so number 2 was obsolete. I looked up on Google for some solutions and a sudden SO page suggested that executing the appserver executable like
./dev_appserver.py --php_executable_path="...path..to..the..php-cgi..executable..." helloworld
will solve the problem.
Following this trail I came to notice that my ignorance toward the tutorial's PHP installation paid of here. The problem was actually my PHP installtion was a CLI version. While the app engine essentially wants a CGI version. For doing so, the tutorial has very good instructions, and yes, php-cgi is NOT available in Ubuntu repos. So I did
sudo apt-get install gcc libmysqlclient-dev libxml2-dev
  wget --trust-server-names http://us2.php.net/get/php-5.4.15.tar.bz2/from/us1.php.net/mirror
  tar xjf php-5.4.15.tar.bz2
  cd php-5.4.15
  ./configure --prefix=$PWD/installdir --enable-bcmath --with-mysql
  make install
  cd -
And afterwards executed
./dev_appserver.py --php_executable_path="...path..to..the..php-cgi..executable..." helloworld
And it went smooth. :) By the way, the above method will NOT harm you normal PHP installation.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...