WeixiangYan/CodeTransOcean huggingface dataset:
Task: Iterated digits squaring



C#: using System;
public static class IteratedDigitsSquaring
{
    public static void Main() {
        Console.WriteLine(Count89s(1_000_000));
        Console.WriteLine(Count89s(100_000_000));
    }

    public static int Count89s(int limit) {
        if (limit < 1) return 0;
        int[] end = new int[Math.Min(limit, 9 * 9 * 9 + 2)];
        int result = 0;

        for (int i = 1; i < end.Length; i++) {
            for (end[i] = i; end[i] != 1 && end[i] != 89; end[i] = SquareDigitSum(end[i])) { }
            if (end[i] == 89) result++;
        }
        for (int i = end.Length; i < limit; i++) {
            if (end[SquareDigitSum(i)] == 89) result++;
        }
        return result;

        int SquareDigitSum(int n) {
            int sum = 0;
            while (n > 0) {
                int digit = n % 10;
                sum += digit * digit;
                n /= 10;
            }
            return sum;
        }
    }

}





Python: >>> step = lambda x: sum(int(d) ** 2 for d in str(x))
>>> iterate = lambda x: x if x in [1, 89] else iterate(step(x))
>>> [iterate(x) for x in xrange(1, 20)]
[1, 89, 89, 89, 89, 89, 1, 89, 89, 1, 89, 89, 1, 89, 89, 89, 89, 89, 1]
Task: Iterated digits squaring



C#: using System;
public static class IteratedDigitsSquaring
{
    public static void Main() {
        Console.WriteLine(Count89s(1_000_000));
        Console.WriteLine(Count89s(100_000_000));
    }

    public static int Count89s(int limit) {
        if (limit < 1) return 0;
        int[] end = new int[Math.Min(limit, 9 * 9 * 9 + 2)];
        int result = 0;

        for (int i = 1; i < end.Length; i++) {
            for (end[i] = i; end[i] != 1 && end[i] != 89; end[i] = SquareDigitSum(end[i])) { }
            if (end[i] == 89) result++;
        }
        for (int i = end.Length; i < limit; i++) {
            if (end[SquareDigitSum(i)] == 89) result++;
        }
        return result;

        int SquareDigitSum(int n) {
            int sum = 0;
            while (n > 0) {
                int digit = n % 10;
                sum += digit * digit;
                n /= 10;
            }
            return sum;
        }
    }

}






VB: start_time = Now
cnt = 0
For i = 1 To 100000000
	n = i
	sum = 0
	Do Until n = 1 Or n = 89
		For j = 1 To Len(n)
			sum = sum + (CLng(Mid(n,j,1))^2)
		Next
		n = sum
		sum = 0
	Loop
	If n = 89 Then
		cnt = cnt + 1
	End If
Next
end_time = Now

WScript.Echo "Elapse Time: " & DateDiff("s",start_time,end_time) &_
			vbCrLf & "Count: " & cnt
Task: Iterated digits squaring





Java: import java.util.stream.IntStream;

public class IteratedDigitsSquaring {

    public static void main(String[] args) {
        long r = IntStream.range(1, 100_000_000)
                .parallel()
                .filter(n -> calc(n) == 89)
                .count();
        System.out.println(r);
    }

    private static int calc(int n) {
        while (n != 89 && n != 1) {
            int total = 0;
            while (n > 0) {
                total += Math.pow(n % 10, 2);
                n /= 10;
            }
            n = total;
        }
        return n;
    }
}




VB: start_time = Now
cnt = 0
For i = 1 To 100000000
	n = i
	sum = 0
	Do Until n = 1 Or n = 89
		For j = 1 To Len(n)
			sum = sum + (CLng(Mid(n,j,1))^2)
		Next
		n = sum
		sum = 0
	Loop
	If n = 89 Then
		cnt = cnt + 1
	End If
Next
end_time = Now

WScript.Echo "Elapse Time: " & DateDiff("s",start_time,end_time) &_
			vbCrLf & "Count: " & cnt
Task: Iterated digits squaring




Go: package main

import (
	"fmt"
)

func main() {
	var d, n, o, u, u89 int64

	for n = 1; n < 100000000; n++ {
		o = n
		for {
			u = 0
			for {
				d = o%10
				o = (o - d) / 10
				u += d*d
				if o == 0 {
					break 
				}
			}
			if u == 89 || u == 1 {
				if u == 89 { u89++ }
				break
			}
			o = u
		}
	}
	fmt.Println(u89)
}




Python: >>> step = lambda x: sum(int(d) ** 2 for d in str(x))
>>> iterate = lambda x: x if x in [1, 89] else iterate(step(x))
>>> [iterate(x) for x in xrange(1, 20)]
[1, 89, 89, 89, 89, 89, 1, 89, 89, 1, 89, 89, 1, 89, 89, 89, 89, 89, 1]
Task: Iterated digits squaring




Go: package main

import (
	"fmt"
)

func main() {
	var d, n, o, u, u89 int64

	for n = 1; n < 100000000; n++ {
		o = n
		for {
			u = 0
			for {
				d = o%10
				o = (o - d) / 10
				u += d*d
				if o == 0 {
					break 
				}
			}
			if u == 89 || u == 1 {
				if u == 89 { u89++ }
				break
			}
			o = u
		}
	}
	fmt.Println(u89)
}





VB: start_time = Now
cnt = 0
For i = 1 To 100000000
	n = i
	sum = 0
	Do Until n = 1 Or n = 89
		For j = 1 To Len(n)
			sum = sum + (CLng(Mid(n,j,1))^2)
		Next
		n = sum
		sum = 0
	Loop
	If n = 89 Then
		cnt = cnt + 1
	End If
Next
end_time = Now

WScript.Echo "Elapse Time: " & DateDiff("s",start_time,end_time) &_
			vbCrLf & "Count: " & cnt
Task: Iterated digits squaring

C: #include <stdio.h>

typedef unsigned long long ull;

int is89(int x)
{
	while (1) {
		int s = 0;
		do s += (x%10)*(x%10); while ((x /= 10));

		if (s == 89) return 1;
		if (s == 1) return 0;
		x = s;
	}
}


int main(void)
{
	
	ull sums[32*81 + 1] = {1, 0};

	for (int n = 1; ; n++) {
		for (int i = n*81; i; i--) {
			for (int j = 1; j < 10; j++) {
				int s = j*j;
				if (s > i) break;
				sums[i] += sums[i-s];
			}
		}

		ull count89 = 0;
		for (int i = 1; i < n*81 + 1; i++) {
			if (!is89(i)) continue;

			if (sums[i] > ~0ULL - count89) {
				printf("counter overflow for 10^%d\n", n);
				return 0;
			}
			count89 += sums[i];
		}

		printf("1->10^%d: %llu\n", n, count89);
	}

	return 0;
}


C++: #include <iostream>


unsigned int sum_square_digits(unsigned int n) {
        int i,num=n,sum=0;
        
        while (num > 0) {
                
                int digit=num % 10;
                num=(num - digit)/10;
                
                sum+=digit*digit;
        }
        return sum;
}
int main(void) {
        unsigned int i=0,result=0, count=0;
        for (i=1; i<=100000000; i++) {
                
                if ((i != 1) || (i != 89)) {
                        result = sum_square_digits(i);
                }
                
                else {
                        result = i;
                }
                
                while ((result != 1) && (result != 89)) {
                        result = sum_square_digits(result);
                }
                if (result == 89) {
                        count++;
                }
        }
        std::cout << count << std::endl;
        return 0;
}
Task: Iterated digits squaring

C: #include <stdio.h>

typedef unsigned long long ull;

int is89(int x)
{
	while (1) {
		int s = 0;
		do s += (x%10)*(x%10); while ((x /= 10));

		if (s == 89) return 1;
		if (s == 1) return 0;
		x = s;
	}
}


int main(void)
{
	
	ull sums[32*81 + 1] = {1, 0};

	for (int n = 1; ; n++) {
		for (int i = n*81; i; i--) {
			for (int j = 1; j < 10; j++) {
				int s = j*j;
				if (s > i) break;
				sums[i] += sums[i-s];
			}
		}

		ull count89 = 0;
		for (int i = 1; i < n*81 + 1; i++) {
			if (!is89(i)) continue;

			if (sums[i] > ~0ULL - count89) {
				printf("counter overflow for 10^%d\n", n);
				return 0;
			}
			count89 += sums[i];
		}

		printf("1->10^%d: %llu\n", n, count89);
	}

	return 0;
}







Python: >>> step = lambda x: sum(int(d) ** 2 for d in str(x))
>>> iterate = lambda x: x if x in [1, 89] else iterate(step(x))
>>> [iterate(x) for x in xrange(1, 20)]
[1, 89, 89, 89, 89, 89, 1, 89, 89, 1, 89, 89, 1, 89, 89, 89, 89, 89, 1]
Task: Iterated digits squaring


C++: #include <iostream>


unsigned int sum_square_digits(unsigned int n) {
        int i,num=n,sum=0;
        
        while (num > 0) {
                
                int digit=num % 10;
                num=(num - digit)/10;
                
                sum+=digit*digit;
        }
        return sum;
}
int main(void) {
        unsigned int i=0,result=0, count=0;
        for (i=1; i<=100000000; i++) {
                
                if ((i != 1) || (i != 89)) {
                        result = sum_square_digits(i);
                }
                
                else {
                        result = i;
                }
                
                while ((result != 1) && (result != 89)) {
                        result = sum_square_digits(result);
                }
                if (result == 89) {
                        count++;
                }
        }
        std::cout << count << std::endl;
        return 0;
}



Go: package main

import (
	"fmt"
)

func main() {
	var d, n, o, u, u89 int64

	for n = 1; n < 100000000; n++ {
		o = n
		for {
			u = 0
			for {
				d = o%10
				o = (o - d) / 10
				u += d*d
				if o == 0 {
					break 
				}
			}
			if u == 89 || u == 1 {
				if u == 89 { u89++ }
				break
			}
			o = u
		}
	}
	fmt.Println(u89)
}
Task: Iterated digits squaring




Go: package main

import (
	"fmt"
)

func main() {
	var d, n, o, u, u89 int64

	for n = 1; n < 100000000; n++ {
		o = n
		for {
			u = 0
			for {
				d = o%10
				o = (o - d) / 10
				u += d*d
				if o == 0 {
					break 
				}
			}
			if u == 89 || u == 1 {
				if u == 89 { u89++ }
				break
			}
			o = u
		}
	}
	fmt.Println(u89)
}


Java: import java.util.stream.IntStream;

public class IteratedDigitsSquaring {

    public static void main(String[] args) {
        long r = IntStream.range(1, 100_000_000)
                .parallel()
                .filter(n -> calc(n) == 89)
                .count();
        System.out.println(r);
    }

    private static int calc(int n) {
        while (n != 89 && n != 1) {
            int total = 0;
            while (n > 0) {
                total += Math.pow(n % 10, 2);
                n /= 10;
            }
            n = total;
        }
        return n;
    }
}
Task: Iterated digits squaring





Java: import java.util.stream.IntStream;

public class IteratedDigitsSquaring {

    public static void main(String[] args) {
        long r = IntStream.range(1, 100_000_000)
                .parallel()
                .filter(n -> calc(n) == 89)
                .count();
        System.out.println(r);
    }

    private static int calc(int n) {
        while (n != 89 && n != 1) {
            int total = 0;
            while (n > 0) {
                total += Math.pow(n % 10, 2);
                n /= 10;
            }
            n = total;
        }
        return n;
    }
}



Python: >>> step = lambda x: sum(int(d) ** 2 for d in str(x))
>>> iterate = lambda x: x if x in [1, 89] else iterate(step(x))
>>> [iterate(x) for x in xrange(1, 20)]
[1, 89, 89, 89, 89, 89, 1, 89, 89, 1, 89, 89, 1, 89, 89, 89, 89, 89, 1]
Task: Iterated digits squaring







Python: >>> step = lambda x: sum(int(d) ** 2 for d in str(x))
>>> iterate = lambda x: x if x in [1, 89] else iterate(step(x))
>>> [iterate(x) for x in xrange(1, 20)]
[1, 89, 89, 89, 89, 89, 1, 89, 89, 1, 89, 89, 1, 89, 89, 89, 89, 89, 1]


VB: start_time = Now
cnt = 0
For i = 1 To 100000000
	n = i
	sum = 0
	Do Until n = 1 Or n = 89
		For j = 1 To Len(n)
			sum = sum + (CLng(Mid(n,j,1))^2)
		Next
		n = sum
		sum = 0
	Loop
	If n = 89 Then
		cnt = cnt + 1
	End If
Next
end_time = Now

WScript.Echo "Elapse Time: " & DateDiff("s",start_time,end_time) &_
			vbCrLf & "Count: " & cnt
Task: Iterated digits squaring


C++: #include <iostream>


unsigned int sum_square_digits(unsigned int n) {
        int i,num=n,sum=0;
        
        while (num > 0) {
                
                int digit=num % 10;
                num=(num - digit)/10;
                
                sum+=digit*digit;
        }
        return sum;
}
int main(void) {
        unsigned int i=0,result=0, count=0;
        for (i=1; i<=100000000; i++) {
                
                if ((i != 1) || (i != 89)) {
                        result = sum_square_digits(i);
                }
                
                else {
                        result = i;
                }
                
                while ((result != 1) && (result != 89)) {
                        result = sum_square_digits(result);
                }
                if (result == 89) {
                        count++;
                }
        }
        std::cout << count << std::endl;
        return 0;
}






Python: >>> step = lambda x: sum(int(d) ** 2 for d in str(x))
>>> iterate = lambda x: x if x in [1, 89] else iterate(step(x))
>>> [iterate(x) for x in xrange(1, 20)]
[1, 89, 89, 89, 89, 89, 1, 89, 89, 1, 89, 89, 1, 89, 89, 89, 89, 89, 1]
Task: Longest substrings without repeating characters

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

struct substr {
    const char *start;
    size_t length;
};

struct substr *lswrc(const char *str) {
    size_t length = strlen(str);
    struct substr *arr = malloc(sizeof(struct substr) * (length + 1));
    if (!arr) return NULL;
  
    size_t start=0, i=0, maxStart=0, maxEnd=0, n=0;
    bool used[256] = {false};
    
    for (i=0; i<length; i++) {
        while (used[(unsigned char) str[i]]) 
            used[(unsigned char) str[start++]] = false;
        used[(unsigned char) str[i]] = true;
        if (i-start >= maxEnd-maxStart) {
            maxStart = start;
            maxEnd = i;
            size_t len = maxEnd - maxStart + 1;
            while (n>0 && arr[n-1].length < len) n--;
            arr[n].start = &str[start];
            arr[n].length = len;
            n++;
        }
    }
    
    arr[n].start = NULL;
    arr[n].length = 0;
    return realloc(arr, sizeof(struct substr) * (n+1));
}

int main() {
    char *examples[] = {"xyzyabcybdfd", "xyzyab", "zzzzz", "a", "", NULL};
    
    for (size_t i=0; examples[i]; i++) {
        printf("Original string: \"%s\"\n", examples[i]);
        printf("Longest substrings: ");
        struct substr *ls = lswrc(examples[i]);
        for (size_t j=0; ls[j].start; j++)
            printf("\"%.*s\" ", (int)ls[j].length, ls[j].start);
        printf("\n");
        free(ls);
    }
    
    return 0;
}




Go: package main

import "fmt"

func substrings(s string) []string {
    n := len(s)
    if n == 0 {
        return []string{""}
    }
    var ss []string
    for i := 0; i < n; i++ {
        for le := 1; le <= n-i; le++ {
            ss = append(ss, s[i:i+le])
        }
    }
    return ss
}

func distinctRunes(chars []rune) []rune {
    m := make(map[rune]bool)
    for _, char := range chars {
        m[char] = true
    }
    var l []rune
    for k := range m {
        l = append(l, k)
    }
    return l
}

func distinctStrings(strings []string) []string {
    m := make(map[string]bool)
    for _, str := range strings {
        m[str] = true
    }
    var l []string
    for k := range m {
        l = append(l, k)
    }
    return l
}

func main() {
    fmt.Println("The longest substring(s) of the following without repeating characters are:\n")
    strs := []string{"xyzyabcybdfd", "xyzyab", "zzzzz", "a", ""}
    for _, s := range strs {
        var longest []string
        longestLen := 0
        for _, ss := range substrings(s) {
            if len(distinctRunes([]rune(ss))) == len(ss) {
                if len(ss) >= longestLen {
                    if len(ss) > longestLen {
                        longest = longest[:0]
                        longestLen = len(ss)
                    }
                    longest = append(longest, ss)
                }
            }
        }
        longest = distinctStrings(longest)
        fmt.Printf("String = '%s'\n", s)
        fmt.Println(longest)
        fmt.Printf("Length = %d\n\n", len(longest[0]))
    }
}
Task: Longest substrings without repeating characters

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

struct substr {
    const char *start;
    size_t length;
};

struct substr *lswrc(const char *str) {
    size_t length = strlen(str);
    struct substr *arr = malloc(sizeof(struct substr) * (length + 1));
    if (!arr) return NULL;
  
    size_t start=0, i=0, maxStart=0, maxEnd=0, n=0;
    bool used[256] = {false};
    
    for (i=0; i<length; i++) {
        while (used[(unsigned char) str[i]]) 
            used[(unsigned char) str[start++]] = false;
        used[(unsigned char) str[i]] = true;
        if (i-start >= maxEnd-maxStart) {
            maxStart = start;
            maxEnd = i;
            size_t len = maxEnd - maxStart + 1;
            while (n>0 && arr[n-1].length < len) n--;
            arr[n].start = &str[start];
            arr[n].length = len;
            n++;
        }
    }
    
    arr[n].start = NULL;
    arr[n].length = 0;
    return realloc(arr, sizeof(struct substr) * (n+1));
}

int main() {
    char *examples[] = {"xyzyabcybdfd", "xyzyab", "zzzzz", "a", "", NULL};
    
    for (size_t i=0; examples[i]; i++) {
        printf("Original string: \"%s\"\n", examples[i]);
        printf("Longest substrings: ");
        struct substr *ls = lswrc(examples[i]);
        for (size_t j=0; ls[j].start; j++)
            printf("\"%.*s\" ", (int)ls[j].length, ls[j].start);
        printf("\n");
        free(ls);
    }
    
    return 0;
}







Python: def longest_substring(s = "xyzyab"):
    substr = [s[x:y] for x in range(len(s)) for y in range(x+1, len(s) + 1)]
    no_reps = []
    for sub in substr:
        if len(sub) == len(set(sub)) and sub not in no_reps:
            no_reps.append(sub)
    max_len = max(len(sub) for sub in no_reps) if no_reps else 0
    max_subs = [sub for sub in no_reps if len(sub) == max_len]
    return max_subs

if __name__ == '__main__':
    for s in ["xyzyabcybdfd", "xyzyab", "zzzzz", "a", "α⊆϶α϶", "",
              [1, 2, 3, 4, 1, 2, 5, 6, 1, 7, 8, 1, 0]]:
        print(f"{s} => {longest_substring(s)}")
Task: Longest substrings without repeating characters

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

struct substr {
    const char *start;
    size_t length;
};

struct substr *lswrc(const char *str) {
    size_t length = strlen(str);
    struct substr *arr = malloc(sizeof(struct substr) * (length + 1));
    if (!arr) return NULL;
  
    size_t start=0, i=0, maxStart=0, maxEnd=0, n=0;
    bool used[256] = {false};
    
    for (i=0; i<length; i++) {
        while (used[(unsigned char) str[i]]) 
            used[(unsigned char) str[start++]] = false;
        used[(unsigned char) str[i]] = true;
        if (i-start >= maxEnd-maxStart) {
            maxStart = start;
            maxEnd = i;
            size_t len = maxEnd - maxStart + 1;
            while (n>0 && arr[n-1].length < len) n--;
            arr[n].start = &str[start];
            arr[n].length = len;
            n++;
        }
    }
    
    arr[n].start = NULL;
    arr[n].length = 0;
    return realloc(arr, sizeof(struct substr) * (n+1));
}

int main() {
    char *examples[] = {"xyzyabcybdfd", "xyzyab", "zzzzz", "a", "", NULL};
    
    for (size_t i=0; examples[i]; i++) {
        printf("Original string: \"%s\"\n", examples[i]);
        printf("Longest substrings: ");
        struct substr *ls = lswrc(examples[i]);
        for (size_t j=0; ls[j].start; j++)
            printf("\"%.*s\" ", (int)ls[j].length, ls[j].start);
        printf("\n");
        free(ls);
    }
    
    return 0;
}








VB: function nrls(byval s1)
  dim i,x
  if len(s1)<2 then nlrs=s :exit function
  s=lcase(replace(s1," ",""))
    redim a(127)
  dim ls:ls=len(s)
  start=1
  so=""
  ml=1
  a(asc(left(s,1)))=1
  for i=2 to ls+1
    if i>ls then
       rep=true
    else   
      x=asc(mid(s,i,1))
      rep=a(x)<>0
    end if
    if a(x)<>0 then 
      if (i-start)>ml then  
           so=  mid(s,start,i-start)  
           ml=(i-start)
      elseif   (i-start)=ml  then          
           so=so & " " & mid(s,start,i-start)
      end if  
      xx=a(x)
      for j= 96 to 122: 
        if a(j)<=x then a(j)=0
      next         
      start=xx+1
    end if
    a(x)=i
  next
  nrls=trim(so)
  erase a
end function

sub test (f)
  wscript.stdout.writeline "Original:   "&f
  wscript.stdout.writeline "Substrings: "&nrls(f)&vbcrlf  
end sub 

test "dabale arroz a la zorra el abad"
test "The quick brown fox jumps over the lazy dog"
test "ae"
test "aa"
test "abdefghij"
Task: Longest substrings without repeating characters


C++: #include <iostream>
#include <fstream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

std::vector<std::string> longest_substrings_without_repeats(const std::string& str) {
    size_t max_length = 0;
    std::vector<std::string> result;
    size_t length = str.size();
    for (size_t offset = 0; offset < length; ++offset) {
        std::set<char> characters;
        size_t len = 0;
        for (; offset + len < length; ++len) {
            if (characters.find(str[offset + len]) != characters.end())
                break;
            characters.insert(str[offset + len]);
        }
        if (len > max_length) {
            result.clear();
            max_length = len;
        }
        if (len == max_length)
            result.push_back(str.substr(offset, max_length));
    }
    return result;
}

void print_strings(const std::vector<std::string>& strings) {
    std::cout << "[";
    for (size_t i = 0, n = strings.size(); i < n; ++i) {
        if (i > 0)
            std::cout << ", ";
        std::cout << '\'' << strings[i] << '\'';
    }
    std::cout << "]";
}

void test1() {
    for (std::string str : { "xyzyabcybdfd", "xyzyab", "zzzzz", "a", "thisisastringtest", "" }) {
        std::cout << "Input: '" << str << "'\nResult: ";
        print_strings(longest_substrings_without_repeats(str));
        std::cout << "\n\n";
    }
}

std::string slurp(std::istream& in) {
    std::ostringstream out;
    out << in.rdbuf();
    return out.str();
}

void test2(const std::string& filename) {
    std::ifstream in(filename);
    if (!in) {
        std::cerr << "Cannot open file '" << filename << "'.\n";
        return;
    }
    std::cout << "Longest substring with no repeats found in '" << filename << "': ";
    print_strings(longest_substrings_without_repeats(slurp(in)));
    std::cout << "\n";
}

int main() {
    test1();
    test2("unixdict.txt");
}






Python: def longest_substring(s = "xyzyab"):
    substr = [s[x:y] for x in range(len(s)) for y in range(x+1, len(s) + 1)]
    no_reps = []
    for sub in substr:
        if len(sub) == len(set(sub)) and sub not in no_reps:
            no_reps.append(sub)
    max_len = max(len(sub) for sub in no_reps) if no_reps else 0
    max_subs = [sub for sub in no_reps if len(sub) == max_len]
    return max_subs

if __name__ == '__main__':
    for s in ["xyzyabcybdfd", "xyzyab", "zzzzz", "a", "α⊆϶α϶", "",
              [1, 2, 3, 4, 1, 2, 5, 6, 1, 7, 8, 1, 0]]:
        print(f"{s} => {longest_substring(s)}")