3
1

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.

ホクホクの芋を簡単に作りたかった

Last updated at Posted at 2019-07-05

@mattn さんの ホクホクのイモ という記事を読んで、もっと簡単にホクホクの芋が食べられたらなと思った。

ホクホクのイモとは?

こちらの記事がとても分かりやすかったです。
https://qiita.com/matarillo/items/0876311a901e313c36e8

  1. 「ホクイモ」の4字から、ランダムに、かつ重複せずに2文字取り出す。取り出す順番は考慮する。 (例:"モホ")
  2. で取り出した文字を2回繰り返す。(例:「モホモホ」)
  3. と同じ処理をもう一度やりなおす。その際、さっき何を取り出したかは影響しない。(例:"クモ")
    1. と 3. の文字を「の」でつなぐ。(例:「モホモホのクモ」)

golang版

package main

import (
	"fmt"
	"math/rand"
	"strings"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	rs := []rune("ホイクモ")
	hoku := func(j []int) string {
		return string(rs[j[0]]) + string(rs[j[1]])
	}
	fmt.Printf("%sの%s", strings.Repeat(hoku(rand.Perm(4)[:2]), 2), hoku(rand.Perm(4)[:2]))
}

javascript版は打ち合わせしながら考える・・・

JS版

node.jsを使用しました

let hokuimo = "ホクイモ".split("")

let r = (max) => Math.floor(Math.random() * (max))
let f = (arr, rv) => {
  return arr.length > 2 ? arr[rv] + f(arr.filter((v, i) => i != rv), r(arr.length -1)) : ""
}

console.log(`${f(hokuimo, r(hokuimo.length)).repeat(2)}${f(hokuimo, r(hokuimo.length))}`)

@tkturbo さんのコメントを見てJSでワンライナーできないかなと思ってこんな感じにしてみました。


((h = (s = "ホクイモ", n = true, r = Math.floor(Math.random() * s.length), t1 = s.substr(r, 1)) => n ? (rp = 2) => (t1 + h(s.replace(t1, ""), false)).repeat(rp) : t1) => console.log(`${h()()}${h()(1)}`))()

基本の処理はコメントの内容ですが、ワンライナーで処理できるように所々工夫しています。


もっと簡単な書き方があったら教えてください・・・


### 2019/7/8 追記

jsのロジックに誤りがあったので修正しました・・・
> かつ重複せずに2文字取り出す
3
1
3

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?