2 new Coding Contracts

also run format and lint on it this time 😉
This commit is contained in:
Hedrauta
2022-04-07 06:16:14 +02:00
parent 7d4bf90b40
commit e286ee1de5
2 changed files with 156 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
import { getRandomInt } from "../utils/helpers/getRandomInt";
import { HammingEncode, HammingDecode } from "../utils/HammingCodeTools";
/* tslint:disable:completed-docs no-magic-numbers arrow-return-shorthand */
/* Function that generates a valid 'data' for a contract type */
@@ -1008,4 +1009,62 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
return true;
},
},
{
name: "HammingCodes: Integer to encoded Binary",
numTries: 10,
difficulty: 5,
desc: (n: number): string => {
return [
"You are given the following decimal Value: \n",
`${n} \n`,
"Convert it into a binary string and encode it as a 'Hamming-Code'. eg:\n ",
"Value 8 will result into binary '1000', which will be encoded",
"with the pattern 'pppdpddd', where p is a paritybit and d a databit,\n",
"or '10101' (Value 21) will result into (pppdpdddpd) '1111101011'.\n\n",
"NOTE: You need an parity Bit on Index 0 as an 'overall'-paritybit. \n",
"NOTE 2: You should watch the HammingCode-video from 3Blue1Brown, which explains the 'rule' of encoding,",
"including the first Index parity-bit mentioned on the first note.\n\n",
"Now the only one rule for this encoding:\n",
" It's not allowed to add additional leading '0's to the binary value\n",
"That means, the binary value has to be encoded as it is",
].join(" ");
},
gen: (): number => {
return getRandomInt(Math.pow(2, 4), Math.pow(2, getRandomInt(1, 57)));
},
solver: (data: number, ans: string): boolean => {
return ans === HammingEncode(data);
},
},
{
name: "HammingCodes: Encoded Binary to Integer",
difficulty: 8,
numTries: 10,
desc: (n: string): string => {
return [
"You are given the following encoded binary String: \n",
`'${n}' \n`,
"Treat it as a Hammingcode with 1 'possible' error on an random Index.\n",
"Find the 'possible' wrong bit, fix it and extract the decimal value, which is hidden inside the string.\n\n",
"Note: The length of the binary string is dynamic, but it's encoding/decoding is following Hammings 'rule'\n",
"Note 2: Index 0 is an 'overall' parity bit. Watch the Hammingcode-video from 3Blue1Brown for more information\n",
"Note 3: There's a ~55% chance for an altered Bit. So... MAYBE there is an altered Bit 😉\n",
"Extranote for automation: return the decimal value as a string",
].join(" ");
},
gen: (): string => {
const _alteredBit = Math.round(Math.random());
const _buildArray: Array<string> = HammingEncode(
getRandomInt(Math.pow(2, 4), Math.pow(2, getRandomInt(1, 57))),
).split("");
if (_alteredBit) {
const _randomIndex: number = getRandomInt(0, _buildArray.length - 1);
_buildArray[_randomIndex] = _buildArray[_randomIndex] == "0" ? "1" : "0";
}
return _buildArray.join("");
},
solver: (data: string, ans: string): boolean => {
return parseInt(ans, 10) === HammingDecode(data);
},
},
];