from numpy import square def find_squares_in_array(arr): return sum(map(square, arr))
using System.Linq;public class Program{public static int FindSquaresInArray(int[] arr) => arr.Sum(x => x*x);}- from numpy import square
- def find_squares_in_array(arr):
- return sum(map(square, arr))
from solution import find_squares_in_array import codewars_test as test @test.describe("find_squares_in_array") def sample_tests(): @test.it('Sample Tests') def sample_tests(): test.assert_equals(find_squares_in_array([]), 0) test.assert_equals(find_squares_in_array([1, 2, 3, 4, 5]), 55) test.assert_equals(find_squares_in_array([4]), 16) test.assert_equals(find_squares_in_array([0, 0, 0, 0]), 0)
using NUnit.Framework;using System;- from solution import find_squares_in_array
- import codewars_test as test
namespace Solution{[TestFixture]public class SolutionTest{[Test]public void TestEmptyArray(){int[] emptyArray = new int[0];Assert.AreEqual(0, Program.FindSquaresInArray(emptyArray));}- @test.describe("find_squares_in_array")
- def sample_tests():
[Test]public void TestArrayWithPositiveIntegers(){int[] positiveArray = { 1, 2, 3, 4, 5 };Assert.AreEqual(55, Program.FindSquaresInArray(positiveArray));}[Test]public void TestArrayWithSingleElement(){int[] singleElementArray = { 4 };Assert.AreEqual(16, Program.FindSquaresInArray(singleElementArray));}[Test]public void TestArrayWithAllZeros(){int[] zeroArray = { 0, 0, 0, 0 };Assert.AreEqual(0, Program.FindSquaresInArray(zeroArray));}}}- @test.it('Sample Tests')
- def sample_tests():
- test.assert_equals(find_squares_in_array([]), 0)
- test.assert_equals(find_squares_in_array([1, 2, 3, 4, 5]), 55)
- test.assert_equals(find_squares_in_array([4]), 16)
- test.assert_equals(find_squares_in_array([0, 0, 0, 0]), 0)
get_fibs=lambda a=1,b=1:[(t:=a,a:=b,b:=b+t)[0]for _ in range(100)]
def output(thing=[1,1]):for _ in range(100): thing.append(thing[-1]+thing[-2])return thing- get_fibs=lambda a=1,b=1:[(t:=a,a:=b,b:=b+t)[0]for _ in range(100)]
import codewars_test as test from solution import get_fibs @test.describe("Example") def test_group(): @test.it("test case") def test_case(): fibs = get_fibs() test.assert_equals(fibs, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075]) test.assert_equals(len(fibs), 100)
- import codewars_test as test
# TODO Write testsfrom solution import output # or from solution import example- from solution import get_fibs
# test.assert_equals(actual, expected, [optional] message)- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(output(), [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075, 573147844013817084101, 927372692193078999176])- fibs = get_fibs()
- test.assert_equals(fibs, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075])
- test.assert_equals(len(fibs), 100)
-1
firstNonRepeatingCharacter=s=>(j=s.split``,r=j.reduce((o,c)=>(o[c]=(o[c]|0)+1,o),{}),j.find(c=>r[c]==1)??null)
firstNonRepeatingCharacter=s=>(j=s.split``,r=j.reduce((o,c)=>(o[c]=(o[c]??0)+1,o),{}),j.find(c=>r[c]==1)??null)- firstNonRepeatingCharacter=s=>(j=s.split``,r=j.reduce((o,c)=>(o[c]=(o[c]|0)+1,o),{}),j.find(c=>r[c]==1)??null)
#include <stdio.h> prnt_mltply(m,n){for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)printf("%d * %d = %d\n",i,j,i*j);return m*n;}
- #include <stdio.h>
int prnt_mltply(int m, int n) {for (int i = 0; i < m; i++) for (int j = 1; j <= n; j++) printf("%d * %d = %d", i + 1, j, (i+1) * j);return m * n;}- prnt_mltply(m,n){for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)printf("%d * %d = %d
- ",i,j,i*j);return m*n;}
okay
const firstNonRepeatingCharacter = (str) => { let counts = {}; for(const char of str) { counts[char] = 1 + (counts[char] ? counts[char] : 0); } for(const char in counts) { if(counts[char] == 1) { return char; } } return null; }
- const firstNonRepeatingCharacter = (str) => {
let chars = [], counts = [];for(char of str) {let index = chars.indexOf(char);if(index < 0) {index = counts.length;chars.push(char);counts.push(0);}counts[index]++;- let counts = {};
- for(const char of str) {
- counts[char] = 1 + (counts[char] ? counts[char] : 0);
- }
for(let i = 0; i < chars.length; i++) {if(counts[i] === 1) {return chars[i];- for(const char in counts) {
- if(counts[char] == 1) {
- return char;
- }
- }
return null};- return null;
- }
shorter, barely
heh heh heh
n=s=>[...s].map(e=>("aeiou".includes(e)?"aeiou".indexOf(e)+1:e)).join`` d=s=>[...s].map(e=>("12345".includes(e)?"aeiou"[e-1]:e)).join``
n=s=>[...s].map(e=>("aeiou".includes(e)?"aeiou".indexOf(e)+1:e)).join('')d=s=>[...s].map(e=>("12345".includes(e)?"aeiou"[e-1]:e)).join('')- n=s=>[...s].map(e=>("aeiou".includes(e)?"aeiou".indexOf(e)+1:e)).join``
- d=s=>[...s].map(e=>("12345".includes(e)?"aeiou"[e-1]:e)).join``
dumbRockPaperScissors =(a,b)=> a==b?`Draw`:`Player ${(a!={'Rock':'Paper','Paper':'Scissors','Scissors':'Rock'}[b])+1} wins`
function dumbRockPaperScissors(player1, player2) {if (player1 === player2) return "Draw";const condition = {Scissors: "Paper",Rock: "Scissors",Paper: "Rock",};return player2 === condition[player1] ? "Player 1 wins" : "Player 2 wins";}- dumbRockPaperScissors
- =(a,b)=>
- a==b?`Draw`:`Player ${(a!={'Rock':'Paper','Paper':'Scissors','Scissors':'Rock'}[b])+1} wins`
Algorithms
Arrays
Mathematics
Geometry
make_move=lambda c,s:((d:=__import__('collections').Counter(s)),(c[0]+d['l']*-1+d['r'],c[1]+d['b']*-1+d['t']))[1]
makeMove=(c,s)=>[...s].reduce((r,d)=>[r[0]+(d=='r')-(d=='l'),r[1]+(d=='t')-(d=='b')],c)- make_move=lambda c,s:((d:=__import__('collections').Counter(s)),(c[0]+d['l']*-1+d['r'],c[1]+d['b']*-1+d['t']))[1]
import codewars_test as test from solution import make_move @test.describe("Fixed Tests") def fixed_tests(): @test.it("Basic Test Cases") def basic_test_cases(): tests = ( ( ( 0, 0), "ttrbrrbllrt", ( 2, 1) ), ( ( 4, 1), "bllbrt", ( 3, 0) ), ( (-2, 4), "trlbb", (-2, 3) ), ( ( 5, 5), "trlbrb", ( 6, 4) ) ) for coords, moves, expected in tests: submitted = make_move(coords, moves) test.assert_equals(submitted, expected)
// 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 deprecateddescribe("Solution", function() {it("should test for something", function() {assert.deepEqual(makeMove([0, 0], "ttrbrrbllrt"), [2, 1]);assert.deepEqual(makeMove([4,1], "bllbrt"), [3,0]);assert.deepEqual(makeMove([-2, 4], "trlbb"), [-2,3]);assert.deepEqual(makeMove([5,5], "trlbrb"), [6, 4]);});});- import codewars_test as test
- from solution import make_move
- @test.describe("Fixed Tests")
- def fixed_tests():
- @test.it("Basic Test Cases")
- def basic_test_cases():
- tests = (
- ( ( 0, 0), "ttrbrrbllrt", ( 2, 1) ),
- ( ( 4, 1), "bllbrt", ( 3, 0) ),
- ( (-2, 4), "trlbb", (-2, 3) ),
- ( ( 5, 5), "trlbrb", ( 6, 4) )
- )
- for coords, moves, expected in tests:
- submitted = make_move(coords, moves)
- test.assert_equals(submitted, expected)
Algorithms
Arrays
Mathematics
Geometry
makeMove =(c,s)=> [...s].reduce((r,d)=>[r[0]+(d=='r')-(d=='l'),r[1]+(d=='t')-(d=='b')],c)
function makeMove(init, sequence) {let coords = [...init];for(const dir of sequence) {coords[0] += (dir == 'r') - (dir == 'l');coords[1] += (dir == 't') - (dir == 'b');}return coords;}- makeMove
- =(c,s)=>
- [...s].reduce((r,d)=>[r[0]+(d=='r')-(d=='l'),r[1]+(d=='t')-(d=='b')],c)
Algorithms
Arrays
Mathematics
Geometry
function makeMove(init, sequence) { let coords = [...init]; for(const dir of sequence) { coords[0] += (dir == 'r') - (dir == 'l'); coords[1] += (dir == 't') - (dir == 'b'); } return coords; }
- function makeMove(init, sequence) {
[...sequence].forEach(move => {switch (move) {case 't':init[1]++;break;case 'b':init[1]--;break;case 'r':init[0]++;break;case 'l':init[0]--;break;default:break;}});return init;- let coords = [...init];
- for(const dir of sequence) {
- coords[0] += (dir == 'r') - (dir == 'l');
- coords[1] += (dir == 't') - (dir == 'b');
- }
- return coords;
- }
mod preloaded; use preloaded::nand; fn not(a: bool) -> bool { nand(a, a) } fn and(a: bool, b: bool) -> bool { nand(nand(a, b), nand(a, b)) } fn or(a: bool, b: bool) -> bool { nand(not(a), not(b)) } fn nand_nand_or(a: bool, b: bool) -> bool { nand(nand(a, b), or(a, b)) } fn xor(a: bool, b: bool) -> bool { nand(nand_nand_or(a, b), nand_nand_or(a, b)) } fn nor(a: bool, b: bool) -> bool { nand(or(a, b), or(a, b)) } fn xnor(a: bool, b: bool) -> bool { nand(nand(a, b), nand(nand(a, a), not(b))) }
- mod preloaded;
- use preloaded::nand;
- fn not(a: bool) -> bool {
- nand(a, a)
- }
- fn and(a: bool, b: bool) -> bool {
- nand(nand(a, b), nand(a, b))
- }
- fn or(a: bool, b: bool) -> bool {
nand(nand(a, a), nand(b, b))- nand(not(a), not(b))
- }
- fn nand_nand_or(a: bool, b: bool) -> bool {
- nand(nand(a, b), or(a, b))
- }
- fn xor(a: bool, b: bool) -> bool {
nand(nand(nand(a, b), nand(nand(a, a), nand(b, b))), nand(nand(a, b), nand(nand(a, a), nand(b, b))))- nand(nand_nand_or(a, b), nand_nand_or(a, b))
- }
- fn nor(a: bool, b: bool) -> bool {
nand(nand(nand(a, a), nand(b, b)), nand(nand(a, a), nand(b, b)))- nand(or(a, b), or(a, b))
- }
- fn xnor(a: bool, b: bool) -> bool {
nand(nand(a, b), nand(nand(a, a), nand(b, b)))- nand(nand(a, b), nand(nand(a, a), not(b)))
- }
public class Program { public static int FindSquaresInArray(int[] arr) { int sum = 0; foreach(int n in arr) { sum += n * n; } return sum; } }
using System;- public class Program
- {
- public static int FindSquaresInArray(int[] arr)
- {
- int sum = 0;
for (int i = 0; i < arr.Length; i++)- foreach(int n in arr)
- {
sum += arr[i] * arr[i];- sum += n * n;
- }
- return sum;
- }