Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.
You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.
A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.
To make Hello World return string modifiable, can always allocate memory for the string.
Here the code should produce "Xello World." when return string modified.
char* Hi (void) { char* ans=malloc(13); strcpy(ans,"Hello World."); return ans; }
const char* Hi (void)- char* Hi (void)
- {
return("Hello World.");}char *oopsHi(void){return ("Hello World.");- char* ans=malloc(13);
- strcpy(ans,"Hello World.");
- return ans;
- }
#include <criterion/criterion.h> const char* Hi (); Test(the_hello_world_function, should_pass_all_the_tests_provided) { cr_assert_eq(strcmp(Hi(),"Hello World."), 0); char *p = Hi(); *p = 'X'; printf("%s\n",p); }
// TODO: Replace examples and use TDD development by writing your own tests. The code provided here is just a how-to example.- #include <criterion/criterion.h>
// replace with the actual method being tested- const char* Hi ();
Test(the_multiply_function, should_pass_all_the_tests_provided) {cr_assert_eq(Hi(""), "Hello World.");#if 0 // if'd out, reenable to observe crashchar oopsHi();char *p = oopsHi();- Test(the_hello_world_function, should_pass_all_the_tests_provided) {
- cr_assert_eq(strcmp(Hi(),"Hello World."), 0);
- char *p = Hi();
- *p = 'X';
#endif- printf("%s\n",p);
- }
Some of what I did may or may not ruin the language generalizability of the solution :/
// e.g.: // simplify( 15, 5) == [ 3, 1] // simplify(-50, 20) == [- 5, 2] // simplify( 20, - 2) == [-10, 1] // simplify(- 6, - 3) == [ 2, 1] // simplify(- 0, 3) == [ 0, 1] // simplify( 0, 0) == undefined // simplify( 5, - 0) == undefined function simplify(n, d){ if (d === 0) return undefined; if (n === 0) return [0, 1]; // fraction_sign contains the sign of the fraction (1 if positive, -1 if negative) const fraction_sign = Math.sign(n * d); // fraction_gcd contains the greatest common divisor of n and d const fraction_gcd = gcd(Math.abs(n), Math.abs(d)); // we calculate the reduced numerator (it has the same sign as the fraction) const result_n = fraction_sign * Math.abs(n) / fraction_gcd; // we calculate the reduced denominator const result_d = Math.abs(d) / fraction_gcd; return [result_n, result_d]; } // gcd(x, y) calculates the greatest common divisor of x and y // x and y must be non-negative integers // USED ALGORITHM: binary method // BASED ON: https://en.wikipedia.org/wiki/Binary_GCD_algorithm function gcd(x, y){ if (x === y) return x; if (x > y) return gcd(y, x); if (x === 0) return y; switch(2 * (x % 2) + (y % 2)) { case 0: // both are divisible by 2 return gcd(x >> 1, y >> 1) << 1; case 1: // only y is divisible by 2 return gcd(x >> 1, y); case 2: // only x is divisible by 2 return gcd(x, y >> 1); case 3: // neither are divisible by 2 return gcd((y - x) >> 1, x); default: // default case should not run return 0; } }
- // e.g.:
- // simplify( 15, 5) == [ 3, 1]
- // simplify(-50, 20) == [- 5, 2]
- // simplify( 20, - 2) == [-10, 1]
- // simplify(- 6, - 3) == [ 2, 1]
- // simplify(- 0, 3) == [ 0, 1]
- // simplify( 0, 0) == undefined
- // simplify( 5, - 0) == undefined
- function simplify(n, d){
if (d === 0){- if (d === 0)
- return undefined;
}- if (n === 0)
- return [0, 1];
- // fraction_sign contains the sign of the fraction (1 if positive, -1 if negative)
const fraction_sign = ((n < 0) ? -1 : 1) * ((d < 0) ? -1 : 1);- const fraction_sign = Math.sign(n * d);
- // fraction_gcd contains the greatest common divisor of n and d
- const fraction_gcd = gcd(Math.abs(n), Math.abs(d));
- // we calculate the reduced numerator (it has the same sign as the fraction)
- const result_n = fraction_sign * Math.abs(n) / fraction_gcd;
- // we calculate the reduced denominator
- const result_d = Math.abs(d) / fraction_gcd;
- return [result_n, result_d];
- }
- // gcd(x, y) calculates the greatest common divisor of x and y
- // x and y must be non-negative integers
- // USED ALGORITHM: binary method
- // BASED ON: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
- function gcd(x, y){
if (x === y){- if (x === y)
- return x;
}if (x === 0){- if (x > y)
- return gcd(y, x);
- if (x === 0)
- return y;
}if (y === 0){return x;}if (x % 2 === 0){if (y % 2 === 1){return gcd(x >> 1, y);}else {- switch(2 * (x % 2) + (y % 2)) {
- case 0: // both are divisible by 2
- return gcd(x >> 1, y >> 1) << 1;
}}if (y % 2 === 0){return gcd(x, y >> 1);}if (x > y){return gcd((x - y) >> 1, y);- case 1: // only y is divisible by 2
- return gcd(x >> 1, y);
- case 2: // only x is divisible by 2
- return gcd(x, y >> 1);
- case 3: // neither are divisible by 2
- return gcd((y - x) >> 1, x);
- default: // default case should not run
- return 0;
- }
return gcd((y - x) >> 1, x);- }
describe("Examples", function(){ it("Example #1", function(){ console.log("15 / 5 == 3 / 1"); Test.assertDeepEquals(simplify(15, 5), [3, 1]); }); it("Example #2", function(){ console.log("-50 / 20 == -5 / 2"); Test.assertDeepEquals(simplify(-50, 20), [-5, 2]); }); it("Example #3", function(){ console.log("20 / -2 == -10 / 1"); Test.assertDeepEquals(simplify(20, -2), [-10, 1]); }); it("Example #4", function(){ console.log("-6 / -3 == 2 / 1"); Test.assertDeepEquals(simplify(-6, -3), [2, 1]); }); it("Example #5", function(){ console.log("-0 / 3 == 0 / 1"); Test.assertDeepEquals(simplify(-0, 3), [0, 1]); }); it("Example #6", function(){ console.log("0 / 0 == undefined"); Test.assertEquals(simplify(0, 0), undefined); }); it("Example #7", function(){ console.log("5 / -0 == undefined"); Test.assertEquals(simplify(5, -0), undefined); }); });
- describe("Examples", function(){
- it("Example #1", function(){
- console.log("15 / 5 == 3 / 1");
- Test.assertDeepEquals(simplify(15, 5), [3, 1]);
- });
- it("Example #2", function(){
- console.log("-50 / 20 == -5 / 2");
- Test.assertDeepEquals(simplify(-50, 20), [-5, 2]);
- });
- it("Example #3", function(){
- console.log("20 / -2 == -10 / 1");
- Test.assertDeepEquals(simplify(20, -2), [-10, 1]);
- });
- it("Example #4", function(){
- console.log("-6 / -3 == 2 / 1");
- Test.assertDeepEquals(simplify(-6, -3), [2, 1]);
- });
- it("Example #5", function(){
- console.log("-0 / 3 == 0 / 1");
- Test.assertDeepEquals(simplify(-0, 3), [0, 1]);
- });
- it("Example #6", function(){
- console.log("0 / 0 == undefined");
- Test.assertEquals(simplify(0, 0), undefined);
- });
- it("Example #7", function(){
- console.log("5 / -0 == undefined");
- Test.assertEquals(simplify(5, -0), undefined);
- });
- });
Fixed test cases.
var nextGeneration = grid => grid.map((row, rowIndex) => row.map((cell, colIndex) => { let neighbours = -cell; [-1, 0, 1].forEach(dr => [-1, 0, 1].forEach(dc => neighbours += (grid[rowIndex + dr] || [])[colIndex + dc] || 0 ) ); if(neighbours == 3) return 1; if(neighbours != 2) return 0; return cell; }) );
function nextGeneration(grid) {return grid.map((row, rowIndex) => {return row.map((cell, colIndex) => {// Non circular gridif(!rowIndex || !colIndex || rowIndex == grid.length - 1 || colIndex == row.length - 1) {return 0;}const neighbours = grid[rowIndex ][colIndex -1] +grid[rowIndex ][colIndex +1] +grid[rowIndex -1][colIndex -1] +grid[rowIndex -1][colIndex ] +grid[rowIndex -1][colIndex +1] +grid[rowIndex +1][colIndex -1] +grid[rowIndex +1][colIndex ] +grid[rowIndex +1][colIndex +1];- var nextGeneration = grid =>
- grid.map((row, rowIndex) =>
- row.map((cell, colIndex) => {
- let neighbours = -cell;
- [-1, 0, 1].forEach(dr =>
- [-1, 0, 1].forEach(dc =>
- neighbours += (grid[rowIndex + dr] || [])[colIndex + dc] || 0
- )
- );
if(neighbours == 3) return 1;else if(neighbours != 2) return 0;- if(neighbours == 3)
- return 1;
- if(neighbours != 2)
- return 0;
- return cell;
});});}- })
- );
describe("Given empty grid", () => { it("when next generation, should return empty", () => { Test.assertDeepEquals(nextGeneration([]), []); }); }); describe("Given a single cell", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0], [0,1,0], [0,0,0], ]), [ [0,0,0], [0,0,0], [0,0,0], ]); }); }); describe("Given a cell with 1 neighbour at rows", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0], [0,1,1,0], [0,0,0,0], ]), [ [0,0,0,0], [0,0,0,0], [0,0,0,0], ]); }); }); describe("Given a cell with 2 neighbours at rows", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,0,0,0,0], ]), [ [0,0,1,0,0], [0,0,1,0,0], [0,0,1,0,0], ]); }); }); describe("Given a cell with 2 neighbours at cols", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0], [0,1,0], [0,1,0], [0,1,0], [0,0,0], ]), [ [0,0,0], [0,0,0], [1,1,1], [0,0,0], [0,0,0], ]); }); }); describe("Given a cell with 2 neighbours at \ (diagonal)", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,0,0], [0,0,1,0,0], [0,0,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 2 neighbours at / (diagonal)", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,0,0,1,0], [0,0,1,0,0], [0,1,0,0,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 4 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,1,0], [0,0,1,0,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,1,0,0], [0,1,0,1,0], [0,0,1,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 5 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,0,1,0,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,1,0,0], [0,1,1,1,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 6 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,1,1,0,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,1,0,0], [0,1,0,1,0], [1,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 7 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,1,1,1,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,1,0,0], [0,1,0,1,0], [1,0,0,0,1], [0,1,0,1,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 8 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,1,1,1,0], [0,1,1,1,0], [0,0,0,0,0], ]), [ [0,0,1,0,0], [0,1,0,1,0], [1,0,0,0,1], [0,1,0,1,0], [0,0,1,0,0], ]); }); }); describe("Given a die cell with 2 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a die cell with 3 neighbours", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a die cell with 4 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Corner test", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0], [1,1,0], [1,1,0], ]), [ [0,0,0], [1,1,0], [1,1,0], ]); }); });
- describe("Given empty grid", () => {
- it("when next generation, should return empty", () => {
- Test.assertDeepEquals(nextGeneration([]), []);
- });
- });
- describe("Given a single cell", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0],
- [0,1,0],
- [0,0,0],
- ]), [
- [0,0,0],
- [0,0,0],
- [0,0,0],
- ]);
- });
- });
- describe("Given a cell with 1 neighbour at rows", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0],
- [0,1,1,0],
- [0,0,0,0],
- ]), [
- [0,0,0,0],
- [0,0,0,0],
- [0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 2 neighbours at rows", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,0,0,0,0],
- ]), [
[0,0,0,0,0],- [0,0,1,0,0],
[0,0,0,0,0],- [0,0,1,0,0],
- [0,0,1,0,0],
- ]);
- });
- });
- describe("Given a cell with 2 neighbours at cols", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0],
- [0,1,0],
- [0,1,0],
- [0,1,0],
- [0,0,0],
- ]), [
- [0,0,0],
- [0,0,0],
[0,1,0],- [1,1,1],
- [0,0,0],
- [0,0,0],
- ]);
- });
- });
- describe("Given a cell with 2 neighbours at \ (diagonal)", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,1,0,0],
- [0,0,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 2 neighbours at / (diagonal)", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,0,0,1,0],
- [0,0,1,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 4 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,1,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,1,0,1,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 5 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,0,1,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
[0,0,0,0,0],- [0,0,1,0,0],
- [0,1,1,1,0],
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 6 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,1,1,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
[0,0,0,0,0],- [0,0,1,0,0],
- [0,1,0,1,0],
[0,0,0,0,0],- [1,0,0,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 7 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,1,1,1,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
[0,0,0,0,0],- [0,0,1,0,0],
- [0,1,0,1,0],
[0,0,0,0,0],- [1,0,0,0,1],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 8 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,1,1,1,0],
- [0,1,1,1,0],
- [0,0,0,0,0],
- ]), [
[0,0,0,0,0],- [0,0,1,0,0],
- [0,1,0,1,0],
[0,0,0,0,0],- [1,0,0,0,1],
- [0,1,0,1,0],
[0,0,0,0,0],- [0,0,1,0,0],
- ]);
- });
- });
- describe("Given a die cell with 2 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a die cell with 3 neighbours", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a die cell with 4 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Corner test", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0],
- [1,1,0],
- [1,1,0],
- ]), [
- [0,0,0],
- [1,1,0],
- [1,1,0],
- ]);
- });
- });
class Person attr_accessor :name def initialize(name) self.name = name end end def change_name(new_name, thing) thing.name = new_name if thing.respond_to?(:name=) end
- class Person
- attr_accessor :name
- def initialize(name)
@name = name- self.name = name
- end
- end
- def change_name(new_name, thing)
if thing.respond_to?(:name=)# do somethingthing.name = new_nameelsenilend- thing.name = new_name if thing.respond_to?(:name=)
- end
I prefer looking for overlaps on a min and max of the opposing range.
static void normalize(int & num) { num %= 24; } static bool willTheyMeet( int startHour1, int endHour1, int startHour2, int endHour2) { normalize(startHour1); normalize(startHour2); normalize(endHour1); normalize(endHour2); return (startHour2 >= startHour1 && startHour2 <= endHour1) || (endHour2 >= startHour1 && endHour2 <= endHour1) || (startHour1 >= startHour2 && startHour1 <= endHour2) || (endHour1 >= startHour2 && endHour1 <= endHour2); }
#include <cmath>static float hourToRad(int hour)- static void normalize(int & num)
- {
return (float) hour * (M_PI / 12.0);};- num %= 24;
- }
- static bool willTheyMeet(
int startHour1,int endHour1,int startHour2,int endHour2){auto a1 = hourToRad(startHour1);auto a2 = hourToRad(endHour1);auto b1 = hourToRad(startHour2);auto b2 = hourToRad(endHour2);if (a1 == b2 || a2 == b1)return false;float da = (a2 - a1) / 2.0;float db = (b2 - b1) / 2.0;float ma = (a2 + a1) / 2.0;float mb = (b2 + b1) / 2.0;float cda = cos(da);float cdb = cos(db);return cos(ma - b1) >= cda ||cos(ma - b2) >= cda ||cos(mb - a1) >= cdb ||cos(mb - a2) >= cdb;};- int startHour1,
- int endHour1,
- int startHour2,
- int endHour2) {
- normalize(startHour1);
- normalize(startHour2);
- normalize(endHour1);
- normalize(endHour2);
- return (startHour2 >= startHour1 && startHour2 <= endHour1) ||
- (endHour2 >= startHour1 && endHour2 <= endHour1) ||
- (startHour1 >= startHour2 && startHour1 <= endHour2) ||
- (endHour1 >= startHour2 && endHour1 <= endHour2);
- }
While your lambdas are a cool language feature, they serve no purpose outside of being cool and they take the ability away from the compiler to optimize your code. If you take your test conditions and invert them, then use them as the escape conditions, you reduce your compiled assember instruction count from 104 instructions to 61 instructions, according to GodBolt. If you take out the individual ifs and use a single if with the original logic you had, not-inverted, then you get 51 instructions.
Check them out here: https://godbolt.org/z/R_Of7C
#include <string> bool testPassword(std::string password) // 58 instructions { if (password.size() < 8) return false; if (password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos) return false; if (password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") == std::string::npos) return false; if (password.find_first_of("0123456789") == std::string::npos) return false; return true; } /* 51 instructions #include <string> bool testPassword(std::string password) { if (password.size() >= 8 && password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos && password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos && password.find_first_of("0123456789") != std::string::npos) return true; return false; } */
- #include <string>
bool testPassword(std::string password)- bool testPassword(std::string password) // 58 instructions
- {
auto const has_length{[&]() { return password.size() >= 8; }};auto const has_upper{[&]() { return password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos; }};auto const has_special{[&]() { return password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos; }};auto const has_digit{[&]() { return password.find_first_of("0123456789") != std::string::npos; }};return has_length() && has_upper() && has_special() && has_digit();}- if (password.size() < 8) return false;
- if (password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos) return false;
- if (password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") == std::string::npos) return false;
- if (password.find_first_of("0123456789") == std::string::npos) return false;
- return true;
- }
- /* 51 instructions
- #include <string>
- bool testPassword(std::string password)
- {
- if (password.size() >= 8 &&
- password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos &&
- password.find_first_of("!\"#$%&'()*+'-./;:<>=or?") != std::string::npos &&
- password.find_first_of("0123456789") != std::string::npos)
- return true;
- return false;
- }
- */
def decrypt(code, amount): return ''.join([chr((ord(c)-amount+65)%26+65) if c.isalpha() else c for c in code])
def decrypt(code,amount):word = ""for i in code:if i==" ":word+=" "continuetmp = ord(i)-amountif tmp<65:tmp+=26word+=chr(tmp)return word- def decrypt(code, amount):
- return ''.join([chr((ord(c)-amount+65)%26+65) if c.isalpha() else c for c in code])