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.
This is bobble sort alghoritm.
# Python3 Optimized implementation
# of Bubble sort
# An optimized version of Bubble Sort
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
swapped = False
# Last i elements are already
# in place
for j in range(0, n-i-1):
# traverse the array from 0 to
# n-i-1. Swap if the element
# found is greater than the
# next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
# IF no two elements were swapped
# by inner loop, then break
if swapped == False:
break
# Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print ("Sorted array :")
for i in range(len(arr)):
print ("%d" %arr[i],end=" ")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
int foo() { return 1; }
int bar() { return 2; }
int baz() { return 3; }
Describe(function_foo) {
It(should_return_zero) {
Assert::That(foo(), Equals(0));
}
};
Describe(function_bar) {
It(should_return_zero) {
Assert::That(bar(), Equals(0));
}
};
Describe(function_baz) {
It(should_return_zero) {
Assert::That(baz(), Equals(0));
}
};
Your task is make a calculator.
its rule is like polish notation.
you only need to deal with add, minum, multiply and divide.
e.g.
run(add,1,2) -> 3
run(minum,10,2,2) -> 6
run(multiply,2,25,2) -> 100
(The number of elements to be calculated is unlimited)
add = lambda *args:sum(args)
def minum(*args):
xxx = args[0]
for i in range(1,len(args)):
xxx = xxx - args[i]
return xxx
def multiply(*args):
xxx = 1
for i in args:
xxx = xxx * i
return xxx
def divide(*args):
xxx = args[0]
for i in args[1:]:
xxx = xxx / i
return xxx
def run(func,*args):
return func(*args)
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(run(add,100,100,1000,500,300),2000)
test.assert_equals(run(minum,1000,100),900)
test.assert_equals(run(multiply,12,2),24)
test.assert_equals(run(divide,100,25),4)
right
function returnAscending(n){
return +(n+'').split('').sort(function(a,b){return a-b }).join('');
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});
2048 is a single-player sliding block puzzle game designed by Italian web developer Gabriele Cirulli. The objective of the game is to slide numbered tiles on a grid to combine them to create a tile with the number 2048; however, one can continue to play the game after reaching the goal, creating tiles with larger numbers.
The question is how high you can go.
Every turn, a new tile will randomly appear in an empty spot on the board with a value of either 2 or 4. If two tiles of the same number collide, they will merge into a tile with the total value of the two tiles that collided.
For a given board size, return the highest numbers, and sum of all numbers.
For example, for a normal 4x4 board and you are really lucky, the best possible board is:
131072 65536 32768 16384
1024 2048 4096 8192
512 256 128 64
4 8 16 32
The best possible block is 131072, the sum of all blocks is 262140
def calculate_2048(size):
sq = 4
for _ in range(0,size*size-1): #calculating the highest square by simply multiplying it by 2
sq *= 2
highest = sq
sumtotal = sq
for _ in range(0,size*size-1): #calculating the highest score by adding up every number back down to 4
sq = int(sq/2)
sumtotal = int(sumtotal+sq)
return highest, int(sumtotal)
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
import random
# test.assert_equals(actual, expected, [optional] message)
@test.describe("2048")
def test_group():
@test.it("lower numbers")
def test_case():
test.assert_equals(calculate_2048(4), (131072,262140))
test.assert_equals(calculate_2048(1), (4,4))
test.assert_equals(calculate_2048(5), (67108864,134217724))
@test.it("random cases")
def random_tests():
sizes = [random.randint(1,20) for _ in range(10)]
for s in sizes:
test.assert_equals(calculate_2048(s), (2**(s*s+1),2**(s*s+2)-4)) #don't tell the actual solver it's this easy
Converts the string to Brainfuck.
#include <stdio.h>
#include <string.h>
// #define DEBUG
char s[4096] = "";
int s2b(int argc, char *argv[]) {
register int i = 0,j;
register int k,m;
register int l,last = 0;
if (argc != 2) {
printf("Usage: %s [string]",*argv);
return 1;
}
while (argv[1][++i]!=0)
continue;
for (j=0; j<i; j++) {
register int t;
t = argv[1][j];
l = k = t-last;
last = t;
#ifdef DEBUG
printf("\n%d, %d, %d, %d\n",last,k,j,i);
#endif
if (k >= 0) {
k >>= 3; // k /= 8
l &= 7; //在更多的机器工作,求余
if (k == 1)
strcat(s,">++++++++");
else {
if (k != 0) {
strcat(s,"++++++++[>");
for (m=0; m<k; m++)
strcat(s,"+");
strcat(s,"<-]>");
} else {
strcat(s,">");
}
}
for (m=0; m<l; m++)
strcat(s,"+");
strcat(s,".");
} else {
k = -k >> 3; // k /= 8
l = -l & 7; //在更多的机器工作,求余
if (k == 1)
strcat(s,">--------");
else {
strcat(s,"++++++++[>");
for (m=0; m<k; m++)
strcat(s,"-");
strcat(s,"<-]>");
}
for (m=0; m<l; m++)
strcat(s,"-");
strcat(s,".");
}
#ifdef DEBUG
printf("\n%d, %d, %d\n",m,k,l);
#endif
strcat(s,"<");
}
return 0;
}
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
#include <criterion/criterion.h>
#include <string.h>
// replace with the actual method being tested
int s2b(int argc, char *argv[]);
extern char s[];
Test(the_multiply_function, should_pass_all_the_tests_provided) {
char *argv[] ={"","hello, world!"};
s2b(2,argv);
cr_assert_eq(strcmp(s,"++++++++[>+++++++++++++<-]>.<++++++++[><-]>---.<>+++++++.<>.<>+++.<++++++++[>--------<-]>---.<>------------.<++++++++[>++++++++++<-]>+++++++.<>--------.<>+++.<++++++++[><-]>------.<>--------.<++++++++[>--------<-]>---.<"), 0);
}
fn around(x: usize, y: usize) -> HashSet<(usize, usize)> {
return [
(Some(x), y.checked_add(1)),
(Some(x), y.checked_sub(1)),
(x.checked_add(1), Some(y)),
(x.checked_sub(1), Some(y)),
]
.iter()
.filter_map(|(vv, hh)| vv.and_then(|vvv| hh.and_then(|hhh| Some((vvv, hhh)))))
.collect();
}
// Add your tests here.
// See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(
around(0, 1),
[(0, 2), (0, 0), (1, 1)].iter().cloned().collect()
);
assert_eq!(
around(1, 1),
[(1, 2), (1, 0), (2, 1), (0, 1)].iter().cloned().collect()
);
assert_eq!(
around(1, 0),
[(1, 1), (2, 0), (0, 0)].iter().cloned().collect()
);
assert_eq!(around(0, 0), [(1, 0), (0, 1)].iter().cloned().collect()); }
}
Create a function to be able to add, substract, multiply or divide all the numbers given in the array provided.
Parameters:
1. Array
2. Operator:
-'Add'
-'Substract'
-'Divide'
-'Multiply'
Example:
function: getResult(array, operator)
array: [4, 2, 1]
operator: 'Add'
result: 7
operator: 'Substract'
result: 1
operator: 'Divide'
result: 2
operator: 'Multiply'
result: 8
function getResult(array, operator) {
//Good luck!
}
const chai = require("chai");
const assert = chai.assert;
describe("Result:", function() {
//Zero
let test1 = [0, 1, 2],
test2 = [1, 0, 2],
test3 = [1, 2, 0],
//Negatives
test4 = [-1, 1, 2],
test5 = [1, -1, 2],
test6 = [1, 2, -1],
//String
test7 = ['a', 1, 2],
test8 = [1, 'a', 2],
test9 = [1, 2, 'a'],
//Empty String
test10 = ['', 1, 2],
test11 = [1, '', 2],
test12 = [1, 2, ''],
//Null
test13 = [null, 1, 2],
test14 = [1, null, 2],
test15 = [1, 2, null],
//Large array
test16 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
//Empty array
test17 = [],
//Null
test18 = null,
//Empty String
test19 = '',
//All same
test20 = ['', '', ''],
test21 = ['a', 'a', 'a'],
test22 = [null, null, null],
test23 = [0, 0, 0],
test24 = [-1, -1, -1],
test25 = ['', '', ''];
let res1 = getResult(test1, 'Add'),
res2 = getResult(test1, 'Substract');
res3 = getResult(test1, 'Multiply');
res4 = getResult(test1, 'Divide');
res5 = getResult(test2, 'Add');
res6 = getResult(test2, 'Substract');
res7 = getResult(test2, 'Multiply');
res8 = getResult(test2, 'Divide');
res9 = getResult(test3, 'Add');
res10 = getResult(test3, 'Substract');
res11 = getResult(test3, 'Multiply');
res12 = getResult(test3, 'Divide');
res13 = getResult(test4, 'Add');
res14 = getResult(test4, 'Substract');
res15 = getResult(test4, 'Multiply');
res16 = getResult(test4, 'Divide');
res17 = getResult(test5, 'Add');
res18 = getResult(test5, 'Substract');
res19 = getResult(test5, 'Multiply');
res20 = getResult(test5, 'Divide');
res21 = getResult(test6, 'Add');
res22 = getResult(test6, 'Substract');
res23 = getResult(test6, 'Multiply');
res24 = getResult(test6, 'Divide');
res25 = getResult(test7, 'Add');
res26 = getResult(test7, 'Substract');
res27 = getResult(test7, 'Multiply');
res28 = getResult(test7, 'Divide');
res29 = getResult(test8, 'Add');
res30 = getResult(test8, 'Substract');
res31 = getResult(test8, 'Multiply');
res32 = getResult(test8, 'Divide');
res33 = getResult(test9, 'Add');
res34 = getResult(test9, 'Substract');
res35 = getResult(test9, 'Multiply');
res36 = getResult(test9, 'Divide');
res37 = getResult(test10, 'Add');
res38 = getResult(test10, 'Substract');
res39 = getResult(test10, 'Multiply');
res40 = getResult(test10, 'Divide');
res41 = getResult(test11, 'Add');
res42 = getResult(test11, 'Substract');
res43 = getResult(test11, 'Multiply');
res44 = getResult(test11, 'Divide');
res45 = getResult(test12, 'Add');
res46 = getResult(test12, 'Substract');
res47 = getResult(test12, 'Multiply');
res48 = getResult(test12, 'Divide');
res49 = getResult(test13, 'Add');
res50 = getResult(test13, 'Substract');
res51 = getResult(test13, 'Multiply');
res52 = getResult(test13, 'Divide');
res53 = getResult(test14, 'Add');
res54 = getResult(test14, 'Substract');
res55 = getResult(test14, 'Multiply');
res56 = getResult(test14, 'Divide');
res57 = getResult(test15, 'Add');
res58 = getResult(test15, 'Substract');
res59 = getResult(test15, 'Multiply');
res60 = getResult(test15, 'Divide');
res61 = getResult(test16, 'Add');
res62 = getResult(test16, 'Substract');
res63 = getResult(test16, 'Multiply');
res64 = getResult(test16, 'Divide');
res65 = getResult(test17, 'Add');
res66 = getResult(test17, 'Substract');
res67 = getResult(test17, 'Multiply');
res68 = getResult(test17, 'Divide');
res69 = getResult(test18, 'Add');
res70 = getResult(test18, 'Substract');
res71 = getResult(test18, 'Multiply');
res72 = getResult(test18, 'Divide');
res73 = getResult(test19, 'Add');
res74 = getResult(test19, 'Substract');
res75 = getResult(test19, 'Multiply');
res76 = getResult(test19, 'Divide');
res77 = getResult(test20, 'Add');
res78 = getResult(test20, 'Substract');
res79 = getResult(test20, 'Multiply');
res80 = getResult(test20, 'Divide');
res81 = getResult(test21, 'Add');
res82 = getResult(test21, 'Substract');
res83 = getResult(test21, 'Multiply');
res84 = getResult(test21, 'Divide');
res85 = getResult(test22, 'Add');
res86 = getResult(test22, 'Substract');
res87 = getResult(test22, 'Multiply');
res88 = getResult(test22, 'Divide');
res89 = getResult(test23, 'Add');
res90 = getResult(test23, 'Substract');
res91 = getResult(test23, 'Multiply');
res92 = getResult(test23, 'Divide');
res93 = getResult(test24, 'Add');
res94 = getResult(test24, 'Substract');
res95 = getResult(test24, 'Multiply');
res96 = getResult(test24, 'Divide');
res97 = getResult(test25, 'Add');
res98 = getResult(test25, 'Substract');
res99 = getResult(test25, 'Multiply');
res100 = getResult(test25, 'Divide');
it("Test", function() {
console.log('Add the array: ', test1);
assert.equal(res1, 3);
});
it("Test", function() {
console.log('Substract the array: ', test1);
assert.equal(res2, -3);
});
it("Test", function() {
console.log('Multiply the array: ', test1);
assert.equal(res3, 0);
});
it("Test", function() {
console.log('Divide the array: ', test1);
assert.equal(res4, 0);
});
it("Test", function() {
console.log('Add the array: ', test2);
assert.equal(res5, 3);
});
it("Test", function() {
console.log('Substract the array: ', test2);
assert.equal(res6, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test2);
assert.equal(res7, 0);
});
it("Test", function() {
console.log('Divide the array: ', test2);
assert.equal(res8, Infinity);
});
it("Test", function() {
console.log('Add the array: ', test3);
assert.equal(res9, 3);
});
it("Test", function() {
console.log('Substract the array: ', test3);
assert.equal(res10, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test3);
assert.equal(res11, 0);
});
it("Test", function() {
console.log('Divide the array: ', test3);
assert.equal(res12, Infinity);
});
it("Test", function() {
console.log('Add the array: ', test4);
assert.equal(res13, 2);
});
it("Test", function() {
console.log('Substract the array: ', test4);
assert.equal(res14, -4);
});
it("Test", function() {
console.log('Multiply the array: ', test4);
assert.equal(res15, -2);
});
it("Test", function() {
console.log('Divide the array: ', test4);
assert.equal(res16, -0.5);
});
it("Test", function() {
console.log('Add the array: ', test5);
assert.equal(res17, 2);
});
it("Test", function() {
console.log('Substract the array: ', test5);
assert.equal(res18, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test5);
assert.equal(res19, -2);
});
it("Test", function() {
console.log('Divide the array: ', test5);
assert.equal(res20, -0.5);
});
it("Test", function() {
console.log('Add the array: ', test6);
assert.equal(res21, 2);
});
it("Test", function() {
console.log('Substract the array: ', test6);
assert.equal(res22, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test6);
assert.equal(res23, -2);
});
it("Test", function() {
console.log('Divide the array: ', test6);
assert.equal(res24, -0.5);
});
it("Test", function() {
console.log('Add the array: ', test7);
assert.equal(res25, 3);
});
it("Test", function() {
console.log('Substract the array: ', test7);
assert.equal(res26, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test7);
assert.equal(res27, 2);
});
it("Test", function() {
console.log('Divide the array: ', test7);
assert.equal(res28, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test8);
assert.equal(res29, 3);
});
it("Test", function() {
console.log('Substract the array: ', test8);
assert.equal(res30, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test8);
assert.equal(res31, 2);
});
it("Test", function() {
console.log('Divide the array: ', test8);
assert.equal(res32, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test9);
assert.equal(res33, 3);
});
it("Test", function() {
console.log('Substract the array: ', test9);
assert.equal(res34, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test9);
assert.equal(res35, 2);
});
it("Test", function() {
console.log('Divide the array: ', test9);
assert.equal(res36, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test10);
assert.equal(res37, 3);
});
it("Test", function() {
console.log('Substract the array: ', test10);
assert.equal(res38, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test10);
assert.equal(res39, 2);
});
it("Test", function() {
console.log('Divide the array: ', test10);
assert.equal(res40, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test11);
assert.equal(res41, 3);
});
it("Test", function() {
console.log('Substract the array: ', test11);
assert.equal(res42, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test11);
assert.equal(res43, 2);
});
it("Test", function() {
console.log('Divide the array: ', test11);
assert.equal(res44, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test12);
assert.equal(res45, 3);
});
it("Test", function() {
console.log('Substract the array: ', test12);
assert.equal(res46, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test12);
assert.equal(res47, 2);
});
it("Test", function() {
console.log('Divide the array: ', test12);
assert.equal(res48, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test13);
assert.equal(res49, 3);
});
it("Test", function() {
console.log('Substract the array: ', test13);
assert.equal(res50, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test13);
assert.equal(res51, 2);
});
it("Test", function() {
console.log('Divide the array: ', test13);
assert.equal(res52, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test14);
assert.equal(res53, 3);
});
it("Test", function() {
console.log('Substract the array: ', test14);
assert.equal(res54, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test14);
assert.equal(res55, 2);
});
it("Test", function() {
console.log('Divide the array: ', test14);
assert.equal(res56, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test15);
assert.equal(res57, 3);
});
it("Test", function() {
console.log('Substract the array: ', test15);
assert.equal(res58, -1);
});
it("Test", function() {
console.log('Multiply the array: ', test15);
assert.equal(res59, 2);
});
it("Test", function() {
console.log('Divide the array: ', test15);
assert.equal(res60, 0.5);
});
it("Test", function() {
console.log('Add the array: ', test16);
assert.equal(res61, 465);
});
it("Test", function() {
console.log('Substract the array: ', test16);
assert.equal(res62, -463);
});
it("Test", function() {
console.log('Multiply the array: ', test16);
assert.equal(res63, 2.6525285981219103e+32);
});
it("Test", function() {
console.log('Divide the array: ', test16);
assert.equal(res64, 3.769987628815906e-33);
});
it("Test", function() {
console.log('Add the array: ', test17);
assert.equal(res65, 0);
});
it("Test", function() {
console.log('Substract the array: ', test17);
assert.equal(res66, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test17);
assert.equal(res67, 0);
});
it("Test", function() {
console.log('Divide the array: ', test17);
assert.equal(res68, 0);
});
it("Test", function() {
console.log('Add the array: ', test18);
assert.equal(res69, 0);
});
it("Test", function() {
console.log('Substract the array: ', test18);
assert.equal(res70, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test18);
assert.equal(res71, 0);
});
it("Test", function() {
console.log('Divide the array: ', test18);
assert.equal(res72, 0);
});
it("Test", function() {
console.log('Add the array: ', test19);
assert.equal(res73, 0);
});
it("Test", function() {
console.log('Substract the array: ', test19);
assert.equal(res74, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test19);
assert.equal(res75, 0);
});
it("Test", function() {
console.log('Divide the array: ', test19);
assert.equal(res76, 0);
});
it("Test", function() {
console.log('Add the array: ', test20);
assert.equal(res77, 0);
});
it("Test", function() {
console.log('Substract the array: ', test20);
assert.equal(res78, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test20);
assert.equal(res79, 0);
});
it("Test", function() {
console.log('Divide the array: ', test20);
assert.equal(res80, 0);
});
it("Test", function() {
console.log('Add the array: ', test21);
assert.equal(res81, 0);
});
it("Test", function() {
console.log('Substract the array: ', test21);
assert.equal(res82, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test21);
assert.equal(res83, 0);
});
it("Test", function() {
console.log('Divide the array: ', test21);
assert.equal(res84, 0);
});
it("Test", function() {
console.log('Add the array: ', test22);
assert.equal(res85, 0);
});
it("Test", function() {
console.log('Substract the array: ', test22);
assert.equal(res86, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test22);
assert.equal(res87, 0);
});
it("Test", function() {
console.log('Divide the array: ', test22);
assert.equal(res88, 0);
});
it("Test", function() {
console.log('Add the array: ', test23);
assert.equal(res89, 0);
});
it("Test", function() {
console.log('Substract the array: ', test23);
assert.equal(res90, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test23);
assert.equal(res91, 0);
});
it("Test", function() {
console.log('Divide the array: ', test23);
assert.equal(res92, Infinity);
});
it("Test", function() {
console.log('Add the array: ', test24);
assert.equal(res93, -3);
});
it("Test", function() {
console.log('Substract the array: ', test24);
assert.equal(res94, 1);
});
it("Test", function() {
console.log('Multiply the array: ', test24);
assert.equal(res95, -1);
});
it("Test", function() {
console.log('Divide the array: ', test24);
assert.equal(res96, -1);
});
it("Test", function() {
console.log('Add the array: ', test25);
assert.equal(res97, 0);
});
it("Test", function() {
console.log('Substract the array: ', test25);
assert.equal(res98, 0);
});
it("Test", function() {
console.log('Multiply the array: ', test25);
assert.equal(res99, 0);
});
it("Test", function() {
console.log('Divide the array: ', test25);
assert.equal(res100, 0);
});
});
Given a positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form :
"(p1n1)(p2n2)...(pk**nk)"
where a ** b means a to the power of b
with the p(i) in increasing order and n(i) empty if n(i) is 1.
Example: n = 86240 should return "(25)(5)(72)(11)"
require 'prime'
def primeFactors(n)
result = ''
Prime.prime_division(n).each do |n|
result += '(' + n[0].to_s
n[1] < 2 ? result += ')' : result += '**' + n[1].to_s + ')'
end
result
end
Test.assert_equals(primeFactors(7775460), "(2**2)(3**3)(5)(7)(11**2)(17)")
For some unknown reason (there is a reason, but "for some unknown reason" sounds more fun) there is no product function in python
Let's add it!
note: The product of an empty list is 0.
def prod(numbers):
if numbers == []: return 0
product = 1
for number in numbers:
product *= number
return product
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Simple tests")
def test_group():
@test.it("Short lists")
def test_case():
test.assert_equals(prod([1,1]), 1)
test.assert_equals(prod([]), 0)
test.assert_equals(prod(range(1,10)), 362880)