private IEnumerable <QuizInfoForDb> CreateQuizInfoForDb(MatchingBlock matchingBlock, IGrouping <string, QuizAnswer> answers) { var ans = answers.ToList() .Select(x => new QuizInfoForDb { QuizId = matchingBlock.Id, IsRightAnswer = matchingBlock.Matches.FirstOrDefault(m => m.GetHashForFixedItem() == x.ItemId)?. GetHashForMovableItem() == x.Text, ItemId = x.ItemId, Text = x.Text, QuizType = typeof(MatchingBlock), QuizBlockScore = 0, QuizBlockMaxScore = matchingBlock.MaxScore }).ToList(); var isRightQuizBlock = ans.All(x => x.IsRightAnswer); var blockScore = isRightQuizBlock ? matchingBlock.MaxScore : 0; foreach (var info in ans) { info.QuizBlockScore = blockScore; } return(ans); }
private static QuizAnswerInfo GetMatchingBlockAnswerInfo(IReadOnlyDictionary <string, List <UserQuiz> > answers, MatchingBlock block, int questionIndex) { IEnumerable <UserQuiz> userAnswers = new List <UserQuiz>(); UserQuiz firstAnswer = null; if (answers.ContainsKey(block.Id)) { userAnswers = answers[block.Id].Where(q => q.ItemId != null); firstAnswer = userAnswers.FirstOrDefault(); } var isRightMatches = new List <bool>(); foreach (var match in block.Matches) { var userAnswer = userAnswers.FirstOrDefault(a => a.ItemId == match.GetHashForFixedItem()); isRightMatches.Add(userAnswer != null && userAnswer.IsRightAnswer); } return(new MatchingBlockAnswerInfo { Id = questionIndex.ToString(), IsRightMatches = isRightMatches, Score = firstAnswer?.QuizBlockScore ?? 0, MaxScore = firstAnswer?.QuizBlockMaxScore ?? 0, }); }
public static MatchingBlock[] GetMatchingBlocks(int len1, int len2, OpCode[] ops) { int n = ops.Length; int noOfMB, i; int o = 0; noOfMB = 0; for (i = n; i-- != 0; o++) { if (ops[o].EditType == EditType.KEEP) { noOfMB++; while (i != 0 && ops[o].EditType == EditType.KEEP) { i--; o++; } if (i == 0) { break; } } } MatchingBlock[] matchingBlocks = new MatchingBlock[noOfMB + 1]; int mb = 0; o = 0; matchingBlocks[mb] = new MatchingBlock(); for (i = n; i != 0; i--, o++) { if (ops[o].EditType == EditType.KEEP) { matchingBlocks[mb].SourcePos = ops[o].SourceBegin; matchingBlocks[mb].DestPos = ops[o].DestBegin; while (i != 0 && ops[o].EditType == EditType.KEEP) { i--; o++; } if (i == 0) { matchingBlocks[mb].Length = len1 - matchingBlocks[mb].SourcePos; mb++; break; } matchingBlocks[mb].Length = ops[o].SourceBegin - matchingBlocks[mb].SourcePos; mb++; matchingBlocks[mb] = new MatchingBlock(); } } Debug.Assert(mb != noOfMB); MatchingBlock finalBlock = new MatchingBlock { SourcePos = len1, DestPos = len2, Length = 0 }; matchingBlocks[mb] = finalBlock; return(matchingBlocks); }
private static MatchingBlock[] GetMatchingBlocks(int len1, int len2, EditOp[] ops) { int n = ops.Length; int numberOfMatchingBlocks, i, SourcePos, DestPos; numberOfMatchingBlocks = 0; int o = 0; SourcePos = DestPos = 0; EditType type; for (i = n; i != 0;) { while (ops[o].EditType == EditType.KEEP && --i != 0) { o++; } if (i == 0) { break; } if (SourcePos < ops[o].SourcePos || DestPos < ops[o].DestPos) { numberOfMatchingBlocks++; SourcePos = ops[o].SourcePos; DestPos = ops[o].DestPos; } type = ops[o].EditType; switch (type) { case EditType.REPLACE: do { SourcePos++; DestPos++; i--; o++; } while (i != 0 && ops[o].EditType == type && SourcePos == ops[o].SourcePos && DestPos == ops[o].DestPos); break; case EditType.DELETE: do { SourcePos++; i--; o++; } while (i != 0 && ops[o].EditType == type && SourcePos == ops[o].SourcePos && DestPos == ops[o].DestPos); break; case EditType.INSERT: do { DestPos++; i--; o++; } while (i != 0 && ops[o].EditType == type && SourcePos == ops[o].SourcePos && DestPos == ops[o].DestPos); break; default: break; } } if (SourcePos < len1 || DestPos < len2) { numberOfMatchingBlocks++; } MatchingBlock[] matchingBlocks = new MatchingBlock[numberOfMatchingBlocks + 1]; o = 0; SourcePos = DestPos = 0; int mbIndex = 0; for (i = n; i != 0;) { while (ops[o].EditType == EditType.KEEP && --i != 0) { o++; } if (i == 0) { break; } if (SourcePos < ops[o].SourcePos || DestPos < ops[o].DestPos) { MatchingBlock mb = new MatchingBlock(); mb.SourcePos = SourcePos; mb.DestPos = DestPos; mb.Length = ops[o].SourcePos - SourcePos; SourcePos = ops[o].SourcePos; DestPos = ops[o].DestPos; matchingBlocks[mbIndex++] = mb; } type = ops[o].EditType; switch (type) { case EditType.REPLACE: do { SourcePos++; DestPos++; i--; o++; } while (i != 0 && ops[o].EditType == type && SourcePos == ops[o].SourcePos && DestPos == ops[o].DestPos); break; case EditType.DELETE: do { SourcePos++; i--; o++; } while (i != 0 && ops[o].EditType == type && SourcePos == ops[o].SourcePos && DestPos == ops[o].DestPos); break; case EditType.INSERT: do { DestPos++; i--; o++; } while (i != 0 && ops[o].EditType == type && SourcePos == ops[o].SourcePos && DestPos == ops[o].DestPos); break; default: break; } } if (SourcePos < len1 || DestPos < len2) { Debug.Assert(len1 - SourcePos == len2 - DestPos); MatchingBlock mb = new MatchingBlock(); mb.SourcePos = SourcePos; mb.DestPos = DestPos; mb.Length = len1 - SourcePos; matchingBlocks[mbIndex++] = mb; } Debug.Assert(numberOfMatchingBlocks == mbIndex); MatchingBlock finalBlock = new MatchingBlock(); finalBlock.SourcePos = len1; finalBlock.DestPos = len2; finalBlock.Length = 0; matchingBlocks[mbIndex] = finalBlock; return(matchingBlocks); }
private static IEnumerable <MatchingBlock> GetMatchingBlocks(int len1, int len2, EditOperations[] ops) { var n = ops.Length; int i; var numberOfMatchingBlocks = 0; var o = 0; var spos = 0; var dpos = 0; EditType type; for (i = n; i != 0;) { while (ops[o].Type == EditType.Keep && --i != 0) { o++; } if (i == 0) { break; } if (spos < ops[o].Spos || dpos < ops[o].Dpos) { numberOfMatchingBlocks++; spos = ops[o].Spos; dpos = ops[o].Dpos; } type = ops[o].Type; switch (type) { case EditType.Replace: do { spos++; dpos++; i--; o++; } while (i != 0 && ops[o].Type == type && spos == ops[o].Spos && dpos == ops[o].Dpos); break; case EditType.Delete: do { spos++; i--; o++; } while (i != 0 && ops[o].Type == type && spos == ops[o].Spos && dpos == ops[o].Dpos); break; case EditType.Insert: do { dpos++; i--; o++; } while (i != 0 && ops[o].Type == type && spos == ops[o].Spos && dpos == ops[o].Dpos); break; } } if (spos < len1 || dpos < len2) { numberOfMatchingBlocks++; } var matchingBlocks = new MatchingBlock[numberOfMatchingBlocks + 1]; o = 0; spos = dpos = 0; var mbIndex = 0; for (i = n; i != 0;) { while (ops[o].Type == EditType.Keep && --i != 0) { o++; } if (i == 0) { break; } if (spos < ops[o].Spos || dpos < ops[o].Dpos) { var mb = new MatchingBlock { Spos = spos, Dpos = dpos, Length = ops[o].Spos - spos }; spos = ops[o].Spos; dpos = ops[o].Dpos; matchingBlocks[mbIndex++] = mb; } type = ops[o].Type; switch (type) { case EditType.Replace: do { spos++; dpos++; i--; o++; } while (i != 0 && ops[o].Type == type && spos == ops[o].Spos && dpos == ops[o].Dpos); break; case EditType.Delete: do { spos++; i--; o++; } while (i != 0 && ops[o].Type == type && spos == ops[o].Spos && dpos == ops[o].Dpos); break; case EditType.Insert: do { dpos++; i--; o++; } while (i != 0 && ops[o].Type == type && spos == ops[o].Spos && dpos == ops[o].Dpos); break; } } if (spos < len1 || dpos < len2) { if (len1 - spos != len2 - dpos) { throw new ArgumentException(@"¯\_(ツ)_/¯"); } var mb = new MatchingBlock { Spos = spos, Dpos = dpos, Length = len1 - spos }; matchingBlocks[mbIndex++] = mb; } if (numberOfMatchingBlocks != mbIndex) { throw new ArgumentException(@"¯\_(ツ)_/¯"); } var finalBlock = new MatchingBlock { Spos = len1, Dpos = len2, Length = 0 }; matchingBlocks[mbIndex] = finalBlock; return(matchingBlocks); }