Histoire originale «Honnêtement, c'est incroyable de trouver ça. «C'est un peu plus compliqué que le fizzbuzz, donc je pense que c'est bon pour étudier les langues.
«J'ai fait une erreur dans les bases, alors on m'a fait remarquer. «Nous avons également bénéficié d'une refactorisation.
** Bouse quatre fois **
def kiyoshi():
    zd=deque(list(),5)
    while ''.join(zd)!='Zunzunzundoko':
        word=choice(['Bouse','Doco'])
        print(word)
        zd.append(word)
    print('Ki yo shi!')
Comme ça? deque C'est assez simple à utiliser.
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(['Bouse','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(['Bouse','Doco'])
            print(word)
            zd.append(word)
            c+=1
    return c
rslts=[kiyoshi2() for i in range(10000)]
pd.DataFrame(rslts).hist(bins=30)

L'algorithme d'origine semble utiliser compteur zun.
def kiyoshi_org():
    zun=0
    while True:
        word=choice(['Bouse','Doco'])
        print (word)
        if word == 'Bouse':
            zun+=1
        elif zun>=3:
            print("Ki yo shi!")
            break
        else: zun=0
 kiyoshi.rb
#! ruby -Ku
require "kconv"
def kiyoshi()
  zd=[]
  while zd.join!="Zunzunzundoko" do
    word=["Bouse","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 = ["Bouse","Doco"].sample
    p word
    if word == "Bouse"
      zun+=1
    elsif zun <= 3
      zun = 0
    else
      p "Ki yo shi!"
      break
    end
  end
end
kiyoshi()
kiyoshi_org()
Je ne suis pas habitué à la syntaxe do end.
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