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.
Welcome! This kata will allow you to explore Newton's laws of motion through code. Newton's laws of motion are fundamental principles that describe how objects move in the presence of external forces. This exercise is designed to help you better understand Newton's laws and apply them in a practical context.
You will recieve all the parameters through int arrays of size 4 like this:
[null, 100, 110, 120] Force
[130, null, 140, 150] Radius
[160, 170, null, 180] Mass 1
[190, 200, 210, null] Mass 2
You will have to find out and replace all nulls with their respective value using Newton's laws of motion. So the objective of this kata is to find the unknown value in the formula:
F=G\frac{m_1m_2}{r^2}
Use the formula and values with all decimals, don't round anything up or the value won't be correct (use the gravity constant as it is don't try rounding it up).
You will apply the formula index by index meaning index F[0] in the Force array will be placed in the formula with index 0 from the other 3 arrays (r[0], mass1[0] and mass2[0]), the same with index 1, 2 and 3.
All values will be correct, there will be no negatives, zeros or insanely big values.
The output will be a Double type Array with the 4 values in order. Meaning that index 0 will be the Force, index 1 will be the radius, index 2 will be mass 1 and index 3 will be mass 2.
import java.lang.Math; import java.util.*; public class Kata{ final static double g = 6.674*Math.pow(10,-11); public static double[] newton(Integer[] force, Integer[] radius, Integer[] mass1, Integer[] mass2){ double[] result = new double[4]; for(int i = 0; i<4; i++){ if(force[i] == null){ if(radius[i]==null || mass1[i]==null|| mass2[i]==null){ result[i]=-1; }else{ result[i] = (g*mass1[i]*mass2[i])/Math.pow(radius[i],2); } }else if(radius[i] == null){ if(force[i]==null || mass1[i]==null|| mass2[i]==null){ result[i]=-1; }else{ result[i] = Math.sqrt((g * mass1[i] * mass2[i]) / force[i]); } }else if(mass1[i] == null){ if(force[i]==null || radius[i]==null|| mass2[i]==null){ result[i]=-1; }else{ result[i] = (force[i]*Math.pow(radius[i],2))/(g*mass2[i]); } }else if(mass2[i] == null){ if(force[i]==null || radius[i]==null|| mass1[i]==null){ result[i]=-1; }else{ result[i] = (force[i]*Math.pow(radius[i],2))/(g*mass1[i]); } }else{ result[i] = 0; } } return result; } }
- import java.lang.Math;
- import java.util.*;
- public class Kata{
- final static double g = 6.674*Math.pow(10,-11);
public static double[] newton(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){- public static double[] newton(Integer[] force, Integer[] radius, Integer[] mass1, Integer[] mass2){
- double[] result = new double[4];
double g = 6.674*Math.pow(10,-11);result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2);result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]);result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]);result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]);- for(int i = 0; i<4; i++){
- if(force[i] == null){
- if(radius[i]==null || mass1[i]==null|| mass2[i]==null){
- result[i]=-1;
- }else{
- result[i] = (g*mass1[i]*mass2[i])/Math.pow(radius[i],2);
- }
- }else if(radius[i] == null){
- if(force[i]==null || mass1[i]==null|| mass2[i]==null){
- result[i]=-1;
- }else{
- result[i] = Math.sqrt((g * mass1[i] * mass2[i]) / force[i]);
- }
- }else if(mass1[i] == null){
- if(force[i]==null || radius[i]==null|| mass2[i]==null){
- result[i]=-1;
- }else{
- result[i] = (force[i]*Math.pow(radius[i],2))/(g*mass2[i]);
- }
- }else if(mass2[i] == null){
- if(force[i]==null || radius[i]==null|| mass1[i]==null){
- result[i]=-1;
- }else{
- result[i] = (force[i]*Math.pow(radius[i],2))/(g*mass1[i]);
- }
- }else{
- result[i] = 0;
- }
- }
- return result;
- }
- }
import java.lang.Math; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { private static void Test(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2,double[] expected) { double[] actual = Kata.newton(f, r, m1, m2); int n=0; for(int i=0;i<4;i++) { if(actual[i]==expected[i]) n++; } assertEquals(4, n); } double[] newtonSol(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){ double[] result = new double[4]; double g = 6.674*Math.pow(10,-11); result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2); result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]); result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]); result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]); return result; } @Test void testRandom() { for(int i=0;i<40;i++){ Integer[] f=new Integer[4]; Integer[] r=new Integer[4]; Integer[] m1=new Integer[4]; Integer[] m2=new Integer[4]; for(int d=0;d<4;d++){ f[d]=(int)(Math.random()*250)+10; r[d]=(int)(Math.random()*250)+10; m1[d]=(int)(Math.random()*250)+10; m2[d]=(int)(Math.random()*250)+10; } f[0]=null; r[1]=null; m1[2]=null; m2[3]=null; double[] expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); } } @Test void testFixed(){ Integer[] f={null, 100, 150, 120}; Integer[] r={100, null, 150, 120}; Integer[] m1={150, 100, null, 120}; Integer[] m2={120, 100, 150, null}; double[] expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); f= new Integer[] {null, 106, 150, 140}; r=new Integer[] {100, null, 65, 120}; m1=new Integer[] {150, 112, null, 120}; m2=new Integer[] {120, 448, 150, null}; expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); f=new Integer[] {null, 106, 150, 240}; r=new Integer[] {100, null, 65, 120}; m1=new Integer[] {103, 112, null, 120}; m2=new Integer[] {120, 241, 111, null}; expected = newtonSol(f, r, m1, m2); Test(f,r,m1,m2,expected); } @Test void testFixed2(){ double [] expected = newtonSol(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40}, new Integer[]{20,60,null,40},new Integer[]{20,40,60,null}); Test(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40}, new Integer[]{20,60,null,40},new Integer[]{20,40,60,null},expected); expected = newtonSol(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14}, new Integer[]{17,17,null,17},new Integer[]{15,15,15,null}); Test(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14}, new Integer[]{17,17,null,17},new Integer[]{15,15,15,null},expected); expected = newtonSol(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2}, new Integer[]{3,1,null,2},new Integer[]{3,2,1,null}); Test(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2}, new Integer[]{3,1,null,2},new Integer[]{3,2,1,null},expected); } }
- import java.lang.Math;
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- private static void Test(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2,double[] expected) {
- double[] actual = Kata.newton(f, r, m1, m2);
- int n=0;
- for(int i=0;i<4;i++) {
- if(actual[i]==expected[i]) n++;
- }
- assertEquals(4, n);
- }
- double[] newtonSol(Integer[] f, Integer[] r, Integer[] m1, Integer[] m2){
- double[] result = new double[4];
- double g = 6.674*Math.pow(10,-11);
- result[0] = (g*m1[0]*m2[0])/Math.pow(r[0],2);
- result[1] = Math.sqrt((g * m1[1] * m2[1]) / f[1]);
- result[2] = (f[2]*Math.pow(r[2],2))/(g*m2[2]);
- result[3] = (f[3]*Math.pow(r[3],2))/(g*m1[3]);
- return result;
- }
- @Test
- void testRandom() {
- for(int i=0;i<40;i++){
- Integer[] f=new Integer[4];
- Integer[] r=new Integer[4];
- Integer[] m1=new Integer[4];
- Integer[] m2=new Integer[4];
- for(int d=0;d<4;d++){
- f[d]=(int)(Math.random()*250)+10;
- r[d]=(int)(Math.random()*250)+10;
- m1[d]=(int)(Math.random()*250)+10;
- m2[d]=(int)(Math.random()*250)+10;
- }
f[0]=null;r[1]=null;m1[2]=null;m2[3]=null;- f[0]=null; r[1]=null; m1[2]=null; m2[3]=null;
- double[] expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- }
- }
- @Test
- void testFixed(){
- Integer[] f={null, 100, 150, 120};
- Integer[] r={100, null, 150, 120};
- Integer[] m1={150, 100, null, 120};
- Integer[] m2={120, 100, 150, null};
- double[] expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- f= new Integer[] {null, 106, 150, 140};
- r=new Integer[] {100, null, 65, 120};
- m1=new Integer[] {150, 112, null, 120};
- m2=new Integer[] {120, 448, 150, null};
- expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- f=new Integer[] {null, 106, 150, 240};
- r=new Integer[] {100, null, 65, 120};
- m1=new Integer[] {103, 112, null, 120};
- m2=new Integer[] {120, 241, 111, null};
- expected = newtonSol(f, r, m1, m2);
- Test(f,r,m1,m2,expected);
- }
- @Test
- void testFixed2(){
double [] expected = newtonSol(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},new Integer[]{20,60,null,40},new Integer[]{20,40,60,null});Test(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},new Integer[]{20,60,null,40},new Integer[]{20,40,60,null},expected);- double [] expected = newtonSol(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},
- new Integer[]{20,60,null,40},new Integer[]{20,40,60,null});
- Test(new Integer[]{null,20,40,60},new Integer[]{20,null,60,40},
- new Integer[]{20,60,null,40},new Integer[]{20,40,60,null},expected);
expected = newtonSol(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},new Integer[]{17,17,null,17},new Integer[]{15,15,15,null});Test(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},new Integer[]{17,17,null,17},new Integer[]{15,15,15,null},expected);- expected = newtonSol(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},
- new Integer[]{17,17,null,17},new Integer[]{15,15,15,null});
- Test(new Integer[]{null,110,80,90},new Integer[]{90,null,117,14},
- new Integer[]{17,17,null,17},new Integer[]{15,15,15,null},expected);
expected = newtonSol(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},new Integer[]{3,1,null,2},new Integer[]{3,2,1,null});Test(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},new Integer[]{3,1,null,2},new Integer[]{3,2,1,null},expected);- expected = newtonSol(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},
- new Integer[]{3,1,null,2},new Integer[]{3,2,1,null});
- Test(new Integer[]{null,2,3,1},new Integer[]{1,null,1,2},
- new Integer[]{3,1,null,2},new Integer[]{3,2,1,null},expected);
- }
- }
from dataclasses import dataclass, field @dataclass class UserInfo: first_name: str last_name: str age: int job: str = field(default=None) hobbies: list[str] = field(default=None) @property def full_name(self): return f'{self.first_name} {self.last_name}' @property def email(self): return f'{self.first_name[0]}{self.last_name}@matrix.com'
from dataclasses import dataclass- from dataclasses import dataclass, field
- @dataclass
- class UserInfo:
- first_name: str
- last_name: str
- age: int
job: str | Nonehobbies: list[str] | None- job: str = field(default=None)
- hobbies: list[str] = field(default=None)
- @property
- def full_name(self):
- return f'{self.first_name} {self.last_name}'
- @property
- def email(self):
- return f'{self.first_name[0]}{self.last_name}@matrix.com'
import codewars_test as test from solution import UserInfo @test.describe("Example") def test_group(): @test.it("test case") def test_case(): sample_user = UserInfo('agent', 'smith', 101, 'an Agent of the system', ['capture/kill neo', 'destroy zion','escape the matrix' ] ) test.assert_equals(sample_user.first_name, 'agent') test.assert_equals(sample_user.last_name, 'smith') test.assert_equals(sample_user.full_name, 'agent smith') test.assert_equals(sample_user.job, 'an Agent of the system') test.assert_equals(sample_user.hobbies, ['capture/kill neo', 'destroy zion','escape the matrix' ]) test.assert_equals(sample_user.email, 'asmith@matrix.com')
- import codewars_test as test
- from solution import UserInfo
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
sample_user = UserInfo('agent', 'smith', 101, 'an Agent of the system', ['capture/kill neo', 'detroy zion','escape the matrix' ] )- sample_user = UserInfo('agent', 'smith', 101, 'an Agent of the system', ['capture/kill neo', 'destroy zion','escape the matrix' ] )
- test.assert_equals(sample_user.first_name, 'agent')
- test.assert_equals(sample_user.last_name, 'smith')
- test.assert_equals(sample_user.full_name, 'agent smith')
- test.assert_equals(sample_user.job, 'an Agent of the system')
test.assert_equals(sample_user.hobbies, ['capture/kill neo', 'detroy zion','escape the matrix' ])- test.assert_equals(sample_user.hobbies, ['capture/kill neo', 'destroy zion','escape the matrix' ])
- test.assert_equals(sample_user.email, 'asmith@matrix.com')
INTRODUCTION
Welcome soldiers, we need your help! The enemy army
is going to attack and we have to get ready.
Get together quickly by battalions!
TASK
Your task as a colonel is to order your lieutenants
with their respective battalion, for each lieutenant
a letter is assigned and each battalion is formed by
numbers and the letter corresponding to its lieutenant.
Your duty is to assign them in such a way that you
return a string like the following:
"a:11a1 b:b22 c:33c".
EXAMPLES
keyAlphabet( "a b c 11a b22 c33" ) => returns "a:11a b:b22 c:c33"
keyAlphabet( "a b c 11a1 2b2 33c3 13a1 12a1" ) => returns "a:11a1,12a1,13a1 b:2b2 c:33c3"
NOTES
- There may be more than one battalion for the same lieutenant.
- Both lieutenants and battalions must be ordered.
- Be careful with the spaces.
import java.util.*; public class Kata { public static String keyAlphabet(String str) { String[] arr = str.split(" "); List<String> listKey = new ArrayList<String>(); PriorityQueue<String> listValue = new PriorityQueue<>(); TreeMap<String, String> alphabet = new TreeMap<>(); for (String item : arr) { if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') { listKey.add(item); } else { listValue.add(item); } } List<String> values = new ArrayList<>(listValue); for (String value : values) { for (String s : listKey) { if (value.contains(s)) { if (alphabet.containsKey(s)) { alphabet.put(s, alphabet.get(s) + "," + value); } else { alphabet.put(s, value); } } } } return ""; } }
- import java.util.*;
- public class Kata {
- public static String keyAlphabet(String str) {
- String[] arr = str.split(" ");
- List<String> listKey = new ArrayList<String>();
- PriorityQueue<String> listValue = new PriorityQueue<>();
- TreeMap<String, String> alphabet = new TreeMap<>();
- for (String item : arr) {
- if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {
- listKey.add(item);
- } else {
- listValue.add(item);
- }
- }
- List<String> values = new ArrayList<>(listValue);
- for (String value : values) {
- for (String s : listKey) {
- if (value.contains(s)) {
- if (alphabet.containsKey(s)) {
- alphabet.put(s, alphabet.get(s) + "," + value);
- } else {
- alphabet.put(s, value);
- }
- }
- }
- }
- return "";
- }
- }
This fork add cases that test what should happen if:
- Stop to refuel in the course.
- Overpass the destination.
- Even reach to first station.
class Solution{ public static int lastPoint (int fuel, int consumption, int[]stations){ //DO YOUR MAGIC!! YES return -1; } }
- class Solution{
- public static int lastPoint (int fuel, int consumption, int[]stations){
//DO YOUR MAGIC!!- //DO YOUR MAGIC!! YES
- return -1;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void basicTest() { assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } )); assertEquals(45, Solution.lastPoint(80, 5, new int[] { 10, 15, 30, 45 } )); assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } )); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
void testSomething() {// assertEquals("expected", "actual");- void basicTest() {
- assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } ));
- assertEquals(45, Solution.lastPoint(80, 5, new int[] { 10, 15, 30, 45 } ));
- assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } ));
- }
- }