WeixiangYan/CodeTransOcean huggingface dataset:
Task: Ordered words



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

static class Program
{
    static void Main(string[] args)
    {
        WebClient client = new WebClient();
        string text = client.DownloadString("http:
        string[] words = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

        var query = from w in words
                    where IsOrderedWord(w)
                    group w by w.Length into ows
                    orderby ows.Key descending
                    select ows;

        Console.WriteLine(string.Join(", ", query.First().ToArray()));
    }

    private static bool IsOrderedWord(string w)
    {
        for (int i = 1; i < w.Length; i++)
            if (w[i] < w[i - 1])
                return false;

        return true;
    }
}









Ada: with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; 
use Ada.Text_IO;

procedure Ordered_Words is
   package Word_Vectors is new Ada.Containers.Indefinite_Vectors
      (Index_Type => Positive, Element_Type => String);
   use Word_Vectors;
   File : File_Type;
   Ordered_Words : Vector;
   Max_Length : Positive := 1;
begin
   Open (File, In_File, "unixdict.txt");
   while not End_Of_File (File) loop
      declare
         Word : String := Get_Line (File);
      begin
         if (for all i in Word'First..Word'Last-1 => Word (i) <= Word(i+1)) then
            if Word'Length > Max_Length then
               Max_Length := Word'Length;
               Ordered_Words.Clear;
               Ordered_Words.Append (Word);
            elsif Word'Length = Max_Length then
               Ordered_Words.Append (Word);
            end if;
         end if;
      end;
   end loop;
   for Word of Ordered_Words loop
     Put_Line (Word);
   end loop;
   Close (File);
end Ordered_Words;
Task: Ordered words

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


#define MAXLEN 100
typedef char TWord[MAXLEN];


typedef struct Node {
    TWord word;
    struct Node *next;
} Node;


int is_ordered_word(const TWord word) {
    assert(word != NULL);
    int i;

    for (i = 0; word[i] != '\0'; i++)
        if (word[i] > word[i + 1] && word[i + 1] != '\0')
            return 0;

    return 1;
}


Node* list_prepend(Node* words_list, const TWord new_word) {
    assert(new_word != NULL);
    Node *new_node = malloc(sizeof(Node));
    if (new_node == NULL)
        exit(EXIT_FAILURE);

    strcpy(new_node->word, new_word);
    new_node->next = words_list;
    return new_node;
}


Node* list_destroy(Node *words_list) {
    while (words_list != NULL) {
        Node *temp = words_list;
        words_list = words_list->next;
        free(temp);
    }

    return words_list;
}


void list_print(Node *words_list) {
    while (words_list != NULL) {
        printf("\n%s", words_list->word);
        words_list = words_list->next;
    }
}


int main() {
    FILE *fp = fopen("unixdict.txt", "r");
    if (fp == NULL)
        return EXIT_FAILURE;

    Node *words = NULL;
    TWord line;
    unsigned int max_len = 0;

    while (fscanf(fp, "%99s\n", line) != EOF) {
        if (strlen(line) > max_len && is_ordered_word(line)) {
            max_len = strlen(line);
            words = list_destroy(words);
            words = list_prepend(words, line);
        } else if (strlen(line) == max_len && is_ordered_word(line)) {
            words = list_prepend(words, line);
        }
    }

    fclose(fp);
    list_print(words);

    return EXIT_SUCCESS;
}











Ada: with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; 
use Ada.Text_IO;

procedure Ordered_Words is
   package Word_Vectors is new Ada.Containers.Indefinite_Vectors
      (Index_Type => Positive, Element_Type => String);
   use Word_Vectors;
   File : File_Type;
   Ordered_Words : Vector;
   Max_Length : Positive := 1;
begin
   Open (File, In_File, "unixdict.txt");
   while not End_Of_File (File) loop
      declare
         Word : String := Get_Line (File);
      begin
         if (for all i in Word'First..Word'Last-1 => Word (i) <= Word(i+1)) then
            if Word'Length > Max_Length then
               Max_Length := Word'Length;
               Ordered_Words.Clear;
               Ordered_Words.Append (Word);
            elsif Word'Length = Max_Length then
               Ordered_Words.Append (Word);
            end if;
         end if;
      end;
   end loop;
   for Word of Ordered_Words loop
     Put_Line (Word);
   end loop;
   Close (File);
end Ordered_Words;
Task: Ordered words


C++: #include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

bool ordered(const std::string &word)
{
    return std::is_sorted(word.begin(), word.end()); 
}

int main()
{
    std::ifstream infile("unixdict.txt");
    if (!infile) {
        std::cerr << "Can't open word file\n";
        return -1;
    }

    std::vector<std::string> words;
    std::string word;
    int longest = 0;

    while (std::getline(infile, word)) {
        int length = word.length();
        if (length < longest) continue; 
        
        if (ordered(word)) {
            if (longest < length) {
                longest = length; 
                words.clear(); 
            }
            words.push_back(word);
        }
    }
    std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}










Ada: with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; 
use Ada.Text_IO;

procedure Ordered_Words is
   package Word_Vectors is new Ada.Containers.Indefinite_Vectors
      (Index_Type => Positive, Element_Type => String);
   use Word_Vectors;
   File : File_Type;
   Ordered_Words : Vector;
   Max_Length : Positive := 1;
begin
   Open (File, In_File, "unixdict.txt");
   while not End_Of_File (File) loop
      declare
         Word : String := Get_Line (File);
      begin
         if (for all i in Word'First..Word'Last-1 => Word (i) <= Word(i+1)) then
            if Word'Length > Max_Length then
               Max_Length := Word'Length;
               Ordered_Words.Clear;
               Ordered_Words.Append (Word);
            elsif Word'Length = Max_Length then
               Ordered_Words.Append (Word);
            end if;
         end if;
      end;
   end loop;
   for Word of Ordered_Words loop
     Put_Line (Word);
   end loop;
   Close (File);
end Ordered_Words;
Task: Ordered words




Go: package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
)

func main() {
    
    b, err := ioutil.ReadFile("unixdict.txt")
    if err != nil {
        fmt.Println(err)
        return
    }
    
    bss := bytes.Split(b, []byte{'\n'})

    
    var longest int
    var list [][]byte
    for _, bs := range bss {
        
        
        if len(bs) < longest {
            continue
        }
        
        var lastLetter byte
        for i := 0; ; i++ {
            if i == len(bs) {
                
                
                if len(bs) > longest {
                    longest = len(bs)
                    list = list[:0]
                }
                list = append(list, bs)
                break
            }
            
            b := bs[i]
            if b < 'a' || b > 'z' {
                continue 
            }
            if b < lastLetter {
                break 
            }
            
            lastLetter = b
        }
    }
    
    for _, bs := range list {
        fmt.Println(string(bs))
    }
}








Ada: with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; 
use Ada.Text_IO;

procedure Ordered_Words is
   package Word_Vectors is new Ada.Containers.Indefinite_Vectors
      (Index_Type => Positive, Element_Type => String);
   use Word_Vectors;
   File : File_Type;
   Ordered_Words : Vector;
   Max_Length : Positive := 1;
begin
   Open (File, In_File, "unixdict.txt");
   while not End_Of_File (File) loop
      declare
         Word : String := Get_Line (File);
      begin
         if (for all i in Word'First..Word'Last-1 => Word (i) <= Word(i+1)) then
            if Word'Length > Max_Length then
               Max_Length := Word'Length;
               Ordered_Words.Clear;
               Ordered_Words.Append (Word);
            elsif Word'Length = Max_Length then
               Ordered_Words.Append (Word);
            end if;
         end if;
      end;
   end loop;
   for Word of Ordered_Words loop
     Put_Line (Word);
   end loop;
   Close (File);
end Ordered_Words;
Task: Ordered words





Java: import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class Ordered {

	private static boolean isOrderedWord(String word){
		char[] sortedWord = word.toCharArray();
		Arrays.sort(sortedWord);
		return word.equals(new String(sortedWord));
	}
	
	public static void main(String[] args) throws IOException{
		List<String> orderedWords = new LinkedList<String>();
		BufferedReader in = new BufferedReader(new FileReader(args[0]));
		while(in.ready()){
			String word = in.readLine();
			if(isOrderedWord(word)) orderedWords.add(word);
		}
		in.close();
		
		Collections.<String>sort(orderedWords, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return new Integer(o2.length()).compareTo(o1.length());
			}
		});
		
		int maxLen = orderedWords.get(0).length();
		for(String word: orderedWords){
			if(word.length() == maxLen){
				System.out.println(word);
			}else{
				break;
			}
		}
	}
}







Ada: with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; 
use Ada.Text_IO;

procedure Ordered_Words is
   package Word_Vectors is new Ada.Containers.Indefinite_Vectors
      (Index_Type => Positive, Element_Type => String);
   use Word_Vectors;
   File : File_Type;
   Ordered_Words : Vector;
   Max_Length : Positive := 1;
begin
   Open (File, In_File, "unixdict.txt");
   while not End_Of_File (File) loop
      declare
         Word : String := Get_Line (File);
      begin
         if (for all i in Word'First..Word'Last-1 => Word (i) <= Word(i+1)) then
            if Word'Length > Max_Length then
               Max_Length := Word'Length;
               Ordered_Words.Clear;
               Ordered_Words.Append (Word);
            elsif Word'Length = Max_Length then
               Ordered_Words.Append (Word);
            end if;
         end if;
      end;
   end loop;
   for Word of Ordered_Words loop
     Put_Line (Word);
   end loop;
   Close (File);
end Ordered_Words;
Task: Ordered words







Python: import urllib.request

url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
words = urllib.request.urlopen(url).read().decode("utf-8").split()
ordered = [word for word in words if word==''.join(sorted(word))]
maxlen = len(max(ordered, key=len))
maxorderedwords = [word for word in ordered if len(word) == maxlen]
print(' '.join(maxorderedwords))





Ada: with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; 
use Ada.Text_IO;

procedure Ordered_Words is
   package Word_Vectors is new Ada.Containers.Indefinite_Vectors
      (Index_Type => Positive, Element_Type => String);
   use Word_Vectors;
   File : File_Type;
   Ordered_Words : Vector;
   Max_Length : Positive := 1;
begin
   Open (File, In_File, "unixdict.txt");
   while not End_Of_File (File) loop
      declare
         Word : String := Get_Line (File);
      begin
         if (for all i in Word'First..Word'Last-1 => Word (i) <= Word(i+1)) then
            if Word'Length > Max_Length then
               Max_Length := Word'Length;
               Ordered_Words.Clear;
               Ordered_Words.Append (Word);
            elsif Word'Length = Max_Length then
               Ordered_Words.Append (Word);
            end if;
         end if;
      end;
   end loop;
   for Word of Ordered_Words loop
     Put_Line (Word);
   end loop;
   Close (File);
end Ordered_Words;
Task: Ordered words








VB: Set objFSO = CreateObject("Scripting.FileSystemObject")
Set infile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\" &_
			"unixdict.txt",1)
list = ""
length = 0

Do Until inFile.AtEndOfStream
	line = infile.ReadLine
	If IsOrdered(line) Then
		If Len(line) > length Then
			length = Len(line)
			list = line & vbCrLf
		ElseIf Len(line) = length Then
			list = list & line & vbCrLf
		End If
	End If
Loop

WScript.StdOut.Write list

Function IsOrdered(word)
	IsOrdered = True
	prev_val = 0
	For i = 1 To Len(word)
		If i = 1 Then
			prev_val = Asc(Mid(word,i,1))
		ElseIf Asc(Mid(word,i,1)) >= prev_val Then
			prev_val = Asc(Mid(word,i,1))
		Else
			IsOrdered = False
			Exit For
		End If
	Next
End Function




Ada: with Ada.Text_IO, Ada.Containers.Indefinite_Vectors; 
use Ada.Text_IO;

procedure Ordered_Words is
   package Word_Vectors is new Ada.Containers.Indefinite_Vectors
      (Index_Type => Positive, Element_Type => String);
   use Word_Vectors;
   File : File_Type;
   Ordered_Words : Vector;
   Max_Length : Positive := 1;
begin
   Open (File, In_File, "unixdict.txt");
   while not End_Of_File (File) loop
      declare
         Word : String := Get_Line (File);
      begin
         if (for all i in Word'First..Word'Last-1 => Word (i) <= Word(i+1)) then
            if Word'Length > Max_Length then
               Max_Length := Word'Length;
               Ordered_Words.Clear;
               Ordered_Words.Append (Word);
            elsif Word'Length = Max_Length then
               Ordered_Words.Append (Word);
            end if;
         end if;
      end;
   end loop;
   for Word of Ordered_Words loop
     Put_Line (Word);
   end loop;
   Close (File);
end Ordered_Words;
Task: Ordered words

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


#define MAXLEN 100
typedef char TWord[MAXLEN];


typedef struct Node {
    TWord word;
    struct Node *next;
} Node;


int is_ordered_word(const TWord word) {
    assert(word != NULL);
    int i;

    for (i = 0; word[i] != '\0'; i++)
        if (word[i] > word[i + 1] && word[i + 1] != '\0')
            return 0;

    return 1;
}


Node* list_prepend(Node* words_list, const TWord new_word) {
    assert(new_word != NULL);
    Node *new_node = malloc(sizeof(Node));
    if (new_node == NULL)
        exit(EXIT_FAILURE);

    strcpy(new_node->word, new_word);
    new_node->next = words_list;
    return new_node;
}


Node* list_destroy(Node *words_list) {
    while (words_list != NULL) {
        Node *temp = words_list;
        words_list = words_list->next;
        free(temp);
    }

    return words_list;
}


void list_print(Node *words_list) {
    while (words_list != NULL) {
        printf("\n%s", words_list->word);
        words_list = words_list->next;
    }
}


int main() {
    FILE *fp = fopen("unixdict.txt", "r");
    if (fp == NULL)
        return EXIT_FAILURE;

    Node *words = NULL;
    TWord line;
    unsigned int max_len = 0;

    while (fscanf(fp, "%99s\n", line) != EOF) {
        if (strlen(line) > max_len && is_ordered_word(line)) {
            max_len = strlen(line);
            words = list_destroy(words);
            words = list_prepend(words, line);
        } else if (strlen(line) == max_len && is_ordered_word(line)) {
            words = list_prepend(words, line);
        }
    }

    fclose(fp);
    list_print(words);

    return EXIT_SUCCESS;
}
Task: Ordered words



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

static class Program
{
    static void Main(string[] args)
    {
        WebClient client = new WebClient();
        string text = client.DownloadString("http:
        string[] words = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

        var query = from w in words
                    where IsOrderedWord(w)
                    group w by w.Length into ows
                    orderby ows.Key descending
                    select ows;

        Console.WriteLine(string.Join(", ", query.First().ToArray()));
    }

    private static bool IsOrderedWord(string w)
    {
        for (int i = 1; i < w.Length; i++)
            if (w[i] < w[i - 1])
                return false;

        return true;
    }
}
Task: Ordered words


C++: #include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

bool ordered(const std::string &word)
{
    return std::is_sorted(word.begin(), word.end()); 
}

int main()
{
    std::ifstream infile("unixdict.txt");
    if (!infile) {
        std::cerr << "Can't open word file\n";
        return -1;
    }

    std::vector<std::string> words;
    std::string word;
    int longest = 0;

    while (std::getline(infile, word)) {
        int length = word.length();
        if (length < longest) continue; 
        
        if (ordered(word)) {
            if (longest < length) {
                longest = length; 
                words.clear(); 
            }
            words.push_back(word);
        }
    }
    std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
Task: Ordered words





Java: import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class Ordered {

	private static boolean isOrderedWord(String word){
		char[] sortedWord = word.toCharArray();
		Arrays.sort(sortedWord);
		return word.equals(new String(sortedWord));
	}
	
	public static void main(String[] args) throws IOException{
		List<String> orderedWords = new LinkedList<String>();
		BufferedReader in = new BufferedReader(new FileReader(args[0]));
		while(in.ready()){
			String word = in.readLine();
			if(isOrderedWord(word)) orderedWords.add(word);
		}
		in.close();
		
		Collections.<String>sort(orderedWords, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return new Integer(o2.length()).compareTo(o1.length());
			}
		});
		
		int maxLen = orderedWords.get(0).length();
		for(String word: orderedWords){
			if(word.length() == maxLen){
				System.out.println(word);
			}else{
				break;
			}
		}
	}
}
Task: Ordered words







Python: import urllib.request

url = 'http://www.puzzlers.org/pub/wordlists/unixdict.txt'
words = urllib.request.urlopen(url).read().decode("utf-8").split()
ordered = [word for word in words if word==''.join(sorted(word))]
maxlen = len(max(ordered, key=len))
maxorderedwords = [word for word in ordered if len(word) == maxlen]
print(' '.join(maxorderedwords))
Task: Ordered words








VB: Set objFSO = CreateObject("Scripting.FileSystemObject")
Set infile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\" &_
			"unixdict.txt",1)
list = ""
length = 0

Do Until inFile.AtEndOfStream
	line = infile.ReadLine
	If IsOrdered(line) Then
		If Len(line) > length Then
			length = Len(line)
			list = line & vbCrLf
		ElseIf Len(line) = length Then
			list = list & line & vbCrLf
		End If
	End If
Loop

WScript.StdOut.Write list

Function IsOrdered(word)
	IsOrdered = True
	prev_val = 0
	For i = 1 To Len(word)
		If i = 1 Then
			prev_val = Asc(Mid(word,i,1))
		ElseIf Asc(Mid(word,i,1)) >= prev_val Then
			prev_val = Asc(Mid(word,i,1))
		Else
			IsOrdered = False
			Exit For
		End If
	Next
End Function
Task: Ordered words




Go: package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
)

func main() {
    
    b, err := ioutil.ReadFile("unixdict.txt")
    if err != nil {
        fmt.Println(err)
        return
    }
    
    bss := bytes.Split(b, []byte{'\n'})

    
    var longest int
    var list [][]byte
    for _, bs := range bss {
        
        
        if len(bs) < longest {
            continue
        }
        
        var lastLetter byte
        for i := 0; ; i++ {
            if i == len(bs) {
                
                
                if len(bs) > longest {
                    longest = len(bs)
                    list = list[:0]
                }
                list = append(list, bs)
                break
            }
            
            b := bs[i]
            if b < 'a' || b > 'z' {
                continue 
            }
            if b < lastLetter {
                break 
            }
            
            lastLetter = b
        }
    }
    
    for _, bs := range list {
        fmt.Println(string(bs))
    }
}
Task: Ordered words



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

static class Program
{
    static void Main(string[] args)
    {
        WebClient client = new WebClient();
        string text = client.DownloadString("http:
        string[] words = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

        var query = from w in words
                    where IsOrderedWord(w)
                    group w by w.Length into ows
                    orderby ows.Key descending
                    select ows;

        Console.WriteLine(string.Join(", ", query.First().ToArray()));
    }

    private static bool IsOrderedWord(string w)
    {
        for (int i = 1; i < w.Length; i++)
            if (w[i] < w[i - 1])
                return false;

        return true;
    }
}








Fortran: 
 module ordered_module

 implicit none 

	
	integer,parameter :: file_unit = 1000
	character(len=*),parameter :: filename = 'unixdict.txt'

	
	integer,parameter :: max_chars = 50

	type word 
	  character(len=max_chars) :: str    
	  integer	:: n = 0    
	  logical	:: ordered = .false.    
	end type word
 
	
	type(word),dimension(:),allocatable :: dict
 
	contains

 
	
		function count_lines_in_file(fid) result(n_lines)
	
		implicit none
 
		integer             :: n_lines
		integer,intent(in)  :: fid		
		character(len=1)    :: tmp
		integer             :: i
		integer             :: ios
 
		
 
		rewind(fid)	  
 
		n_lines = 0
		do 
			read(fid,'(A1)',iostat=ios) tmp
			if (ios < 0) exit      
			n_lines = n_lines + 1  
		end do
 
		rewind(fid)   
 
	
		end function count_lines_in_file
	

	
	 pure elemental function ordered_word(word) result(yn)
	
	
	
	 
	 implicit none
	 character(len=*),intent(in) :: word
	 logical :: yn
	 
	 integer :: i
	 
	 yn = .true.
	 do i=1,len_trim(word)-1
	 	if (ichar(word(i+1:i+1))<ichar(word(i:i))) then
	 		yn = .false.
	 		exit
	 	end if
	 end do
	 
	
	 end function ordered_word
	
 

 end module ordered_module



 program main

 use ordered_module
 implicit none

	integer :: i,n,n_max
 
	
	open(unit=file_unit,file=filename)     		
	n = count_lines_in_file(file_unit)      
	allocate(dict(n))                       
	do i=1,n                                
		read(file_unit,'(A)') dict(i)%str   
		dict(i)%n = len_trim(dict(i)%str)   
	end do		
	close(file_unit)                        

	
	dict%ordered = ordered_word(dict%str)	

	
	n_max = maxval(dict%n, mask=dict%ordered)
		
	
	do i=1,n
		if (dict(i)%ordered .and. dict(i)%n==n_max) write(*,'(A,A)',advance='NO') trim(dict(i)%str),' '
	end do
	write(*,*) ''


 end program main