入力: D a B c d c C A E c e B b 出力: (a A) (B B b) (c c C c) (D d) (E e) 0222デフォルトの名無しさん2020/08/07(金) 13:43:57.69ID:uQ4nbdFf>>221 js const inputStr = "D a B c d c C A E c e B b" const expectedStr = "(a A) (B B b) (c c C c) (D d) (E e)"
console.log("out :" + resultStr) console.log("expect:" + expectedStr) console.log(resultStr == expectedStr) 0223デフォルトの名無しさん2020/08/07(金) 13:48:23.69ID:uQ4nbdFf const inputStr = "D a B c d c C A E c e B b" const expectedStr = "(a A) (B B b) (c c C c) (D d) (E e)"
sort いらなかった 0224デフォルトの名無しさん2020/08/07(金) 14:51:48.30ID:5vmbSLbR>>221 Ruby # その 1 def igrouping( str ) gList = {} str.split.each{|ch| k = ch.downcase gList[ k ] ||= [] gList[ k ] << ch } gList.keys.sort.map{|k| '(' + gList[ k ].join(' ') + ')' }.join(' ') end
instr = 'D a B c d c C A E c e B b' puts "入力: #{instr}" puts "出力: #{igrouping( instr )}" 0225デフォルトの名無しさん2020/08/07(金) 14:52:06.69ID:5vmbSLbR>>221 Ruby # その 2 def igrouping( str ) i = 0 '(' + str.split.sort_by{|x| [ x.downcase, i += 1 ] }.join(' ').gsub( /(\w) (?!\1)/i ){"#{$1}) (#{$2}"} + ')' end
instr = 'D a B c d c C A E c e B b' puts "入力: #{instr}" puts "出力: #{igrouping( instr )}" 0226デフォルトの名無しさん2020/08/07(金) 16:23:53.54ID:uQ4nbdFf <<=って演算子ほしいな 0227デフォルトの名無しさん2020/08/07(金) 19:28:59.75ID:P4+Ga4Bm>>221 Ruby
$input = "D a B c d c C A E c e B b"; map{push @{$h{uc $_}}, $_}(split / /, $input); print map{map{"(".(join" ", @$_).")"}$h{$_}} sort{uc $a cmp uc $b} keys %h;
>>249 Perl5 use feature qw{signatures say}; sub strot_cmp($a, $b) { say "入力 $a $b"; @a = "$a$a" =~ /^(.*)($b)(.*)$/; say "出力 ", (@a and "$a[2]$a[0]" eq $b) ? 'true' : 'false'; } strot_cmp qw{abcde deabc}; strot_cmp qw{abcde cdeba}; strot_cmp qw{aaaaa aaa};
実行結果 ~ $ perl 18_249_strot_cmp.pl The signatures feature is experimental at 18_249_strot_cmp.pl line 2. 入力 abcde deabc 出力 true 入力 abcde cdeba 出力 false 入力 aaaaa aaa 出力 false 0258デフォルトの名無しさん2020/08/09(日) 22:40:05.17ID:/K790vfJ octave文法好みだけど結構古い言語なんだな 02592532020/08/09(日) 23:21:31.30ID:dFp53gtH>>221 Python3
s = "D a B c d c C A E c e B b".split(); d = {} for c in s: u = c.upper() if u in d: d[u] += ' ' + c else: d[u] = c ks = sorted(d.keys()) print(' '.join(map(lambda x: '(%s)' % d[x], ks)))
実行結果 ~ $ py 18_221_NCS_sort_grp.py (a A) (B B b) (c c C c) (D d) (E e) 0260デフォルトの名無しさん2020/08/09(日) 23:56:04.43ID:DzMx1xPb Perlだとこの程度でも正規表現使わないといかんのか? 02612532020/08/10(月) 00:02:07.64ID:5Aj+dIHX>>259 を少し簡略化
s = "D a B c d c C A E c e B b".split(); d = {} for c in s: u = c.upper() d[u] = d[u]+' '+c if u in d else c print(' '.join(map(lambda x: '(%s)' % d[x], sorted(d.keys()) ))) 02622532020/08/10(月) 00:03:02.27ID:5Aj+dIHX>>260 substrとかでも書ける 02632532020/08/10(月) 01:07:06.98ID:mTkq56mc>>260 >>257は一致する場合に対応するようあせって書いた面があり 正規表現を使うのはやりすぎ感があったのは確かなので 顕に正規表現を使わずに書きを考えてみた。 もっとも簡素な書き方として思いついたのがsplitで三分割して 前後の余った文字列を連結したらそれも一致するかなので
>>249 Perl5 use feature qw{signatures say}; sub strot_cmp($a, $b) { say "入力 $a $b"; @a = split "$b", "$a$a", 3; say "出力 ", ("$a[2]$a[0]" eq $b) ? 'true' : 'false'; } strot_cmp qw{abcde deabc}; strot_cmp qw{abcde cdeba}; strot_cmp qw{aaaaa aaa};
M = 256 i = p = s = 0 h = Hash.new(0) m = [0] * M while i < str.length case str[i] when ?+ then m[p] += 1 when ?- then m[p] -= 1 when ?[ then h[s += 1] = i when ?] then m[p].zero? ? s -= 1 : i = h[s] when ?> then p += 1 when ?< then p -= 1 when ?. then $><<m[p].chr when ?, then m[p] += $<.getc.ord end i += 1 end