0

言語にとらわれない。

一般的なスクリプト言語が優先される任意の言語でソリューションを提供します。

文字列を処理します... これらの 4 つを例として配列で使用します。

examples = ["The quick brown fox jumps over the lazy dog.(JSON-CAT5).tar.gz",
            "The quick brown fox jumps over the lazy dog.(JSON-CAT5).txt",
            "The quick & swift mule, kicks the lazy dev.txt",
            "Now we-come,to.the_payoff.txt"]

以下のルールに従って変換してください。

  1. 他の規則に関係なく、最初の単語は常に大文字になります。
  2. ルール 3 または 4 でカバーされていない限り、すべての単語は大文字で表記されます。
  3. ホワイトリストに表示される部分文字列は、大文字と小文字が区別されます。この例では["the", "JSON"]
  4. ブラックリストに表示される部分文字列は、文字列から削除されます。私たちの例では["-CAT5","(",")"]
  5. 正規表現に一致する部分文字列/(\.tar)?\.[^.]*$/iは常に小文字です。
  6. [" ", "_", ",", "-"]["."] に変換される句読点のリスト。
  7. 複数"."つまり。"..."シングルに置き換えられ"."ます(つまり、スクイーズされます)。
  8. セパレーター「.」、ブラックリスト、ホワイトリストはすべて簡単に交換できる必要があり、上部に vars / arrays として指定するだけで完全に受け入れられます。

この場合、最終的には次のようになります。

  1. The.Quick.Brown.Fox.Jumps.Over.the.Lazy.Dog.JSON.txt
  2. The.Quick.Brown.Fox.Jumps.Over.the.Lazy.Dog.JSON.tar.gz

回答は以下に提供されています。他の言語での代替案を参照してください。

アップデート

テストケースにさらにいくつかのサンプル文字列を追加しました。

4

4 に答える 4

1

私は初心者の Perl プログラマーで、Perl を使用してあなたのタスクを実行しようとしています。

パールの例:

#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

my @examples = (
  qq(The quick brown fox jumps over the lazy dog.(JSON-CAT5).tar.gz),
  qq(The quick brown fox jumps over the lazy dog.(JSON-CAT5).txt),
  qq(The quick & swift mule, kicks the lazy dev.txt),
  qq(Now we-come,to.the_payoff.txt)
);

my @result;

my @whitelist = ( 'we', 'to', 'the', 'of', 'if', 'a', 'is', 'JSON' );
my @blacklist = ( '-CAT5', '(',')' );
my @separators = ( '-', '_', ' ', ',' );
my $separator = '.';

foreach my $eg (@examples) {
  $eg =~ s/(\.tar)?\.[^.]*$//pi;

  my $ext = ${^MATCH};

  my $blist = join('|', map { qr/\Q$_/i } @blacklist );
  my $slist = join('|', map { qr/\Q$_/i } @separators );

  $eg =~ s/$blist//gi;
  $eg =~ s/$slist/$separator/gi;

  my @vals = split /\./, $eg;
  push my @words, ucfirst shift @vals;
  foreach my $w (@vals) {
    $w = ucfirst $w unless $w ~~ @whitelist;
    push @words, $w;
  }

  $eg = join $separator, @words;
  $eg .= $ext;
  $eg =~ s/\.\.+/\./g; # squeeze
  push @result, $eg;
}

say for @result;
于 2013-01-02T20:22:07.790 に答える
1

やるべきことは何もなく、Pythonで遊んでいます...

Python の例

import re

examples = (
  "The quick brown fox jumps over the lazy dog.(JSON-CAT5).tar.gz",
  "The quick brown fox jumps over the lazy dog.(JSON-CAT5).txt",
  "The quick & swift mule, kicks the lazy dev.txt",
  "Now we-come,to.the_payoff.txt"
)

result = []

whitelist = ( 'we', 'to', 'the', 'of', 'if', 'a', 'is', 'JSON' )
blacklist = ( '-CAT5', '(', ')' )
separators = ( '-', '_', ' ', ',' )
separator = '.'

for eg in examples:
    ext = re.search(r'(\.tar)?\.[^.]*$', eg).group(0)
    eg = eg.replace(ext, '')

    for ignore in blacklist:
        eg = eg.replace(ignore, '')

    for sep in separators:
        eg = eg.replace(sep, separator)

    vals = enumerate(eg.split('.'))

    words = [w.capitalize() if i == 0 or not w in whitelist else w for i,w in vals]
    words.append(ext)

    eg = separator.join(words)
    eg = re.sub(r'\.\.+', '.', eg) # squeeze

    result.append(eg)

for r in result:
    print r
于 2013-02-06T13:51:23.760 に答える
1

ルビーの例

#!/usr/bin/env ruby

examples = ["The quick brown fox jumps over the lazy dog.(JSON-CAT5).tar.gz",
            "The quick brown fox jumps over the lazy dog.(JSON-CAT5).txt",
            "The quick & swift mule, kicks the lazy dev.txt",
            "Now we-come,to.the_payoff.txt"]

result = []

whitelist = ["we", "to", "the", "of", "if", "a", "is", "JSON", "HTTP", "HTTPS", "HTML"] # you get the idea.
blacklist = ["-CAT5", "(", ")"]
separators = ["-", "_", ",", " "]
separator = "."

examples.each do |eg|

  extension = eg.match(/(\.tar)?\.[^.]*$/i)[0]
  eg.sub!(extension, "")
  blacklist.each{ |b| eg.gsub!(b, "") }
  separators.each{ |s| eg.gsub!(s, separator) }
  words = eg.split(".")
  words.each {|w|
    w.capitalize! if words.first == w
    unless whitelist.include? w
      w.capitalize!
    end
  }
  n = "#{words.join('.')}#{extension}".squeeze(".")
  result << n
end

puts result

私たちに与えます...

The.Quick.Brown.Fox.Jumps.Over.the.Lazy.Dog.JSON.tar.gz
The.Quick.Brown.Fox.Jumps.Over.the.Lazy.Dog.JSON.txt
The.Quick.&.Swift.Mule.Kicks.the.Lazy.Dev.txt
Now.we.Come.to.the.Payoff.txt

考えられる欠点として、ホワイトリストとブラックリストでは大文字と小文字が区別されます。

于 2013-01-02T14:01:40.207 に答える
1
ArrayList final = new ArrayList();

String[] arr = input.split("[\\W]");

final.add(arr[0].charAt(0).toUpper() + arr[0].substring(1));

for(int i=1; i<length; i++)
{
    if(whitelist.contains(arr[i]))
    {
        final.add(arr[i]);
        continue;
    }

    if(blacklist.contains(arr[i]))
    {
        continue;
    }

    arr[i] = arr[i].charAt(0).toUpper() + arr[i].substring(1);

}

String output = "";

int i=0;
for(i=0; i<finalList.size()-1;i++)
    output += finalList.get(i) + ".";

output += finalList.get(i);
于 2013-01-02T14:16:18.653 に答える