[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

What language can most elegantly do this?: > Make a comma-separated

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 35
Thread images: 2

File: 1502791803001.jpg (29KB, 542x650px) Image search: [Google]
1502791803001.jpg
29KB, 542x650px
What language can most elegantly do this?:

> Make a comma-separated list of numbers, with the beginning number being how many characters long the entire string is, up until 2046.

So example output:
>1,3,5,7,9,11,14,17
etc. (Notice jump from 11 to 14)
>>
what
>>
You could do this in 2 seconds in excel
>>
>>61919369
Fucking what?
>>
>>61919369
Your algorithm is fucked, either it should start at 0 (how many characters long the string currently is) or 11 should be 12 (how many characters long the string will be with the next number added).
>>
>>61919632
The request is weird but it's well defined, you're just a shitty programmer. I'm thinking about it OP, let you know as soon as I got it
>>
>>61919369
try perl for anything to do with text processing
>>
>>61919747
Ok so I think I have it:
out = ['1']
while int(out[-1]) <= 2046:
base = int(out[-1]) + 1
new = len(out[-1])
while base + new < len(','.join(out + [str(base + new)])):
new += 1
out.append(str(base + new))
print ','.join(out), 'Current lenght:', len(','.join(out))
>>
>>61919747
>you're just a shitty programmer
You're an idiot. >>61920109 outputs
1,3,5,7,9,12,15,18...

which is EXACTLY what I was saying, that if the number is the length of the string including the current number, that OP's example didn't make sense.
>>
Sage for brainlet.
>>
>>61919369
>American education churns out people who are unable to express themselves
Sad!
>>
I've read the request 10 times and I still do not get it. What the fuck are you asking? Step by step:
1. You want a comma-separated list of numbers
2. What string are you referring to?
3. What is the beginning number?
>>
>>61920109
Using len() is cheating.
>>
>>61919369
numbers = 1 : f 1 [2 .. 2046]
where f _ [] = []
f l (x:xs) | l' == x = x : f l' xs
| otherwise = f l xs
where l' = l + (length $ show x) + 1

main = print $ numbers

This prints the numbers surrounded with brackets. Removing those is left as an exercise for the reader.
>>
>>61923176
Now this gets me hard.
>>
>>61921591
It took a while to parse OP's retardese, but the challenge is supposed to be to output a string of comma separated numbers, where each number gives the length of the part of the string from the beginning to after the current number.

So the length of "1" should be 1, the length of "1,3" should be 3, the length of "1,3,5" should be 5, etc. The string should end at a length of at least 2046
>>
As you probably expected, Python though someone could probably do in a Bash/awk one liner.

>>61919394
>>61919460
brainlets
>>
>>61924084
I did it myself as said in Bash and works for your provided output.
j=0; k="1,"; while (( j<=17 )); do j=$((${#k}+1)) && k="$k$j,"; done; echo $k

Now go amaze your professor.
>>
>>61919369
the output is static, the most elegant implementation of the algorithm in any language is a string literal.
>>
fucking lol. just increment by a delta, and increment delta after each power of 10. delta is 2, then 3, 4, 5...It's in linear time. The len() solution is O(N^2), which is retarded.
>>
>>61924315
>fucking lol
Sign of an idiot. Provide an implementation or shut up. The len() solution is O(n+1)->O(n). Go read your textbook kid.
>>
Something like this?
val it = Iterator.iterate((0,1)){case (a,b) => (b,b+1+b.toString.length)}.map(_._2)
println(it.take(2046).mkString(","))


lazy val as: Stream[Int] = 0 #:: 1 #:: as.zip(as.drop(1)).map{case (a,b) => b.toString.length + b + 1}
println(as.drop(1).take(2046).mkString(","))


Arguably Scala isn't absolutely ideal for code golf... yet.
>>
>>61919369
nigger just make a function lmao
no need for dem loops or shit
power of mathemadicks
>>
>>61925300
You could definitely do that.

So what's the function you came up with to calculate position n?
>>
>>61925355
too tired
you do it
>>
>>61924315
actually that's pretty good
#include <math.h>
#include <stdio.h>

int main() {
int acc = 1;
while (acc <= 2046) {
printf("%d%c", acc, acc == 2046 ? '\0' : ',');
acc += 1 + (log10(acc) + 1);
}
return 0;
}
>>
>>61923176
What language even is this.
>>
File: durr.png (1MB, 680x1203px) Image search: [Google]
durr.png
1MB, 680x1203px
>>61919369
Brainfuck is the most elegant programing language in the world prove me wrong
>>
>>61925368
Sure as fuck can't be arsed. It was your idea and I already provided one of the solutions that deal with the length of the prior number explicitly.

There's obviously a pattern at which position the prior string gets 1 longer, though.
>>
>>61925440
Haskell.
>>
>>61925483
yea a lovely O(1) solution with math blows anything else out of the water
>>
Could you do it in erlang please?
>>
>>61925521
Sure, and I bet the professor who asked this wanted to see that solution.
>>
>>61925611
im not doing his homework for him
>>
Use C and then have a loop with a counter and a print line that adds “X, “ over and over. Tard
Thread posts: 35
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.