[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]

/rcg/ - Rosetta Code General #1

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: 44
Thread images: 4

File: rosetta.jpg (103KB, 495x600px) Image search: [Google]
rosetta.jpg
103KB, 495x600px
ITT:
>Solve a problem in a programming language(s) of your choice
>Solve a solved a problem in a different programming language
>Improve an already posted solution in the same programming language
>Discuss posted solutions
>General programming language flame wars

I'll start: https://github.com/MariusMacijauskas/deadline

I will post the solutions here anyway, for convenience.
>>
File: c.png (77KB, 1200x1276px) Image search: [Google]
c.png
77KB, 1200x1276px
>>61435322
C
// Print how much ahead/ago the deadline is

#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

struct tm DEADLINE = {
.tm_year = 2017 - 1900,
.tm_mon = 7 - 1,
.tm_mday = 16,
.tm_hour = 23,
.tm_min = 59,
.tm_sec = 59,
.tm_isdst = -1
};

static int makeStr(char *buff, size_t buffSize, long n, const char *name) {
const char *fStr;
if (n > 1) fStr = "%ld %ss, ";
else if (n == 1) fStr = "%ld %s, ";
else fStr = "";
return snprintf(buff, buffSize, fStr, n, name);
}

int main(void) {
time_t now = time(NULL), deadline = mktime(&DEADLINE);
if (now == (time_t)(-1) || deadline == (time_t)(-1)) return EXIT_FAILURE;

double diff = difftime(deadline, now);
if (diff >= LONG_MAX || diff <= LONG_MIN) return EXIT_FAILURE;
long sec = diff;

const char *postfix;
if (sec > 0) {
postfix = " ahead.";
} else if (sec == 0) {
postfix = "Now.";
} else {
postfix = " ago.";
sec = -sec;
}

long days = sec / 86400;
sec %= 86400;
long hours = sec / 3600;
sec %= 3600;
long min = sec / 60;
sec %= 60;

char bigbuff[256] = {0}, tinybuff[32];
const long counts[] = {days, hours, min, sec};
const char *names[] = {"day", "hour", "minute", "second"};
for (int i = 0; i < 4; ++i) {
if (makeStr(tinybuff, sizeof(tinybuff), counts[i], names[i]) < 0) {
return EXIT_FAILURE;
}
strcat(bigbuff, tinybuff);
}

char *lastComma = strrchr(bigbuff, ',');
if (lastComma != NULL) *lastComma = '\0';

strcat(bigbuff, postfix);

if (puts(bigbuff) == EOF) return EXIT_FAILURE;
}
>>
File: lua.png (72KB, 1200x1200px) Image search: [Google]
lua.png
72KB, 1200x1200px
>>61435322
Lua
#!/usr/bin/env lua5.3

-- Print how much ahead/ago the deadline is

local DEADLINE = os.time{year=2017, month=7, day=16, hour=23, min=59, sec=59}

-- Convert os.difftime output into a more human-friendly string representation
local function difftimeStr(sec)
assert(math.tointeger(sec) ~= nil, "arg for difftimeStr must be an int")

local postfix = ""
if sec > 0 then
postfix = " ahead."
elseif sec == 0 then
postfix = "Now."
else
postfix = " ago."
sec = -sec
end

local days = sec // 86400
sec = sec - days * 86400
local hours = sec // 3600
sec = sec - hours * 3600
local min = sec // 60
sec = sec - min * 60

local function s(value, unit)
if value > 1 then
return ("%d %ss, "):format(value, unit)
elseif value == 1 then
return ("%d %s, "):format(value, unit)
else
return ""
end
end

local S = s(days,"day")..s(hours,"hour")..s(min,"minute")..s(sec,"second")

return S:sub(0, S:len() - 2)..postfix
end

print(difftimeStr(os.difftime(DEADLINE, os.time())))
>>
File: python.png (8KB, 512x512px) Image search: [Google]
python.png
8KB, 512x512px
>>61435322
Python
#!/usr/bin/env python3

from time import time, mktime

DEADLINE = mktime((2017, 7, 16, 23, 59, 59, -1, -1, -1))


# Convert second difference into a more human-friendly string representation
def difftimeStr(sec):
sec = int(sec)

postfix = ""
if sec > 0:
postfix = " ahead."
elif sec == 0:
postfix = "Now."
else:
postfix = " ago."
sec = -sec

days = sec // 86400
sec %= 86400
hours = sec // 3600
sec %= 3600
min = sec // 60
sec %= 60

def s(value, unit):
if value > 1:
return "{} {}s, ".format(value, unit)
elif value == 1:
return "{} {}, ".format(value, unit)
else:
return ""

S = s(days, "day") + s(hours, "hour") + s(min, "minute") + s(sec, "second")

return S[:-2] + postfix


print(difftimeStr(DEADLINE - time()))
>>
>>61435322
I like this thread, OP. Keep this up
>>
>>61435322
Sooo... When is the deadline?
>>
>>61435770
1 day, 15 hours, 17 minutes, 10 seconds ago.
>>
>>61435822
No, where did you get the arbitrary deadline?
>>
>>61435834
It's in the source. You configure according to your own personal needs.
>>
I was going to give a Haskell solution but the Time libraries are absolute trash.
>>
Not sure what you actually mean but
import std.stdio, std.datetime, std.conv;

void main()
{
immutable DEADLINE = DateTime(2017, 7, 16, 23, 59, 59).to!SysTime;
immutable now = Clock.currTime;

writefln("%s %s.", (DEADLINE - now).abs, now > DEADLINE ? "ago" : "ahead");
}
>>
>>61435864
What does Haskell excel at? Give me a simple example I could implement in languages I know.
>>
>>61436030
I should've use all capital `NOW` instead of "now". My bad.

Any ways. D Masterrace!
I am looking at Nim's times library. To be honest, I can't find peace in any other language anymore after getting D-pilled
>>
>>61436030
This is very nice.

I'm currently trying to figure out how to make it identical, that is:

>Remove milliseconds and microseconds
>Remove " and"
>Print "Now." if times (rounded to seconds) are equal.

I'll post when I get any progress.
>>
Hopefully, I got this right:

module Main where

import Data.List

import Data.Time.Clock
import Data.Time.Calendar


deadline :: UTCTime
deadline = UTCTime { utctDay = fromGregorian 2017 7 16
, utctDayTime = from24Hour 23 59 59
}

from24Hour :: Num a => a -> a -> a -> a
from24Hour h m s = 60*(60*h + m) + s

splitTime :: Integer -> (Integer, Integer, Integer, Integer)
splitTime t = (d, h, m, s)
where
(m', s) = t `divMod` 60
(h', m) = m' `divMod` 60
(d , h) = h' `divMod` 24

showUnit :: Integer -> String -> String
showUnit 0 _ = ""
showUnit 1 u = "1 " ++ u
showUnit x u = show x ++ " " ++ u ++ "s"

showTime :: (Integer, Integer, Integer, Integer) -> String
showTime (d, h, m, s) = intercalate ", " $ filter (not . null) $
map (uncurry showUnit) [ (d, "day")
, (h, "hour")
, (m, "minute")
, (s, "second")
]

showDeadlineDiff :: Integer -> String
showDeadlineDiff t = let t' = splitTime $ abs t in
case compare t 0 of
LT -> showTime t' ++ " ago."
EQ -> "Now."
GT -> showTime t' ++ " ahead."

main :: IO ()
main = do
now <- getCurrentTime
putStrLn $ showDeadlineDiff $ round $ diffUTCTime deadline now
>>
>>61436186

Disregard >>61436772

Again, this is a very bad hack but here goes
import std.stdio, std.datetime, std.conv, std.algorithm;

void main()
{
immutable DEADLINE = DateTime(2017, 7, 16, 23, 59, 59).to!SysTime;
immutable NOW = Clock.currTime;
immutable INTERVAL = (DEADLINE - NOW)
.abs
.to!string;
//for some reason calling until here will print the
//full type. I am looking into this.


if (NOW > DEADLINE) writeln(INTERVAL.until("secs"), "seconds ago.");
else if (NOW < DEADLINE) writeln(INTERVAL.until("secs"), "seconds ahead.");
else writeln("Now.");
}
>>
Guile Scheme

(import (ice-9 receive) (srfi srfi-19))

(define *units* '((86400 "day") (3600 "hour") (60 "minute") (1 "second")))
(define *deadline* (date->time-utc (make-date 0 59 59 23 16 7 2017 0)))

(define* (print-seconds secs #:optional (units *units*))
(if (> secs 0)
(receive (val rem)
(euclidean/ secs (caar units))
(if (> val 0)
(format #t (if (> val 1) "~A ~As " "~A ~A ") val (cadar units)))
(print-seconds rem (cdr units)))))

(define (print-time-diff x y)
(let ((diff (time-second (time-difference x y))))
(print-seconds (abs diff))
(display (cond ((> diff 0) "ahead.") ((< diff 0) "ago.") (else "Now.")))))

(print-time-diff *deadline* (current-time time-utc))
>>
Why are you guys making it so hard on yourself? lol
>>
>>61437066
A challenge is no fun it it's not a challenge.
>>
>>61436186
Okay I got it now, immutable makes popFront impossible so it can't iterate over the range. The solution was
1. Either make it an array by force with .array()
2. Or, make it mutable. So the obvious solution is
import std.stdio, std.datetime, std.conv, std.algorithm;

void main()
{
immutable DEADLINE = DateTime(2017, 7, 16, 23, 59, 59).to!SysTime;
immutable NOW = Clock.currTime;
auto INTERVAL = (DEADLINE - NOW)
.abs
.to!string
.until("secs");

if (NOW > DEADLINE) writeln(INTERVAL, "seconds ago.");
else if (NOW < DEADLINE) writeln(INTERVAL, "seconds ahead.");
else writeln("Now.");
}
>>
>>61435322
Might do this when I get home from work.
>>
>>61435322

Assembly
(It compiles, save for the UDIVS division instruction, so let's pretend it works)

;Hexadecimal time is made of 0x0000HMMS, where H goes from 0 to 0xF (16) hours,
;MM goes from 0 to 0xFF (256) minutes, and S goes from 0 to 0xF (16) seconds

BASETIME EQU 0x00008000 ; current time right before starting code in hex = 12:00 PM
DEADLINE EQU 0x0000C800 ; deadline time in hex = 9:30 PM
DELAY EQU 0x00E4E1BF ; delay time in clock cycles for one hexadecimal second
; using the wait loop defined below on an FRDM KL25z128 board (.625 decimal seconds)


AREA asm_area, CODE, READONLY
EXPORT asm_main

LDR r1,=BASETIME ; Register that contains current time
LDR r2,=DEADLINE
LDR r3,=0 ; Register that contains the time until deadline / after deadline

beforeDeadline

LDR r0,=DELAY
BL wait

ADDS r1, 1 ; Add a hexadecimal second

SUBS r3, r2, r1 ; Get the remaining time before the deadline
BL read

CMP r1, r2 ; See if it's the current time and the deadline time match
BEQ afterDeadline

B beforeDeadline

afterDeadline

LDR r0,=DELAY
BL wait

ADDS r1, 1

SUBS r3, r1, r2 ; Get the amount of time that has passed after the deadline
BL read

B afterDeadline


wait
SUBS R0, #1
BNE wait
BX LR


read
LSLS r4, r1, #16
LSRS r4, #28
MULS r4, #24
UDIVS r4, 0x10 ;r4 contains human readable HOURS from deadline in realtime (0 - 23 hours)

LSLS r5, r1, #20
LSRS r5, #24
MULS r5, #60
UDIVS r5, 0x100 ;r5 contains human readable MINUTES from deadline in realtime (0-59 minutes)

LSLS r6, r1, #28
LSRS r6, #28
MULS r6, #60
UDIVS r6, 0x10 ;r6 contains human readable SECONDS from deadline in realtime (0-59 seconds)

LSR r7, r1, #16 ;r7 contains human readable DAYS from deadline in realtime

BX LR

END ; This never happens though _(._.)_

>>
>>61437301
Sorry, I had to go but now I'm home. This approach is indeed hacky because it fails when there are no seconds or there is only one second (the later is easily fixable, the former is not, AFAIK). So right now I'm thinking about splitting Duration into fields and then converting it back into Duration. I will try to do this.

>>61436868
I'm onto this too, just after the D one, since this is way too foreign to my eyes.
>>
>>61438932
0 seconds?
>>
>>61438925
What kind of assembly? I did some x64 on gas, nasm and yasm, and this doesn't look like any of that. Forgive me for asking, I'm total newbie when it comes to assembly.

I thought about doing it with in-built Linux syscalls (I know, unportable), but I'll save it for later (until I analyze the programs I've got).
>>
>>61438985
Set deadline to one minute ahead and test it periodically.
>>
>>61439008
I don't understand. I think if there is a difference of 0 seconds it will print 0 seconds
If the interval is 1 sec, it actually will, post 1 sec
You are write about second/seconds though, it's fixable by just using second(s)
>>
>>61439040
4 seconds ahead.
> ./a.out
3 seconds ahead.
> ./a.out
2 seconds ahead.
> ./a.out
2 seconds ahead.
> ./a.out
1 seconds ahead.
> ./a.out
979 ms and 390 μsseconds ahead.
> ./a.out
240 ms and 989 μsseconds ahead.
> ./a.out
381 ms and 837 μsseconds ago.
> ./a.out
1 seconds ago.
> ./a.out
1 seconds ago.
>>
>>61439065
also

1 minute, 59 seconds ago.
> ./a.out
2 minutes, 559 ms, and 240 μsseconds ago.
> ./a.out
2 minutes, 1 seconds ago.
> ./a.out
2 minutes, 1 seconds ago.
>>
>>61439065
>>61439082
How are you computing?
    
immutable DEADLINE2 = DateTime(2017, 7, 16, 23, 59, 58).to!SysTime;
immutable DEADLINE3 = DateTime(2017, 7, 16, 23, 59, 57).to!SysTime; //Clock.currTime will automatically return SysTime
auto INTERVAL2 = (DEADLINE2 - DEADLINE3)
.abs
.to!string
.until("secs");
writeln(INTERVAL2); //prints 1 sec


And when it's the same, DEADLINE2 = NOW is checked and thus "Now" would get printed
>>
>>61438990
>>61438925
It's ARM assembly I guess.

Also I looked at the ARM Cortex M0 instruction set again (used by the board I specified in the code), and it doesn't actually have a division operation, so that read loop won't work...
Oh well, human readable time is for the weak anyway.
>>
>>61439144
I used this: >>61437301
import std.stdio, std.datetime, std.conv, std.algorithm;

void main()
{
immutable DEADLINE = DateTime(2017, 7, 18, 20, 03, 30).to!SysTime;
immutable NOW = Clock.currTime;
auto INTERVAL = (DEADLINE - NOW)
.abs
.to!string
.until("secs");

if (NOW > DEADLINE) writeln(INTERVAL, "seconds ago.");
else if (NOW < DEADLINE) writeln(INTERVAL, "seconds ahead.");
else writeln("Now.");
}
>>
Okay I think you are missing something.
>>61438932
>it fails when there are no seconds difference
Remember that I checked NOW and DEADLINE, INTERVAL for the case of "Now"
>or there is only one second
Pretty sure 1 sec would work
>>
>>61439239
>INTERVAL for the case of "Now"
Not* INTERVAL I meant
>>
#!/bin/bash
python ebinscript.py
>>
>>61439239
>Remember that I checked NOW and DEADLINE, INTERVAL for the case of "Now"
Your NOW variables holds milliseconds and microseconds, so it will be practically never be equal to DEADLINE. You have to round it to have 0 milliseconds and 0 microseconds.

>Pretty sure 1 sec would work
It does. It doesn't work when there is 1 minute, 0 seconds left, because "0 secs" are not printed and
.until("secs");
doesn't work.
>>
>>61435322
another general, another filter
sigh
>>
>>61439239
I'm trying to figure out how to do

auto INTERVAL = (DEADLINE - NOW)
.split("days", "hours", "minutes", "seconds");


and then turn in back into Duration.
>>
>>61439291
Do you want only and only Day, Month, Hour, Minute and Second?
>>
>>61439369
Why are you avoiding years?
>>
>>61439512
Because years do not have a defined number of seconds. Some have 365 days, some have 366 days. And so don't months. Some months have 28, some 30 and some 31 days. Days are the biggest unit that have defined number of seconds, so they the last ones.

>>61439482
Yes, a correct number of days should be printed.

>>61436868
Works fine (not thoroughly tested yet) with a few tweaks. Added comma separators and discovered a new way to attach postfix (printing backspace characters) and newlines at the end of the postfix. Now trying to figure out how to print local time instead of UTC.
>>
>>61439620
>>61436868
(import (ice-9 receive) (srfi srfi-19))

(define *units* '((86400 "day") (3600 "hour") (60 "minute") (1 "second")))
(define *deadline* (date->time-utc (make-date 0 50 28 20 18 7 2017 0)))

(define* (print-seconds secs #:optional (units *units*))
(if (> secs 0)
(receive (val rem)
(euclidean/ secs (caar units))
(if (> val 0)
(format #t (if (> val 1) "~A ~As, " "~A ~A, ") val (cadar units)))
(print-seconds rem (cdr units)))))

(define (print-time-diff x y)
(let ((diff (time-second (time-difference x y))))
(print-seconds (abs diff))
(display (cond ((> diff 0) "\b\b ahead.\n") ((< diff 0) "\b\bago.\n") (else "\b\bNow.\n")))))

(print-time-diff *deadline* (current-time time-utc))
>>
>>61439369
import std.stdio, std.datetime, std.conv, std.algorithm, std.format, core.time;

void main()
{
auto DEADLINE = DateTime(2017, 7, 16, 23, 59, 59);
auto NOW = Clock.currTime.to!DateTime;

auto INTERVAL = (DEADLINE - NOW)
.abs
.split!("days", "hours", "minutes", "seconds");

if (NOW > DEADLINE) writeln(INTERVAL.time_format, " ago.");
else if (NOW < DEADLINE) writeln(INTERVAL.time_format, " ahead.");
else writeln("Now.");
}

string time_format(T)(T t)
{
return
format!
"%s day(s), %s hour(s), %s minute(s) %s second(s)"
(t.days, t.hours, t.minutes, t.seconds);
}
>>
Similar thread and my contributions to it:

>>61436178
>>61441368
>>61441477
>>61441569

https://github.com/MariusMacijauskas/x
Thread posts: 44
Thread images: 4


[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.