Skip to content

Instantly share code, notes, and snippets.

@liyonghelpme
Created December 19, 2019 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liyonghelpme/3306eee9ff4fb375755094e9f180b5fe to your computer and use it in GitHub Desktop.
Save liyonghelpme/3306eee9ff4fb375755094e9f180b5fe to your computer and use it in GitHub Desktop.
enum OpType {
None,
Remove,
Add,
Replace,
}
class MethodDetail {
method : OpType
num : number
}
enum ProblemT {
Less,
More,
Same,
MisUp,
MisLow,
MisNum
}
class SNode {
start : number
len : number
char : string
solved : boolean = false
removeNum : number = 0
addNum : number = 0
replaceNum : number = 0
}
class ProblemDetail {
type : ProblemT
lessNum : number
moreNum : number
sameNode :SNode = null
solved : boolean = false
needAddNum : number = 0
}
class StrP{
password : string
problems : ProblemDetail[] = []
//从操作角度分析
//解决哪些问题
//寻找所有问题
//执行某个操作 问题解决了一部分 没有 解决所有问题
//复杂在于 过多的if 判断逻辑
//列出问题
//Remove Add Replace 确定步骤解决方案
//解决部分问题 确定问题的解决进度
//接着进入下一个循环
Check() {
this.CollectProblem()
this.FixProblem()
}
CollectProblem(){
if(this.password.length < 6){
let p = new ProblemDetail();
p.type = ProblemT.Less
p.lessNum = 6-this.password.length
this.problems.push(p)
}
if(this.password.length > 20) {
let p = new ProblemDetail()
p.type = ProblemT.More
p.moreNum = this.password.length-20
this.problems.push(p)
}
let misUp = true
for(let i = 0; i < this.password.length;i++){
let pc = this.password[i]
if(pc == pc.toUpperCase()){
misUp = false
break
}
}
if(misUp){
var p = new ProblemDetail()
p.type = ProblemT.MisUp
this.problems.push(p)
}
let mislow = true
for(let i = 0; i < this.password.length;i++){
let pc = this.password[i]
if(pc == pc.toLowerCase()){
mislow = false
break
}
}
if(mislow){
var p = new ProblemDetail()
p.type = ProblemT.MisLow
this.problems.push(p)
}
let misNum = true
for(let i = 0; i < this.password.length;i++){
let pc = this.password[i]
if(!isNaN(Number(pc)))
{
misNum = false
break
}
}
if(misNum){
let p = new ProblemDetail()
p.type = ProblemT.MisNum
this.problems.push(p)
}
let curChar : string = null
let newNode = new SNode()
newNode.start = 0
newNode.len = 0
newNode.char = curChar
for(let i = 0; i < this.password.length; i++)
{
let pc = this.password[i]
if(pc != curChar)
{
if(newNode.len > 1) {
// this.sameNodes.push(newNode)
let p = new ProblemDetail()
p.type = ProblemT.Same
p.sameNode = newNode
newNode = new SNode()
newNode.start = i
newNode.len = 1
newNode.char = pc
}else {
newNode.start = i
newNode.len = 1
newNode.char = pc
}
curChar = pc
}
else {
newNode.len++;
}
}
if(newNode.len > 1) {
let p = new ProblemDetail()
p.type = ProblemT.Same
p.sameNode = newNode
}
}
//每个问题有最优解 如果满足这个最优解的话则使用对应的解法来解决问题
AddSolve() {
let addNum = 0
let toHanderProblem : ProblemDetail[] = []
for(let i = 0; i < this.problems.length; i++){
let p = this.problems[i]
if(!p.solved){
switch(p.type) {
case ProblemT.Less:
case ProblemT.MisLow:
case ProblemT.MisNum:
case ProblemT.MisUp:
case ProblemT.Same:
toHanderProblem.push(p)
break
}
}
}
if(toHanderProblem.length > 0) {
}
}
SolveUse(method : OpType){
//寻找删除的数量 解决相关问题
if(method == OpType.Add) {
this.AddSolve()
}
}
FixProblem(){
while(true) {
let method = OpType.None
for(let i = 0; i < this.problems.length; i++){
let p = this.problems[i]
if(!p.solved){
switch(p.type){
case ProblemT.Less: {
method = OpType.Add
break
}
case ProblemT.More: {
method = OpType.Add;
break
}
case ProblemT.MisLow:
case ProblemT.MisNum:
case ProblemT.MisUp:
case ProblemT.Same: {
method = OpType.Replace
break
}
default: {
method = OpType.None
break
}
}
}
if(method != OpType.None) break
}
//寻找当前解决问题的方法
if(method != OpType.None){
this.SolveUse(method)
}else {
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment