Skip to content

Instantly share code, notes, and snippets.

@ayushjain7
Last active February 28, 2016 04:50
Show Gist options
  • Save ayushjain7/0467287fecab236738ab to your computer and use it in GitHub Desktop.
Save ayushjain7/0467287fecab236738ab to your computer and use it in GitHub Desktop.
1. maximum Difference
2. k-subsequence
3. single-line and multi-line comments
4. unique domains.
3.
import java.io.*;
public class Solution {
public static void main(String args[] ) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line ;
int count = 0;
boolean multiLine = false;
while((line = in.readLine())!=null){
int index = line.indexOf("//");
if(index >= 0)
System.out.println(line.substring(index));
else{
if(multiLine){
int index1 = line.indexOf("*/");
if(index1 >= 0){
System.out.println(line.substring(0, index1+2).trim());
multiLine = false;
}else{
System.out.println(line.trim());
}
}else{
int beginIndex = line.indexOf("/*");
int endIndex = line.indexOf("*/");
if(beginIndex >= 0){
if(endIndex > beginIndex){
System.out.println(line.substring(beginIndex, endIndex+2));
}
else {
System.out.println(line.substring(beginIndex));
multiLine =true;
}
}
}
}
}
}
}
1.
/*
* Complete the function below.
*/
static int maxDifference(int[] a) {
if(a == null || a.length <= 1)
return 0;
int maxDiff = Integer.MIN_VALUE;
int min = a[0];
for(int i =1; i <a.length; i++){
/*for(int j = i+1; j<a.length; j++){
if(max < a[j]-a[i]){
max = a[j]-a[i];
}
}*/
if(a[i]-min > maxDiff)
maxDiff = a[i]-min;
min = Math.min(min, a[i]);
}
return maxDiff;
}
@prakhar1989
Copy link

Regex Matching on URLs
from urlparse import urlparse
import re

PATTERN = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
text = '<p>Hello World</p><a href="http://gist.github.com">More Examples</a><a href="http://github.com">Even More Examples</a>'
urls = [urlparse(u).hostname for u in re.findall(PATTERN, text)]
hosts = set([".".join(u.split('.')[-2:]) for u in urls])
print hosts # [github.com]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment