2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「進捗・どう・です・か」をbashで

Last updated at Posted at 2016-09-12

超今さらだけど乗っかってみる

進捗どうですか.sh
#!/bin/bash

a=(進捗 どう です か)
b=(_ _ _ _)

n=0
while :; do
    i=$((RANDOM % ${#a[*]}))
    echo -n ${a[$i]}

    # array rotate
    b[ ${#b[@]} ]=${a[$i]}
    b=("${b[@]:1:4}")

    n=$((n+${#a[$i]}))

    if [ "${a[*]}" == "${b[*]}" ]; then
        break
    fi
done

(IFS=; echo "${a[*]}???")
echo "${n}文字で煽られました"

ついでにruby版

tap 使うのはなんかフェアじゃない気もするなぁ。

進捗どうですか.rb
#!/usr/bin/env ruby

n=0
puts "\n%s???" % Array.new(4).tap { |b|
  %w(進捗 どう です か).tap { |a|
    (print b.rotate!(1)[b.size-1] = a.sample) or n += b[b.size-1].size while b.join != a.join
  }
}.join
puts "%d文字で煽られました" % n

ついでにpython2系

やり方は全部同じだけど。

進捗どうですか.py
#!/usr/bin/env python
# coding: utf-8

import sys
import random
import itertools
from collections import deque

a = [u"進捗", u"どう", u"です", u""]
b = deque(u"____")

n=0
for i in (random.choice(a) for x in itertools.count()):
    b.popleft()
    b.append(i)
    sys.stdout.write(i)
    n += len(i)
    if u"".join(b) == u"".join(a):
        break

print u"\n%s???" % u"".join(a)
print u"%d文字で煽られました" % n

なお、進捗は芳しくない。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?