Friday, March 14, 2014

Reversed Binary Numbers (Difficulty Level: Easy)

My friend sent me a quiz how to make a reverse binary from input number and put it back as number.
It is easy level though, I had fun of it
If you are interested to get more quiz, please check this https://code.google.com/codejam/
There are many programing quiz in Internet. I see there are so many genius in the world.
I wonder when I can resolve the difficult level of quiz. I must study math again.


Task

Your task will be to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and reversing it gives 1011, which corresponds to number 11.

Input

The input contains a single line with an integer N, 1 ≤ N ≤ 1000000000.

Output

Output one line with one integer, the number we get by reversing the binary representation of N.

Sample input 1
13
Sample output 1
11
Sample input 2
47
Sample output 2
61




package puzzle;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author jack
 */
public class Reversebinary {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            String input;

            while ((input = br.readLine()) != null) {
                int inputValue = Integer.parseInt(input);

//                System.out.println(Integer.toBinaryString(inputValue));

                StringBuffer revertedString = new StringBuffer();
                revertedString.append(Integer.toBinaryString(inputValue)).reverse();

//                System.out.println(revertedString);
                System.out.println(Long.parseLong(revertedString.toString(), 2));

            }

        } catch (java.lang.NumberFormatException ne) {
            System.out.println("Please type numbers");
        } catch (IOException io) {
            io.printStackTrace();

        }
    }
}

How to get the apk out of the non-rooted Android device

I got a request to test updated Android application. but, it often happens that I need to test without apk file.
Due to complicated commpany rules, politics and security, the requester can't send me the apk file.. But, they push me to do..
To resolve this situation, luckily I had one Android device which is installed the latest apk. but, I need to test it on another device.
I can extract the apk from an Anroid device and install it on the other devices.

To do this step, Android SDK is required. so that you will be able use 'adb' command.

Step to extract apk file from an Android device.
1. Connect the device to the computer
2. start command prompt or shell
3. adb shell pm list package -f -3 (to display all the installed apps on the device)
C:\>adb shell pm list package -f -3
package:/data/app/autotechniksteeg.preistafel-1.apk=autotechniksteeg.preistafel
...
package:/data/app/uk.amazon.mShop.android-2.apk=uk.amazon.mShop.android
...

4. adb pull {apk name} (This will save the required apk on the current directory of the computer)
C:\>adb pull /data/app/uk.amazon.mShop.android-2.apk


If you remove '-3' option, it will show all apk which is pre-loaded app on your device. I used 'adb shell pm list package -f -3' command.
If you like to know more about 'pm list package' filter option, please refer below.
pm list packages: prints all packages, optionally only
  those whose package name contains the text in FILTER.  Options:
    -f: see their associated file.
    -d: filter to only show disbled packages.
    -e: filter to only show enabled packages.
    -s: filter to only show system packages.
    -3: filter to only show third party packages.
    -i: see the installer for the packages.
    -u: also include uninstalled packages.

Thursday, October 24, 2013

receive argument on expect shell script

I just wanted to scp easily. I just simply made a expect shell script which receive one argument.
I just excute one script like below

cp.sh file

and copy finished
#!/usr/bin/expect -f

set filename [lindex $argv 0];

spawn scp -v /temp/$filename root@192.168.1.1:/tmp/root/
expect "Enter passphrase"
send "\r"
...

Thursday, August 8, 2013

Manipulate Access Control List (ACL) on mysql

I had to limit of access to mysql server due to security improvement task.
We like to allow only few host to the mysql. It was very easy

mysql>use mysql
mysql> select host, user from user;
----------------------------+
host  user
----------------------------+
%  dmitry
host : % means all (It is security breach)
host side should be ip address of web server
or
ip address of admin pc.
example)
-- Replace unlimited access rule to only allow 192.168.0.5 host rule
mysql>update user set host='192.168.0.5' where host = '%' and user in ('dmitry');
mysql>commit;
mysql>flush privileges;
Query OK, 0 rows affected (0.01 sec)


-- Remove unlimited access rule

mysql> delete from user where host = '%';
mysql> commit;
mysql> flush privileges;


you can insert more hosts if you like to.

Monday, July 29, 2013

Android Json file read instead of property file read


While I am building an Android application. I wanted to read a property file and wanted to act my app depends on the property file configuration. But, property file can't handle array unless I add dependency Apache Common Configuration. As I am building an mobile application, it should be compact. for the reason, I just used Android JSONObject to resolve this.

Create "country.json" file and locate it Android device "/" directory

{
    "country": [
        {
            "name": "germany",
            "code": "DE",
            "continent": "Europe",
            "eat": "sausage"
        },    
        {
            "name": "france",
            "code": "FR",
            "continent": "Europe",
            "eat": "croissant"
        },
        {
            "name": "korea",
            "code": "KR",
            "continent": "ASIA",
            "eat": "rice"
        },
        {
            "name": "japan",
            "code": "JP",
            "continent": "ASIA",
            "eat": "fish"
        }
    ]
}


Create 2 method and use "getCountryList()" on your need.
private List<Country> getCountryList() throws Exception {

 File dirSDCard = Environment.getExternalStorageDirectory();
 File yourFile = new File(dirSDCard, "country.json");
 InputStream jsonStream = new FileInputStream(yourFile);
 JSONObject jsonObject = new JSONObject(InputStreamToString(jsonStream));
 JSONArray jsonArray = jsonObject.getJSONArray("country");
 List<Country> countryList = new ArrayList<Country>();

 for (int i = 0; i < jsonArray.length(); i++) {

  JSONObject jsonCountry = jsonArray.getJSONObject(i);
  Country country = new Country();
  country.setName(jsoncountry.getString("name"));
  country.setCode(jsoncountry.getString("code"));
  country.setContinent(jsoncountry.getString("continent"));
  country.setEat(jsoncountry.getString("eat"));

  countryList.add(country);
 }

 return countryList;
}

private String InputStreamToString(InputStream is) {

 BufferedReader r = new BufferedReader(new InputStreamReader(is));
 StringBuilder total = new StringBuilder();
 String line;
 try {
  while ((line = r.readLine()) != null) {
   total.append(line);
  }
 } catch (IOException e) {
  e.printStackTrace();
 }
 return total.toString();

}

Monday, June 17, 2013

mybatis various datasource sample jndi, hsql, oracle, mysql, sqlite configuration setup with Springframework

While doing various projects, I had to use different DBs. I just summarized datasource for Mybatis samples which is after testing. You can simply comment out to for your prefer datasource.
If you like to know detail to implement this sample with Spring framework, please follow mybatis-integration-with-spring.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

<!-- JNDI datasource -->
<!-- 
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
 <property name="jndiName" value="jdbc/oraclePool" />
 <property name="resourceRef" value="true" />
</bean>
-->

<!-- Hsql datasource -->
<!-- 
<jdbc:embedded-database id="dataSource">
 <jdbc:script location="classpath:hsql/schema.sql" />
 <jdbc:script location="classpath:hsql/data.sql" />
</jdbc:embedded-database>
-->

<!-- Oracle JDBC datasource -->
<!-- 
<bean id="dataSource"
 class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 <property name="driverClass" value="oracle.jdbc.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@HOST:1111:ORA" />
 <property name="username" value="ID" />
 <property name="password" value="PASSWORD" />
</bean>
-->

<!-- mysql JDBC datasource -->
<!-- 
<bean id="dataSource"
 class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 <property name="driverClass" value="com.mysql.jdbc.Driver" />
 <property name="url" value="jdbc:mysql://localhost:3306/mydata" />
 <property name="username" value="user123" />
 <property name="password" value="12345678" />
</bean>
 -->

<!-- sqlite JDBC datasource -->
<!-- 
<bean id="dataSource"
 class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 <property name="driverClass" value="org.sqlite.JDBC" />
 <property name="url" value="jdbc:sqlite:C:/yourdb.sqlite" />
</bean>
 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <property name="typeAliasesPackage" value="com.devtrigger.model" />
 <property name="mapperLocations" value="classpath*:dao/**/*.xml" />
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
 <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

<!-- scan for mapper interface files and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.devtrigger.dao" />
</bean>

<bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
</bean>

</beans>

Thursday, March 14, 2013

Useful SVN adminitrator command

Export svn
- When I don't know SVN credential. but I have access to repository server
$svn export --force file:///home/svn/myrepos ./myrepos


Check User access control
$cat /home/svn/myrepos/conf/authz
$cat /home/svn/myrepos/conf/passwd
$cat /home/svn/myrepos/conf/svnserve.conf


Create version 1.6 compatible Repository
$svnadmin create myrepos --pre-1.6-compatible


Dump svn repository
$svnadmin dump /home/svn/myrepos > /home/backup/svn/myrepos.dump


Incremental dump svn repository
- SVN incremental Backup
$ svnadmin dump myrepos --revision 0:1000 > dumpfile1
$ svnadmin dump myrepos --revision 1001:2000 --incremental > dumpfile2
$ svnadmin dump myrepos --revision 2001:3000 --incremental > dumpfile3
$ svnadmin dump <repos> -r 58:HEAD --deltify > <file2> 


Import dump file
$ cd /home/svn
$ svnadmin load --bypass-prop-validation myrepos < /home/backup/svn/myrepos.dump


Incremental import dump file
- SVN recovery 
* incremental :
$ svnadmin load < ~/repos-0-1000.svn_dump
$ svnadmin load < ~/repos-1000-2000.svn_dump
$ svnadmin load < ~/repos-2000-3000.svn_dump


Kill SVN daemon on Solaris
$ pkill -KILL svnserve


Start SVN daemon
$ svnserve -d -r /home/svn/


Pack svn
- SVN revision pack per 1000 revision
$ svnadmin pack /home/svn/sfc