-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rry
committed
Mar 14, 2016
1 parent
a3e7f1f
commit 9d0cd63
Showing
222 changed files
with
432 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified
0
DataStructure/LinkedLists/1-print-the-elements-of-a-linked-list.cpp
100644 → 100755
Empty file.
Empty file modified
0
DataStructure/LinkedLists/2-insert-a-node-at-the-tail-of-a-linked-list.cpp
100644 → 100755
Empty file.
Empty file modified
0
DataStructure/LinkedLists/3-insert-a-node-at-the-head-of-a-linked-list.cpp
100644 → 100755
Empty file.
Empty file modified
0
DataStructure/LinkedLists/4-insert-a-node-at-a-specific-position-in-a-linked-list.cpp
100644 → 100755
Empty file.
Empty file modified
0
DataStructure/LinkedLists/5-delete-a-node-from-a-linked-list.cpp
100644 → 100755
Empty file.
Empty file modified
0
DataStructure/LinkedLists/6-print-the-elements-of-a-linked-list-in-reverse.cpp
100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function processData(input) { | ||
var regex=/<a.*?href="(.*?)".*?>(.*?)<\/a>/ig; | ||
var output=[]; | ||
input.replace(regex,function(_,href,text){ | ||
output.push(href.trim()+','+text.replace(/<.*?>/g,'').trim()) | ||
}); | ||
console.log(output.join('\n')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$html = ''; | ||
while ($t --) { | ||
$html .= trim(fgets($handle)); | ||
} | ||
|
||
if (preg_match_all('/<a .*?href="(.*?)".*?>(.*?)<\/a>/', $html, $matches)) { | ||
foreach ($matches[1] as $i => $link) { | ||
printf("%s,%s\n", $link, trim(strip_tags($matches[2][$i]))); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Regex/Applications/10-detecting-valid-latitude-and-longitude.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$lines = []; | ||
while ($t --) { | ||
$lines[] = trim(fgets($handle)); | ||
} | ||
|
||
foreach ($lines as $line) { | ||
if (preg_match_all('/\(\+?-?((?:[1-9]\d*)|(?:[1-9]\d*)\.\d+), \+?-?((?:[1-9]\d*)|(?:[1-9]\d*)\.\d+)\)/', $line, $matches)) { | ||
if ($matches[1][0] > 90 || $matches[2][0] > 180) { | ||
echo "Invalid\n"; | ||
} else { | ||
echo "Valid\n"; | ||
} | ||
} else { | ||
echo "Invalid\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$twitters = ''; | ||
while ($t --) { | ||
$twitters .= trim(fgets($handle)) . " "; | ||
} | ||
|
||
echo preg_match_all('/HackerRank/i', $twitters); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
|
||
$html = ''; | ||
$t = 3600; | ||
while ($t --) { | ||
$html .= trim(fgets($handle)) . "\n"; | ||
} | ||
|
||
preg_match_all('/(?:<h3><a href="\/questions\/(\d*).*>(.*)<\/a>|class="relativetime">(.*)<\/span>)/', $html, $matches); | ||
|
||
for ($i = 0; $i < count($matches[0]) / 2; $i ++) { | ||
printf("%s;%s;%s\n", $matches[1][$i * 2], $matches[2][$i * 2], $matches[3][($i + 1) * 2 - 1]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$regex = '/^[a-z]{0,3}\d{2,8}[A-Z]{3,}$/'; | ||
while ($t --) { | ||
fscanf($handle, "%s", $s); | ||
|
||
echo preg_match($regex, $s) ? "VALID\n" : "INVALID\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$regex = '/^[A-Z]{5}[0-9]{4}[A-Z]$/'; | ||
while ($t --) { | ||
fscanf($handle, "%s", $s); | ||
|
||
echo preg_match($regex, $s) ? "VALID\n" : "INVALID\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
while ($t --) { | ||
$s = trim(fgets($handle)); | ||
|
||
if ($s == 'hackerrank') { | ||
echo "0\n"; | ||
} else { | ||
$pos = strpos($s, 'hackerrank'); | ||
if ($pos === false) { | ||
echo "-1\n"; | ||
} else if ($pos === 0) { | ||
echo "1\n"; | ||
} else if ($pos + 10 == strlen($s)) { | ||
echo "2\n"; | ||
} else { | ||
echo "-1\n"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
$languages = ['C','CPP','JAVA','PYTHON','PERL','PHP','RUBY','CSHARP','HASKELL','CLOJURE','BASH','SCALA','ERLANG','CLISP','LUA','BRAINFUCK','JAVASCRIPT','GO','D','OCAML','R','PASCAL','SBCL','DART','GROOVY','OBJECTIVEC']; | ||
|
||
while ($t --) { | ||
$s = trim(fgets($handle)); | ||
|
||
if (preg_match('/(\d{1,5}) ([A-Z]+)/', $s, $matches)) { | ||
print_r($matches); | ||
if ($matches[1] >= 10000 && $matches[1] < 100000 && in_array($matches[2], $languages)) { | ||
echo "VALID\n"; | ||
} else { | ||
echo "INVALID\n"; | ||
} | ||
} else { | ||
echo "INVALID\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
|
||
$code = ''; | ||
$t = 1000; | ||
while ($t --) { | ||
$code .= trim(fgets($handle)) . "\n"; | ||
} | ||
|
||
$regex_c = '/#include/'; | ||
$regex_java = '/import java/'; | ||
|
||
if (preg_match($regex_c, $code)) { | ||
echo "C\n"; | ||
} else if (preg_match($regex_java, $code)) { | ||
echo "Java\n"; | ||
} else { | ||
echo "Python\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
$regex = '/^(\d{1,3})(\?|(?: )|(?:-))(\d{1,3})\2(\d{4,10})$/'; | ||
|
||
while ($t --) { | ||
$string = trim(fgets($handle)); | ||
|
||
if (preg_match($regex, $string, $matches)) { | ||
printf("CountryCode=%s,LocalAreaCode=%s,Number=%s\n", $matches[1], $matches[3], $matches[4]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
$regex = '//'; | ||
|
||
while ($t --) { | ||
$string = trim(fgets($handle)); | ||
|
||
if (preg_match($regex, $string, $matches)) { | ||
printf("CountryCode=%s,LocalAreaCode=%s,Number=%s\n", $matches[1], $matches[3], $matches[4]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$html = ''; | ||
while ($t --) { | ||
$html .= trim(fgets($handle)); | ||
} | ||
|
||
$tags = []; | ||
$regex = '/(<\s*([\w]+) ?.*?>|\[.*?\])/'; | ||
|
||
if (preg_match_all($regex, $html, $matches)) { | ||
foreach ($matches[2] as $g) { | ||
$tags[] = $g ?: 'a'; | ||
} | ||
} | ||
|
||
$tags = array_unique($tags); | ||
sort($tags); | ||
|
||
echo implode(";", $tags) . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
$regex_1 = '/<([a-zA-Z0-9]+) ?(?:\w*="?\'?[^"\']*\'?"? ?){0,}/'; | ||
$regex_2 = '/ (\w*)=/'; | ||
|
||
$code = []; | ||
|
||
while ($t --) { | ||
$code[] = trim(fgets($handle)); | ||
} | ||
|
||
$tags = []; | ||
|
||
foreach ($code as $c) { | ||
if (preg_match_all($regex_1, $c, $matches_1)) { | ||
for ($i = 0; $i < count($matches_1[0]); $i ++) { | ||
if (strpos($matches_1[0][$i], '=') !== false) { | ||
preg_match_all($regex_2, $matches_1[0][$i], $matches_2); | ||
|
||
foreach ($matches_2[1] as $attr) { | ||
if (! isset($tags[$matches_1[1][$i]])) { | ||
$tags[$matches_1[1][$i]][] = $attr; | ||
} else { | ||
if (! in_array($attr, $tags[$matches_1[1][$i]])) { | ||
$tags[$matches_1[1][$i]][] = $attr; | ||
} | ||
} | ||
} | ||
} else { | ||
if (! isset($tags[$matches_1[1][$i]])) { | ||
$tags[$matches_1[1][$i]] = []; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
ksort($tags); | ||
|
||
foreach ($tags as $tag => $attributes) { | ||
sort($attributes); | ||
|
||
printf("%s:%s\n", $tag, implode(',', $attributes)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$words = ''; | ||
while ($t --) { | ||
$words .= trim(fgets($handle)) . " "; | ||
} | ||
|
||
fscanf($handle, "%d", $n); | ||
|
||
while ($n --) { | ||
fscanf($handle, "%s", $s); | ||
$regex = '/' . substr($s, 0, strlen($s) - 2) . '[sz]' . substr($s, strlen($s) - 1) . '/'; | ||
|
||
printf("%d\n", preg_match_all($regex, $words)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$words = ''; | ||
while ($t --) { | ||
$words .= trim(fgets($handle)) . " "; | ||
} | ||
|
||
fscanf($handle, "%d", $n); | ||
|
||
while ($n --) { | ||
fscanf($handle, "%s", $s); | ||
$s = str_replace("our", "ou?r", $s); | ||
$regex = '/(?=\b' . $s . '\b)/i'; | ||
|
||
if (preg_match_all($regex, $words, $matches)) { | ||
echo count($matches[0]) . "\n"; | ||
} else { | ||
echo "0\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
while ($t --) { | ||
$lines[] = trim(fgets($handle)); | ||
} | ||
|
||
fscanf($handle, "%d", $n); | ||
|
||
while ($n --) { | ||
$count = 0; | ||
fscanf($handle, "%s", $pattern); | ||
$regex = "/[0-9a-zA-Z_]+" . $pattern . "[0-9a-zA-Z_]+/"; | ||
foreach ($lines as $line) { | ||
foreach (explode(" ", $line) as $word) { | ||
if (preg_match($regex, $word)) { | ||
$count ++; | ||
} | ||
} | ||
} | ||
|
||
echo $count . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
while ($t --) { | ||
fscanf($handle, "%s", $username); | ||
|
||
$regex = '/^[\._]\d+[a-zA-Z]*_?$/'; | ||
|
||
echo preg_match($regex, $username) ? "VALID\n" : "INVALID\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$ip4_regex = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'; | ||
$ip6_regex = '/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/'; | ||
|
||
while ($t --) { | ||
fscanf($handle, "%s", $str); | ||
|
||
if (preg_match($ip4_regex, $str)) { | ||
printf("IPv4\n"); | ||
} else if (preg_match($ip6_regex, $str)) { | ||
printf("IPv6\n"); | ||
} else { | ||
printf("Neither\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
$handle = fopen("php://stdin", "r"); | ||
fscanf($handle, "%d", $t); | ||
|
||
$lines = ''; | ||
while ($t --) { | ||
$lines .= ' ' . trim(fgets($handle)); | ||
} | ||
|
||
fscanf($handle, "%d", $n); | ||
|
||
while ($n --) { | ||
fscanf($handle, "%s", $word); | ||
$count = 0; | ||
|
||
$regex = '/(\b' . $word. '\b)/'; | ||
|
||
printf("%d\n", preg_match_all($regex, $lines)); | ||
} |
Oops, something went wrong.