Control Flow
Basic Language Features
Fundamentals
Tests added.
const returnAscending=n=>[...n+''].sort().reduce((a,b)=>a*10+ +b,0);
function returnAscending(n){return +(n+'').split('').sort(function(a,b){return a-b }).join('');}- const returnAscending=n=>[...n+''].sort().reduce((a,b)=>a*10+ +b,0);
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("should test for something", function() { assert.equal(returnAscending(15342), 12345); }); });
// 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);- assert.equal(returnAscending(15342), 12345);
- });
- });
Now with tests shipped.
const returnAscending = (...args) => args.sort((a,b)=> a-b ).join('');
const returnAscending = (...args) => args.sort(function(a,b){return a-b }).join('');- const returnAscending = (...args) => args.sort((a,b)=> a-b ).join('');
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("should test for something", function() { assert.equal(returnAscending(1, 5, 4, 2, 3), '12345') }); });
// 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);- assert.equal(returnAscending(1, 5, 4, 2, 3), '12345')
- });
- });
Removed null
check from random tests.
module FindMaxNumber where import Data.List.NonEmpty find_max :: Ord a => NonEmpty a -> a find_max (x:|xs) = foldr max x xs
- module FindMaxNumber where
- import Data.List.NonEmpty
- find_max :: Ord a => NonEmpty a -> a
find_max (x:|xs) = foldr (\ a b -> if a > b then a else b) x xs- find_max (x:|xs) = foldr max x xs
module FindMaxNumberSpec where import Prelude hiding (take, iterate, repeat) import Test.Hspec import Test.QuickCheck import Data.List.NonEmpty import FindMaxNumber instance Arbitrary a => Arbitrary (NonEmpty a) where arbitrary = fromList <$> (arbitrary1 `suchThat` (not . null)) spec = do describe "Test find_max" $ do it "find_max [1,2,3,4,5]" $ do find_max (fromList [1..5]) `shouldBe` 5 it "Random tests" $ do property $ \ xs -> find_max xs == maximum (xs :: NonEmpty Int)
- module FindMaxNumberSpec where
- import Prelude hiding (take, iterate, repeat)
- import Test.Hspec
- import Test.QuickCheck
- import Data.List.NonEmpty
- import FindMaxNumber
- instance Arbitrary a => Arbitrary (NonEmpty a) where
- arbitrary = fromList <$> (arbitrary1 `suchThat` (not . null))
- spec = do
- describe "Test find_max" $ do
- it "find_max [1,2,3,4,5]" $ do
- find_max (fromList [1..5]) `shouldBe` 5
- it "Random tests" $ do
property $ \ xs -> if null xs then True else find_max xs == maximum (xs :: NonEmpty Int)- property $ \ xs -> find_max xs == maximum (xs :: NonEmpty Int)
module FindMaxNumber where find_max :: Ord a => [a] -> a find_max [] = error "find_max: Empty list" find_max (x:xs) = foldr (\ a b -> if a > b then a else b) x xs
- module FindMaxNumber where
- find_max :: Ord a => [a] -> a
find_max = maximum- find_max [] = error "find_max: Empty list"
- find_max (x:xs) = foldr (\ a b -> if a > b then a else b) x xs
module FindMaxNumberSpec where import Test.Hspec import Test.QuickCheck import FindMaxNumber spec = do describe "Test find_max" $ do it "find_max [1,2,3,4,5]" $ do find_max [1, 2, 3, 4, 5] `shouldBe` 5 it "Random tests" $ do property $ \ xs -> if null xs then True else find_max xs == maximum (xs :: [Int])
- module FindMaxNumberSpec where
- import Test.Hspec
- import Test.QuickCheck
- import FindMaxNumber
- spec = do
- describe "Test find_max" $ do
- it "find_max [1,2,3,4,5]" $ do
find_max [1, 2, 3, 4, 5] `shouldBe` 5- find_max [1, 2, 3, 4, 5] `shouldBe` 5
- it "Random tests" $ do
- property $ \ xs -> if null xs then True else find_max xs == maximum (xs :: [Int])
def isIterable(l): if isinstance(l, str): return False try: l[0] return True except: return False def unpackList(l): output = [] stack = l while stack != []: val = stack.pop() if isIterable(val): stack += list(val) else: output.append(str(val)) return ' '.join(output[::-1])
- def isIterable(l):
- if isinstance(l, str):
- return False
- try:
- l[0]
- return True
- except:
- return False
def unpackIterable(l):- def unpackList(l):
- output = []
for item in l:if isIterable(item) and not isinstance(item, str):output += unpackIterable(item)else:output.append(item)return output- stack = l
def unpackList(l):outputList = []for item in l:if isIterable(item) and not isinstance(item, str):outputList += unpackIterable(item)- while stack != []:
- val = stack.pop()
- if isIterable(val):
- stack += list(val)
- else:
outputList.append(item)return " ".join(map(str, outputList))- output.append(str(val))
- return ' '.join(output[::-1])