Saturday, December 22, 2012

Lowercase all but the first letter

Problem
User's input all uppercase names. It needs to be normal casing or it messes up
the formatting of the page.
 
Solution
Run this MySQL script  
  
 
UPDATE `table` 
SET `field` = REPLACE(CONCAT(UPPER(LEFT(`field`, 1)), 
LOWER(SUBSTRING(`field`, 2))),' , ', ', ')
 
References
1. http://stackoverflow.com/questions/7238965/mysql-update-script-problem-all-txt-is-lower-case-just-first-letter-is-upper-ca  

Saturday, December 15, 2012

SRM 563 div 2 250-pt

Problem
http://community.topcoder.com/stat?c=problem_statement&pm=12334

Approach
Check if T contains S. If so, remove it, and see if they're equal. I realized i couldn't just do a straight replace, because it would replace all occurrences. So i went to look at the documentation and discovered the replaceFirst method. This is exactly what I needed.

Code 

public class FoxAndHandleEasy
{
public String isPossible(String S, String T)
{
if (T.replaceFirst(S, "").equals(S))
return "Yes";
else
return "No";
}
}

Friday, December 14, 2012

SRM 564 div 2 250-pt

Problem
http://community.topcoder.com/stat?c=problem_statement&pm=12325 

Approach
Create two new strings:
1. reverse = append new chars to the front
2. faux = only add chars if it's different from the previous one

Looking at the documentation for appending chars to Strings in Java i relized i should be using the StringBuffer class, and then I found the reverse() function, which meant I didn't need to build the reverse string, just use the reverse() method.

So my approach was correct, I just didn't know about reverse() and didn't think to use StringBuffer right away, so I need to get more familiar with Java.


Code
public class FauxPalindromes
{
public String classifyIt(String word)
{
StringBuffer reverse = new StringBuffer(word);
reverse.reverse();
if (reverse.toString().equals(word))
return "PALINDROME";

char lastChar = word.charAt(0);
StringBuffer faux = new StringBuffer();
faux.append(lastChar);

for (int i = 1; i < word.length(); i++)
{
if (word.charAt(i) != lastChar)
{
lastChar = word.charAt(i);
faux.append(lastChar);
}
}
reverse = new StringBuffer(faux.toString());
reverse.reverse();

if (reverse.toString().equals(faux.toString()))
return "FAUX";

return "NOT EVEN FAUX";
}
}

The owner of this queue does not have sufficient privileges to work with queue

In CRM
1. Settings
2. Customizations
3. Customize the system
4. Entities
5. Email
6. Uncheck "Automatically move records to the owner's default queue when a record is created or assigned" checkbox
7. Save and Publish

http://social.microsoft.com/Forums/en/crm/thread/b35ad118-ac41-4aab-8a08-969737bbff2b

Tuesday, December 11, 2012

TopCoder SRM 562 Div 2 250 pt

I haven't done TopCoder in quite some time. In fact, I haven't done much real coding in awhile. Most coding I've been doing is high level easy stuff. I want to get back to thinking algorithmically, so I'm going to start making myself do TopCoder practice problems every day. So here's the question link and my approach and code. 

http://community.topcoder.com/stat?c=problem_statement&pm=12319&rd=15184

My approach and solution

import java.util.Arrays;

public class CucumberMarket
{
public String check(int[] price, int budget, int k)
{
/*
If any combination of k elements in price > budget then return NO, else YES

So need to build all possible combos of size k, check if the value is greater than budget.

This calls for a recursive solution.

Wait, we just need the k largest elements in price[] and then see if the sum > budget. So I can just sort the array

*/
Arrays.sort(price); //sorts in ascending order
int sum = 0;
for(int i = price.length - 1; i >= price.length - k; i--)
{
sum += price[i];
}
if(sum > budget)
return "NO";
else
return "YES";

}
}
Code references
1. Java array sort - http://viralpatel.net/blogs/java-tip-how-to-sort-array-in-java-java-util-arrays/

Friday, December 7, 2012

Detecting and removing web addresses from user entered text

Problem
I'm working on a website where users enter product descriptions. Some people put their own website in the description. This is a problem because it may potentially lead to customers buying directly from their website, thus the website loses revenue.

Solution

//ex: http://www.google.com
     $txt = preg_replace('#https?://www\.[a-z\.0-9]+/?[\w|/.]*#i', '', $txt);
//ex: www.google.com
   $txt = preg_replace('#www?\.[a-z\.0-9]+/?[\w|/.]*#i', '', $txt);
//ex: http://google.com
    $txt = preg_replace('#https?://[a-z\.0-9]+/?[\w|/.]*#i', '', $txt);

the part at the end, /?\S*, gets everything after an initial slash and stops after whitespace. For example: www.google.com/hello/hi/bye

References
1. http://regexpal.com/
2. http://stackoverflow.com/questions/13756938/remove-all-urls-from-text-with-php 
There was an error in this gadget