WeixiangYan/CodeTransOcean huggingface dataset:
Task: Days between dates




Go: package main

import (
    "fmt"
    "log"
    "time"
)

const layout = "2006-01-02" 


func daysBetween(date1, date2 string) int {
    t1, err := time.Parse(layout, date1)
    check(err)
    t2, err := time.Parse(layout, date2)
    check(err)
    days := int(t1.Sub(t2).Hours() / 24)
    if days < 0 {
        days = -days
    }
    return days
}
    
func check(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {
    date1, date2 := "2019-01-01", "2019-09-30"
    days := daysBetween(date1, date2)
    fmt.Printf("There are %d days between %s and %s\n", days, date1, date2)

    date1, date2 = "2015-12-31", "2016-09-30"
    days = daysBetween(date1, date2)
    fmt.Printf("There are %d days between %s and %s\n", days, date1, date2)
}








Ada: with Ada.Calendar;
with Ada.Text_IO;
with Ada.Integer_Text_IO;

with GNAT.Calendar.Time_IO;

procedure Days_Between_Dates is

   function Days_Between (Lower_Date  : in Ada.Calendar.Time;
                          Higher_Date : in Ada.Calendar.Time) return Integer
   is
      use Ada.Calendar;
      Diff : constant Duration := Higher_Date - Lower_Date;
   begin
      return Integer (Diff / Day_Duration'Last);
   end Days_Between;

   procedure Put_Days_Between (Lower_Date  : in String;
                               Higher_Date : in String;
                               Comment     : in String)
   is
      use Ada.Text_IO;
      use Ada.Integer_Text_IO;
      use GNAT.Calendar.Time_IO;

      Diff : constant Integer := Days_Between (Lower_Date  => Value (Lower_Date),
                                               Higher_Date => Value (Higher_Date));
   begin
      Put ("Days between " & Lower_Date & " and " & Higher_Date & " is ");
      Put (Diff, Width => 5);
      Put (" days  
      Put (Comment);
      New_Line;
   end Put_Days_Between;

begin
   Put_Days_Between ("1995-11-21", "1995-11-21", "Identical dates");
   Put_Days_Between ("2019-01-01", "2019-01-02", "Positive span");
   Put_Days_Between ("2019-01-02", "2019-01-01", "Negative span");
   Put_Days_Between ("2019-01-01", "2019-03-01", "Non-leap year");
   Put_Days_Between ("2020-01-01", "2020-03-01", "Leap year");
   Put_Days_Between ("1902-01-01", "1968-12-25", "Past");
   Put_Days_Between ("2090-01-01", "2098-12-25", "Future");
   Put_Days_Between ("1902-01-01", "2098-12-25", "Long span");
end Days_Between_Dates;
Task: Days between dates





Java: import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class DaysBetweenDates {
    public static void main(String[] args) {
        LocalDate fromDate = LocalDate.parse("2019-01-01");
        LocalDate toDate = LocalDate.parse("2019-10-19");
        long diff = ChronoUnit.DAYS.between(fromDate, toDate);
        System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff);
    }
}







Ada: with Ada.Calendar;
with Ada.Text_IO;
with Ada.Integer_Text_IO;

with GNAT.Calendar.Time_IO;

procedure Days_Between_Dates is

   function Days_Between (Lower_Date  : in Ada.Calendar.Time;
                          Higher_Date : in Ada.Calendar.Time) return Integer
   is
      use Ada.Calendar;
      Diff : constant Duration := Higher_Date - Lower_Date;
   begin
      return Integer (Diff / Day_Duration'Last);
   end Days_Between;

   procedure Put_Days_Between (Lower_Date  : in String;
                               Higher_Date : in String;
                               Comment     : in String)
   is
      use Ada.Text_IO;
      use Ada.Integer_Text_IO;
      use GNAT.Calendar.Time_IO;

      Diff : constant Integer := Days_Between (Lower_Date  => Value (Lower_Date),
                                               Higher_Date => Value (Higher_Date));
   begin
      Put ("Days between " & Lower_Date & " and " & Higher_Date & " is ");
      Put (Diff, Width => 5);
      Put (" days  
      Put (Comment);
      New_Line;
   end Put_Days_Between;

begin
   Put_Days_Between ("1995-11-21", "1995-11-21", "Identical dates");
   Put_Days_Between ("2019-01-01", "2019-01-02", "Positive span");
   Put_Days_Between ("2019-01-02", "2019-01-01", "Negative span");
   Put_Days_Between ("2019-01-01", "2019-03-01", "Non-leap year");
   Put_Days_Between ("2020-01-01", "2020-03-01", "Leap year");
   Put_Days_Between ("1902-01-01", "1968-12-25", "Past");
   Put_Days_Between ("2090-01-01", "2098-12-25", "Future");
   Put_Days_Between ("1902-01-01", "2098-12-25", "Long span");
end Days_Between_Dates;
Task: Days between dates







Python: 
import sys



def days( y,m,d ):
  
  m = (m + 9) % 12 
  y = y - m/10

  
  result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 )
  return result

def diff(one,two):
  [y1,m1,d1] = one.split('-')
  [y2,m2,d2] = two.split('-')
  
  year2 = days( int(y2),int(m2),int(d2))
  year1 = days( int(y1), int(m1), int(d1) )
  return year2 - year1

if __name__ == "__main__":
  one = sys.argv[1]
  two = sys.argv[2]
  print diff(one,two)





Ada: with Ada.Calendar;
with Ada.Text_IO;
with Ada.Integer_Text_IO;

with GNAT.Calendar.Time_IO;

procedure Days_Between_Dates is

   function Days_Between (Lower_Date  : in Ada.Calendar.Time;
                          Higher_Date : in Ada.Calendar.Time) return Integer
   is
      use Ada.Calendar;
      Diff : constant Duration := Higher_Date - Lower_Date;
   begin
      return Integer (Diff / Day_Duration'Last);
   end Days_Between;

   procedure Put_Days_Between (Lower_Date  : in String;
                               Higher_Date : in String;
                               Comment     : in String)
   is
      use Ada.Text_IO;
      use Ada.Integer_Text_IO;
      use GNAT.Calendar.Time_IO;

      Diff : constant Integer := Days_Between (Lower_Date  => Value (Lower_Date),
                                               Higher_Date => Value (Higher_Date));
   begin
      Put ("Days between " & Lower_Date & " and " & Higher_Date & " is ");
      Put (Diff, Width => 5);
      Put (" days  
      Put (Comment);
      New_Line;
   end Put_Days_Between;

begin
   Put_Days_Between ("1995-11-21", "1995-11-21", "Identical dates");
   Put_Days_Between ("2019-01-01", "2019-01-02", "Positive span");
   Put_Days_Between ("2019-01-02", "2019-01-01", "Negative span");
   Put_Days_Between ("2019-01-01", "2019-03-01", "Non-leap year");
   Put_Days_Between ("2020-01-01", "2020-03-01", "Leap year");
   Put_Days_Between ("1902-01-01", "1968-12-25", "Past");
   Put_Days_Between ("2090-01-01", "2098-12-25", "Future");
   Put_Days_Between ("1902-01-01", "2098-12-25", "Long span");
end Days_Between_Dates;
Task: Days between dates








VB: Imports System.Globalization

Module Module1

    Function DateDiff(d1 As String, d2 As String) As Integer
        Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture)
        Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture)
        Return (b - a).TotalDays
    End Function

    Sub Main()
        Console.WriteLine(DateDiff("1970-01-01", "2019-10-18"))
    End Sub

End Module




Ada: with Ada.Calendar;
with Ada.Text_IO;
with Ada.Integer_Text_IO;

with GNAT.Calendar.Time_IO;

procedure Days_Between_Dates is

   function Days_Between (Lower_Date  : in Ada.Calendar.Time;
                          Higher_Date : in Ada.Calendar.Time) return Integer
   is
      use Ada.Calendar;
      Diff : constant Duration := Higher_Date - Lower_Date;
   begin
      return Integer (Diff / Day_Duration'Last);
   end Days_Between;

   procedure Put_Days_Between (Lower_Date  : in String;
                               Higher_Date : in String;
                               Comment     : in String)
   is
      use Ada.Text_IO;
      use Ada.Integer_Text_IO;
      use GNAT.Calendar.Time_IO;

      Diff : constant Integer := Days_Between (Lower_Date  => Value (Lower_Date),
                                               Higher_Date => Value (Higher_Date));
   begin
      Put ("Days between " & Lower_Date & " and " & Higher_Date & " is ");
      Put (Diff, Width => 5);
      Put (" days  
      Put (Comment);
      New_Line;
   end Put_Days_Between;

begin
   Put_Days_Between ("1995-11-21", "1995-11-21", "Identical dates");
   Put_Days_Between ("2019-01-01", "2019-01-02", "Positive span");
   Put_Days_Between ("2019-01-02", "2019-01-01", "Negative span");
   Put_Days_Between ("2019-01-01", "2019-03-01", "Non-leap year");
   Put_Days_Between ("2020-01-01", "2020-03-01", "Leap year");
   Put_Days_Between ("1902-01-01", "1968-12-25", "Past");
   Put_Days_Between ("2090-01-01", "2098-12-25", "Future");
   Put_Days_Between ("1902-01-01", "2098-12-25", "Long span");
end Days_Between_Dates;
Task: Days between dates

C: #include<stdbool.h>
#include<string.h>
#include<stdio.h>

typedef struct{
    int year, month, day;
}date;

date extractDate(char* str){
    return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')};
}

bool isValidDate(char* str){
    date newDate;
    
    if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){
        return false;
    }

    newDate = extractDate(str);

    if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || 
      ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || 
      newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){
          return false;
      }

    return true;
}

int diffDays(date date1,date date2){
    int days1, days2;
    
    date1.month = (date1.month + 9)%12;
    date1.year = date1.year - date1.month/10;

    date2.month = (date2.month + 9)%12;
    date2.year = date2.year - date2.month/10;

    days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 );
    days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 );

    return days2 - days1;
}

int main(int argc,char** argv)
{
    if(argc!=3){
        return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]);
    }

    if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){
        return printf("Dates are invalid.\n");
    }

    printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2])));

    return 0;
}
Task: Days between dates



C#: using System;
using System.Globalization;

public class Program
{
    public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18"));

    public static int DateDiff(string d1, string d2) {
        var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        return (int)(b - a).TotalDays;
    }
}
Task: Days between dates





Java: import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class DaysBetweenDates {
    public static void main(String[] args) {
        LocalDate fromDate = LocalDate.parse("2019-01-01");
        LocalDate toDate = LocalDate.parse("2019-10-19");
        long diff = ChronoUnit.DAYS.between(fromDate, toDate);
        System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff);
    }
}
Task: Days between dates







Python: 
import sys



def days( y,m,d ):
  
  m = (m + 9) % 12 
  y = y - m/10

  
  result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 )
  return result

def diff(one,two):
  [y1,m1,d1] = one.split('-')
  [y2,m2,d2] = two.split('-')
  
  year2 = days( int(y2),int(m2),int(d2))
  year1 = days( int(y1), int(m1), int(d1) )
  return year2 - year1

if __name__ == "__main__":
  one = sys.argv[1]
  two = sys.argv[2]
  print diff(one,two)
Task: Days between dates








VB: Imports System.Globalization

Module Module1

    Function DateDiff(d1 As String, d2 As String) As Integer
        Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture)
        Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture)
        Return (b - a).TotalDays
    End Function

    Sub Main()
        Console.WriteLine(DateDiff("1970-01-01", "2019-10-18"))
    End Sub

End Module
Task: Days between dates




Go: package main

import (
    "fmt"
    "log"
    "time"
)

const layout = "2006-01-02" 


func daysBetween(date1, date2 string) int {
    t1, err := time.Parse(layout, date1)
    check(err)
    t2, err := time.Parse(layout, date2)
    check(err)
    days := int(t1.Sub(t2).Hours() / 24)
    if days < 0 {
        days = -days
    }
    return days
}
    
func check(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {
    date1, date2 := "2019-01-01", "2019-09-30"
    days := daysBetween(date1, date2)
    fmt.Printf("There are %d days between %s and %s\n", days, date1, date2)

    date1, date2 = "2015-12-31", "2016-09-30"
    days = daysBetween(date1, date2)
    fmt.Printf("There are %d days between %s and %s\n", days, date1, date2)
}
Task: Days between dates

C: #include<stdbool.h>
#include<string.h>
#include<stdio.h>

typedef struct{
    int year, month, day;
}date;

date extractDate(char* str){
    return (date){.year = 1000 * (str[0]-'0') + 100 * (str[1]-'0') + 10 * (str[2]-'0') + (str[3]-'0'), .month = 10*(str[5]-'0') + (str[6]-'0'), .day = 10*(str[8]-'0') + (str[9]-'0')};
}

bool isValidDate(char* str){
    date newDate;
    
    if(strlen(str)!=10 && str[4]!='-' && str[7]!='-'){
        return false;
    }

    newDate = extractDate(str);

    if(newDate.year<=0 || newDate.month<=0 || newDate.day<=0 || newDate.month>12 || (newDate.month==2 && newDate.day>29) || 
      ((newDate.month==1 || newDate.month==3 || newDate.month==5 || newDate.month==7 || newDate.month==8 || newDate.month==10 || newDate.month==12) && newDate.day>31) || 
      newDate.day>30 || (newDate.year%4==0 && newDate.month==2 && newDate.month>28)){
          return false;
      }

    return true;
}

int diffDays(date date1,date date2){
    int days1, days2;
    
    date1.month = (date1.month + 9)%12;
    date1.year = date1.year - date1.month/10;

    date2.month = (date2.month + 9)%12;
    date2.year = date2.year - date2.month/10;

    days1 = 365*date1.year + date1.year/4 - date1.year/100 + date1.year/400 + (date1.month*306 + 5)/10 + ( date1.day - 1 );
    days2 = 365*date2.year + date2.year/4 - date2.year/100 + date2.year/400 + (date2.month*306 + 5)/10 + ( date2.day - 1 );

    return days2 - days1;
}

int main(int argc,char** argv)
{
    if(argc!=3){
        return printf("Usage : %s <yyyy-mm-dd> <yyyy-mm-dd>",argv[0]);
    }

    if(isValidDate(argv[1])&&isValidDate(argv[2]) == false){
        return printf("Dates are invalid.\n");
    }

    printf("Days Difference : %d\n", diffDays(extractDate(argv[1]),extractDate(argv[2])));

    return 0;
}









COBOL: COBOL 
      

       identification division.
       program-id. days-between.

       procedure division.
       compute tally = 
         function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
         -
         function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
       display tally

       compute tally = 
         function integer-of-formatted-date('YYYYMMDD', '20191124')
         -
         function integer-of-formatted-date('YYYYMMDD', '20000101')
       display tally

       goback.
       end program days-between.
Task: Days between dates



C#: using System;
using System.Globalization;

public class Program
{
    public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18"));

    public static int DateDiff(string d1, string d2) {
        var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        return (int)(b - a).TotalDays;
    }
}







COBOL: COBOL 
      

       identification division.
       program-id. days-between.

       procedure division.
       compute tally = 
         function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
         -
         function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
       display tally

       compute tally = 
         function integer-of-formatted-date('YYYYMMDD', '20191124')
         -
         function integer-of-formatted-date('YYYYMMDD', '20000101')
       display tally

       goback.
       end program days-between.
Task: Days between dates





Java: import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class DaysBetweenDates {
    public static void main(String[] args) {
        LocalDate fromDate = LocalDate.parse("2019-01-01");
        LocalDate toDate = LocalDate.parse("2019-10-19");
        long diff = ChronoUnit.DAYS.between(fromDate, toDate);
        System.out.printf("Number of days between %s and %s: %d\n", fromDate, toDate, diff);
    }
}





COBOL: COBOL 
      

       identification division.
       program-id. days-between.

       procedure division.
       compute tally = 
         function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
         -
         function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
       display tally

       compute tally = 
         function integer-of-formatted-date('YYYYMMDD', '20191124')
         -
         function integer-of-formatted-date('YYYYMMDD', '20000101')
       display tally

       goback.
       end program days-between.
Task: Days between dates







Python: 
import sys



def days( y,m,d ):
  
  m = (m + 9) % 12 
  y = y - m/10

  
  result = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 )
  return result

def diff(one,two):
  [y1,m1,d1] = one.split('-')
  [y2,m2,d2] = two.split('-')
  
  year2 = days( int(y2),int(m2),int(d2))
  year1 = days( int(y1), int(m1), int(d1) )
  return year2 - year1

if __name__ == "__main__":
  one = sys.argv[1]
  two = sys.argv[2]
  print diff(one,two)



COBOL: COBOL 
      

       identification division.
       program-id. days-between.

       procedure division.
       compute tally = 
         function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
         -
         function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
       display tally

       compute tally = 
         function integer-of-formatted-date('YYYYMMDD', '20191124')
         -
         function integer-of-formatted-date('YYYYMMDD', '20000101')
       display tally

       goback.
       end program days-between.
Task: Days between dates








VB: Imports System.Globalization

Module Module1

    Function DateDiff(d1 As String, d2 As String) As Integer
        Dim a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture)
        Dim b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture)
        Return (b - a).TotalDays
    End Function

    Sub Main()
        Console.WriteLine(DateDiff("1970-01-01", "2019-10-18"))
    End Sub

End Module


COBOL: COBOL 
      

       identification division.
       program-id. days-between.

       procedure division.
       compute tally = 
         function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
         -
         function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
       display tally

       compute tally = 
         function integer-of-formatted-date('YYYYMMDD', '20191124')
         -
         function integer-of-formatted-date('YYYYMMDD', '20000101')
       display tally

       goback.
       end program days-between.
Task: Days between dates




Go: package main

import (
    "fmt"
    "log"
    "time"
)

const layout = "2006-01-02" 


func daysBetween(date1, date2 string) int {
    t1, err := time.Parse(layout, date1)
    check(err)
    t2, err := time.Parse(layout, date2)
    check(err)
    days := int(t1.Sub(t2).Hours() / 24)
    if days < 0 {
        days = -days
    }
    return days
}
    
func check(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func main() {
    date1, date2 := "2019-01-01", "2019-09-30"
    days := daysBetween(date1, date2)
    fmt.Printf("There are %d days between %s and %s\n", days, date1, date2)

    date1, date2 = "2015-12-31", "2016-09-30"
    days = daysBetween(date1, date2)
    fmt.Printf("There are %d days between %s and %s\n", days, date1, date2)
}






COBOL: COBOL 
      

       identification division.
       program-id. days-between.

       procedure division.
       compute tally = 
         function integer-of-formatted-date('YYYY-MM-DD', '2019-11-24')
         -
         function integer-of-formatted-date('YYYY-MM-DD', '2000-01-01')
       display tally

       compute tally = 
         function integer-of-formatted-date('YYYYMMDD', '20191124')
         -
         function integer-of-formatted-date('YYYYMMDD', '20000101')
       display tally

       goback.
       end program days-between.
Task: Look-and-say sequence



C#: using System;
using System.Text;
using System.Linq;

class Program
{
    static string lookandsay(string number)
    {
        StringBuilder result = new StringBuilder();

        char repeat = number[0];
        number = number.Substring(1, number.Length-1)+" ";
        int times = 1;
      
        foreach (char actual in number)
        {
            if (actual != repeat)
            {
                result.Append(Convert.ToString(times)+repeat);
                times = 1;
                repeat = actual;
            }
            else
            {
                times += 1;
            }
        }
        return result.ToString();
    }

    static void Main(string[] args)
    {
        string num = "1"; 

        foreach (int i in Enumerable.Range(1, 10)) {
             Console.WriteLine(num);
             num = lookandsay(num);             
        }
    }
}









Ada: with Ada.Text_IO, Ada.Strings.Fixed;
use  Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;

function "+" (S : String) return String is
   Item : constant Character := S (S'First);
begin
   for Index in S'First + 1..S'Last loop
      if Item /= S (Index) then
         return Trim (Integer'Image (Index - S'First), Both) & Item & (+(S (Index..S'Last)));
      end if;
   end loop;
   return Trim (Integer'Image (S'Length), Both) & Item;
end "+";
Task: Look-and-say sequence

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

int main()
{
	char *a = malloc(2), *b = 0, *x, c;
	int cnt, len = 1;

	for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
		puts(x = a);
		for (len = 0, cnt = 1; (c = *a); ) {
			if (c == *++a)
				cnt++;
			else if (c) {
				len += sprintf(b + len, "%d%c", cnt, c);
				cnt = 1;
			}
		}
	}

	return 0;
}











Ada: with Ada.Text_IO, Ada.Strings.Fixed;
use  Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;

function "+" (S : String) return String is
   Item : constant Character := S (S'First);
begin
   for Index in S'First + 1..S'Last loop
      if Item /= S (Index) then
         return Trim (Integer'Image (Index - S'First), Both) & Item & (+(S (Index..S'Last)));
      end if;
   end loop;
   return Trim (Integer'Image (S'Length), Both) & Item;
end "+";
Task: Look-and-say sequence


C++: #include <iostream>
#include <sstream>
#include <string>

std::string lookandsay(const std::string& s)
{
    std::ostringstream r;

    for (std::size_t i = 0; i != s.length();) {
        auto new_i = s.find_first_not_of(s[i], i + 1);

        if (new_i == std::string::npos)
            new_i = s.length();

        r << new_i - i << s[i];
        i = new_i;
    }
    return r.str();
}

int main()
{
    std::string laf = "1";

    std::cout << laf << '\n';
    for (int i = 0; i < 10; ++i) {
        laf = lookandsay(laf);
        std::cout << laf << '\n';
    }
}










Ada: with Ada.Text_IO, Ada.Strings.Fixed;
use  Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;

function "+" (S : String) return String is
   Item : constant Character := S (S'First);
begin
   for Index in S'First + 1..S'Last loop
      if Item /= S (Index) then
         return Trim (Integer'Image (Index - S'First), Both) & Item & (+(S (Index..S'Last)));
      end if;
   end loop;
   return Trim (Integer'Image (S'Length), Both) & Item;
end "+";