Zundokokiyoshi with python / ruby / Lua

Original story ――Honestly, it's amazing to come up with this. ――It's a little more complicated than fizzbuzz, so I think it's good for studying languages.

python version

Final code

――I made a mistake in the basics, so I was pointed out. ――We also received some refactoring.

** Dung four times **

def kiyoshi():
    zd=deque(list(),5)
    while ''.join(zd)!='Zunzunzundoko':
        word=choice(['Dung','Doco'])
        print(word)
        zd.append(word)
    print('Ki yo shi!')

Wrong code below

Like this? deque It's pretty simple to use.

from collections import deque
from random import choice
def kiyoshi():
    zd=deque(list(),4)
    while True:
        if "".join(zd)=='Zunzunzundoko':
            print("Ki yo shi!")
            break
        else:
            word=choice(['Dung','Doco'])
            print(word)
            zd.append(word)

monitoring

%matplotlib inline
import pandas as pd
def kiyoshi2():
    c=0
    zd=deque(list(),4)
    while True:
        if "".join(zd)=='Zunzunzundoko':
            print("Ki yo shi!")
            break
        else:
            word=choice(['Dung','Doco'])
            print(word)
            zd.append(word)
            c+=1
    return c
rslts=[kiyoshi2() for i in range(10000)]
pd.DataFrame(rslts).hist(bins=30)

kiyoshi.png

The original algorithm seems to use zun counter.

def kiyoshi_org():
    zun=0
    while True:
        word=choice(['Dung','Doco'])
        print (word)
        if word == 'Dung':
            zun+=1
        elif zun>=3:
            print("Ki yo shi!")
            break
        else: zun=0

ruby version

-Here is smarter. ――Ruby is fun.

kiyoshi.rb


#! ruby -Ku
require "kconv"

def kiyoshi()
  zd=[]
  while zd.join!="Zunzunzundoko" do
    word=["Dung","Doco"].sample
    p word
    zd<<word
    zd.slice!(0) if zd.length>=6
  end
  p "Ki yo shi!"
end

def kiyoshi_org()
  zun=0
  while true do
    word = ["Dung","Doco"].sample
    p word
    if word == "Dung"
      zun+=1
    elsif zun <= 3
      zun = 0
    else
      p "Ki yo shi!"
      break
    end
  end
end

kiyoshi()
kiyoshi_org()

I'm not used to the do end syntax.

Lua version

--I wrote lua for the first time. I think it's early. ――I searched for an array concatenation, but couldn't find it. ――Japanese characters are garbled, so for the time being, use Roman characters.

kiyoshi.lua


function kiyoshi_org()
  words={"zun","doko"}
  zun=0
  while true do
    word = words[math.random (#words)]
    print (word)
    if word == "zun" then zun = zun + 1
    elseif zun < 4 then zun =0
    else break
    end
  end
  print "ki yo shi!"
end


function kiyoshi()
  words={"zun","doko"}
  zd={}
  while true do
    word = words[math.random (#words)]
    print (word)
    table.insert(zd, word)
    str=""
    for i,value in ipairs(zd) do
      str = str .. value
    end
    if #zd==5 then
      if str == "zunzunzunzundoko" then break
      else table.remove(zd,1)
      end
    end
  end
  print "ki yo shi!"
end

What should I do next ...

Recommended Posts

Zundokokiyoshi with python / ruby / Lua
Zundokokiyoshi with python
Scraping with Node, Ruby and Python
Dynamic proxy with python, ruby, PHP
Build Vim with MinGW. (+ lua, + python)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Try calling Python from Ruby with thrift
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
Zundokokiyoshi with TensorFlow
Excel with Python
Python beginner Zundokokiyoshi
Microcomputer with Python
Cast with python
How to enjoy programming with Minecraft (Ruby, Python)
I tried using mecab with python2.7, ruby2.3, php7
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learn Python with ChemTHEATER
Run prepDE.py with python3
Collecting tweets with Python
3. 3. AI programming with Python
Learn Zundokokiyoshi with LSTM
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
Ruby, Python and map
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python