WeixiangYan/CodeTransOcean huggingface dataset:
Task: Largest int from concatenated ints



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

class Program
{
    static void Main(string[] args)
    {
        var source1 = new int[] { 1, 34, 3, 98, 9, 76, 45, 4 };
        var source2 = new int[] { 54, 546, 548, 60 };

        var largest1 = LargestPossibleSequence(source1);
        var largest2 = LargestPossibleSequence(source2);

        Console.WriteLine("The largest possible integer from set 1 is: {0}", largest1);
        Console.WriteLine("The largest possible integer from set 2 is: {0}", largest2);
    }

    static long LargestPossibleSequence(int[] ints)
    {
        return long.Parse(string.Join("", ints.OrderBy(i => i, new IntConcatenationComparer()).Reverse()));
    }
}

class IntConcatenationComparer : IComparer<int>
{
    public int Compare(int x, int y)
    {
        var xy = int.Parse(x.ToString() + y.ToString());
        var yx = int.Parse(y.ToString() + x.ToString());

        return xy - yx;
    }
}




PHP: function maxnum($nums) {
    usort($nums,  function ($x, $y) { return strcmp("$y$x", "$x$y"); });
    return implode('', $nums);
}

echo maxnum(array(1, 34, 3, 98, 9, 76, 45, 4)), "\n";
echo maxnum(array(54, 546, 548, 60)), "\n";
Task: Largest int from concatenated ints


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

std::string findLargestConcat ( std::vector< int > & mynumbers ) {
   std::vector<std::string> concatnumbers ;
   std::sort ( mynumbers.begin( ) , mynumbers.end( ) ) ;
   do {
      std::ostringstream numberstream ;
      for ( int i : mynumbers ) 
	 numberstream << i ;
      concatnumbers.push_back( numberstream.str( ) ) ;
   } while ( std::next_permutation( mynumbers.begin( ) ,
	    mynumbers.end( ) )) ;
   return *( std::max_element( concatnumbers.begin( ) ,
	 concatnumbers.end( ) ) ) ;
}

int main( ) {
   std::vector<int> mynumbers = { 98, 76 , 45 , 34, 9 , 4 , 3 , 1 } ;
   std::vector<int> othernumbers = { 54 , 546 , 548 , 60 } ;
   std::cout << "The largest concatenated int is " <<
      findLargestConcat( mynumbers ) << " !\n" ;
   std::cout << "And here it is " << findLargestConcat( othernumbers ) 
      << " !\n" ;
   return 0 ;
}





PHP: function maxnum($nums) {
    usort($nums,  function ($x, $y) { return strcmp("$y$x", "$x$y"); });
    return implode('', $nums);
}

echo maxnum(array(1, 34, 3, 98, 9, 76, 45, 4)), "\n";
echo maxnum(array(54, 546, 548, 60)), "\n";
Task: Largest int from concatenated ints




Go: 

package main

import (
    "fmt"
    "math/big"
    "sort"
    "strconv"
    "strings"
)

type c struct {
    i     int
    s, rs string
}

type cc []*c

func (c cc) Len() int           { return len(c) }
func (c cc) Less(i, j int) bool { return c[j].rs < c[i].rs }
func (c cc) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }


func li(is ...int) *big.Int {
    ps := make(cc, len(is))
    ss := make([]c, len(is))
    ml := 0
    for j, i := range is {
        p := &ss[j]
        ps[j] = p
        p.i = i
        p.s = strconv.Itoa(i)
        if len(p.s) > ml {
            ml = len(p.s)
        }
    }
    for _, p := range ps {
        p.rs = strings.Repeat(p.s, (ml+len(p.s)-1)/len(p.s))
    }
    sort.Sort(ps)
    s := make([]string, len(ps))
    for i, p := range ps {
        s[i] = p.s
    }
    b, _ := new(big.Int).SetString(strings.Join(s, ""), 10)
    return b
}

func main() {
    fmt.Println(li(1, 34, 3, 98, 9, 76, 45, 4))
    fmt.Println(li(54, 546, 548, 60))
}



PHP: function maxnum($nums) {
    usort($nums,  function ($x, $y) { return strcmp("$y$x", "$x$y"); });
    return implode('', $nums);
}

echo maxnum(array(1, 34, 3, 98, 9, 76, 45, 4)), "\n";
echo maxnum(array(54, 546, 548, 60)), "\n";
Task: Largest int from concatenated ints






PHP: function maxnum($nums) {
    usort($nums,  function ($x, $y) { return strcmp("$y$x", "$x$y"); });
    return implode('', $nums);
}

echo maxnum(array(1, 34, 3, 98, 9, 76, 45, 4)), "\n";
echo maxnum(array(54, 546, 548, 60)), "\n";


Python: try:
    cmp     
    def maxnum(x):
        return ''.join(sorted((str(n) for n in x),
                              cmp=lambda x,y:cmp(y+x, x+y)))
except NameError:
    
    from functools import cmp_to_key
    def cmp(x, y):
        return -1 if x<y else ( 0 if x==y else 1)
    def maxnum(x):
        return ''.join(sorted((str(n) for n in x),
                              key=cmp_to_key(lambda x,y:cmp(y+x, x+y))))

for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]:
    print('Numbers: %r\n  Largest integer: %15s' % (numbers, maxnum(numbers)))
Task: Largest int from concatenated ints






PHP: function maxnum($nums) {
    usort($nums,  function ($x, $y) { return strcmp("$y$x", "$x$y"); });
    return implode('', $nums);
}

echo maxnum(array(1, 34, 3, 98, 9, 76, 45, 4)), "\n";
echo maxnum(array(54, 546, 548, 60)), "\n";



VB: Function largestint(list)
	nums = Split(list,",")
	Do Until IsSorted = True
		IsSorted = True
		For i = 0 To UBound(nums)
			If i <> UBound(nums) Then
				a = nums(i)
				b = nums(i+1)
				If CLng(a&b) < CLng(b&a) Then
					tmpnum = nums(i)
					nums(i) = nums(i+1)
					nums(i+1) = tmpnum
					IsSorted = False
				End If
			End If
		Next
	Loop
	For j = 0 To UBound(nums)
		largestint = largestint & nums(j)
	Next
End Function

WScript.StdOut.Write largestint(WScript.Arguments(0))
WScript.StdOut.WriteLine
Task: Longest common prefix

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

char* lcp(int num,...){
	va_list vaList,vaList2;
	int i,j,len,min;
	char* dest;
	char** strings = (char**)malloc(num*sizeof(char*));
	
	va_start(vaList,num);
	va_start(vaList2,num);
	
	for(i=0;i<num;i++){
		len = strlen(va_arg(vaList,char*));
		strings[i] = (char*)malloc((len + 1)*sizeof(char));
		
		strcpy(strings[i],va_arg(vaList2,char*));
		
		if(i==0)
			min = len;
		else if(len<min)
			min = len;
	}
	
	if(min==0)
		return "";
	
	for(i=0;i<min;i++){
		for(j=1;j<num;j++){
			if(strings[j][i]!=strings[0][i]){
				if(i==0)
					return "";
				else{
					dest = (char*)malloc(i*sizeof(char));
					strncpy(dest,strings[0],i-1);
					return dest;
				}
			}
		}
	}
	
	dest = (char*)malloc((min+1)*sizeof(char));
	strncpy(dest,strings[0],min);
	return dest;
}

int main(){

	printf("\nLongest common prefix : %s",lcp(3,"interspecies","interstellar","interstate"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","throne"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","dungeon"));
        printf("\nLongest common prefix : %s",lcp(3,"throne","","throne"));
        printf("\nLongest common prefix : %s",lcp(1,"cheese"));
        printf("\nLongest common prefix : %s",lcp(1,""));
        printf("\nLongest common prefix : %s",lcp(0,NULL));
        printf("\nLongest common prefix : %s",lcp(2,"prefix","suffix"));
        printf("\nLongest common prefix : %s",lcp(2,"foo","foobar"));
	return 0;
}





Java: public class LCP {
    public static String lcp(String... list){
        if(list == null) return "";
        String ret = "";
        int idx = 0;

        while(true){
            char thisLetter = 0;
            for(String word : list){
                if(idx == word.length()){ 
                    return ret;
                }
                if(thisLetter == 0){ 
                    thisLetter = word.charAt(idx);
                }
                if(thisLetter != word.charAt(idx)){ 
                    return ret;
                }
            }
            ret += thisLetter;
            idx++;
        }
    }
    
    public static void main(String[] args){
        System.out.println(lcp("interspecies","interstellar","interstate"));
        System.out.println(lcp("throne","throne"));
        System.out.println(lcp("throne","dungeon"));
        System.out.println(lcp("throne","","throne"));
        System.out.println(lcp("cheese"));
        System.out.println(lcp(""));
        System.out.println(lcp(null));
        System.out.println(lcp("prefix","suffix"));
        System.out.println(lcp("foo","foobar"));
    }
}
Task: Longest common prefix

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

char* lcp(int num,...){
	va_list vaList,vaList2;
	int i,j,len,min;
	char* dest;
	char** strings = (char**)malloc(num*sizeof(char*));
	
	va_start(vaList,num);
	va_start(vaList2,num);
	
	for(i=0;i<num;i++){
		len = strlen(va_arg(vaList,char*));
		strings[i] = (char*)malloc((len + 1)*sizeof(char));
		
		strcpy(strings[i],va_arg(vaList2,char*));
		
		if(i==0)
			min = len;
		else if(len<min)
			min = len;
	}
	
	if(min==0)
		return "";
	
	for(i=0;i<min;i++){
		for(j=1;j<num;j++){
			if(strings[j][i]!=strings[0][i]){
				if(i==0)
					return "";
				else{
					dest = (char*)malloc(i*sizeof(char));
					strncpy(dest,strings[0],i-1);
					return dest;
				}
			}
		}
	}
	
	dest = (char*)malloc((min+1)*sizeof(char));
	strncpy(dest,strings[0],min);
	return dest;
}

int main(){

	printf("\nLongest common prefix : %s",lcp(3,"interspecies","interstellar","interstate"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","throne"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","dungeon"));
        printf("\nLongest common prefix : %s",lcp(3,"throne","","throne"));
        printf("\nLongest common prefix : %s",lcp(1,"cheese"));
        printf("\nLongest common prefix : %s",lcp(1,""));
        printf("\nLongest common prefix : %s",lcp(0,NULL));
        printf("\nLongest common prefix : %s",lcp(2,"prefix","suffix"));
        printf("\nLongest common prefix : %s",lcp(2,"foo","foobar"));
	return 0;
}







Python: import os.path

def lcp(*s):
    return os.path.commonprefix(s)

assert lcp("interspecies","interstellar","interstate") == "inters"
assert lcp("throne","throne") == "throne"
assert lcp("throne","dungeon") == ""
assert lcp("cheese") == "cheese"
assert lcp("") == ""
assert lcp("prefix","suffix") == ""
assert lcp("foo","foobar") == "foo"
Task: Longest common prefix

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

char* lcp(int num,...){
	va_list vaList,vaList2;
	int i,j,len,min;
	char* dest;
	char** strings = (char**)malloc(num*sizeof(char*));
	
	va_start(vaList,num);
	va_start(vaList2,num);
	
	for(i=0;i<num;i++){
		len = strlen(va_arg(vaList,char*));
		strings[i] = (char*)malloc((len + 1)*sizeof(char));
		
		strcpy(strings[i],va_arg(vaList2,char*));
		
		if(i==0)
			min = len;
		else if(len<min)
			min = len;
	}
	
	if(min==0)
		return "";
	
	for(i=0;i<min;i++){
		for(j=1;j<num;j++){
			if(strings[j][i]!=strings[0][i]){
				if(i==0)
					return "";
				else{
					dest = (char*)malloc(i*sizeof(char));
					strncpy(dest,strings[0],i-1);
					return dest;
				}
			}
		}
	}
	
	dest = (char*)malloc((min+1)*sizeof(char));
	strncpy(dest,strings[0],min);
	return dest;
}

int main(){

	printf("\nLongest common prefix : %s",lcp(3,"interspecies","interstellar","interstate"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","throne"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","dungeon"));
        printf("\nLongest common prefix : %s",lcp(3,"throne","","throne"));
        printf("\nLongest common prefix : %s",lcp(1,"cheese"));
        printf("\nLongest common prefix : %s",lcp(1,""));
        printf("\nLongest common prefix : %s",lcp(0,NULL));
        printf("\nLongest common prefix : %s",lcp(2,"prefix","suffix"));
        printf("\nLongest common prefix : %s",lcp(2,"foo","foobar"));
	return 0;
}








VB: Function lcp(s)
	
	str = Split(s,",")
	
	For i = 0 To UBound(str)
		If i = 0 Then
			l = Len(str(i))
		ElseIf Len(str(i)) < l Then
			l = Len(str(i))
		End If
	Next
	
	idx = 0
	For j = 1 To l
		For k = 0 To UBound(str)
			If UBound(str) = 0 Then
				idx = Len(str(0))
			Else
				If k = 0 Then
					tstr = Mid(str(k),j,1)
				ElseIf k <> UBound(str) Then
					If Mid(str(k),j,1) <> tstr Then
						Exit For
					End If
				Else
					If Mid(str(k),j,1) <> tstr Then
						Exit For
					Else
						idx = idx + 1
					End If
				End If	
			End If
		Next
		If idx = 0 Then
			Exit For
		End If
	Next
	
	If idx = 0 Then
		lcp = "No Matching Prefix"
	Else
		lcp = Mid(str(0),1,idx)
	End If
End Function


test = Array("interspecies,interstellar,interstate","throne,throne","throne,dungeon","cheese",_
		"","prefix,suffix")
		
For n = 0 To UBound(test)
	WScript.StdOut.Write "Test case " & n & " " & test(n) & " = " & lcp(test(n))
	WScript.StdOut.WriteLine
Next
Task: Longest common prefix



C#: using System;

namespace LCP {
    class Program {
        public static string LongestCommonPrefix(params string[] sa) {
            if (null == sa) return ""; 
            string ret = "";
            int idx = 0;

            while (true) {
                char thisLetter = '\0';
                foreach (var word in sa) {
                    if (idx == word.Length) {
                        
                        return ret;
                    }
                    if (thisLetter == '\0') {
                        
                        thisLetter = word[idx];
                    }
                    if (thisLetter != word[idx]) {
                        return ret;
                    }
                }

                
                ret += thisLetter;
                idx++;
            }
        }

        static void Main(string[] args) {
            Console.WriteLine(LongestCommonPrefix("interspecies", "interstellar", "interstate"));
            Console.WriteLine(LongestCommonPrefix("throne", "throne"));
            Console.WriteLine(LongestCommonPrefix("throne", "dungeon"));
            Console.WriteLine(LongestCommonPrefix("throne", "", "throne"));
            Console.WriteLine(LongestCommonPrefix("cheese"));
            Console.WriteLine(LongestCommonPrefix(""));
            Console.WriteLine(LongestCommonPrefix(null));
            Console.WriteLine(LongestCommonPrefix("prefix", "suffix"));
            Console.WriteLine(LongestCommonPrefix("foo", "foobar"));
        }
    }
}





Python: import os.path

def lcp(*s):
    return os.path.commonprefix(s)

assert lcp("interspecies","interstellar","interstate") == "inters"
assert lcp("throne","throne") == "throne"
assert lcp("throne","dungeon") == ""
assert lcp("cheese") == "cheese"
assert lcp("") == ""
assert lcp("prefix","suffix") == ""
assert lcp("foo","foobar") == "foo"
Task: Longest common prefix





Java: public class LCP {
    public static String lcp(String... list){
        if(list == null) return "";
        String ret = "";
        int idx = 0;

        while(true){
            char thisLetter = 0;
            for(String word : list){
                if(idx == word.length()){ 
                    return ret;
                }
                if(thisLetter == 0){ 
                    thisLetter = word.charAt(idx);
                }
                if(thisLetter != word.charAt(idx)){ 
                    return ret;
                }
            }
            ret += thisLetter;
            idx++;
        }
    }
    
    public static void main(String[] args){
        System.out.println(lcp("interspecies","interstellar","interstate"));
        System.out.println(lcp("throne","throne"));
        System.out.println(lcp("throne","dungeon"));
        System.out.println(lcp("throne","","throne"));
        System.out.println(lcp("cheese"));
        System.out.println(lcp(""));
        System.out.println(lcp(null));
        System.out.println(lcp("prefix","suffix"));
        System.out.println(lcp("foo","foobar"));
    }
}



Python: import os.path

def lcp(*s):
    return os.path.commonprefix(s)

assert lcp("interspecies","interstellar","interstate") == "inters"
assert lcp("throne","throne") == "throne"
assert lcp("throne","dungeon") == ""
assert lcp("cheese") == "cheese"
assert lcp("") == ""
assert lcp("prefix","suffix") == ""
assert lcp("foo","foobar") == "foo"
Task: Longest common prefix





Java: public class LCP {
    public static String lcp(String... list){
        if(list == null) return "";
        String ret = "";
        int idx = 0;

        while(true){
            char thisLetter = 0;
            for(String word : list){
                if(idx == word.length()){ 
                    return ret;
                }
                if(thisLetter == 0){ 
                    thisLetter = word.charAt(idx);
                }
                if(thisLetter != word.charAt(idx)){ 
                    return ret;
                }
            }
            ret += thisLetter;
            idx++;
        }
    }
    
    public static void main(String[] args){
        System.out.println(lcp("interspecies","interstellar","interstate"));
        System.out.println(lcp("throne","throne"));
        System.out.println(lcp("throne","dungeon"));
        System.out.println(lcp("throne","","throne"));
        System.out.println(lcp("cheese"));
        System.out.println(lcp(""));
        System.out.println(lcp(null));
        System.out.println(lcp("prefix","suffix"));
        System.out.println(lcp("foo","foobar"));
    }
}




VB: Function lcp(s)
	
	str = Split(s,",")
	
	For i = 0 To UBound(str)
		If i = 0 Then
			l = Len(str(i))
		ElseIf Len(str(i)) < l Then
			l = Len(str(i))
		End If
	Next
	
	idx = 0
	For j = 1 To l
		For k = 0 To UBound(str)
			If UBound(str) = 0 Then
				idx = Len(str(0))
			Else
				If k = 0 Then
					tstr = Mid(str(k),j,1)
				ElseIf k <> UBound(str) Then
					If Mid(str(k),j,1) <> tstr Then
						Exit For
					End If
				Else
					If Mid(str(k),j,1) <> tstr Then
						Exit For
					Else
						idx = idx + 1
					End If
				End If	
			End If
		Next
		If idx = 0 Then
			Exit For
		End If
	Next
	
	If idx = 0 Then
		lcp = "No Matching Prefix"
	Else
		lcp = Mid(str(0),1,idx)
	End If
End Function


test = Array("interspecies,interstellar,interstate","throne,throne","throne,dungeon","cheese",_
		"","prefix,suffix")
		
For n = 0 To UBound(test)
	WScript.StdOut.Write "Test case " & n & " " & test(n) & " = " & lcp(test(n))
	WScript.StdOut.WriteLine
Next
Task: Longest common prefix







Python: import os.path

def lcp(*s):
    return os.path.commonprefix(s)

assert lcp("interspecies","interstellar","interstate") == "inters"
assert lcp("throne","throne") == "throne"
assert lcp("throne","dungeon") == ""
assert lcp("cheese") == "cheese"
assert lcp("") == ""
assert lcp("prefix","suffix") == ""
assert lcp("foo","foobar") == "foo"


VB: Function lcp(s)
	
	str = Split(s,",")
	
	For i = 0 To UBound(str)
		If i = 0 Then
			l = Len(str(i))
		ElseIf Len(str(i)) < l Then
			l = Len(str(i))
		End If
	Next
	
	idx = 0
	For j = 1 To l
		For k = 0 To UBound(str)
			If UBound(str) = 0 Then
				idx = Len(str(0))
			Else
				If k = 0 Then
					tstr = Mid(str(k),j,1)
				ElseIf k <> UBound(str) Then
					If Mid(str(k),j,1) <> tstr Then
						Exit For
					End If
				Else
					If Mid(str(k),j,1) <> tstr Then
						Exit For
					Else
						idx = idx + 1
					End If
				End If	
			End If
		Next
		If idx = 0 Then
			Exit For
		End If
	Next
	
	If idx = 0 Then
		lcp = "No Matching Prefix"
	Else
		lcp = Mid(str(0),1,idx)
	End If
End Function


test = Array("interspecies,interstellar,interstate","throne,throne","throne,dungeon","cheese",_
		"","prefix,suffix")
		
For n = 0 To UBound(test)
	WScript.StdOut.Write "Test case " & n & " " & test(n) & " = " & lcp(test(n))
	WScript.StdOut.WriteLine
Next
Task: Longest common prefix

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

char* lcp(int num,...){
	va_list vaList,vaList2;
	int i,j,len,min;
	char* dest;
	char** strings = (char**)malloc(num*sizeof(char*));
	
	va_start(vaList,num);
	va_start(vaList2,num);
	
	for(i=0;i<num;i++){
		len = strlen(va_arg(vaList,char*));
		strings[i] = (char*)malloc((len + 1)*sizeof(char));
		
		strcpy(strings[i],va_arg(vaList2,char*));
		
		if(i==0)
			min = len;
		else if(len<min)
			min = len;
	}
	
	if(min==0)
		return "";
	
	for(i=0;i<min;i++){
		for(j=1;j<num;j++){
			if(strings[j][i]!=strings[0][i]){
				if(i==0)
					return "";
				else{
					dest = (char*)malloc(i*sizeof(char));
					strncpy(dest,strings[0],i-1);
					return dest;
				}
			}
		}
	}
	
	dest = (char*)malloc((min+1)*sizeof(char));
	strncpy(dest,strings[0],min);
	return dest;
}

int main(){

	printf("\nLongest common prefix : %s",lcp(3,"interspecies","interstellar","interstate"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","throne"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","dungeon"));
        printf("\nLongest common prefix : %s",lcp(3,"throne","","throne"));
        printf("\nLongest common prefix : %s",lcp(1,"cheese"));
        printf("\nLongest common prefix : %s",lcp(1,""));
        printf("\nLongest common prefix : %s",lcp(0,NULL));
        printf("\nLongest common prefix : %s",lcp(2,"prefix","suffix"));
        printf("\nLongest common prefix : %s",lcp(2,"foo","foobar"));
	return 0;
}



C#: using System;

namespace LCP {
    class Program {
        public static string LongestCommonPrefix(params string[] sa) {
            if (null == sa) return ""; 
            string ret = "";
            int idx = 0;

            while (true) {
                char thisLetter = '\0';
                foreach (var word in sa) {
                    if (idx == word.Length) {
                        
                        return ret;
                    }
                    if (thisLetter == '\0') {
                        
                        thisLetter = word[idx];
                    }
                    if (thisLetter != word[idx]) {
                        return ret;
                    }
                }

                
                ret += thisLetter;
                idx++;
            }
        }

        static void Main(string[] args) {
            Console.WriteLine(LongestCommonPrefix("interspecies", "interstellar", "interstate"));
            Console.WriteLine(LongestCommonPrefix("throne", "throne"));
            Console.WriteLine(LongestCommonPrefix("throne", "dungeon"));
            Console.WriteLine(LongestCommonPrefix("throne", "", "throne"));
            Console.WriteLine(LongestCommonPrefix("cheese"));
            Console.WriteLine(LongestCommonPrefix(""));
            Console.WriteLine(LongestCommonPrefix(null));
            Console.WriteLine(LongestCommonPrefix("prefix", "suffix"));
            Console.WriteLine(LongestCommonPrefix("foo", "foobar"));
        }
    }
}