This repository was archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Bullet match #11
Merged
Merged
Bullet match #11
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
20698e3
Add problem description.
bugKrusha 318f50b
Add challenge and protocol conformation.
24a54f0
Add rotate function.
4eb0cb1
Add generateMarkings + helpers.
7b69607
Update verifyOutput.
8925778
Update OutputType to Bool.
67489de
Add entry.
c5e64c2
Add tests.
a30f307
Formatting.
62c06c5
Folder changes.
8fd7879
Finalize challenge.
cc5361d
Modify solution to fix a bug.
026c891
Add ().
912daa7
Change 0 and 1 to false and true respectively, in the problem statement.
e23ec8a
Add new markings for testing.
d968610
Update challenge test to use same data for all tests.
cb6f92d
Folder changes.
3d3c875
Add mirroring note to problem statement.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// BulletMatch.swift | ||
// CodeChallenge | ||
// | ||
// Created by Jon-Tait Beason on 12/19/16. | ||
// Copyright © 2016 iosdevelopers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
/** | ||
Forensic specialists examine spent cartridges for unique | ||
markings left by parts of a gun. They can then compare | ||
these markings to the ones in any gun to determine if there | ||
is a match. A match means that the bullet was fired from | ||
that gun. | ||
|
||
Given two markings, one from a spent cartridge and the other | ||
from a suspected weapon, both as Strings, return true if they match | ||
and false if they don't. | ||
|
||
EXAMPLES: | ||
Match, returns true | ||
"| ||| | |" and | ||
"| ||| | |" | ||
|
||
Match, returns true though one is rotated. Bullets can be rotated. | ||
Your solution should account for the possible rotation of bullets. | ||
"|| ||| | " and | ||
" | || |||" | ||
|
||
Doesn't match, returns false | ||
"| ||| | |" and | ||
"||| | | |" | ||
|
||
NOTE: Markings cannot be flipped. | ||
"| ||" is not a match for | ||
"|| |" | ||
|
||
Problem adapted from http://bit.ly/2h57Wxe | ||
*/ | ||
|
||
final class BulletChallenge: CodeChallengeType { | ||
typealias InputType = (bulletMarkings: String, gunMarkings: String) | ||
typealias OutputType = Bool | ||
|
||
var title = "Bullet Challenge" | ||
private let separator = "#" | ||
|
||
var entries: [CodeChallengeEntry<BulletChallenge>] = [ | ||
bugKrushaBulletMatchEntry | ||
] | ||
|
||
func generateDataset() -> [InputType] { | ||
let bundle = Bundle(for: BulletChallenge.self) | ||
guard | ||
let dataUrl = bundle.url(forResource: "markings_short", withExtension: "json"), | ||
let data = try? Data(contentsOf: dataUrl), | ||
let jsonObjects = try? JSONSerialization.jsonObject(with: data, options: .allowFragments), | ||
let vd = jsonObjects as? [String: Bool] | ||
else { fatalError() } | ||
|
||
verificationData = vd | ||
let markings = verificationData.keys.map { key -> (String, String) in | ||
let components = key.components(separatedBy: self.separator) | ||
let gunMarkings = components[1] | ||
let bulletMarkings = components[0] | ||
return (bulletMarkings: bulletMarkings, gunMarkings: gunMarkings) | ||
} | ||
|
||
return Array(markings) | ||
} | ||
|
||
func verifyOutput(_ output: Bool, forInput input: (bulletMarkings: String, gunMarkings: String)) -> Bool { | ||
return verificationData[input.bulletMarkings + separator + input.gunMarkings] == output | ||
} | ||
} | ||
|
||
private var verificationData = [String: Bool]() | ||
|
||
|
44 changes: 44 additions & 0 deletions
44
CodeChallenge/Challenges/BulletMatch/Entries/BugKrushaBulletMatchEntry.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// BugKrushaBulletMatchEntry.swift | ||
// CodeChallenge | ||
// | ||
// Created by Jon-Tait Beason on 12/19/16. | ||
// Copyright © 2016 iosdevelopers. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
let bugKrushaBulletMatchEntry = CodeChallengeEntry<BulletChallenge>(name: "bugKrusha") { input in | ||
let count = input.0.characters.count | ||
|
||
guard | ||
count == input.1.characters.count | ||
else { return false } | ||
|
||
var resets = count | ||
var bulletIndex = 0 | ||
var gunIndex = 0 | ||
var matches = 0 | ||
|
||
let bulletMarks = input.0.characters.flatMap { String($0) } | ||
var gunMarks = input.1.characters.flatMap { String($0) } | ||
|
||
while resets >= 0 && gunIndex < count { | ||
if bulletMarks[bulletIndex] == gunMarks[gunIndex] { | ||
gunIndex += 1 | ||
matches += 1 | ||
bulletIndex += 1 | ||
} else { | ||
let last = gunMarks.removeLast() | ||
gunMarks = [last] + gunMarks | ||
gunIndex = 0 | ||
bulletIndex = 0 | ||
matches = 0 | ||
|
||
resets -= 1 | ||
} | ||
|
||
if matches == count { return true } | ||
} | ||
return false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the dataset being random because then each test run is going to be run with different inputs, which feels like not the most empirical of comparisons. That may not matter for this particular problem, I haven't spent enough time with it to know, but I thought I'd bring it up.
The alternative would be to generate a dataset randomly as you've done here, and then write that out to a file and add that file to the repo and then just load the file here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Word. Lemme look into that.