WeixiangYan/CodeTransOcean huggingface dataset:
Task: Power set






PHP: <?php
function get_subset($binary, $arr) {


  $subset = array();
  foreach (range(0, count($arr)-1) as $i) {
    if ($binary[$i]) {
      $subset[] = $arr[count($arr) - $i - 1];
    } 
  }
  return $subset;
}

function print_array($arr) {
  if (count($arr) > 0) {
    echo join(" ", $arr);
  } else {
    echo "(empty)";
  }
  echo '<br>';
}

function print_power_sets($arr) {
  echo "POWER SET of [" . join(", ", $arr) . "]<br>";
  foreach (power_set($arr) as $subset) {
    print_array($subset);
  }
}
  
function power_set($arr) {  
  $binary = array();
  foreach (range(1, count($arr)) as $i) {
    $binary[] = false;
  }
  $n = count($arr);
  $powerset = array();
  
  while (count($binary) <= count($arr)) {
    $powerset[] = get_subset($binary, $arr);
    $i = 0;
    while (true) {
      if ($binary[$i]) {
        $binary[$i] = false;
        $i += 1;
      } else {
        $binary[$i] = true;
        break;
      }
    }
    $binary[$i] = true;
  }
  
  return $powerset;
}
 
print_power_sets(array());
print_power_sets(array('singleton'));
print_power_sets(array('dog', 'c', 'b', 'a'));
?>



VB: Option Base 1
Private Function power_set(ByRef st As Collection) As Collection
    Dim subset As Collection, pwset As New Collection
    For i = 0 To 2 ^ st.Count - 1
        Set subset = New Collection
        For j = 1 To st.Count
            If i And 2 ^ (j - 1) Then subset.Add st(j)
        Next j
        pwset.Add subset
    Next i
    Set power_set = pwset
End Function
Private Function print_set(ByRef st As Collection) As String
    
    Dim s() As String, t() As String
    ReDim s(st.Count)
    
    For i = 1 To st.Count
        If st(i).Count > 0 Then
            ReDim t(st(i).Count)
            For j = 1 To st(i).Count
                Select Case TypeName(st(i)(j))
                    Case "Integer": t(j) = CStr(st(i)(j))
                    Case "Collection": t(j) = "{}" 
                End Select
            Next j
            s(i) = "{" & Join(t, ", ") & "}"
        Else
            s(i) = "{}"
        End If
    Next i
    print_set = "{" & Join(s, ", ") & "}"
End Function
Public Sub rc()
    Dim rcset As New Collection, result As Collection
    For i = 1 To 4
        rcset.Add i
    Next i
    Debug.Print print_set(power_set(rcset))
    Set rcset = New Collection
    Debug.Print print_set(power_set(rcset))
    Dim emptyset As New Collection
    rcset.Add emptyset
    Debug.Print print_set(power_set(rcset))
    Debug.Print
End Sub
Task: Determine if a string is collapsible


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

template<typename char_type>
std::basic_string<char_type> collapse(std::basic_string<char_type> str) {
    auto i = std::unique(str.begin(), str.end());
    str.erase(i, str.end());
    return str;
}

void test(const std::string& str) {
    std::cout << "original string: <<<" << str << ">>>, length = " << str.length() << '\n';
    std::string collapsed(collapse(str));
    std::cout << "result string: <<<" << collapsed << ">>>, length = " << collapsed.length() << '\n';
    std::cout << '\n';
}

int main(int argc, char** argv) {
    test("");
    test("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ");
    test("..1111111111111111111111111111111111111111111111111111111111111117777888");
    test("I never give 'em hell, I just tell the truth, and they think it's hell. ");
    test("                                                    --- Harry S Truman  ");
    return 0;
}



Go: package main

import "fmt"



func collapse(s string) (string, int, int) {
    r := []rune(s)
    le, del := len(r), 0
    for i := le - 2; i >= 0; i-- {
        if r[i] == r[i+1] {
            copy(r[i:], r[i+1:])
            del++
        }
    }
    if del == 0 {
        return s, le, le
    }
    r = r[:le-del]
    return string(r), le, len(r)
}
    
func main() {
    strings:= []string {
        "",
        `"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln `,
        "..1111111111111111111111111111111111111111111111111111111111111117777888",
        "I never give 'em hell, I just tell the truth, and they think it's hell. ",
        "                                                   ---  Harry S Truman  ",
        "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
        "headmistressship",
        "aardvark",
        "😍😀🙌💃😍😍😍🙌",
    }
    for _, s := range strings {
        cs, olen, clen := collapse(s)
        fmt.Printf("original : length = %2d, string = «««%s»»»\n", olen, s)
        fmt.Printf("collapsed: length = %2d, string = «««%s»»»\n\n", clen, cs)
    }
}
Task: Determine if a string is collapsible


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

template<typename char_type>
std::basic_string<char_type> collapse(std::basic_string<char_type> str) {
    auto i = std::unique(str.begin(), str.end());
    str.erase(i, str.end());
    return str;
}

void test(const std::string& str) {
    std::cout << "original string: <<<" << str << ">>>, length = " << str.length() << '\n';
    std::string collapsed(collapse(str));
    std::cout << "result string: <<<" << collapsed << ">>>, length = " << collapsed.length() << '\n';
    std::cout << '\n';
}

int main(int argc, char** argv) {
    test("");
    test("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ");
    test("..1111111111111111111111111111111111111111111111111111111111111117777888");
    test("I never give 'em hell, I just tell the truth, and they think it's hell. ");
    test("                                                    --- Harry S Truman  ");
    return 0;
}




Java: 

public class StringCollapsible {

    public static void main(String[] args) {
        for ( String s : new String[] {
              "", 
              "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", 
              "..1111111111111111111111111111111111111111111111111111111111111117777888", 
              "I never give 'em hell, I just tell the truth, and they think it's hell. ", 
              "                                                    --- Harry S Truman  ",
              "122333444455555666666777777788888888999999999",
              "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
              "headmistressship"}) {
            String result = collapse(s);
            System.out.printf("old:  %2d <<<%s>>>%nnew:  %2d <<<%s>>>%n%n", s.length(), s, result.length(), result);
        }
    }
    
    private static String collapse(String in) {
        StringBuilder sb = new StringBuilder();
        for ( int i = 0 ; i < in.length() ; i++ ) {
            if ( i == 0 || in.charAt(i-1) != in.charAt(i) ) {
                sb.append(in.charAt(i));
            }
        }
        return sb.toString();
    }

}
Task: Determine if a string is collapsible


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

template<typename char_type>
std::basic_string<char_type> collapse(std::basic_string<char_type> str) {
    auto i = std::unique(str.begin(), str.end());
    str.erase(i, str.end());
    return str;
}

void test(const std::string& str) {
    std::cout << "original string: <<<" << str << ">>>, length = " << str.length() << '\n';
    std::string collapsed(collapse(str));
    std::cout << "result string: <<<" << collapsed << ">>>, length = " << collapsed.length() << '\n';
    std::cout << '\n';
}

int main(int argc, char** argv) {
    test("");
    test("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ");
    test("..1111111111111111111111111111111111111111111111111111111111111117777888");
    test("I never give 'em hell, I just tell the truth, and they think it's hell. ");
    test("                                                    --- Harry S Truman  ");
    return 0;
}






Python: from itertools import groupby

def collapser(txt):
    return ''.join(item for item, grp in groupby(txt))

if __name__ == '__main__':
    strings = [
            "",
            '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ',
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 'em hell, I just tell the truth, and they think it's hell. ",
            "                                                   ---  Harry S Truman  ",
            "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
            "headmistressship",
            "aardvark",
            "😍😀🙌💃😍😍😍🙌",
            ]
    for txt in strings:
        this = "Original"
        print(f"\n{this:14} Size: {len(txt)} «««{txt}»»»" )
        this = "Collapsed"
        sqz = collapser(txt)
        print(f"{this:>14} Size: {len(sqz)} «««{sqz}»»»" )
Task: Determine if a string is collapsible


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

template<typename char_type>
std::basic_string<char_type> collapse(std::basic_string<char_type> str) {
    auto i = std::unique(str.begin(), str.end());
    str.erase(i, str.end());
    return str;
}

void test(const std::string& str) {
    std::cout << "original string: <<<" << str << ">>>, length = " << str.length() << '\n';
    std::string collapsed(collapse(str));
    std::cout << "result string: <<<" << collapsed << ">>>, length = " << collapsed.length() << '\n';
    std::cout << '\n';
}

int main(int argc, char** argv) {
    test("");
    test("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ");
    test("..1111111111111111111111111111111111111111111111111111111111111117777888");
    test("I never give 'em hell, I just tell the truth, and they think it's hell. ");
    test("                                                    --- Harry S Truman  ");
    return 0;
}







VB: Module Module1

    Function Collapse(s As String) As String
        If String.IsNullOrEmpty(s) Then
            Return ""
        End If
        Return s(0) + New String(Enumerable.Range(1, s.Length - 1).Where(Function(i) s(i) <> s(i - 1)).Select(Function(i) s(i)).ToArray)
    End Function

    Sub Main()
        Dim input() = {
             "",
            "The better the 4-wheel drive, the further you
            "headmistressship",
            ControlChars.Quote + "If I were two-faced, would I be wearing this one?" + ControlChars.Quote + " --- Abraham Lincoln ",
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 
            "                                                    --- Harry S Truman  "
           }
        For Each s In input
            Console.WriteLine($"old: {s.Length} «««{s}»»»")
            Dim c = Collapse(s)
            Console.WriteLine($"new: {c.Length} «««{c}»»»")
        Next
    End Sub

End Module
Task: Determine if a string is collapsible



C#: using System;
using static System.Linq.Enumerable;

public class Program
{
    static void Main()
    {
        string[] input = {
            "",
            "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
            "headmistressship",
            "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 'em hell, I just tell the truth, and they think it's hell. ",
            "                                                    --- Harry S Truman  "
        };
        foreach (string s in input) {
            Console.WriteLine($"old: {s.Length} «««{s}»»»");
            string c = Collapse(s);
            Console.WriteLine($"new: {c.Length} «««{c}»»»");
        }
    }

    static string Collapse(string s) => string.IsNullOrEmpty(s) ? "" :
        s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}


Go: package main

import "fmt"



func collapse(s string) (string, int, int) {
    r := []rune(s)
    le, del := len(r), 0
    for i := le - 2; i >= 0; i-- {
        if r[i] == r[i+1] {
            copy(r[i:], r[i+1:])
            del++
        }
    }
    if del == 0 {
        return s, le, le
    }
    r = r[:le-del]
    return string(r), le, len(r)
}
    
func main() {
    strings:= []string {
        "",
        `"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln `,
        "..1111111111111111111111111111111111111111111111111111111111111117777888",
        "I never give 'em hell, I just tell the truth, and they think it's hell. ",
        "                                                   ---  Harry S Truman  ",
        "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
        "headmistressship",
        "aardvark",
        "😍😀🙌💃😍😍😍🙌",
    }
    for _, s := range strings {
        cs, olen, clen := collapse(s)
        fmt.Printf("original : length = %2d, string = «««%s»»»\n", olen, s)
        fmt.Printf("collapsed: length = %2d, string = «««%s»»»\n\n", clen, cs)
    }
}
Task: Determine if a string is collapsible



C#: using System;
using static System.Linq.Enumerable;

public class Program
{
    static void Main()
    {
        string[] input = {
            "",
            "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
            "headmistressship",
            "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 'em hell, I just tell the truth, and they think it's hell. ",
            "                                                    --- Harry S Truman  "
        };
        foreach (string s in input) {
            Console.WriteLine($"old: {s.Length} «««{s}»»»");
            string c = Collapse(s);
            Console.WriteLine($"new: {c.Length} «««{c}»»»");
        }
    }

    static string Collapse(string s) => string.IsNullOrEmpty(s) ? "" :
        s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}



Java: 

public class StringCollapsible {

    public static void main(String[] args) {
        for ( String s : new String[] {
              "", 
              "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", 
              "..1111111111111111111111111111111111111111111111111111111111111117777888", 
              "I never give 'em hell, I just tell the truth, and they think it's hell. ", 
              "                                                    --- Harry S Truman  ",
              "122333444455555666666777777788888888999999999",
              "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
              "headmistressship"}) {
            String result = collapse(s);
            System.out.printf("old:  %2d <<<%s>>>%nnew:  %2d <<<%s>>>%n%n", s.length(), s, result.length(), result);
        }
    }
    
    private static String collapse(String in) {
        StringBuilder sb = new StringBuilder();
        for ( int i = 0 ; i < in.length() ; i++ ) {
            if ( i == 0 || in.charAt(i-1) != in.charAt(i) ) {
                sb.append(in.charAt(i));
            }
        }
        return sb.toString();
    }

}
Task: Determine if a string is collapsible



C#: using System;
using static System.Linq.Enumerable;

public class Program
{
    static void Main()
    {
        string[] input = {
            "",
            "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
            "headmistressship",
            "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 'em hell, I just tell the truth, and they think it's hell. ",
            "                                                    --- Harry S Truman  "
        };
        foreach (string s in input) {
            Console.WriteLine($"old: {s.Length} «««{s}»»»");
            string c = Collapse(s);
            Console.WriteLine($"new: {c.Length} «««{c}»»»");
        }
    }

    static string Collapse(string s) => string.IsNullOrEmpty(s) ? "" :
        s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}





Python: from itertools import groupby

def collapser(txt):
    return ''.join(item for item, grp in groupby(txt))

if __name__ == '__main__':
    strings = [
            "",
            '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ',
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 'em hell, I just tell the truth, and they think it's hell. ",
            "                                                   ---  Harry S Truman  ",
            "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
            "headmistressship",
            "aardvark",
            "😍😀🙌💃😍😍😍🙌",
            ]
    for txt in strings:
        this = "Original"
        print(f"\n{this:14} Size: {len(txt)} «««{txt}»»»" )
        this = "Collapsed"
        sqz = collapser(txt)
        print(f"{this:>14} Size: {len(sqz)} «««{sqz}»»»" )
Task: Determine if a string is collapsible



C#: using System;
using static System.Linq.Enumerable;

public class Program
{
    static void Main()
    {
        string[] input = {
            "",
            "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
            "headmistressship",
            "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 'em hell, I just tell the truth, and they think it's hell. ",
            "                                                    --- Harry S Truman  "
        };
        foreach (string s in input) {
            Console.WriteLine($"old: {s.Length} «««{s}»»»");
            string c = Collapse(s);
            Console.WriteLine($"new: {c.Length} «««{c}»»»");
        }
    }

    static string Collapse(string s) => string.IsNullOrEmpty(s) ? "" :
        s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}






VB: Module Module1

    Function Collapse(s As String) As String
        If String.IsNullOrEmpty(s) Then
            Return ""
        End If
        Return s(0) + New String(Enumerable.Range(1, s.Length - 1).Where(Function(i) s(i) <> s(i - 1)).Select(Function(i) s(i)).ToArray)
    End Function

    Sub Main()
        Dim input() = {
             "",
            "The better the 4-wheel drive, the further you
            "headmistressship",
            ControlChars.Quote + "If I were two-faced, would I be wearing this one?" + ControlChars.Quote + " --- Abraham Lincoln ",
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 
            "                                                    --- Harry S Truman  "
           }
        For Each s In input
            Console.WriteLine($"old: {s.Length} «««{s}»»»")
            Dim c = Collapse(s)
            Console.WriteLine($"new: {c.Length} «««{c}»»»")
        Next
    End Sub

End Module
Task: Determine if a string is collapsible




Go: package main

import "fmt"



func collapse(s string) (string, int, int) {
    r := []rune(s)
    le, del := len(r), 0
    for i := le - 2; i >= 0; i-- {
        if r[i] == r[i+1] {
            copy(r[i:], r[i+1:])
            del++
        }
    }
    if del == 0 {
        return s, le, le
    }
    r = r[:le-del]
    return string(r), le, len(r)
}
    
func main() {
    strings:= []string {
        "",
        `"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln `,
        "..1111111111111111111111111111111111111111111111111111111111111117777888",
        "I never give 'em hell, I just tell the truth, and they think it's hell. ",
        "                                                   ---  Harry S Truman  ",
        "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
        "headmistressship",
        "aardvark",
        "😍😀🙌💃😍😍😍🙌",
    }
    for _, s := range strings {
        cs, olen, clen := collapse(s)
        fmt.Printf("original : length = %2d, string = «««%s»»»\n", olen, s)
        fmt.Printf("collapsed: length = %2d, string = «««%s»»»\n\n", clen, cs)
    }
}


Java: 

public class StringCollapsible {

    public static void main(String[] args) {
        for ( String s : new String[] {
              "", 
              "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", 
              "..1111111111111111111111111111111111111111111111111111111111111117777888", 
              "I never give 'em hell, I just tell the truth, and they think it's hell. ", 
              "                                                    --- Harry S Truman  ",
              "122333444455555666666777777788888888999999999",
              "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
              "headmistressship"}) {
            String result = collapse(s);
            System.out.printf("old:  %2d <<<%s>>>%nnew:  %2d <<<%s>>>%n%n", s.length(), s, result.length(), result);
        }
    }
    
    private static String collapse(String in) {
        StringBuilder sb = new StringBuilder();
        for ( int i = 0 ; i < in.length() ; i++ ) {
            if ( i == 0 || in.charAt(i-1) != in.charAt(i) ) {
                sb.append(in.charAt(i));
            }
        }
        return sb.toString();
    }

}
Task: Determine if a string is collapsible





Java: 

public class StringCollapsible {

    public static void main(String[] args) {
        for ( String s : new String[] {
              "", 
              "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", 
              "..1111111111111111111111111111111111111111111111111111111111111117777888", 
              "I never give 'em hell, I just tell the truth, and they think it's hell. ", 
              "                                                    --- Harry S Truman  ",
              "122333444455555666666777777788888888999999999",
              "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
              "headmistressship"}) {
            String result = collapse(s);
            System.out.printf("old:  %2d <<<%s>>>%nnew:  %2d <<<%s>>>%n%n", s.length(), s, result.length(), result);
        }
    }
    
    private static String collapse(String in) {
        StringBuilder sb = new StringBuilder();
        for ( int i = 0 ; i < in.length() ; i++ ) {
            if ( i == 0 || in.charAt(i-1) != in.charAt(i) ) {
                sb.append(in.charAt(i));
            }
        }
        return sb.toString();
    }

}



Python: from itertools import groupby

def collapser(txt):
    return ''.join(item for item, grp in groupby(txt))

if __name__ == '__main__':
    strings = [
            "",
            '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ',
            "..1111111111111111111111111111111111111111111111111111111111111117777888",
            "I never give 'em hell, I just tell the truth, and they think it's hell. ",
            "                                                   ---  Harry S Truman  ",
            "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
            "headmistressship",
            "aardvark",
            "😍😀🙌💃😍😍😍🙌",
            ]
    for txt in strings:
        this = "Original"
        print(f"\n{this:14} Size: {len(txt)} «««{txt}»»»" )
        this = "Collapsed"
        sqz = collapser(txt)
        print(f"{this:>14} Size: {len(sqz)} «««{sqz}»»»" )