Monday, August 10, 2015

#codingexercise

Question: If a number only has factors 2, 3, and 5, it is an ugly number . For example, 6 and 10 are two ugly numbers, but 14 is not because it has a factor of 7. Usually 1 is considered to be the first ugly number. What is the arbitrary kth  ugly number? Can we do this efficiently?
Solution:
The straightforward solution repeatedly divides by 2, 3 and 5 each until the remainder is 1 in which case the number is ugly or it is more than one. However, this solution is not efficient since it does the same check for non ugly numbers. Instead we could store previously encountered ugly numbers and use their factors.
int GetUglyNumber(int index)
{
if (index <= 0)
return 0;
var uglyNums = new int[index];
uglyNums[0] = 1;
int nextUglyIndex = 1;
int index2 = 0;
int index3 = 0;
int index5 = 0;
while (nextUglyIndex < index)
{
  int min = Math.Min(uglyNums[index2]*2, uglyNums[index3]*3);
  min = Math.Min(min, uglyNums[index5]*5);
  uglyNums[nextUglyIndex] = min;
  while (uglyNums[index2] * 2 <= uglyNums[nextUglyIndex])
      ++index2;
  while (uglyNums[index3] * 3 <= uglyNums[nextUglyIndex])
      ++index3;
  while (uglyNums[index5] * 5 <= uglyNums[nextUglyIndex])
      ++index5;
++nextUglyIndex;

}
return uglyNums[nextUglyIndex-1];
}

We briefly discuss SUMMONS which is the first example of a multi document summarization system and illustrates one of the stages of text summarization  - topic identification and fusion. It tackles news events about a single topic and produces a briefing that merges relevant information about each event and how reports by different news agencies have evolved over time. Rather than working with raw text SUMMONS reads a database previously built by a template based message understanding system. Therefore there are two stages : one that takes fulltext as input and fills template slots and then synthesizes a summary and the other that takes summary opertors and a set of rules and with connective phrases smoothens out a summary.
#codingexercise
Public class compare:IComparer
{
Int compare (int i, int j){
Return i%10 <j%10;
}
}

No comments:

Post a Comment