Skip to content

Instantly share code, notes, and snippets.

@recuraki
Created October 22, 2013 08:32
Show Gist options
  • Save recuraki/7097140 to your computer and use it in GitHub Desktop.
Save recuraki/7097140 to your computer and use it in GitHub Desktop.
任意の単語が出るまでがんばる。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
def RandomCharByString(stInput = None):
if stInput == None:
yield False
liInput = list(stInput)
while True:
inIndex = random.randint(0, len(liInput) - 1)
yield liInput[inIndex]
stCandidate = "hoge"
stTargetString = "hoge"
stBuffer, inMatchCount = "", 0
stRandomBuffer = RandomCharByString(stCandidate)
inTryCounter = 0
while True:
inTryCounter += 1
stCurrentChar = stRandomBuffer.next()
stBuffer = stBuffer + stCurrentChar
print stCurrentChar,
if stCurrentChar == stTargetString[inMatchCount]:
inMatchCount += 1
if inMatchCount == len(stTargetString):
break
else:
inMatchCount = 0
print "MATCH"
print "Count: " + str(inTryCounter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment