[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
QUICK WRITE A PROGRAM IN YOUR FAVORITE LANGUAGE THAT COUNTS
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 127
Thread images: 10
File: Nevermore.jpg (29 KB, 412x430) Image search: [Google]
Nevermore.jpg
29 KB, 412x430
QUICK

WRITE A PROGRAM IN YOUR FAVORITE LANGUAGE THAT COUNTS FROM 1 TO 10 IN REAL TIME IN THE COMMAND PROMPT OR TERMINAL

OR THIS BIRD IS GONNA STAB YOU
>>
for x in range(0,10): print x; do sleep (0) while time != real 
>>
>>55480427
for i in {0..10}
do
sleep 1
print $i
done
>>
>>55480427
What do you mean, "in real time"?
>>
you got lucky when autism programmer proud dropout hipster lang shits showed up

you keep making this thread trying to get the same reddit tier success when your comment gets upboated and you strut around town like you're a celebrity

every "programmer" on this board hasn't made it 3 minutes past any interview because /g/ only cares about programming because it lets them be the ultimate hipster because you can always stretch a hello world out into more psuedo complicated shit
>>
File: bird stealing knife.gif (2 MB, 300x173) Image search: [Google]
bird stealing knife.gif
2 MB, 300x173
Got your knife, faggot!
>>
We'll have to contact someone about these stabbing birds. I'm not sure I can deal with them any longer.
>>
>>55480427
<html>
<head>
<title>he fucking chalenge Me!</title>
</head>
<body>
<h1>1</h1>
<h1>2</h1>
<h1>3</h1>
<h1>4</h1>
<h1>5</h1>
<h1>6</h1>
<h1>7</h1>
<h1>8</h1>
<h1>9</h1>
<h1>10</h1>
</body>
</html>
>>
>>55480626
Prints one number per second
>>
>>55480427
Do I get stabbed? Probably shouldn't ask.
>>
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
print(10)
>>
>Our system thinks your post is spam

int i = 0;
int j = 1;
System.out.println(String.format(%d),(i+j));
System.out.println(String.format(%d),((i+j)+(i+j));
System.out.println(String.format(%d),((i+j)+(i+j)+(i+j));
System.out.println(String.format(%d),(i+j)+(i+j)+(i+j)+(i+j));
System.out.println(String.format(%d),(i+j)+(i+j)+(i+j)+(i+j)+(i+j));
System.out.println(String.format(%d),(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j));
System.out.println(String.format(%d),(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j));
System.out.println(String.format(%d),(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j));
System.out.println(String.format(%d),(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j));
System.out.println(String.format(%d),(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j)+(i+j));
>>
#include <iostream>
#include <cstdlib>
template<typename T> void print(T s) { std::cout << s;}
template<typename T> void printLine(T s) { print(s); std::cout << std::endl; }
int main() { unsigned short int i = 1; loop: printLine(i); i++; if (i <= 10) goto loop; return 0; }
>>
>>55480427
use feature qw/say/;
for (my $i = 1; $i < 11; $i++) {
say $i;
sleep(1);
}
>>
>>55480427
Clojure
(pmap #(do (Thread/sleep (* % 1000)) (println %)) (range 1 11))
>>
BIRDMIN NO
>>
>>55480427
from time import sleep
def BIRDY():
for x in range(0, 666):
print(x)
sleep(1)


I honestly really like these threads.
>>
>>55480427
PAL C64 (NTSC one runs faster, so you'd need to fix line 20, and it's not exactly 1 second anyway)
10 for i=1 to 10
15 rem approximate time
20 for j=0 to 600
30 next j
40 print i
50 next i

there's probably a better way than that wait loop, but this is what the BASIC book from the 70s I read as a kid taught me :^)
>>
package main

import "fmt"
import "time"

func main() {
go ophomework()
time.Sleep(time.Second * 10)
}
func ophomework(){
i:=1
for range time.Tick(time.Second *1){
fmt.Println(i)
i++
}
}
>>
seq 1 10 | awk '{system("sleep 1"); print $1}'
>>
(dotimes (i 10) (print (1+ i)) (sleep 1))
>>
>>55480427
import System.Posix.Unistd

main = mapM_ (\x -> putStrLn (show x) >> sleep 1) [1..10]
>>
>>55480647
Lol
>>
>>55480647
>came back to my beloved /g/ after long farewell
>read this
>reminded me why i left
>bye /g/
>>
>>55480427
echo {1..10}
>>
C on Linux
#include <unistd.h>
int i;
for(i=1;i<11;i++){
printf("%d". i);
sleep(1);
}
>>
>>55480647
How's life treating you, Mr. Salty?
>>
>no threads
It's like you never passed programming 101
>>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>

volatile sig_atomic_t countdown = 10;
volatile sig_atomic_t should_print = 1;

static void sigalrm_handler(int num)
{
// Unused
(void)num;

should_print = 1;
--countdown;
}

int main()
{
timer_t timer;
struct sigaction sa = {
.sa_handler = sigalrm_handler,
};
struct sigevent se = {
.sigev_notify = SIGEV_SIGNAL,
.sigev_signo = SIGALRM,
};
struct itimerspec timerspec = {
.it_interval = { .tv_sec = 1 },
.it_value = { .tv_sec = 1 },
};
sigset_t mask, oldmask;

sigaction(SIGALRM, &sa, NULL);
timer_create(CLOCK_MONOTONIC, &se, &timer);
timer_settime(timer, 0, &timerspec, NULL);

sigemptyset(&mask);
sigaddset(&mask, SIGALRM);

printf("\033[?25l");

while (countdown > 0) {
printf("\r%-2d", (int)countdown);
fflush(stdout);

sigprocmask(SIG_BLOCK, &mask, &oldmask);
while (!should_print)
sigsuspend(&oldmask);
sigprocmask(SIG_UNBLOCK, &mask, &oldmask);

should_print = 0;
}

printf("\r0\n\033[?25h");

timer_delete(timer);
}
>>
>>55484152
Found the OS programmer
>>
>>55480647
Did you hit your brain or are you just a dumb bootyblasted /v/ cuck?
>>
>>55480427
If I take that knife, will you die?
>>
>>55484325
SQUAWK
>>
>>55484325
>>55480670
>>
>>55480687
Nigga
>>
var temp = 0;
while(temp < (true + true + true + true + true + true + true + true + true + true))
console.log(temp += true);
>>
File: mfw.png (572 KB, 600x580) Image search: [Google]
mfw.png
572 KB, 600x580
>>55483870
>i = i; i < 11;
>>
<?php
foreach(range(1,10) as $number){
echo("$number\r\n");
sleep(1);
}
?>

PHP masterrace
>>
>>55483870
> C
> Starting at 1
> Actually asking for off-by-one errors
>>
>>55480647
Whatever, I think small programming tasks like this are fun to do when you're bored.
>>
>>55484209
It's clearly the best solution in the thread.
>>
>>55484948
Nu-uh. >>55484838
is
>>
#include <omp.h>
#include <unistd.h>
#include <stdio.h>
int main() {
#pragma omp parallel num_threads(10)
{
int id = omp_get_thread_num();
sleep(id);
printf("%d\n", id+1);
}
}


gcc test.c -fopenmp
>>
echo "1"; sleep(1) ; echo "2"; sleep (1); echo "3"; sleep (1); echo "4"; sleep (1); echo "5"; sleep (1); echo "6"; sleep (1); echo "7"; sleep (1); echo "8"; sleep (1); echo "9"; sleep (1); echo "10"; sleep(1)
>>
File: 1465481389731.jpg (9 KB, 216x203) Image search: [Google]
1465481389731.jpg
9 KB, 216x203
>>55480427
Done.
Source is proprietary of course.
>>
i=0
count=function(){
if(i<10){
console.log(++i)
} else{clearInterval(count);}
}
setInterval(count, 1000)
>>
>>55484986
Wow I bet this runs super fast because it's parallel.
>>
>>55484851
>implying it matters at all in a one-off code snippet
>>
>>55485309
its just a bad habit but makes sense in this scenario, im sure anon was just sperging out
>>
>>55480427
package main

import (
"fmt"
"time"
)

func main() {
for i := 1; i <= 10; i++ {
fmt.Println(i)
<-time.After(time.Second)
}
}
>>
>>55483987
Tell me this is troll

Im a processs automation engineer and my company is devout against thread.sleep(x)
>>
>>55484838
shitty gg
>>
>>55480427

Matlab:

for i=1:10;
disp(i);
pause(1);
end


kinda boring desu senpai
>>
>>55480427
#include<iostream>
#include<Windows.h>
#include<ctime>
using namespace std;
int main(int argc, const char * argv[])
{
time_t start = time(0);
for (int i = 0; i < 10; i++, Sleep(1000))
cout << difftime(time(0), start) + 1 << endl;
return 0;
}
>>
>>55480427
MsgBox ("Faggot",16+0,"Fuck you.")
>>
>>55480427
public static void main(String args[]) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int i = 1;
@Override
public void run() {
System.out.println(i++);
}
}, 0, 1000);
}
>>
>>55486192
Whoops this has no end condition.

public static void main(String args[]) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int i = 1;
@Override
public void run() {
System.out.println(i++);
if (i > 10) {
cancel();
}
}
}, 0, 1000);
}
>>
>>55480427
mapM (\i -> putStrLn (show i) >> threadDelay 1000000) [1..10]
>>
for(var i=1; i < 11; i++) { console.log(i); }
>>
>>55480427

seq 1 10
>>
Nim
import os
for i in 1..10:
echo $i
os.sleep(1000)
>>
NO STABBY BIRD NOT IN THE FACE

   #include <iostream>
#include <unistd.h>
int main()
{
int i;
while( i < 10 )
{
i=i+1;
std::cout<<i<<std::endl;
sleep(1);

}
return 0;
}
>>
>>55480608
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "0"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "1"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "2"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "3"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "4"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "5"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "6"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "7"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "8"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "9"
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "10"
>>
these threads are gay
>>
>>55485200
How can we be sure it works then?
>>
>>55486406
No, you're gay
>>
>>55486449
TrustedCore(TM)
>>
for i in range(10):
print(i + 1)

This will count from 1 to 10 in real time, because it's Python.
>>
Bad question birdy. My favorite language does not have sleep(1)

++++++++[->+>+++++
+>+<<<]>+>>++<<[->
+.>.>+[+++++>+[+++
>+[+>+[+>+[+]<]<]<
]<]<<<]>--------.-
.

I added a busy loop though
>>
>>55486475
muh sides
>>
>>55486518
program sleep function yourself
>>
>>55480427
You fags don't know real, old school programming.

program  loop
implicit none
integer :: i
do i=1,10
print *,i
end do
end program loop


(Fortran)
>>
local i = 0 
while true do
i = i +1
print(i)
wait(1)
end
>>
>>55480689
Guess I'll have to adjust to this.
>>55486692

program  loop
implicit none
integer :: i
do i=1,10
print *,i
call sleep(1)
end do
end program loop
>>
Perl
for (1..10) {
print $_;
sleep 1;
}

Easy
>>
>>55480427
This challenge is way too easy in Java
for (int x; x=1;x++){
system.out.println(x);
Thread.sleep(1000);
}
>>
>>55486192
>>55486211
>needing to create an object just to pause the thread
>>
>>55487063
Whoops, i was a bit too lax for this one

for (int x = 0; x<11 ;x++){
system.out.println(x);
Thread.sleep(1000);
}
>>
>>55487105
damn it, I need sleep
for (int x = 0; x<10 ;x++){
system.out.println((x+1));
Thread.sleep(1000);
}
>>
>>55487063
Forgot to add a try/catch incase something goes wrong with the sleep. Gitgud pajeet
>>
>>55485419
Can you explain why?

Its not like you cant execute other threads if you do OOP right.
>>
>>55487134
I knew about try/catch, but ididnt feel like typing that out for this retardedly easy challenge.

I dont even have a main, why do you think I would bother with try/catch?
>>
1..10 | ForEach-Object {
Write-Host $_
Start-Sleep -Seconds 1
}
>>
>>55480427
print ("hello world!")
>>
>>55487116
then go to bed!
>>
import time
for i in range(10):
print i+1;
time.sleep(1)
>>
>"A program that counts from one to ten in real time."
Done
I work in Venture Capital we have no real skills but still get paid for "coming up with" shit like this.
>>
require("socket")
for i = 1, 10 do
print(i)
socket.select(nil, nil, 1)
end
>>
>>55480647
SOMEONE IS UPSET
>>
>>55480427
Sure buddy:
#!/usr/bin/perl
eval eval '"'.


('`'|'&').('`'|'/').('['^')').'('.('^'^('`'|'/')).'.'.'.'.('^'^('`'|'/')).('^'^('`'|'.'))."\)".'\\'.
'{'.('['^'(').('`'|',').('`'|'%').('`'|'%').('['^'+').'('.('^'^('`'|'/')).')'.';'.('['^'+').('['^')'
).('`'|')').('`'|'.').('['^'/').'\\'.'"'.'\\'.'$'.'_'.'\\'.'\\'.('`'|'.').'\\'.'"'.'\\'.'}'."\"";#;#
>>
>>55487691
Care to explain the logic? I see that it works but I am not familiar with perl.
>>
>>55484838
add some flush() or whatever it's called for added effect
>>
>>55480877
Nigger it didnt wait at all
>>
>>55487691
holy fucking shit it really works. what the hell is this fucking magic?! eval eval and then .... bullshitting. Please explain.
>>
>>55480427
That's a really trivial task

fn main() {
let t = std::time::Duration::from_secs(1);
for i in 1..11 {
std::thread::sleep(t);
println!("{}", i);
}
}
>>
[1...10] |> List.map (func x -> Console.WriteLine(x))
>>
for i in `seq 10`; do echo $i; done
>>
>>55480548
how can time == real if our eyes aren't real
>>
>>55480427
Static void main()
{

For(int x=1,x<11, x++)
{
Console.WriteLine(x);
System.Threading.Thread.Sleep(1000);
}
Console.ReadLine();
}


//There is always a better way to do things
>>
File: monk.png (110 KB, 480x250) Image search: [Google]
monk.png
110 KB, 480x250
>>55487743
>>55487906
That's strong Perl wisdom boys.

http://search.cpan.org/dist/Acme-EyeDrops/lib/Acme/EyeDrops.pm
>>
File: Animation.gif (103 KB, 834x422) Image search: [Google]
Animation.gif
103 KB, 834x422
>>
>>55488256
That fucking piece of shit code.

Ugh..
>>
File: image.png (5 KB, 254x199) Image search: [Google]
image.png
5 KB, 254x199
int x;
cout<<"1"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"2"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"3"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"4"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"5"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"6"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"7"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"8"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"9"<<endl;
cout<<"count 1 second and press x"<<endl;
cin>>x;
cout<<"10"<<endl;
cout<<"BYE!";
>>
>>55480427
import std.stdio

for(int i = 1; i < 11; i++){
writeln(i)
}
>>
>>55487691
>perl buffy.pl (should show Buffy looking in the mirror)

Incredible
>>
>>55480427
hello my name pajeet
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define range

typedef struct {
void (*sleep)();
} sys;

typedef struct {
void (*addOne)();
void (*print)();
int value;
} number;

void _addOne(int *n)
{
++(*n);
}

void _print(int *n)
{
printf("%d\n", *n);
}

int main(void)
{
sys System;
number myNumber;

System.sleep = &sleep;
myNumber.addOne = &_addOne;
myNumber.print = &_print;

myNumber.value = 1;
while (myNumber.value <= 10) {
myNumber.print(&myNumber.value);
myNumber.addOne(&myNumber.value);
System.sleep(1);
}

return 0;
}
>>
for i in 1 2 3 4 5 6 7 8 9 10; do
echo -n "$i "
done
echo " "
>>
>>55480647
>you got lucky when autism programmer proud dropout hipster lang shits showed up
>you keep making this thread trying to get the same reddit tier success when your comment gets upboated and you strut around town like you're a celebrity
>every "programmer" on this board hasn't made it 3 minutes past any interview because /g/ only cares about programming because it lets them be the ultimate hipster because you can always stretch a hello world out into more psuedo complicated shit
best hello world I've ever seen
>>
>>55482390
Why 1+? Otherwise pretty neat solution.
>>
>>55480427
#include <iostream>    
#include <thread>
#include <chrono>
void realtimeTimer(int n)
{
for (auto i = 0; i < n; i++) {
std::cout << i << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}

}
int main()
{
try
{
std::thread thr(realtimeTimer, 10);
thr.join();
}
catch(const std::exception &ex)
{
std::cout << ex.what() << std::endl;
}
return 0;
}
>>
Reminder that everyone failed who used the Sleep function
>>
>>55488335
Underrated
>>
>>55489218
ikr?

but how do different in cpp?
>>
>>55489218
explain.

Honestly, This is one of the few times it is appropriate to use sleep.... right?
>>
File: JA1y4DR.png (277 KB, 3000x2584) Image search: [Google]
JA1y4DR.png
277 KB, 3000x2584
>>55480729
>>
echo 'real 1,2,3,4,5,6,7,8,9,10 time';

Any more brain busters?
>>
>>55480427
#include <chrono>
#include <thread>
#include <iostream>

using namespace std::chrono_literals;

int main()
{
int i = 1;
while (i <= 10)
{
std::this_thread::sleep_for(1s);
std::cout << i << std::endl;
}
return 0;
}
>>
>>55480647
10/copypasta.new
>>
>>55483772
really old hardware?
>>
for i in 1 2 3 4 5 6 7 8 9 10
do
echo $i
sleep 1
done
>>
>>55489100
Not the guy you replied to, but 'i' probably starts at 0.
>>
File: 1410827556549.jpg (122 KB, 679x631) Image search: [Google]
1410827556549.jpg
122 KB, 679x631
>>55486076

>MatLab
>>
>>55485971
What's wrong with it?
>>
>>55491158
You're here.
>>
>>55491340
ok
>>
var e = 1;
setInterval(() => {
if (e > 10) {
return;
}
console.log(e);
e += 1
}, 1000);
Thread replies: 127
Thread images: 10

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.