Skip to content

Instantly share code, notes, and snippets.

@JustCaptcha
Last active December 10, 2019 15:21
Show Gist options
  • Save JustCaptcha/b04b8ba133e65d6e515bf2a8ebc7f69b to your computer and use it in GitHub Desktop.
Save JustCaptcha/b04b8ba133e65d6e515bf2a8ebc7f69b to your computer and use it in GitHub Desktop.
/*
Imagine you have a bike and a driving license. You also found a job board with a list of companies offering a job. To get the job, you need to fulfill some requirements. There are 10.000 companies on the job board, 10 examples are as follows:
"Company A" requires an apartment or house, and property insurance.
"Company B" requires 5 door car or 4 door car, and a driver's license and car insurance.
"Company C" requires a social security number and a work permit.
"Company D" requires an apartment or a flat or a house.
"Company E" requires a driver's license and a 2 door car or a 3 door car or a 4 door car or a 5 door car.
"Company F" requires a scooter or a bike, or a motorcycle and a driver's license and motorcycle insurance.
"Company G" requires a massage qualification certificate and a liability insurance.
"Company H" requires a storage place or a garage.
"Company J" doesn't require anything, you can come and start working immediately.
"Company K" requires a PayPal account.
*/
// My solution uses Regular Expressions approach.
const board = [
"A#(apartment:house)+propertyI",
"B#(5dcar:4dcar)+driverL+carI",
"C#ssn+wp",
"D#(apartment:flat:house)",
"E#driverI+(2dcar:3dcar:4dcar:5dcar)",
"F#<(scooter:bike)><motorcycle+driverL+motorcycleI>",
"G#massageQC+liabilityI",
"H#(storageP:garage)",
"J#",
"K#PayPalA"
]
// You can add some other props in the same format below
// I suppose, there should be some other conditions, for example, one type of house, work permit, etc.
const person = "driverL+bike";
const init = ((board, person) => {
let offers = [];
let personProps = {};
const patternOr = /([a-zA-Z:]*)/
board.map((offerData) => {
let offer = parseOffer(offerData);
offers.push(initConditions(offer.condition));
personProps = initConditions(person);
});
return {
offers: offers,
personProps: personProps
}
});
const check = (offers, personProps) => {
let fit = 0;
let unfit = 0;
offers.map((offer) => {
if(checkPerson(offer, personProps)) {
fit++;
} else {
unfit++;
};
});
return {
fit: fit,
unfit: unfit
}
}
const parseOffer = (offer) => {
const patterCompany = /([A-Za-z]*)#([0-9a-zA-Z:<>\(\)+ ]*)/
patterCompany.test(offer);
return {
companyName: RegExp.$1,
condition: RegExp.$2,
};
}
const initConditions = (condition) => {
let type = "";
let conditions = [];
const patternUnion = /<([a-zA-Z0-9:+\(\)]*)>/g
if(condition == '') {
type = "EMPTY"
return {
type: type,
condition: condition
}
}
if(patternUnion.test(condition)) {
type = "UNION";
let arr = condition.match(patternUnion);
arr.map((item) => {
let newItem = item.replace(patternUnion, "$1");
newItem = newItem.split("+");
conditions.push(newItem);
});
} else {
type = "SINGLE"
conditions = condition.split("+");
};
return {
type: type,
conditions: conditions
}
}
const checkPerson = (requirement, personProps) => {
switch(requirement.type) {
case "SINGLE": {
return singleCheck(requirement.conditions, personProps);
}
case "UNION": {
return requirement.conditions.some((item) => {
return singleCheck(item, personProps);
});
}
case "EMPTY": { return true };
}
}
const singleCheck = (conditions, personProps) => {
return conditions.every(item => {
return personProps.conditions.some(props => item.includes(props));
});
};
let data = init(board, person);
data.offers.map((i) => console.log(i));
let result = check(data.offers, data.personProps);
console.log(`fit: ${result.fit}, unfit: ${result.unfit}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment