2017年5月7日日曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)の Part 1(Starting with the basics)、Chapter 8(Case Study: Word Play)の Exercise 8-1、8-2、8-3、8-4、8-5、8-6.を JavaScript で取り組んでみる。

Exercise 7-1、7-3.

words.txt

コード(Emacs)

HTML5

<pre id="output0"></pre>
<input id="file0" type="file">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample1.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    input_file0 = document.querySelector('#file0'),
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let result = [];

        for (let i = start; i < end; i += 1) {
            result.push(i);
        }
        return result;
    };

let hasNoE = (word) => word.indexOf('e') === -1;

let avoids = (word, letters) =>
    letters.split('').every((letter) => word.indexOf(letter) === -1);

let usesOnly = (word, letters) =>
    word.split('').every((c) => letters.indexOf(c) !== -1);

let usesAll = (word, letters) =>
    letters.split('').every((letter) => word.indexOf(letter) !== -1);

let isAbcedarian = (word) =>
    word.split('')
    .slice(1)
    .every((c, i) => word[i] <= c);

let output = () => {
    let file = input_file0.files[0],
        reader = new FileReader();

    reader.onload = () => {
        let words = reader.result
            .split('\n')
            .map((word) => word.trim())
            .filter((word) => word !== '');

        p('1.');
        p(
            words
                .filter((word) => word.length > 20)
                .join('\n')
        );
        
        p('2.');
        p(`${words.filter(hasNoE).length / words.length * 100}%`);

        p('3.');
        let count = words.length,
            letter = '';

        range('a'.codePointAt(0), 'z'.codePointAt(0))
            .forEach(
                (n) => range(n + 1, 'z'.codePointAt(0) + 1)
                    .forEach((m) => {
                        let letters0 =
                            String.fromCodePoint(n) + String.fromCodePoint(m),
                            count0 = 
                            words.filter(
                                (word) => !avoids(word, letters0)).length;
                        if (count0 < count) {
                            letters = letters0;
                            count = count0;
                        }
                    }));
        p(letters);

        p('4.');
        p(
            words
                .filter((word) => usesOnly(word, 'acefhlo'))
                .join('\n')
        );

        p('5.');
        p('aeiou');
        p(
            words
                .filter((word) => usesAll(word, 'aeiou'))
                .join('\n')
        );
        p('aeiouy');
        p(
            words
                .filter((word) => usesAll(word, 'aeiouy'))
                .join('\n')
        );

        p('6.');
        p(words.filter(isAbcedarian).length);
    };
    reader.readAsText(file);
};

btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();



0 コメント:

コメントを投稿

Comments on Google+: