Return Styles: Pseud0ch, Terminal, Valhalla, Blue Moon.

Pages: 1-

/prog/ Challenge 5: Optimize You're Quotes

Name: Anonymous 2018-01-08 14:27

INPUT: A multi-line string specifying quoted poasts on a 2ch-like board, one poast per line. Lines are prefixed with ``>>''.

OUTPUT: A single, optimized quote link, created by replacing consecutive poast numbers with a range.

EXAMPLE INPUT ⅰ
>>1
>>2
>>3

EXAMPLE OUTPUT ⅰ
>>1-3

EXAMPLE INPUT ⅱ
>>1
>>3
>>2
>>5

EXAMPLE OUTPUT ⅱ
>>1-3,5

EXAMPLE INPUT ⅲ
>>6
>>2
>>3
>>1
>>5

EXAMPLE OUTPUT ⅲ
>>1-3,5-6

🌑 In case of only two consecutive poasts, still replace them with a range.
🌑 Numbers are positive integers from 1 too 1001 (inclusive on both sides).
🌑 Use standard input and standard output as interfaces.
🌑 Shortest code wins. Good luck!

Name: Anonymous 2018-01-08 14:52

will work on it in a while

Name: Anonymous 2018-01-09 9:19

straightforward py3 version, could be reduced further but all my ideas for exec abuse actually make it longer:

import sys,itertools;a=sorted([int(x[2:])for x in sys.stdin.readlines()]);b="";c=0
for x in itertools.count():
if x in a:
if c:continue
c=1;b+=','+str(x)
else:
if not c:continue
c=0;b+='-'+str(x-1)
if x>=max(a):break
print('>>'+b[1:])

Name: Anonymous 2018-01-09 9:53

>>3
Fails for
>>3
>>6

Name: Anonymous 2018-01-09 12:52

it was javashitter quality!
//used optimal instead of optimi(s/z)e in a function name; on-track to meet my "considering diversity" KPI for this quarter
function optimalQuotes(string){
function foo(){
return string.split(">>").filter(function(char){
return char !="\n"&& char!=0;
}).map(function(char){
return Number(char);
}).sort(function(a,b){return a-b})};

function bar(fn){
var result = [fn[0]];
for(n=1;n<fn.length;n++){
if (fn[n]==fn[n-1]+1){
if(fn[n] !=fn[n+1]-1){
result.push(result.pop()+"-"+fn[n])};
}else result.push(fn[n]);};
return result;}
return ">>" + bar(foo());
}

//IHBT,there is no stdin in javascript. in lieu of that here are ur tests plus extra:
console.log(optimalQuotes(">>1\n>>2\n>>3\n"));
console.log(optimalQuotes(">>1\n>>2\n>>3\n>>5\n"));
console.log(optimalQuotes(">>6\n>>2\n>>3\n>>1\n>>5\n"));
console.log(optimalQuotes(">>10\n>>13\n"));

Name: Anonymous 2018-01-14 23:48

Finally got this piece of shit working:
#include <ctype.h>
#include <stdio.h>

i, c;
char t[1025];
main() {
for(c = 'x'; !feof(stdin); ) {
for(; !isdigit(c) && !feof(stdin); c = getchar());
for(i = 0; isdigit(c); c = getchar())
i = i * 10 + c - '0';
t[i%1024] = 1;
}
putchar('>');
for(i = c = 1; i < 1024; ++i) {
if(t[i]) {
printf("%c%d", ",>"[c], i);
for(; t[i+1]; ++i, c |= 2);
if(c & 2)
printf("-%d", i);
c = 0;
}
}
putchar('\n');
return 0;
}

Don't change these.
Name: Email:
Entire Thread Thread List