WeixiangYan/CodeTransOcean huggingface dataset:
Task: Sum of squares







Python: sum([1, 2, 3, 4]²)




Fortran: real, dimension(1000) :: a = (/ (i, i=1, 1000) /)
real, pointer, dimension(:) :: p => a(2:1)       
real :: result, zresult

result = sum(a*a)    

result = sum(a**2)   

zresult = sum(p*p)
Task: Sum of squares








VB: Public Sub sum_of_squares()
    Debug.Print WorksheetFunction.SumSq([{1,2,3,4,5,6,7,8,9,10}])
End Sub



Fortran: real, dimension(1000) :: a = (/ (i, i=1, 1000) /)
real, pointer, dimension(:) :: p => a(2:1)       
real :: result, zresult

result = sum(a*a)    

result = sum(a**2)   

zresult = sum(p*p)
Task: Sum of squares






PHP: function sum_squares(array $args) {
    return array_reduce(
        $args, create_function('$x, $y', 'return $x+$y*$y;'), 0
    );
}





Fortran: real, dimension(1000) :: a = (/ (i, i=1, 1000) /)
real, pointer, dimension(:) :: p => a(2:1)       
real :: result, zresult

result = sum(a*a)    

result = sum(a**2)   

zresult = sum(p*p)
Task: Sum of squares






PHP: function sum_squares(array $args) {
    return array_reduce(
        $args, create_function('$x, $y', 'return $x+$y*$y;'), 0
    );
}
Task: Sum of squares






PHP: function sum_squares(array $args) {
    return array_reduce(
        $args, create_function('$x, $y', 'return $x+$y*$y;'), 0
    );
}






Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Sum_Of_Squares is
   type Float_Array is array (Integer range <>) of Float;

   function Sum_Of_Squares (X : Float_Array) return Float is
      Sum : Float := 0.0;
   begin
      for I in X'Range loop
         Sum := Sum + X (I) ** 2;
      end loop;
      return Sum;
   end Sum_Of_Squares;
   
begin
   Put_Line (Float'Image (Sum_Of_Squares ((1..0 => 1.0)))); 
   Put_Line (Float'Image (Sum_Of_Squares ((3.0, 1.0, 4.0, 1.0, 5.0, 9.0))));
end Test_Sum_Of_Squares;
Task: Convert seconds to compound duration



C#: using System;
using System.Collections.Generic;
using System.Linq;

namespace ConvertSecondsToCompoundDuration
{
  class Program
  {
    static void Main( string[] args )
    {
      foreach ( string arg in args )
      {
        int duration ;
        bool isValid = int.TryParse( arg , out duration ) ;

        if ( !isValid     ) { Console.Error.WriteLine( "ERROR: Not an integer: {0}"           , arg ) ; }
        if ( duration < 0 ) { Console.Error.WriteLine( "ERROR: duration must be non-negative" , arg ) ; }
        
        Console.WriteLine();
        Console.WriteLine( "{0:#,##0} seconds ==> {1}" , duration , FormatAsDuration(duration) ) ;
        
      }
    }
    
    private static string FormatAsDuration( int duration )
    {
      if ( duration < 0 ) throw new ArgumentOutOfRangeException("duration") ;
      return string.Join( ", " , GetDurationParts(duration)  ) ;
    }
    
    private static IEnumerable<string> GetDurationParts( int duration )
    {
      var parts = new[]
      {
        new { Name="wk" , Length = 7*24*60*60*1 , } ,
        new { Name="d"  , Length =   24*60*60*1 , } ,
        new { Name="h"  , Length =      60*60*1 , } ,
        new { Name="m"  , Length =         60*1 , } ,
        new { Name="s"  , Length =            1 , } ,
      } ;
      
      foreach ( var part in parts )
      {
        int n = Math.DivRem( duration , part.Length , out duration ) ;
        if ( n > 0 ) yield return string.Format( "{0} {1}" , n , part.Name ) ;
      }
      
    }
    
  }
  
}









Ada: with Ada.Text_IO;

procedure Convert is
  
   type Time is range 0 .. 10_000*356*20*60*60; 
   subtype Valid_Duration is Time range 1  .. 10_000*356*20*60*60;
   type Units is (WK, D, HR, MIN, SEC);
      
   package IO renames Ada.Text_IO;
   
   Divide_By: constant array(Units) of Time := (1_000*53, 7, 24, 60, 60);
   Split: array(Units) of Time;
   No_Comma: Units;
   X: Time;
   
   Test_Cases: array(Positive range <>) of Valid_Duration :=
     (6, 60, 3659, 7_259, 86_400, 6_000_000, 6_001_200, 6_001_230, 600_000_000);
   
begin
  for Test_Case of Test_Cases loop
     IO.Put(Time'Image(Test_Case) & " SECONDS =");
     X := Test_Case;
	
     
     No_Comma := Units'First;
     for Unit in reverse Units loop 
	Split(Unit) := X mod Divide_By(Unit);
	X := X / Divide_By(Unit);
	if Unit > No_Comma and Split(Unit)>0 then
	   No_Comma := Unit;
	end if;
     end loop;
	
     
     for Unit in Units loop 
	if Split(Unit) > 0 then
	   IO.Put(Time'Image(Split(Unit)) & " " & Units'Image(Unit) & 
		    (if No_Comma > Unit then "," else ""));
	end if;
     end loop;

  IO.New_Line;
  end loop;
end Convert;
Task: Convert seconds to compound duration

C: 

#define _CRT_SECURE_NO_WARNINGS 

#include <stdio.h>
#include <stdlib.h>


char* seconds2string(unsigned long seconds) 
{
    int i;

    const unsigned long s =  1;
    const unsigned long m = 60 * s;
    const unsigned long h = 60 * m;
    const unsigned long d = 24 * h;
    const unsigned long w =  7 * d;

    const unsigned long coeff[5] = { w, d, h, m, s };
    const char units[5][4] = { "wk", "d", "hr", "min", "sec" };

    static char buffer[256];
    char* ptr = buffer;

    for ( i = 0; i < 5; i++ )
    {
        unsigned long value;
        value   = seconds / coeff[i];
        seconds = seconds % coeff[i];
        if ( value )
        {
            if ( ptr != buffer ) 
                ptr += sprintf(ptr, ", ");
            ptr += sprintf(ptr,"%lu %s",value,units[i]);
        }
    }

    return buffer;
}


int main(int argc, char argv[])
{
    unsigned long seconds;

    if ( (argc <  2) && scanf( "%lu", &seconds ) 
    ||   (argc >= 2) && sscanf( argv[1], "%lu", & seconds ) )
    {
        printf( "%s\n", seconds2string(seconds) );
        return EXIT_SUCCESS;
    }

    return EXIT_FAILURE;
}











Ada: with Ada.Text_IO;

procedure Convert is
  
   type Time is range 0 .. 10_000*356*20*60*60; 
   subtype Valid_Duration is Time range 1  .. 10_000*356*20*60*60;
   type Units is (WK, D, HR, MIN, SEC);
      
   package IO renames Ada.Text_IO;
   
   Divide_By: constant array(Units) of Time := (1_000*53, 7, 24, 60, 60);
   Split: array(Units) of Time;
   No_Comma: Units;
   X: Time;
   
   Test_Cases: array(Positive range <>) of Valid_Duration :=
     (6, 60, 3659, 7_259, 86_400, 6_000_000, 6_001_200, 6_001_230, 600_000_000);
   
begin
  for Test_Case of Test_Cases loop
     IO.Put(Time'Image(Test_Case) & " SECONDS =");
     X := Test_Case;
	
     
     No_Comma := Units'First;
     for Unit in reverse Units loop 
	Split(Unit) := X mod Divide_By(Unit);
	X := X / Divide_By(Unit);
	if Unit > No_Comma and Split(Unit)>0 then
	   No_Comma := Unit;
	end if;
     end loop;
	
     
     for Unit in Units loop 
	if Split(Unit) > 0 then
	   IO.Put(Time'Image(Split(Unit)) & " " & Units'Image(Unit) & 
		    (if No_Comma > Unit then "," else ""));
	end if;
     end loop;

  IO.New_Line;
  end loop;
end Convert;
Task: Convert seconds to compound duration


C++: #include <iostream>
#include <vector>

using entry = std::pair<int, const char*>;

void print(const std::vector<entry>& entries, std::ostream& out = std::cout)
{
    bool first = true;
    for(const auto& e: entries) {
        if(!first) out << ", ";
        first = false;
        out << e.first << " " << e.second;
    }
    out << '\n';
}

std::vector<entry> convert(int seconds)
{
    static const entry time_table[] = {
        {7*24*60*60, "wk"}, {24*60*60, "d"}, {60*60, "hr"}, {60, "min"}, {1, "sec"}
    };
    std::vector<entry> result;
    for(const auto& e: time_table) {
        int time = seconds / e.first;
        if(time != 0) result.emplace_back(time, e.second);
        seconds %= e.first;
    }
    return result;
}

int main()
{
    std::cout << "   7259 sec is "; print(convert(   7259));
    std::cout << "  86400 sec is "; print(convert(  86400));
    std::cout << "6000000 sec is "; print(convert(6000000));
}










Ada: with Ada.Text_IO;

procedure Convert is
  
   type Time is range 0 .. 10_000*356*20*60*60; 
   subtype Valid_Duration is Time range 1  .. 10_000*356*20*60*60;
   type Units is (WK, D, HR, MIN, SEC);
      
   package IO renames Ada.Text_IO;
   
   Divide_By: constant array(Units) of Time := (1_000*53, 7, 24, 60, 60);
   Split: array(Units) of Time;
   No_Comma: Units;
   X: Time;
   
   Test_Cases: array(Positive range <>) of Valid_Duration :=
     (6, 60, 3659, 7_259, 86_400, 6_000_000, 6_001_200, 6_001_230, 600_000_000);
   
begin
  for Test_Case of Test_Cases loop
     IO.Put(Time'Image(Test_Case) & " SECONDS =");
     X := Test_Case;
	
     
     No_Comma := Units'First;
     for Unit in reverse Units loop 
	Split(Unit) := X mod Divide_By(Unit);
	X := X / Divide_By(Unit);
	if Unit > No_Comma and Split(Unit)>0 then
	   No_Comma := Unit;
	end if;
     end loop;
	
     
     for Unit in Units loop 
	if Split(Unit) > 0 then
	   IO.Put(Time'Image(Split(Unit)) & " " & Units'Image(Unit) & 
		    (if No_Comma > Unit then "," else ""));
	end if;
     end loop;

  IO.New_Line;
  end loop;
end Convert;
Task: Convert seconds to compound duration




Go: package main

import "fmt"

func main(){
	fmt.Println(TimeStr(7259))
	fmt.Println(TimeStr(86400))
	fmt.Println(TimeStr(6000000))
}

func TimeStr(sec int)(res string){
	wks, sec := sec / 604800,sec % 604800
	ds, sec := sec / 86400, sec % 86400
	hrs, sec := sec / 3600, sec % 3600
	mins, sec := sec / 60, sec % 60
	CommaRequired := false
	if wks != 0 {
		res += fmt.Sprintf("%d wk",wks)
		CommaRequired = true
	}
	if ds != 0 {
		if CommaRequired {
			res += ", "
		}
		res += fmt.Sprintf("%d d",ds)
		CommaRequired = true
	}
	if hrs != 0 {
		if CommaRequired {
			res += ", "
		}
		res += fmt.Sprintf("%d hr",hrs)
		CommaRequired = true
	}
	if mins != 0 {
		if CommaRequired {
			res += ", "
		}
		res += fmt.Sprintf("%d min",mins)
		CommaRequired = true
	}
	if sec != 0 {
		if CommaRequired {
			res += ", "
		}
		res += fmt.Sprintf("%d sec",sec)
	}
	return
}








Ada: with Ada.Text_IO;

procedure Convert is
  
   type Time is range 0 .. 10_000*356*20*60*60; 
   subtype Valid_Duration is Time range 1  .. 10_000*356*20*60*60;
   type Units is (WK, D, HR, MIN, SEC);
      
   package IO renames Ada.Text_IO;
   
   Divide_By: constant array(Units) of Time := (1_000*53, 7, 24, 60, 60);
   Split: array(Units) of Time;
   No_Comma: Units;
   X: Time;
   
   Test_Cases: array(Positive range <>) of Valid_Duration :=
     (6, 60, 3659, 7_259, 86_400, 6_000_000, 6_001_200, 6_001_230, 600_000_000);
   
begin
  for Test_Case of Test_Cases loop
     IO.Put(Time'Image(Test_Case) & " SECONDS =");
     X := Test_Case;
	
     
     No_Comma := Units'First;
     for Unit in reverse Units loop 
	Split(Unit) := X mod Divide_By(Unit);
	X := X / Divide_By(Unit);
	if Unit > No_Comma and Split(Unit)>0 then
	   No_Comma := Unit;
	end if;
     end loop;
	
     
     for Unit in Units loop 
	if Split(Unit) > 0 then
	   IO.Put(Time'Image(Split(Unit)) & " " & Units'Image(Unit) & 
		    (if No_Comma > Unit then "," else ""));
	end if;
     end loop;

  IO.New_Line;
  end loop;
end Convert;
Task: Convert seconds to compound duration





Java: public class CompoundDuration {

    public static void main(String[] args) {
        compound(7259);
        compound(86400);
        compound(6000_000);
    }

    private static void compound(long seconds) {
        StringBuilder sb = new StringBuilder();

        seconds = addUnit(sb, seconds, 604800, " wk, ");
        seconds = addUnit(sb, seconds, 86400, " d, ");
        seconds = addUnit(sb, seconds, 3600, " hr, ");
        seconds = addUnit(sb, seconds, 60, " min, ");
        addUnit(sb, seconds, 1, " sec, ");

        sb.setLength(sb.length() > 2 ? sb.length() - 2 : 0);

        System.out.println(sb);
    }

    private static long addUnit(StringBuilder sb, long sec, long unit, String s) {
        long n;
        if ((n = sec / unit) > 0) {
            sb.append(n).append(s);
            sec %= (n * unit);
        }
        return sec;
    }
}







Ada: with Ada.Text_IO;

procedure Convert is
  
   type Time is range 0 .. 10_000*356*20*60*60; 
   subtype Valid_Duration is Time range 1  .. 10_000*356*20*60*60;
   type Units is (WK, D, HR, MIN, SEC);
      
   package IO renames Ada.Text_IO;
   
   Divide_By: constant array(Units) of Time := (1_000*53, 7, 24, 60, 60);
   Split: array(Units) of Time;
   No_Comma: Units;
   X: Time;
   
   Test_Cases: array(Positive range <>) of Valid_Duration :=
     (6, 60, 3659, 7_259, 86_400, 6_000_000, 6_001_200, 6_001_230, 600_000_000);
   
begin
  for Test_Case of Test_Cases loop
     IO.Put(Time'Image(Test_Case) & " SECONDS =");
     X := Test_Case;
	
     
     No_Comma := Units'First;
     for Unit in reverse Units loop 
	Split(Unit) := X mod Divide_By(Unit);
	X := X / Divide_By(Unit);
	if Unit > No_Comma and Split(Unit)>0 then
	   No_Comma := Unit;
	end if;
     end loop;
	
     
     for Unit in Units loop 
	if Split(Unit) > 0 then
	   IO.Put(Time'Image(Split(Unit)) & " " & Units'Image(Unit) & 
		    (if No_Comma > Unit then "," else ""));
	end if;
     end loop;

  IO.New_Line;
  end loop;
end Convert;
Task: Convert seconds to compound duration







Python: >>> def duration(seconds):
	t= []
	for dm in (60, 60, 24, 7):
		seconds, m = divmod(seconds, dm)
		t.append(m)
	t.append(seconds)
	return ', '.join('%d %s' % (num, unit)
			 for num, unit in zip(t[::-1], 'wk d hr min sec'.split())
			 if num)

>>> for seconds in [7259, 86400, 6000000]:
	print("%7d sec = %s" % (seconds, duration(seconds)))

	
   7259 sec = 2 hr, 59 sec
  86400 sec = 1 d
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
>>>





Ada: with Ada.Text_IO;

procedure Convert is
  
   type Time is range 0 .. 10_000*356*20*60*60; 
   subtype Valid_Duration is Time range 1  .. 10_000*356*20*60*60;
   type Units is (WK, D, HR, MIN, SEC);
      
   package IO renames Ada.Text_IO;
   
   Divide_By: constant array(Units) of Time := (1_000*53, 7, 24, 60, 60);
   Split: array(Units) of Time;
   No_Comma: Units;
   X: Time;
   
   Test_Cases: array(Positive range <>) of Valid_Duration :=
     (6, 60, 3659, 7_259, 86_400, 6_000_000, 6_001_200, 6_001_230, 600_000_000);
   
begin
  for Test_Case of Test_Cases loop
     IO.Put(Time'Image(Test_Case) & " SECONDS =");
     X := Test_Case;
	
     
     No_Comma := Units'First;
     for Unit in reverse Units loop 
	Split(Unit) := X mod Divide_By(Unit);
	X := X / Divide_By(Unit);
	if Unit > No_Comma and Split(Unit)>0 then
	   No_Comma := Unit;
	end if;
     end loop;
	
     
     for Unit in Units loop 
	if Split(Unit) > 0 then
	   IO.Put(Time'Image(Split(Unit)) & " " & Units'Image(Unit) & 
		    (if No_Comma > Unit then "," else ""));
	end if;
     end loop;

  IO.New_Line;
  end loop;
end Convert;
Task: Convert seconds to compound duration








VB: Private Function compound_duration(ByVal seconds As Long) As String
    minutes = 60
    hours = 60 * minutes
    days_ = 24 * hours
    weeks = 7 * days_
    Dim out As String
    w = seconds \ weeks
    seconds = seconds - w * weeks
    d = seconds \ days_
    seconds = seconds - d * days_
    h = seconds \ hours
    seconds = seconds - h * hours
    m = seconds \ minutes
    s = seconds Mod minutes
    out = IIf(w > 0, w & " wk, ", "") & _
        IIf(d > 0, d & " d, ", "") & _
        IIf(h > 0, h & " hr, ", "") & _
        IIf(m > 0, m & " min, ", "") & _
        IIf(s > 0, s & " sec", "")
    If Right(out, 2) = ", " Then
        compound_duration = Left(out, Len(out) - 2)
    Else
        compound_duration = out
    End If
End Function

Public Sub cstcd()
    Debug.Print compound_duration(7259)
    Debug.Print compound_duration(86400)
    Debug.Print compound_duration(6000000)
End Sub




Ada: with Ada.Text_IO;

procedure Convert is
  
   type Time is range 0 .. 10_000*356*20*60*60; 
   subtype Valid_Duration is Time range 1  .. 10_000*356*20*60*60;
   type Units is (WK, D, HR, MIN, SEC);
      
   package IO renames Ada.Text_IO;
   
   Divide_By: constant array(Units) of Time := (1_000*53, 7, 24, 60, 60);
   Split: array(Units) of Time;
   No_Comma: Units;
   X: Time;
   
   Test_Cases: array(Positive range <>) of Valid_Duration :=
     (6, 60, 3659, 7_259, 86_400, 6_000_000, 6_001_200, 6_001_230, 600_000_000);
   
begin
  for Test_Case of Test_Cases loop
     IO.Put(Time'Image(Test_Case) & " SECONDS =");
     X := Test_Case;
	
     
     No_Comma := Units'First;
     for Unit in reverse Units loop 
	Split(Unit) := X mod Divide_By(Unit);
	X := X / Divide_By(Unit);
	if Unit > No_Comma and Split(Unit)>0 then
	   No_Comma := Unit;
	end if;
     end loop;
	
     
     for Unit in Units loop 
	if Split(Unit) > 0 then
	   IO.Put(Time'Image(Split(Unit)) & " " & Units'Image(Unit) & 
		    (if No_Comma > Unit then "," else ""));
	end if;
     end loop;

  IO.New_Line;
  end loop;
end Convert;
Task: Convert seconds to compound duration

C: 

#define _CRT_SECURE_NO_WARNINGS 

#include <stdio.h>
#include <stdlib.h>


char* seconds2string(unsigned long seconds) 
{
    int i;

    const unsigned long s =  1;
    const unsigned long m = 60 * s;
    const unsigned long h = 60 * m;
    const unsigned long d = 24 * h;
    const unsigned long w =  7 * d;

    const unsigned long coeff[5] = { w, d, h, m, s };
    const char units[5][4] = { "wk", "d", "hr", "min", "sec" };

    static char buffer[256];
    char* ptr = buffer;

    for ( i = 0; i < 5; i++ )
    {
        unsigned long value;
        value   = seconds / coeff[i];
        seconds = seconds % coeff[i];
        if ( value )
        {
            if ( ptr != buffer ) 
                ptr += sprintf(ptr, ", ");
            ptr += sprintf(ptr,"%lu %s",value,units[i]);
        }
    }

    return buffer;
}


int main(int argc, char argv[])
{
    unsigned long seconds;

    if ( (argc <  2) && scanf( "%lu", &seconds ) 
    ||   (argc >= 2) && sscanf( argv[1], "%lu", & seconds ) )
    {
        printf( "%s\n", seconds2string(seconds) );
        return EXIT_SUCCESS;
    }

    return EXIT_FAILURE;
}
Task: Convert seconds to compound duration



C#: using System;
using System.Collections.Generic;
using System.Linq;

namespace ConvertSecondsToCompoundDuration
{
  class Program
  {
    static void Main( string[] args )
    {
      foreach ( string arg in args )
      {
        int duration ;
        bool isValid = int.TryParse( arg , out duration ) ;

        if ( !isValid     ) { Console.Error.WriteLine( "ERROR: Not an integer: {0}"           , arg ) ; }
        if ( duration < 0 ) { Console.Error.WriteLine( "ERROR: duration must be non-negative" , arg ) ; }
        
        Console.WriteLine();
        Console.WriteLine( "{0:#,##0} seconds ==> {1}" , duration , FormatAsDuration(duration) ) ;
        
      }
    }
    
    private static string FormatAsDuration( int duration )
    {
      if ( duration < 0 ) throw new ArgumentOutOfRangeException("duration") ;
      return string.Join( ", " , GetDurationParts(duration)  ) ;
    }
    
    private static IEnumerable<string> GetDurationParts( int duration )
    {
      var parts = new[]
      {
        new { Name="wk" , Length = 7*24*60*60*1 , } ,
        new { Name="d"  , Length =   24*60*60*1 , } ,
        new { Name="h"  , Length =      60*60*1 , } ,
        new { Name="m"  , Length =         60*1 , } ,
        new { Name="s"  , Length =            1 , } ,
      } ;
      
      foreach ( var part in parts )
      {
        int n = Math.DivRem( duration , part.Length , out duration ) ;
        if ( n > 0 ) yield return string.Format( "{0} {1}" , n , part.Name ) ;
      }
      
    }
    
  }
  
}
Task: Convert seconds to compound duration


C++: #include <iostream>
#include <vector>

using entry = std::pair<int, const char*>;

void print(const std::vector<entry>& entries, std::ostream& out = std::cout)
{
    bool first = true;
    for(const auto& e: entries) {
        if(!first) out << ", ";
        first = false;
        out << e.first << " " << e.second;
    }
    out << '\n';
}

std::vector<entry> convert(int seconds)
{
    static const entry time_table[] = {
        {7*24*60*60, "wk"}, {24*60*60, "d"}, {60*60, "hr"}, {60, "min"}, {1, "sec"}
    };
    std::vector<entry> result;
    for(const auto& e: time_table) {
        int time = seconds / e.first;
        if(time != 0) result.emplace_back(time, e.second);
        seconds %= e.first;
    }
    return result;
}

int main()
{
    std::cout << "   7259 sec is "; print(convert(   7259));
    std::cout << "  86400 sec is "; print(convert(  86400));
    std::cout << "6000000 sec is "; print(convert(6000000));
}
Task: Convert seconds to compound duration





Java: public class CompoundDuration {

    public static void main(String[] args) {
        compound(7259);
        compound(86400);
        compound(6000_000);
    }

    private static void compound(long seconds) {
        StringBuilder sb = new StringBuilder();

        seconds = addUnit(sb, seconds, 604800, " wk, ");
        seconds = addUnit(sb, seconds, 86400, " d, ");
        seconds = addUnit(sb, seconds, 3600, " hr, ");
        seconds = addUnit(sb, seconds, 60, " min, ");
        addUnit(sb, seconds, 1, " sec, ");

        sb.setLength(sb.length() > 2 ? sb.length() - 2 : 0);

        System.out.println(sb);
    }

    private static long addUnit(StringBuilder sb, long sec, long unit, String s) {
        long n;
        if ((n = sec / unit) > 0) {
            sb.append(n).append(s);
            sec %= (n * unit);
        }
        return sec;
    }
}
Task: Convert seconds to compound duration







Python: >>> def duration(seconds):
	t= []
	for dm in (60, 60, 24, 7):
		seconds, m = divmod(seconds, dm)
		t.append(m)
	t.append(seconds)
	return ', '.join('%d %s' % (num, unit)
			 for num, unit in zip(t[::-1], 'wk d hr min sec'.split())
			 if num)

>>> for seconds in [7259, 86400, 6000000]:
	print("%7d sec = %s" % (seconds, duration(seconds)))

	
   7259 sec = 2 hr, 59 sec
  86400 sec = 1 d
6000000 sec = 9 wk, 6 d, 10 hr, 40 min
>>>
Task: Convert seconds to compound duration








VB: Private Function compound_duration(ByVal seconds As Long) As String
    minutes = 60
    hours = 60 * minutes
    days_ = 24 * hours
    weeks = 7 * days_
    Dim out As String
    w = seconds \ weeks
    seconds = seconds - w * weeks
    d = seconds \ days_
    seconds = seconds - d * days_
    h = seconds \ hours
    seconds = seconds - h * hours
    m = seconds \ minutes
    s = seconds Mod minutes
    out = IIf(w > 0, w & " wk, ", "") & _
        IIf(d > 0, d & " d, ", "") & _
        IIf(h > 0, h & " hr, ", "") & _
        IIf(m > 0, m & " min, ", "") & _
        IIf(s > 0, s & " sec", "")
    If Right(out, 2) = ", " Then
        compound_duration = Left(out, Len(out) - 2)
    Else
        compound_duration = out
    End If
End Function

Public Sub cstcd()
    Debug.Print compound_duration(7259)
    Debug.Print compound_duration(86400)
    Debug.Print compound_duration(6000000)
End Sub
Task: Convert seconds to compound duration




Go: package main

import "fmt"

func main(){
	fmt.Println(TimeStr(7259))
	fmt.Println(TimeStr(86400))
	fmt.Println(TimeStr(6000000))
}

func TimeStr(sec int)(res string){
	wks, sec := sec / 604800,sec % 604800
	ds, sec := sec / 86400, sec % 86400
	hrs, sec := sec / 3600, sec % 3600
	mins, sec := sec / 60, sec % 60
	CommaRequired := false
	if wks != 0 {
		res += fmt.Sprintf("%d wk",wks)
		CommaRequired = true
	}
	if ds != 0 {
		if CommaRequired {
			res += ", "
		}
		res += fmt.Sprintf("%d d",ds)
		CommaRequired = true
	}
	if hrs != 0 {
		if CommaRequired {
			res += ", "
		}
		res += fmt.Sprintf("%d hr",hrs)
		CommaRequired = true
	}
	if mins != 0 {
		if CommaRequired {
			res += ", "
		}
		res += fmt.Sprintf("%d min",mins)
		CommaRequired = true
	}
	if sec != 0 {
		if CommaRequired {
			res += ", "
		}
		res += fmt.Sprintf("%d sec",sec)
	}
	return
}