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.
――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!')
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)
%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)
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
-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.
--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
Recommended Posts