WeixiangYan/CodeTransOcean huggingface dataset:
Task: Compare a list of strings





Java: import java.util.Arrays;

public class CompareListOfStrings {

    public static void main(String[] args) {
        String[][] arr = {{"AA", "AA", "AA", "AA"}, {"AA", "ACB", "BB", "CC"}};
        for (String[] a : arr) {
            System.out.println(Arrays.toString(a));
            System.out.println(Arrays.stream(a).distinct().count() < 2);
            System.out.println(Arrays.equals(Arrays.stream(a).distinct().sorted().toArray(), a));
        }
    }
}






Fortran:       INTEGER MANY,LONG
      PARAMETER (LONG = 6,MANY = 4)	
      CHARACTER*(LONG) STRINGS(MANY)	
      STRINGS(1) = "Fee"
      STRINGS(2) = "Fie"
      STRINGS(3) = "Foe"
      STRINGS(4) = "Fum"
      IF (ALL(STRINGS(1:MANY - 1) .LT. STRINGS(2:MANY))) THEN
        WRITE (6,*) MANY," strings: strictly increasing in order."
       ELSE
        WRITE (6,*) MANY," strings: not strictly increasing in order."
      END IF
      IF (ALL(STRINGS(1:MANY - 1) .EQ. STRINGS(2:MANY))) THEN
        WRITE (6,*) MANY," strings: all equal."
       ELSE
        WRITE (6,*) MANY," strings: not all equal."
      END IF
      END
Task: Compare a list of strings







Python: all(a == nexta for a, nexta in zip(strings, strings[1:])) 
all(a < nexta for a, nexta in zip(strings, strings[1:])) 

len(set(strings)) == 1  
sorted(strings, reverse=True) == strings  




Fortran:       INTEGER MANY,LONG
      PARAMETER (LONG = 6,MANY = 4)	
      CHARACTER*(LONG) STRINGS(MANY)	
      STRINGS(1) = "Fee"
      STRINGS(2) = "Fie"
      STRINGS(3) = "Foe"
      STRINGS(4) = "Fum"
      IF (ALL(STRINGS(1:MANY - 1) .LT. STRINGS(2:MANY))) THEN
        WRITE (6,*) MANY," strings: strictly increasing in order."
       ELSE
        WRITE (6,*) MANY," strings: not strictly increasing in order."
      END IF
      IF (ALL(STRINGS(1:MANY - 1) .EQ. STRINGS(2:MANY))) THEN
        WRITE (6,*) MANY," strings: all equal."
       ELSE
        WRITE (6,*) MANY," strings: not all equal."
      END IF
      END
Task: Compare a list of strings








VB: Private Function IsEqualOrAscending(myList) As String
Dim i&, boolEqual As Boolean, boolAsc As Boolean

    On Error Resume Next
    If UBound(myList) > 0 Then
        If Err.Number > 0 Then
            IsEqualOrAscending = "Error " & Err.Number & " : Empty array"
            On Error GoTo 0
            Exit Function
        Else
            For i = 1 To UBound(myList)
                If myList(i) <> myList(i - 1) Then boolEqual = True
                If myList(i) <= myList(i - 1) Then boolAsc = True
            Next
        End If
    End If
    IsEqualOrAscending = "List : " & Join(myList, ",") & ", IsEqual : " & (Not boolEqual) & ", IsAscending : " & Not boolAsc
End Function



Fortran:       INTEGER MANY,LONG
      PARAMETER (LONG = 6,MANY = 4)	
      CHARACTER*(LONG) STRINGS(MANY)	
      STRINGS(1) = "Fee"
      STRINGS(2) = "Fie"
      STRINGS(3) = "Foe"
      STRINGS(4) = "Fum"
      IF (ALL(STRINGS(1:MANY - 1) .LT. STRINGS(2:MANY))) THEN
        WRITE (6,*) MANY," strings: strictly increasing in order."
       ELSE
        WRITE (6,*) MANY," strings: not strictly increasing in order."
      END IF
      IF (ALL(STRINGS(1:MANY - 1) .EQ. STRINGS(2:MANY))) THEN
        WRITE (6,*) MANY," strings: all equal."
       ELSE
        WRITE (6,*) MANY," strings: not all equal."
      END IF
      END
Task: Compare a list of strings

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

static bool
strings_are_equal(const char **strings, size_t nstrings)
{
    for (size_t i = 1; i < nstrings; i++)
        if (strcmp(strings[0], strings[i]) != 0)
            return false;
    return true;
}

static bool
strings_are_in_ascending_order(const char **strings, size_t nstrings)
{
    for (size_t i = 1; i < nstrings; i++)
        if (strcmp(strings[i - 1], strings[i]) >= 0)
            return false;
    return true;
}









COBOL:        identification division.
       program-id. CompareLists.

       data division.
       working-storage section.
       78  MAX-ITEMS              value 3.
       77  i                      pic 9(2).
       01  the-list.
           05 list-items occurs MAX-ITEMS.
              10 list-item        pic x(3).
       01  results.
           05 filler              pic 9(1).
              88 equal-strings    value 1 when set to false is 0.
           05 filler              pic 9(1).
              88 ordered-strings  value 1 when set to false is 0.

       procedure division.
       main.
           move "AA BB CC" to the-list
           perform check-list
           move "AA AA AA" to the-list
           perform check-list
           move "AA CC BB" to the-list
           perform check-list
           move "AA ACBBB CC" to the-list
           perform check-list
           move "AA" to the-list
           perform check-list
           stop run
           .
       check-list.
           display "list:"
           set equal-strings to true
           set ordered-strings to true
           perform varying i from 1 by 1 until i > MAX-ITEMS
              if list-item(i) <> spaces
                 display function trim(list-item(i)), " " no advancing
                 if i < MAX-ITEMS and list-item(i + 1) <> spaces
                    if list-item(i + 1) <> list-item(i)
                       set equal-strings to false            
                    end-if
                    if list-item(i + 1) <= list-item(i) 
                       set ordered-strings to false            
                    end-if
                 end-if
              end-if
           end-perform
           display " "
           if equal-strings
              display "... is lexically equal"
           else
              display "... is not lexically equal"
           end-if
           if ordered-strings
              display "... is in strict ascending order"
           else
              display "... is not in strict ascending order"
           end-if
           display " "
           .
Task: Compare a list of strings



C#: public static (bool lexicallyEqual, bool strictlyAscending) CompareAListOfStrings(List<string> strings) =>
    strings.Count < 2 ? (true, true) :
    (
        strings.Distinct().Count() < 2,
        Enumerable.Range(1, strings.Count - 1).All(i => string.Compare(strings[i-1], strings[i]) < 0)
    );







COBOL:        identification division.
       program-id. CompareLists.

       data division.
       working-storage section.
       78  MAX-ITEMS              value 3.
       77  i                      pic 9(2).
       01  the-list.
           05 list-items occurs MAX-ITEMS.
              10 list-item        pic x(3).
       01  results.
           05 filler              pic 9(1).
              88 equal-strings    value 1 when set to false is 0.
           05 filler              pic 9(1).
              88 ordered-strings  value 1 when set to false is 0.

       procedure division.
       main.
           move "AA BB CC" to the-list
           perform check-list
           move "AA AA AA" to the-list
           perform check-list
           move "AA CC BB" to the-list
           perform check-list
           move "AA ACBBB CC" to the-list
           perform check-list
           move "AA" to the-list
           perform check-list
           stop run
           .
       check-list.
           display "list:"
           set equal-strings to true
           set ordered-strings to true
           perform varying i from 1 by 1 until i > MAX-ITEMS
              if list-item(i) <> spaces
                 display function trim(list-item(i)), " " no advancing
                 if i < MAX-ITEMS and list-item(i + 1) <> spaces
                    if list-item(i + 1) <> list-item(i)
                       set equal-strings to false            
                    end-if
                    if list-item(i + 1) <= list-item(i) 
                       set ordered-strings to false            
                    end-if
                 end-if
              end-if
           end-perform
           display " "
           if equal-strings
              display "... is lexically equal"
           else
              display "... is not lexically equal"
           end-if
           if ordered-strings
              display "... is in strict ascending order"
           else
              display "... is not in strict ascending order"
           end-if
           display " "
           .
Task: Compare a list of strings


C++: #include <algorithm>
#include <string>


std::all_of( ++(strings.begin()), strings.end(),
             [&](std::string a){ return a == strings.front(); } )  

std::is_sorted( strings.begin(), strings.end(),
                [](std::string a, std::string b){ return !(b < a); }) )  








COBOL:        identification division.
       program-id. CompareLists.

       data division.
       working-storage section.
       78  MAX-ITEMS              value 3.
       77  i                      pic 9(2).
       01  the-list.
           05 list-items occurs MAX-ITEMS.
              10 list-item        pic x(3).
       01  results.
           05 filler              pic 9(1).
              88 equal-strings    value 1 when set to false is 0.
           05 filler              pic 9(1).
              88 ordered-strings  value 1 when set to false is 0.

       procedure division.
       main.
           move "AA BB CC" to the-list
           perform check-list
           move "AA AA AA" to the-list
           perform check-list
           move "AA CC BB" to the-list
           perform check-list
           move "AA ACBBB CC" to the-list
           perform check-list
           move "AA" to the-list
           perform check-list
           stop run
           .
       check-list.
           display "list:"
           set equal-strings to true
           set ordered-strings to true
           perform varying i from 1 by 1 until i > MAX-ITEMS
              if list-item(i) <> spaces
                 display function trim(list-item(i)), " " no advancing
                 if i < MAX-ITEMS and list-item(i + 1) <> spaces
                    if list-item(i + 1) <> list-item(i)
                       set equal-strings to false            
                    end-if
                    if list-item(i + 1) <= list-item(i) 
                       set ordered-strings to false            
                    end-if
                 end-if
              end-if
           end-perform
           display " "
           if equal-strings
              display "... is lexically equal"
           else
              display "... is not lexically equal"
           end-if
           if ordered-strings
              display "... is in strict ascending order"
           else
              display "... is not in strict ascending order"
           end-if
           display " "
           .
Task: Compare a list of strings





Java: import java.util.Arrays;

public class CompareListOfStrings {

    public static void main(String[] args) {
        String[][] arr = {{"AA", "AA", "AA", "AA"}, {"AA", "ACB", "BB", "CC"}};
        for (String[] a : arr) {
            System.out.println(Arrays.toString(a));
            System.out.println(Arrays.stream(a).distinct().count() < 2);
            System.out.println(Arrays.equals(Arrays.stream(a).distinct().sorted().toArray(), a));
        }
    }
}





COBOL:        identification division.
       program-id. CompareLists.

       data division.
       working-storage section.
       78  MAX-ITEMS              value 3.
       77  i                      pic 9(2).
       01  the-list.
           05 list-items occurs MAX-ITEMS.
              10 list-item        pic x(3).
       01  results.
           05 filler              pic 9(1).
              88 equal-strings    value 1 when set to false is 0.
           05 filler              pic 9(1).
              88 ordered-strings  value 1 when set to false is 0.

       procedure division.
       main.
           move "AA BB CC" to the-list
           perform check-list
           move "AA AA AA" to the-list
           perform check-list
           move "AA CC BB" to the-list
           perform check-list
           move "AA ACBBB CC" to the-list
           perform check-list
           move "AA" to the-list
           perform check-list
           stop run
           .
       check-list.
           display "list:"
           set equal-strings to true
           set ordered-strings to true
           perform varying i from 1 by 1 until i > MAX-ITEMS
              if list-item(i) <> spaces
                 display function trim(list-item(i)), " " no advancing
                 if i < MAX-ITEMS and list-item(i + 1) <> spaces
                    if list-item(i + 1) <> list-item(i)
                       set equal-strings to false            
                    end-if
                    if list-item(i + 1) <= list-item(i) 
                       set ordered-strings to false            
                    end-if
                 end-if
              end-if
           end-perform
           display " "
           if equal-strings
              display "... is lexically equal"
           else
              display "... is not lexically equal"
           end-if
           if ordered-strings
              display "... is in strict ascending order"
           else
              display "... is not in strict ascending order"
           end-if
           display " "
           .
Task: Compare a list of strings







Python: all(a == nexta for a, nexta in zip(strings, strings[1:])) 
all(a < nexta for a, nexta in zip(strings, strings[1:])) 

len(set(strings)) == 1  
sorted(strings, reverse=True) == strings  



COBOL:        identification division.
       program-id. CompareLists.

       data division.
       working-storage section.
       78  MAX-ITEMS              value 3.
       77  i                      pic 9(2).
       01  the-list.
           05 list-items occurs MAX-ITEMS.
              10 list-item        pic x(3).
       01  results.
           05 filler              pic 9(1).
              88 equal-strings    value 1 when set to false is 0.
           05 filler              pic 9(1).
              88 ordered-strings  value 1 when set to false is 0.

       procedure division.
       main.
           move "AA BB CC" to the-list
           perform check-list
           move "AA AA AA" to the-list
           perform check-list
           move "AA CC BB" to the-list
           perform check-list
           move "AA ACBBB CC" to the-list
           perform check-list
           move "AA" to the-list
           perform check-list
           stop run
           .
       check-list.
           display "list:"
           set equal-strings to true
           set ordered-strings to true
           perform varying i from 1 by 1 until i > MAX-ITEMS
              if list-item(i) <> spaces
                 display function trim(list-item(i)), " " no advancing
                 if i < MAX-ITEMS and list-item(i + 1) <> spaces
                    if list-item(i + 1) <> list-item(i)
                       set equal-strings to false            
                    end-if
                    if list-item(i + 1) <= list-item(i) 
                       set ordered-strings to false            
                    end-if
                 end-if
              end-if
           end-perform
           display " "
           if equal-strings
              display "... is lexically equal"
           else
              display "... is not lexically equal"
           end-if
           if ordered-strings
              display "... is in strict ascending order"
           else
              display "... is not in strict ascending order"
           end-if
           display " "
           .
Task: Compare a list of strings








VB: Private Function IsEqualOrAscending(myList) As String
Dim i&, boolEqual As Boolean, boolAsc As Boolean

    On Error Resume Next
    If UBound(myList) > 0 Then
        If Err.Number > 0 Then
            IsEqualOrAscending = "Error " & Err.Number & " : Empty array"
            On Error GoTo 0
            Exit Function
        Else
            For i = 1 To UBound(myList)
                If myList(i) <> myList(i - 1) Then boolEqual = True
                If myList(i) <= myList(i - 1) Then boolAsc = True
            Next
        End If
    End If
    IsEqualOrAscending = "List : " & Join(myList, ",") & ", IsEqual : " & (Not boolEqual) & ", IsAscending : " & Not boolAsc
End Function


COBOL:        identification division.
       program-id. CompareLists.

       data division.
       working-storage section.
       78  MAX-ITEMS              value 3.
       77  i                      pic 9(2).
       01  the-list.
           05 list-items occurs MAX-ITEMS.
              10 list-item        pic x(3).
       01  results.
           05 filler              pic 9(1).
              88 equal-strings    value 1 when set to false is 0.
           05 filler              pic 9(1).
              88 ordered-strings  value 1 when set to false is 0.

       procedure division.
       main.
           move "AA BB CC" to the-list
           perform check-list
           move "AA AA AA" to the-list
           perform check-list
           move "AA CC BB" to the-list
           perform check-list
           move "AA ACBBB CC" to the-list
           perform check-list
           move "AA" to the-list
           perform check-list
           stop run
           .
       check-list.
           display "list:"
           set equal-strings to true
           set ordered-strings to true
           perform varying i from 1 by 1 until i > MAX-ITEMS
              if list-item(i) <> spaces
                 display function trim(list-item(i)), " " no advancing
                 if i < MAX-ITEMS and list-item(i + 1) <> spaces
                    if list-item(i + 1) <> list-item(i)
                       set equal-strings to false            
                    end-if
                    if list-item(i + 1) <= list-item(i) 
                       set ordered-strings to false            
                    end-if
                 end-if
              end-if
           end-perform
           display " "
           if equal-strings
              display "... is lexically equal"
           else
              display "... is not lexically equal"
           end-if
           if ordered-strings
              display "... is in strict ascending order"
           else
              display "... is not in strict ascending order"
           end-if
           display " "
           .
Task: Compare a list of strings




Go: package cmp

func AllEqual(strings []string) bool {
	for _, s := range strings {
		if s != strings[0] {
			return false
		}
	}
	return true
}

func AllLessThan(strings []string) bool {
	for i := 1; i < len(strings); i++ {
		if !(strings[i - 1] < s) {
			return false
		}
	}
	return true
}






COBOL:        identification division.
       program-id. CompareLists.

       data division.
       working-storage section.
       78  MAX-ITEMS              value 3.
       77  i                      pic 9(2).
       01  the-list.
           05 list-items occurs MAX-ITEMS.
              10 list-item        pic x(3).
       01  results.
           05 filler              pic 9(1).
              88 equal-strings    value 1 when set to false is 0.
           05 filler              pic 9(1).
              88 ordered-strings  value 1 when set to false is 0.

       procedure division.
       main.
           move "AA BB CC" to the-list
           perform check-list
           move "AA AA AA" to the-list
           perform check-list
           move "AA CC BB" to the-list
           perform check-list
           move "AA ACBBB CC" to the-list
           perform check-list
           move "AA" to the-list
           perform check-list
           stop run
           .
       check-list.
           display "list:"
           set equal-strings to true
           set ordered-strings to true
           perform varying i from 1 by 1 until i > MAX-ITEMS
              if list-item(i) <> spaces
                 display function trim(list-item(i)), " " no advancing
                 if i < MAX-ITEMS and list-item(i + 1) <> spaces
                    if list-item(i + 1) <> list-item(i)
                       set equal-strings to false            
                    end-if
                    if list-item(i + 1) <= list-item(i) 
                       set ordered-strings to false            
                    end-if
                 end-if
              end-if
           end-perform
           display " "
           if equal-strings
              display "... is lexically equal"
           else
              display "... is not lexically equal"
           end-if
           if ordered-strings
              display "... is in strict ascending order"
           else
              display "... is not in strict ascending order"
           end-if
           display " "
           .
Task: Roman numerals_Decode

C: #include <stdio.h>

int digits[26] = { 0, 0, 100, 500, 0, 0, 0, 0, 1, 1, 0, 50, 1000, 0, 0, 0, 0, 0, 0, 0, 5, 5, 0, 10, 0, 0 };


#define VALUE(x) digits[(~0x20 & (x)) - 'A']

int decode(const char * roman)
{
        const char *bigger;
        int current;
        int arabic = 0;
        while (*roman != '\0') {
                current = VALUE(*roman);
                
                bigger = roman;

                
                while (VALUE(*bigger) <= current && *++bigger != '\0');

                if (*bigger == '\0')
                        arabic += current;
                else {
                        arabic += VALUE(*bigger);
                        while (roman < bigger)
                                arabic -= VALUE(* (roman++) );
                }

                roman ++;
        }
        return arabic;
}

int main()
{
        const char * romans[] = { "MCmxC", "MMVIII", "MDClXVI", "MCXLUJ" };
        int i;

        for (i = 0; i < 4; i++)
                printf("%s\t%d\n", romans[i], decode(romans[i]));

        return 0;
}
Task: Roman numerals_Decode



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

namespace Roman
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            
            Console.WriteLine("{0}: {1}", "MCMXC", Decode("MCMXC"));
            Console.WriteLine("{0}: {1}", "MMVIII", Decode("MMVIII"));
            Console.WriteLine("{0}: {1}", "MDCLXVI", Decode("MDCLXVI"));
        }

        
        private static readonly Dictionary<char, int> RomanDictionary = new Dictionary<char, int>
                                                                            {
                                                                                {'I', 1},
                                                                                {'V', 5},
                                                                                {'X', 10},
                                                                                {'L', 50},
                                                                                {'C', 100},
                                                                                {'D', 500},
                                                                                {'M', 1000}
                                                                            };

        private static int Decode(string roman)
        {
            
            roman = roman.ToUpper();

            
            int total = 0, minus = 0;

            for (int i = 0; i < roman.Length; i++) 
            {
                
                int thisNumeral = RomanDictionary[roman[i]] - minus;

                
                if (i >= roman.Length - 1 ||
                    thisNumeral + minus >= RomanDictionary[roman[i + 1]])
                {
                    total += thisNumeral;
                    minus = 0;
                }
                else
                {
                    minus = thisNumeral;
                }
            }

            return total; 
        }
    }
}