Wednesday, 5 April 2017

Clock Angle Problem

While reading the bible for programmers 'Cracking the Coding Interview', I came across an interesting problem of determining the angle between the hour and the minutes hands of an analog clock, given a time.

This drew my attention and I began looking into it. Here is a small explanation about the approach to solve this problem.

What we would need to do is to calculate the rate of change of angle in degrees per minute. The first step to take is to recall that hour hand can turn 360 degree in 12 hours ( that is 12 * 60 = 720 mins)

Rate of change of angle for the hour hand is = 360/720 = 0.5 degrees per minute

Similarly, the minute hand of the clock turns 360 degrees in 60 minutes
So rate of change of angle for the minute hand is = 360/60 = 6 degrees per minute

For a given time X hrs and Y mins

Angle for hour X = 0.5 * ( X *60 + Y)
Angle for minutes Y = 6 * Y

We want to calculate the angle between the hour and the minute hand. So we should be interested in calculating the difference between Angle for hour X and Angle for minutes Y

Difference of Angle between X hrs and Y mins is Z = 0.5 * ( X *60 + Y)  -  6 * Y

The angle between the hour and the minute hand will be the smaller of (360- Z,  Z)


Sunday, 2 April 2017

Getting things done - David Allen

Getting things done or GTD is time management method which are proven successful.
Some quotes from GTD are as below.

#Mind is not for storage. It is to create/have great ideas.

#To have Mind like Water. Be appropriately engaged.

#Carve out mental space.


Important steps in GTD System:

1. Collect
2. Process
3. Organize
4. Review
5. To do 

Integration Broker in peoplesoft

This is an attempt to write in simple words about Integration broker in PeopleSoft.

Integration Broker in short IB, is a tool provided by PeopleSoft to interact between different systems both internal (PeopleSoft systems) and external third party systems.

The interactions can be asynchronous (fire and forget) or synchronous (suspend activity and wait for resource). It is used to invoke PeopleSoft business logic as a web service to outside world or vice versa.

IB has two components : IB Gateway and IB Engine

IB Gateway resides in web server and takes care of  receipts and delivery of messages among systems, whereas Integration Engine resides in App server and takes care of sending/receiving messages to Integration Gateway. It doesn't communicate directly with outside world.

Setting up IB :

1. Activate publish/subscribe processes.
This can be done by starting PSADMIN found in [PS_HOME]\appserv\psadmin

It is a Command line utility, which gives you the option to start pub sub servers.

Pub sub servers are needed for asynchronous messages and not synchronous messages.

2. Configure Gaeway to establish link between webserver and appserver
Here you can configure and choose the gateway which will be used for outbound traffic.

This is done by putting correct server and port in the url in gateway page of IB setup. Also connectors can be loaded here.
Navigation: Peopletools >> IB >> Configure >> Gateway

3.  Configure Node
Here you can configure node which should connect to Integration Gateway.
Provide authentication userid and password. Default user is PS
Setup Gateway properties

In the property file, log level can be set to 5. Verify that password is encrypted or substitute with encrypted password.

4. Setup web service configuration

Here you can setup namespace and target location. Information entered here will be used during setting up XSD's and WSDL.

5. Activate IB Domain
Activating pub/sub processes in IB

Peopletools >> IB >> Configuration >> Quick Configuration

Inside Domain status, perform below actions
click purge domain status
select all domains active
click update

6. Configure Internal nodes

Internal nodes like Anonymous and WSDL_NODE can be configured to put PS as default user id
This completes IB setup.






Word Anagrams Project - Python

Objective : 
To find Anagrams words in English dictionary. Two words are said to be anagrams if the letters can be rearranged to turn one word into another.

Example stop and pots are anagrams to each other.


Steps:
word = open ('words' , 'r')  -- Opens the word file

wordlist =  word.readlines() -- Reads the lines and put them in list

len(wordlist) -- Count the elements in the list

-- Get rid of newline character and lowercase the words

wordclean = [wordlist.strip().lower() for word in wordlist]

-- There still may be duplicates. Use set to remove duplicates
-- Set can have only one instance of any given object
-- and then convert back to a list

wordunique = list(set(wordclean)) -- Duplicates will be removed now, but sorting would be lost

wordunique.sort() -- sort the list

-- The above data transformation could have been done using list comprehension in easier way

wordclean = sorted(list(set([word.strip().lower() for word in open('words' , 'r')])))

-- Finding Anagrams

-- for anagrams , use the fact that sorted(pots) ==  sorted(stop)

def  signature(word):
    return ' '.join(sorted(word))

def anagram(myword):
    return [word for word in wordclean if signature(word) == signature(myword)]

-- Test it

anagram('dictionary')

-- This solution however is very costly, check with %timeit to see how long it takes

-- Need to find faster way to get anagrams for all the words in the file.
-- Creating signature is expensive if we keep repeating for every word in the file

-- A good idea would be to create a python dictionary of all the words indexed by their signature
-- Then getting an anagram of a given word would be as simple as looking inside the dictionary

-- Every item in dictionary would have signature as the key and a list of words as values
-- Loop through the list of words and append to the right item with signature as the key.

words_bysig = { }

for word in wordclean:
    words_bysig[signature(word)].append(word)

-- You may get an error when it tries to add an item 'a' that there is no such item in the dictionary

-- We make use of collections module where there is a default dict object
-- It provides a default value if we try to get a key which doesn't exist.
-- The default value what we want is empty list

import collections

words_bysig = collections.defaultdict(list) -- yields an empty list as the default value


for word in wordclean:
    words_bysig[signature(word)].append(word)

-- Now find anagrams by simple dictionary lookup

def anagram_fast(myword):
    return words_bysig[signature(myword)]

-- Test it


anagram_fast('dictionary')

-- get all the anagrams in the dictionary excluding the trivial ones where the word is an anagram of itself

-- build this using dictionary comprehension

anagram_all = {word : anagram_fast(word) for word in wordlist if len(anagram_fast(word) > 1}

-- Test it with %timeit, it should be relatively very fast